mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 17:25:16 -04:00
Update API routes in private API E2E tests
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
c6cac58a67
commit
691152579b
6 changed files with 149 additions and 226 deletions
|
@ -8,80 +8,31 @@
|
|||
@typescript-eslint/no-unsafe-assignment,
|
||||
@typescript-eslint/no-unsafe-member-access
|
||||
*/
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import request from 'supertest';
|
||||
|
||||
import { PrivateApiModule } from '../../src/api/private/private-api.module';
|
||||
import { AuthModule } from '../../src/auth/auth.module';
|
||||
import { AuthConfig } from '../../src/config/auth.config';
|
||||
import appConfigMock from '../../src/config/mock/app.config.mock';
|
||||
import authConfigMock from '../../src/config/mock/auth.config.mock';
|
||||
import customizationConfigMock from '../../src/config/mock/customization.config.mock';
|
||||
import externalServicesConfigMock from '../../src/config/mock/external-services.config.mock';
|
||||
import mediaConfigMock from '../../src/config/mock/media.config.mock';
|
||||
import { GroupsModule } from '../../src/groups/groups.module';
|
||||
import { HistoryModule } from '../../src/history/history.module';
|
||||
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 { LoggerModule } from '../../src/logger/logger.module';
|
||||
import { MediaModule } from '../../src/media/media.module';
|
||||
import { NotesModule } from '../../src/notes/notes.module';
|
||||
import { PermissionsModule } from '../../src/permissions/permissions.module';
|
||||
import { UserRelationEnum } from '../../src/users/user-relation.enum';
|
||||
import { UsersModule } from '../../src/users/users.module';
|
||||
import { UsersService } from '../../src/users/users.service';
|
||||
import { checkPassword } from '../../src/utils/password';
|
||||
import { setupSessionMiddleware } from '../../src/utils/session';
|
||||
import { TestSetup } from '../test-setup';
|
||||
|
||||
describe('Auth', () => {
|
||||
let app: INestApplication;
|
||||
let userService: UsersService;
|
||||
let testSetup: TestSetup;
|
||||
|
||||
let username: string;
|
||||
let displayname: string;
|
||||
let password: string;
|
||||
let config: ConfigService;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
load: [
|
||||
appConfigMock,
|
||||
authConfigMock,
|
||||
mediaConfigMock,
|
||||
customizationConfigMock,
|
||||
externalServicesConfigMock,
|
||||
],
|
||||
}),
|
||||
PrivateApiModule,
|
||||
NotesModule,
|
||||
PermissionsModule,
|
||||
GroupsModule,
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'sqlite',
|
||||
database: './hedgedoc-e2e-private-auth.sqlite',
|
||||
autoLoadEntities: true,
|
||||
synchronize: true,
|
||||
dropSchema: true,
|
||||
}),
|
||||
LoggerModule,
|
||||
AuthModule,
|
||||
UsersModule,
|
||||
MediaModule,
|
||||
HistoryModule,
|
||||
],
|
||||
}).compile();
|
||||
config = moduleRef.get<ConfigService>(ConfigService);
|
||||
app = moduleRef.createNestApplication();
|
||||
const authConfig = config.get('authConfig') as AuthConfig;
|
||||
setupSessionMiddleware(app, authConfig);
|
||||
await app.init();
|
||||
userService = moduleRef.get(UsersService);
|
||||
testSetup = await TestSetup.create();
|
||||
|
||||
const authConfig = testSetup.configService.get('authConfig') as AuthConfig;
|
||||
setupSessionMiddleware(testSetup.app, authConfig);
|
||||
await testSetup.app.init();
|
||||
|
||||
username = 'hardcoded';
|
||||
displayname = 'Testy';
|
||||
password = 'test_password';
|
||||
|
@ -94,12 +45,12 @@ describe('Auth', () => {
|
|||
password: password,
|
||||
username: username,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/local')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(registrationDto))
|
||||
.expect(201);
|
||||
const newUser = await userService.getUserByUsername(username, [
|
||||
const newUser = await testSetup.userService.getUserByUsername(username, [
|
||||
UserRelationEnum.IDENTITIES,
|
||||
]);
|
||||
expect(newUser.displayName).toEqual(displayname);
|
||||
|
@ -114,31 +65,31 @@ describe('Auth', () => {
|
|||
describe('fails', () => {
|
||||
it('when the user already exits', async () => {
|
||||
const username2 = 'already_existing';
|
||||
await userService.createUser(username2, displayname);
|
||||
await testSetup.userService.createUser(username2, displayname);
|
||||
const registrationDto: RegisterDto = {
|
||||
displayname: displayname,
|
||||
password: password,
|
||||
username: username2,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/local')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(registrationDto))
|
||||
.expect(400);
|
||||
});
|
||||
it('when registration is disabled', async () => {
|
||||
config.get('authConfig').local.enableRegister = false;
|
||||
testSetup.configService.get('authConfig').local.enableRegister = false;
|
||||
const registrationDto: RegisterDto = {
|
||||
displayname: displayname,
|
||||
password: password,
|
||||
username: username,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/local')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(registrationDto))
|
||||
.expect(400);
|
||||
config.get('authConfig').local.enableRegister = true;
|
||||
testSetup.configService.get('authConfig').local.enableRegister = true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -151,8 +102,8 @@ describe('Auth', () => {
|
|||
password: password,
|
||||
username: username,
|
||||
};
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/auth/local/login')
|
||||
const response = await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(loginDto))
|
||||
.expect(201);
|
||||
|
@ -163,8 +114,8 @@ describe('Auth', () => {
|
|||
const changePasswordDto: UpdatePasswordDto = {
|
||||
newPassword: newPassword,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.put('/auth/local')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.put('/api/private/auth/local')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', cookie)
|
||||
.send(JSON.stringify(changePasswordDto))
|
||||
|
@ -174,8 +125,8 @@ describe('Auth', () => {
|
|||
password: newPassword,
|
||||
username: username,
|
||||
};
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/auth/local/login')
|
||||
const response = await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(loginDto))
|
||||
.expect(201);
|
||||
|
@ -184,34 +135,34 @@ describe('Auth', () => {
|
|||
const changePasswordBackDto: UpdatePasswordDto = {
|
||||
newPassword: password,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.put('/auth/local')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.put('/api/private/auth/local')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', cookie)
|
||||
.send(JSON.stringify(changePasswordBackDto))
|
||||
.expect(200);
|
||||
});
|
||||
it('fails, when registration is disabled', async () => {
|
||||
config.get('authConfig').local.enableLogin = false;
|
||||
testSetup.configService.get('authConfig').local.enableLogin = false;
|
||||
// Try to change password
|
||||
const changePasswordDto: UpdatePasswordDto = {
|
||||
newPassword: newPassword,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.put('/auth/local')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.put('/api/private/auth/local')
|
||||
.set('Content-Type', 'application/json')
|
||||
.set('Cookie', cookie)
|
||||
.send(JSON.stringify(changePasswordDto))
|
||||
.expect(400);
|
||||
// enable login again
|
||||
config.get('authConfig').local.enableLogin = true;
|
||||
testSetup.configService.get('authConfig').local.enableLogin = true;
|
||||
// new password doesn't work for login
|
||||
const loginNewPasswordDto: LoginDto = {
|
||||
password: newPassword,
|
||||
username: username,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/local/login')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(loginNewPasswordDto))
|
||||
.expect(401);
|
||||
|
@ -220,8 +171,8 @@ describe('Auth', () => {
|
|||
password: password,
|
||||
username: username,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/local/login')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(loginOldPasswordDto))
|
||||
.expect(201);
|
||||
|
@ -230,13 +181,13 @@ describe('Auth', () => {
|
|||
|
||||
describe('POST /auth/local/login', () => {
|
||||
it('works', async () => {
|
||||
config.get('authConfig').local.enableLogin = true;
|
||||
testSetup.configService.get('authConfig').local.enableLogin = true;
|
||||
const loginDto: LoginDto = {
|
||||
password: password,
|
||||
username: username,
|
||||
};
|
||||
await request(app.getHttpServer())
|
||||
.post('/auth/local/login')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(loginDto))
|
||||
.expect(201);
|
||||
|
@ -245,19 +196,19 @@ describe('Auth', () => {
|
|||
|
||||
describe('DELETE /auth/logout', () => {
|
||||
it('works', async () => {
|
||||
config.get('authConfig').local.enableLogin = true;
|
||||
testSetup.configService.get('authConfig').local.enableLogin = true;
|
||||
const loginDto: LoginDto = {
|
||||
password: password,
|
||||
username: username,
|
||||
};
|
||||
const response = await request(app.getHttpServer())
|
||||
.post('/auth/local/login')
|
||||
const response = await request(testSetup.app.getHttpServer())
|
||||
.post('/api/private/auth/local/login')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(JSON.stringify(loginDto))
|
||||
.expect(201);
|
||||
const cookie = response.get('Set-Cookie')[0];
|
||||
await request(app.getHttpServer())
|
||||
.delete('/auth/logout')
|
||||
await request(testSetup.app.getHttpServer())
|
||||
.delete('/api/private/auth/logout')
|
||||
.set('Cookie', cookie)
|
||||
.expect(200);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue