From 1becc9b3d2e462ca8b3516335366589c2a2d5aa5 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Wed, 3 Feb 2021 21:49:39 +0100 Subject: [PATCH] Tests: Fix Mock Auth This makes it possible to create the user before the mock auth guard does it's magic. This is necessary for some test, where we need the user object before the api is called. Signed-off-by: Philip Molares --- src/auth/mock-auth.guard.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/auth/mock-auth.guard.ts b/src/auth/mock-auth.guard.ts index d4f1236b0..5f1e40c6b 100644 --- a/src/auth/mock-auth.guard.ts +++ b/src/auth/mock-auth.guard.ts @@ -16,7 +16,13 @@ export class MockAuthGuard { async canActivate(context: ExecutionContext) { const req = context.switchToHttp().getRequest(); if (!this.user) { - this.user = await this.usersService.createUser('hardcoded', 'Testy'); + // this assures that we can create the user 'hardcoded', if we need them before any calls are made or + // create them on the fly when the first call to the api is made + try { + this.user = await this.usersService.getUserByUsername('hardcoded'); + } catch (e) { + this.user = await this.usersService.createUser('hardcoded', 'Testy'); + } } req.user = this.user; return true;