Skip to content

Commit

Permalink
inject mock email service in test app
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 15, 2024
1 parent 6ea1a2c commit 916a3da
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 2 additions & 8 deletions api/src/modules/auth/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@ export class AuthenticationService {
throw new UnauthorizedException(`Invalid credentials`);
}

async signup(signupDto: LoginDto): Promise<void> {
async signUp(signupDto: LoginDto): Promise<void> {
const passwordHash = await bcrypt.hash(signupDto.password, 10);
await this.usersService.createUser({
email: signupDto.email,
password: passwordHash,
});
}

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 };
Expand Down
14 changes: 14 additions & 0 deletions api/test/utils/mocks/mock-email.service.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
this.logger.log('Mock Email sent');
return Promise.resolve();
});
}
7 changes: 6 additions & 1 deletion api/test/utils/test-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<DataSource>(getDataSourceToken());
const testApp = moduleFixture.createNestApplication();
// TODO: Add global validation or App level Zod when decided what to use
Expand Down

0 comments on commit 916a3da

Please sign in to comment.