2021-09-02 17:41:32 -04:00
|
|
|
/*
|
2022-02-19 14:47:28 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-09-02 17:41:32 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-disable
|
|
|
|
@typescript-eslint/no-unsafe-assignment,
|
|
|
|
@typescript-eslint/no-unsafe-member-access
|
|
|
|
*/
|
|
|
|
import request from 'supertest';
|
|
|
|
|
|
|
|
import { AuthConfig } from '../../src/config/auth.config';
|
|
|
|
import { LoginDto } from '../../src/identity/local/login.dto';
|
|
|
|
import { RegisterDto } from '../../src/identity/local/register.dto';
|
|
|
|
import { UpdatePasswordDto } from '../../src/identity/local/update-password.dto';
|
|
|
|
import { UserRelationEnum } from '../../src/users/user-relation.enum';
|
|
|
|
import { checkPassword } from '../../src/utils/password';
|
|
|
|
import { setupSessionMiddleware } from '../../src/utils/session';
|
2022-01-06 15:41:36 -05:00
|
|
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
2021-09-02 17:41:32 -04:00
|
|
|
|
|
|
|
describe('Auth', () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-09-02 17:41:32 -04:00
|
|
|
let username: string;
|
|
|
|
let displayname: string;
|
|
|
|
let password: string;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-01-06 15:41:36 -05:00
|
|
|
testSetup = await TestSetupBuilder.create().build();
|
2021-10-15 11:06:56 -04:00
|
|
|
|
|
|
|
const authConfig = testSetup.configService.get('authConfig') as AuthConfig;
|
|
|
|
setupSessionMiddleware(testSetup.app, authConfig);
|
|
|
|
await testSetup.app.init();
|
|
|
|
|
2021-09-02 17:41:32 -04:00
|
|
|
username = 'hardcoded';
|
|
|
|
displayname = 'Testy';
|
|
|
|
password = 'test_password';
|
|
|
|
});
|
|
|
|
|
2022-03-04 07:13:46 -05:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-09-02 17:41:32 -04:00
|
|
|
describe('POST /auth/local', () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const registrationDto: RegisterDto = {
|
|
|
|
displayname: displayname,
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(registrationDto))
|
|
|
|
.expect(201);
|
2021-10-15 11:06:56 -04:00
|
|
|
const newUser = await testSetup.userService.getUserByUsername(username, [
|
2021-09-02 17:41:32 -04:00
|
|
|
UserRelationEnum.IDENTITIES,
|
|
|
|
]);
|
|
|
|
expect(newUser.displayName).toEqual(displayname);
|
|
|
|
await expect(newUser.identities).resolves.toHaveLength(1);
|
|
|
|
await expect(
|
|
|
|
checkPassword(
|
|
|
|
password,
|
|
|
|
(await newUser.identities)[0].passwordHash ?? '',
|
|
|
|
),
|
|
|
|
).resolves.toBeTruthy();
|
|
|
|
});
|
|
|
|
describe('fails', () => {
|
|
|
|
it('when the user already exits', async () => {
|
|
|
|
const username2 = 'already_existing';
|
2021-10-15 11:06:56 -04:00
|
|
|
await testSetup.userService.createUser(username2, displayname);
|
2021-09-02 17:41:32 -04:00
|
|
|
const registrationDto: RegisterDto = {
|
|
|
|
displayname: displayname,
|
|
|
|
password: password,
|
|
|
|
username: username2,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(registrationDto))
|
2021-10-27 13:39:18 -04:00
|
|
|
.expect(409);
|
2021-09-02 17:41:32 -04:00
|
|
|
});
|
|
|
|
it('when registration is disabled', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
testSetup.configService.get('authConfig').local.enableRegister = false;
|
2021-09-02 17:41:32 -04:00
|
|
|
const registrationDto: RegisterDto = {
|
|
|
|
displayname: displayname,
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(registrationDto))
|
|
|
|
.expect(400);
|
2021-10-15 11:06:56 -04:00
|
|
|
testSetup.configService.get('authConfig').local.enableRegister = true;
|
2021-09-02 17:41:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('PUT /auth/local', () => {
|
|
|
|
const newPassword = 'new_password';
|
|
|
|
let cookie = '';
|
|
|
|
beforeEach(async () => {
|
|
|
|
const loginDto: LoginDto = {
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginDto))
|
|
|
|
.expect(201);
|
|
|
|
cookie = response.get('Set-Cookie')[0];
|
|
|
|
});
|
|
|
|
it('works', async () => {
|
|
|
|
// Change password
|
|
|
|
const changePasswordDto: UpdatePasswordDto = {
|
2021-12-27 19:46:40 -05:00
|
|
|
currentPassword: password,
|
2021-09-02 17:41:32 -04:00
|
|
|
newPassword: newPassword,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.put('/api/private/auth/local')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.set('Cookie', cookie)
|
|
|
|
.send(JSON.stringify(changePasswordDto))
|
|
|
|
.expect(200);
|
|
|
|
// Successfully login with new password
|
|
|
|
const loginDto: LoginDto = {
|
|
|
|
password: newPassword,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginDto))
|
|
|
|
.expect(201);
|
|
|
|
cookie = response.get('Set-Cookie')[0];
|
|
|
|
// Reset password
|
|
|
|
const changePasswordBackDto: UpdatePasswordDto = {
|
2021-12-27 19:46:40 -05:00
|
|
|
currentPassword: newPassword,
|
2021-09-02 17:41:32 -04:00
|
|
|
newPassword: password,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.put('/api/private/auth/local')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.set('Cookie', cookie)
|
|
|
|
.send(JSON.stringify(changePasswordBackDto))
|
|
|
|
.expect(200);
|
|
|
|
});
|
|
|
|
it('fails, when registration is disabled', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
testSetup.configService.get('authConfig').local.enableLogin = false;
|
2021-09-02 17:41:32 -04:00
|
|
|
// Try to change password
|
|
|
|
const changePasswordDto: UpdatePasswordDto = {
|
2021-12-27 19:46:40 -05:00
|
|
|
currentPassword: password,
|
2021-09-02 17:41:32 -04:00
|
|
|
newPassword: newPassword,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.put('/api/private/auth/local')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.set('Cookie', cookie)
|
|
|
|
.send(JSON.stringify(changePasswordDto))
|
|
|
|
.expect(400);
|
|
|
|
// enable login again
|
2021-10-15 11:06:56 -04:00
|
|
|
testSetup.configService.get('authConfig').local.enableLogin = true;
|
2021-09-02 17:41:32 -04:00
|
|
|
// new password doesn't work for login
|
|
|
|
const loginNewPasswordDto: LoginDto = {
|
|
|
|
password: newPassword,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginNewPasswordDto))
|
|
|
|
.expect(401);
|
|
|
|
// old password does work for login
|
|
|
|
const loginOldPasswordDto: LoginDto = {
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginOldPasswordDto))
|
|
|
|
.expect(201);
|
|
|
|
});
|
2021-12-27 19:46:40 -05:00
|
|
|
it('fails, when old password is wrong', async () => {
|
|
|
|
// Try to change password
|
|
|
|
const changePasswordDto: UpdatePasswordDto = {
|
|
|
|
currentPassword: 'wrong',
|
|
|
|
newPassword: newPassword,
|
|
|
|
};
|
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.put('/api/private/auth/local')
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.set('Cookie', cookie)
|
|
|
|
.send(JSON.stringify(changePasswordDto))
|
|
|
|
.expect(401);
|
|
|
|
// old password still does work for login
|
|
|
|
const loginOldPasswordDto: LoginDto = {
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginOldPasswordDto))
|
|
|
|
.expect(201);
|
|
|
|
});
|
2021-09-02 17:41:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('POST /auth/local/login', () => {
|
|
|
|
it('works', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
testSetup.configService.get('authConfig').local.enableLogin = true;
|
2021-09-02 17:41:32 -04:00
|
|
|
const loginDto: LoginDto = {
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginDto))
|
|
|
|
.expect(201);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DELETE /auth/logout', () => {
|
|
|
|
it('works', async () => {
|
2021-10-15 11:06:56 -04:00
|
|
|
testSetup.configService.get('authConfig').local.enableLogin = true;
|
2021-09-02 17:41:32 -04:00
|
|
|
const loginDto: LoginDto = {
|
|
|
|
password: password,
|
|
|
|
username: username,
|
|
|
|
};
|
2021-10-15 11:06:56 -04:00
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
|
|
|
.post('/api/private/auth/local/login')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify(loginDto))
|
|
|
|
.expect(201);
|
|
|
|
const cookie = response.get('Set-Cookie')[0];
|
2021-10-15 11:06:56 -04:00
|
|
|
await request(testSetup.app.getHttpServer())
|
|
|
|
.delete('/api/private/auth/logout')
|
2021-09-02 17:41:32 -04:00
|
|
|
.set('Cookie', cookie)
|
2022-02-19 14:47:28 -05:00
|
|
|
.expect(204);
|
2021-09-02 17:41:32 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|