2021-10-14 18:17:28 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
2021-10-14 19:01:29 +00:00
|
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
2021-10-15 14:44:43 +00:00
|
|
|
import { RouterModule, Routes } from 'nest-router';
|
2021-10-14 18:17:28 +00:00
|
|
|
|
|
|
|
import { PrivateApiModule } from '../src/api/private/private-api.module';
|
2021-10-14 18:52:57 +00:00
|
|
|
import { PublicApiModule } from '../src/api/public/public-api.module';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { AuthModule } from '../src/auth/auth.module';
|
|
|
|
import { MockAuthGuard } from '../src/auth/mock-auth.guard';
|
|
|
|
import { TokenAuthGuard } from '../src/auth/token.strategy';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { AuthorsModule } from '../src/authors/authors.module';
|
|
|
|
import { AuthConfig } from '../src/config/auth.config';
|
2021-10-14 18:17:28 +00:00
|
|
|
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';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { FrontendConfigModule } from '../src/frontend-config/frontend-config.module';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { GroupsModule } from '../src/groups/groups.module';
|
|
|
|
import { HistoryModule } from '../src/history/history.module';
|
2021-10-14 19:17:42 +00:00
|
|
|
import { HistoryService } from '../src/history/history.service';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { IdentityModule } from '../src/identity/identity.module';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { IdentityService } from '../src/identity/identity.service';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { ConsoleLoggerService } from '../src/logger/console-logger.service';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { LoggerModule } from '../src/logger/logger.module';
|
|
|
|
import { MediaModule } from '../src/media/media.module';
|
2021-10-14 18:52:57 +00:00
|
|
|
import { MediaService } from '../src/media/media.service';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { MonitoringModule } from '../src/monitoring/monitoring.module';
|
2021-10-14 19:25:17 +00:00
|
|
|
import { AliasService } from '../src/notes/alias.service';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { NotesModule } from '../src/notes/notes.module';
|
2021-10-14 18:52:57 +00:00
|
|
|
import { NotesService } from '../src/notes/notes.service';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { PermissionsModule } from '../src/permissions/permissions.module';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { RevisionsModule } from '../src/revisions/revisions.module';
|
2021-10-14 18:17:28 +00:00
|
|
|
import { UsersModule } from '../src/users/users.module';
|
|
|
|
import { UsersService } from '../src/users/users.service';
|
2021-10-21 17:51:23 +00:00
|
|
|
import { setupSessionMiddleware } from '../src/utils/session';
|
|
|
|
import { setupValidationPipe } from '../src/utils/setup-pipes';
|
2021-10-14 18:17:28 +00:00
|
|
|
|
|
|
|
export class TestSetup {
|
|
|
|
moduleRef: TestingModule;
|
2021-10-14 19:01:29 +00:00
|
|
|
app: NestExpressApplication;
|
2021-10-14 18:17:28 +00:00
|
|
|
|
|
|
|
userService: UsersService;
|
|
|
|
configService: ConfigService;
|
|
|
|
identityService: IdentityService;
|
2021-10-14 18:52:57 +00:00
|
|
|
notesService: NotesService;
|
|
|
|
mediaService: MediaService;
|
2021-10-14 19:17:42 +00:00
|
|
|
historyService: HistoryService;
|
2021-10-14 19:25:17 +00:00
|
|
|
aliasService: AliasService;
|
2021-10-14 18:17:28 +00:00
|
|
|
|
|
|
|
public static async create(): Promise<TestSetup> {
|
|
|
|
const testSetup = new TestSetup();
|
2021-10-15 14:44:43 +00:00
|
|
|
const routes: Routes = [
|
|
|
|
{
|
|
|
|
path: '/api/v2',
|
|
|
|
module: PublicApiModule,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/api/private',
|
|
|
|
module: PrivateApiModule,
|
|
|
|
},
|
|
|
|
];
|
2021-10-14 18:17:28 +00:00
|
|
|
|
|
|
|
testSetup.moduleRef = await Test.createTestingModule({
|
|
|
|
imports: [
|
2021-10-15 14:44:43 +00:00
|
|
|
RouterModule.forRoutes(routes),
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
|
|
|
database: ':memory:',
|
|
|
|
autoLoadEntities: true,
|
|
|
|
synchronize: true,
|
|
|
|
dropSchema: true,
|
|
|
|
}),
|
2021-10-14 18:17:28 +00:00
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
|
|
|
load: [
|
|
|
|
appConfigMock,
|
|
|
|
authConfigMock,
|
|
|
|
mediaConfigMock,
|
|
|
|
customizationConfigMock,
|
|
|
|
externalServicesConfigMock,
|
|
|
|
],
|
|
|
|
}),
|
2021-10-21 17:51:23 +00:00
|
|
|
NotesModule,
|
|
|
|
UsersModule,
|
|
|
|
RevisionsModule,
|
|
|
|
AuthorsModule,
|
2021-10-14 18:52:57 +00:00
|
|
|
PublicApiModule,
|
2021-10-14 18:17:28 +00:00
|
|
|
PrivateApiModule,
|
2021-10-21 17:51:23 +00:00
|
|
|
HistoryModule,
|
|
|
|
MonitoringModule,
|
2021-10-14 18:17:28 +00:00
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
LoggerModule,
|
|
|
|
MediaModule,
|
2021-10-21 17:51:23 +00:00
|
|
|
AuthModule,
|
|
|
|
FrontendConfigModule,
|
|
|
|
IdentityModule,
|
2021-10-14 18:17:28 +00:00
|
|
|
],
|
|
|
|
})
|
|
|
|
.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);
|
2021-10-14 18:52:57 +00:00
|
|
|
testSetup.notesService =
|
|
|
|
testSetup.moduleRef.get<NotesService>(NotesService);
|
|
|
|
testSetup.mediaService =
|
|
|
|
testSetup.moduleRef.get<MediaService>(MediaService);
|
2021-10-14 19:17:42 +00:00
|
|
|
testSetup.historyService =
|
|
|
|
testSetup.moduleRef.get<HistoryService>(HistoryService);
|
2021-10-14 19:25:17 +00:00
|
|
|
testSetup.aliasService =
|
|
|
|
testSetup.moduleRef.get<AliasService>(AliasService);
|
2021-10-14 18:17:28 +00:00
|
|
|
|
|
|
|
testSetup.app = testSetup.moduleRef.createNestApplication();
|
|
|
|
|
2021-10-21 17:51:23 +00:00
|
|
|
setupSessionMiddleware(
|
|
|
|
testSetup.app,
|
|
|
|
testSetup.configService.get<AuthConfig>('authConfig'),
|
|
|
|
);
|
|
|
|
|
2021-10-14 18:17:28 +00:00
|
|
|
return testSetup;
|
|
|
|
}
|
|
|
|
}
|