From 916a3da7c0b1f53c24ade9968f6f1517507c4f08 Mon Sep 17 00:00:00 2001 From: alexeh Date: Sun, 15 Sep 2024 06:00:02 +0200 Subject: [PATCH] inject mock email service in test app --- .../authentication/authentication.controller.ts | 4 ++-- .../auth/authentication/authentication.service.ts | 10 ++-------- api/test/utils/mocks/mock-email.service.ts | 14 ++++++++++++++ api/test/utils/test-manager.ts | 7 ++++++- 4 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 api/test/utils/mocks/mock-email.service.ts diff --git a/api/src/modules/auth/authentication/authentication.controller.ts b/api/src/modules/auth/authentication/authentication.controller.ts index b9c4017d..193b55e8 100644 --- a/api/src/modules/auth/authentication/authentication.controller.ts +++ b/api/src/modules/auth/authentication/authentication.controller.ts @@ -13,13 +13,13 @@ export class AuthenticationController { @Public() @Post('signup') async signup(@Body() signupDto: LoginDto) { - return this.authService.signup(signupDto); + return this.authService.signUp(signupDto); } @Public() @UseGuards(LocalAuthGuard) @Post('login') async login(@GetUser() user: User) { - return this.authService.signIn(user); + return this.authService.logIn(user); } } diff --git a/api/src/modules/auth/authentication/authentication.service.ts b/api/src/modules/auth/authentication/authentication.service.ts index c5565ce4..dbaff07b 100644 --- a/api/src/modules/auth/authentication/authentication.service.ts +++ b/api/src/modules/auth/authentication/authentication.service.ts @@ -20,7 +20,7 @@ export class AuthenticationService { throw new UnauthorizedException(`Invalid credentials`); } - async signup(signupDto: LoginDto): Promise { + async signUp(signupDto: LoginDto): Promise { const passwordHash = await bcrypt.hash(signupDto.password, 10); await this.usersService.createUser({ email: signupDto.email, @@ -28,13 +28,7 @@ export class AuthenticationService { }); } - async login(loginDto: LoginDto): Promise<{ access_token: string }> { - const user = await this.validateUser(loginDto.email, loginDto.password); - return { - access_token: this.jwt.sign({ id: user.id }), - }; - } - async signIn(user: User): Promise<{ user: User; accessToken: string }> { + async logIn(user: User): Promise<{ user: User; accessToken: string }> { const payload: JwtPayload = { id: user.id }; const accessToken: string = this.jwt.sign(payload); return { user, accessToken }; diff --git a/api/test/utils/mocks/mock-email.service.ts b/api/test/utils/mocks/mock-email.service.ts new file mode 100644 index 00000000..40291350 --- /dev/null +++ b/api/test/utils/mocks/mock-email.service.ts @@ -0,0 +1,14 @@ +import { + IEmailServiceInterface, + SendMailDTO, +} from '@api/modules/notifications/email/email-service.interface'; +import { Logger } from '@nestjs/common'; + +export class MockEmailService implements IEmailServiceInterface { + logger: Logger = new Logger(MockEmailService.name); + + sendMail = jest.fn(async (sendMailDTO: SendMailDTO): Promise => { + this.logger.log('Mock Email sent'); + return Promise.resolve(); + }); +} diff --git a/api/test/utils/test-manager.ts b/api/test/utils/test-manager.ts index 95ed2db2..121e9b02 100644 --- a/api/test/utils/test-manager.ts +++ b/api/test/utils/test-manager.ts @@ -11,6 +11,8 @@ import { getDataSourceToken } from '@nestjs/typeorm'; import { clearTestDataFromDatabase } from './db-helpers'; import { createUser } from './mocks/entity-mocks'; import { User } from '@shared/entities/users/user.entity'; +import { IEmailServiceToken } from '@api/modules/notifications/email/email-service.interface'; +import { MockEmailService } from './mocks/mock-email.service'; /** * @description: Abstraction for NestJS testing workflow. For now its a basic implementation to create a test app, but can be extended to encapsulate @@ -34,7 +36,10 @@ export class TestManager { static async createTestManager() { const moduleFixture = await Test.createTestingModule({ imports: [AppModule], - }).compile(); + }) + .overrideProvider(IEmailServiceToken) + .useClass(MockEmailService) + .compile(); const dataSource = moduleFixture.get(getDataSourceToken()); const testApp = moduleFixture.createNestApplication(); // TODO: Add global validation or App level Zod when decided what to use