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