mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 09:46:30 -05:00
Move common test preparations into TestSetup class
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
441e7402a8
commit
386098ed2b
2 changed files with 100 additions and 69 deletions
|
@ -3,86 +3,32 @@
|
|||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
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 { MockAuthGuard } from '../../src/auth/mock-auth.guard';
|
||||
import { TokenAuthGuard } from '../../src/auth/token.strategy';
|
||||
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 { IdentityService } from '../../src/identity/identity.service';
|
||||
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 { User } from '../../src/users/user.entity';
|
||||
import { UsersModule } from '../../src/users/users.module';
|
||||
import { UsersService } from '../../src/users/users.service';
|
||||
import { setupSessionMiddleware } from '../../src/utils/session';
|
||||
import { TestSetup } from '../test-setup';
|
||||
|
||||
describe('Tokens', () => {
|
||||
let app: INestApplication;
|
||||
let userService: UsersService;
|
||||
let identityService: IdentityService;
|
||||
let user: User;
|
||||
let testSetup: TestSetup;
|
||||
let agent: request.SuperAgentTest;
|
||||
|
||||
let user: User;
|
||||
let keyId: string;
|
||||
|
||||
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-me.sqlite',
|
||||
autoLoadEntities: true,
|
||||
synchronize: true,
|
||||
dropSchema: true,
|
||||
}),
|
||||
LoggerModule,
|
||||
AuthModule,
|
||||
UsersModule,
|
||||
MediaModule,
|
||||
HistoryModule,
|
||||
],
|
||||
})
|
||||
.overrideGuard(TokenAuthGuard)
|
||||
.useClass(MockAuthGuard)
|
||||
.compile();
|
||||
const config = moduleRef.get<ConfigService>(ConfigService);
|
||||
identityService = moduleRef.get(IdentityService);
|
||||
app = moduleRef.createNestApplication();
|
||||
userService = moduleRef.get(UsersService);
|
||||
user = await userService.createUser('hardcoded', 'Testy');
|
||||
await identityService.createLocalIdentity(user, 'test');
|
||||
const authConfig = config.get('authConfig') as AuthConfig;
|
||||
setupSessionMiddleware(app, authConfig);
|
||||
await app.init();
|
||||
agent = request.agent(app.getHttpServer());
|
||||
testSetup = await TestSetup.create();
|
||||
|
||||
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
||||
await testSetup.identityService.createLocalIdentity(user, 'test');
|
||||
|
||||
const authConfig = testSetup.configService.get('authConfig') as AuthConfig;
|
||||
setupSessionMiddleware(testSetup.app, authConfig);
|
||||
|
||||
await testSetup.app.init();
|
||||
|
||||
agent = request.agent(testSetup.app.getHttpServer());
|
||||
await agent
|
||||
.post('/auth/local/login')
|
||||
.send({ username: 'hardcoded', password: 'test' })
|
||||
|
|
85
test/test-setup.ts
Normal file
85
test/test-setup.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { PrivateApiModule } from '../src/api/private/private-api.module';
|
||||
import { AuthModule } from '../src/auth/auth.module';
|
||||
import { MockAuthGuard } from '../src/auth/mock-auth.guard';
|
||||
import { TokenAuthGuard } from '../src/auth/token.strategy';
|
||||
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 { IdentityService } from '../src/identity/identity.service';
|
||||
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 { UsersModule } from '../src/users/users.module';
|
||||
import { UsersService } from '../src/users/users.service';
|
||||
|
||||
export class TestSetup {
|
||||
moduleRef: TestingModule;
|
||||
app: INestApplication;
|
||||
|
||||
userService: UsersService;
|
||||
configService: ConfigService;
|
||||
identityService: IdentityService;
|
||||
|
||||
public static async create(): Promise<TestSetup> {
|
||||
const testSetup = new TestSetup();
|
||||
|
||||
testSetup.moduleRef = await Test.createTestingModule({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
load: [
|
||||
appConfigMock,
|
||||
authConfigMock,
|
||||
mediaConfigMock,
|
||||
customizationConfigMock,
|
||||
externalServicesConfigMock,
|
||||
],
|
||||
}),
|
||||
PrivateApiModule,
|
||||
NotesModule,
|
||||
PermissionsModule,
|
||||
GroupsModule,
|
||||
TypeOrmModule.forRoot({
|
||||
type: 'sqlite',
|
||||
database: ':memory:',
|
||||
autoLoadEntities: true,
|
||||
synchronize: true,
|
||||
dropSchema: true,
|
||||
}),
|
||||
LoggerModule,
|
||||
AuthModule,
|
||||
UsersModule,
|
||||
MediaModule,
|
||||
HistoryModule,
|
||||
],
|
||||
})
|
||||
.overrideGuard(TokenAuthGuard)
|
||||
.useClass(MockAuthGuard)
|
||||
.compile();
|
||||
|
||||
testSetup.userService = testSetup.moduleRef.get<UsersService>(UsersService);
|
||||
testSetup.configService =
|
||||
testSetup.moduleRef.get<ConfigService>(ConfigService);
|
||||
testSetup.identityService =
|
||||
testSetup.moduleRef.get<IdentityService>(IdentityService);
|
||||
|
||||
testSetup.app = testSetup.moduleRef.createNestApplication();
|
||||
|
||||
return testSetup;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue