test: allow disabling mocked authentication

This adds a (default true) parameter `withMockAuth` to the
TestSetup class.
If it is false, the TokenAuthGuard is not overridden with a mock
implementation, allowing to test with the real authentication.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-12-09 18:41:55 +01:00
parent bcb75a1d04
commit ff500f1be0

View file

@ -55,7 +55,7 @@ export class TestSetup {
users: User[] = []; users: User[] = [];
public static async create(): Promise<TestSetup> { public static async create(withMockAuth = true): Promise<TestSetup> {
const testSetup = new TestSetup(); const testSetup = new TestSetup();
const routes: Routes = [ const routes: Routes = [
{ {
@ -68,7 +68,7 @@ export class TestSetup {
}, },
]; ];
testSetup.moduleRef = await Test.createTestingModule({ const testingModule = await Test.createTestingModule({
imports: [ imports: [
RouterModule.forRoutes(routes), RouterModule.forRoutes(routes),
TypeOrmModule.forRoot({ TypeOrmModule.forRoot({
@ -104,10 +104,13 @@ export class TestSetup {
FrontendConfigModule, FrontendConfigModule,
IdentityModule, IdentityModule,
], ],
}) });
.overrideGuard(TokenAuthGuard)
.useClass(MockAuthGuard) if (withMockAuth) {
.compile(); testingModule.overrideGuard(TokenAuthGuard).useClass(MockAuthGuard);
}
testSetup.moduleRef = await testingModule.compile();
testSetup.userService = testSetup.moduleRef.get<UsersService>(UsersService); testSetup.userService = testSetup.moduleRef.get<UsersService>(UsersService);
testSetup.configService = testSetup.configService =