Skip to content

Commit f594cbf

Browse files
committed
Fix tests for email confirm
1 parent 736fd80 commit f594cbf

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

apps/api/src/auth/auth.controller.ts

-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ import { AuthService } from './auth.service';
1616
import { UserRegisterDto } from './dto/user-register.dto';
1717
import { AuthenticatedGuard } from './guards/authenticated.guard';
1818
import { LocalAuthGuard } from './guards/local-auth.guard';
19-
import { AuthEmailsService } from '../auth-emails/auth-emails.service';
2019

2120
@Controller('auth')
2221
@ApiTags('auth')
2322
export class AuthController {
2423
constructor(
2524
private readonly authService: AuthService,
2625
private readonly demoService: DemoService,
27-
private readonly authEmailsService: AuthEmailsService,
2826
) {}
2927

3028
@UseGuards(LocalAuthGuard)
@@ -52,7 +50,6 @@ export class AuthController {
5250
@Post('register')
5351
async register(@Body() body: UserRegisterDto): Promise<PrivateUserDto> {
5452
const user = await this.authService.registerUser(body);
55-
await this.authEmailsService.sendEmailConfirmation(user._id);
5653
return User.toPrivateDto(user);
5754
}
5855

apps/api/src/auth/auth.module.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ import { SessionSerializer } from './session.serializer';
1111
import { LocalStrategy } from './strategies/local.strategy';
1212

1313
@Module({
14-
imports: [
15-
UsersModule,
16-
DemoModule,
17-
PassportModule.register({ session: true }),
18-
forwardRef(() => AuthEmailsModule),
19-
],
14+
imports: [UsersModule, DemoModule, PassportModule.register({ session: true })],
2015
controllers: [AuthController],
2116
providers: [
2217
AuthService,

apps/api/src/auth/auth.service.spec.ts

+26-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Test, TestingModule } from '@nestjs/testing';
22
import * as bcrypt from 'bcrypt';
33
import { UsersService } from '../models/users/users.service';
44
import { AuthService } from './auth.service';
5+
import { AuthEmailsService } from '../auth-emails/auth-emails.service';
6+
import { Types } from 'mongoose';
57

68
const MOCK_PASSWORD = 'test-password';
79

@@ -27,6 +29,7 @@ describe('AuthService', () => {
2729

2830
create: jest.fn((data) => {
2931
return {
32+
_id: new Types.ObjectId(),
3033
profile: {
3134
email: data.email,
3235
username: data.username,
@@ -38,10 +41,11 @@ describe('AuthService', () => {
3841
}),
3942
};
4043

41-
beforeAll(async () => {
42-
mockPassword = MOCK_PASSWORD;
43-
mockPasswordHashed = await bcrypt.hash(mockPassword, 4);
44-
});
44+
const mockAuthEmailService = {
45+
sendEmailConfirmation: jest.fn(() => Promise.resolve()),
46+
};
47+
48+
const sendEmailConfirmationSpy = jest.spyOn(mockAuthEmailService, 'sendEmailConfirmation');
4549

4650
beforeEach(async () => {
4751
const module: TestingModule = await Test.createTestingModule({
@@ -51,26 +55,36 @@ describe('AuthService', () => {
5155
provide: UsersService,
5256
useValue: mockAuthService,
5357
},
58+
{
59+
provide: AuthEmailsService,
60+
useValue: mockAuthEmailService,
61+
},
5462
],
5563
}).compile();
5664

5765
service = module.get<AuthService>(AuthService);
5866
});
5967

68+
beforeAll(async () => {
69+
mockPassword = MOCK_PASSWORD;
70+
mockPasswordHashed = await bcrypt.hash(mockPassword, 4);
71+
});
72+
6073
it('should be defined', () => {
6174
expect(service).toBeDefined();
6275
});
6376

64-
it('should register user', () => {
65-
expect(
66-
service.registerUser({
67-
68-
username: 'username',
69-
password: 'password',
70-
}),
71-
).resolves.toEqual(
77+
it('should register user', async () => {
78+
const user = await service.registerUser({
79+
80+
username: 'username',
81+
password: 'password',
82+
});
83+
expect(user).toEqual(
7284
expect.objectContaining({ profile: expect.objectContaining({ username: 'username' }) }),
7385
);
86+
expect(sendEmailConfirmationSpy).toBeCalledTimes(1);
87+
expect(sendEmailConfirmationSpy).toBeCalledWith(user._id);
7488
});
7589

7690
describe('User validation', () => {

apps/api/src/auth/auth.service.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { Injectable } from '@nestjs/common';
1+
import { Injectable, Logger } from '@nestjs/common';
22
import * as bcrypt from 'bcrypt';
33
import { UserDocument } from '../models/users/schemas/user.schema';
44
import { UsersService } from '../models/users/users.service';
55
import { UserRegisterDto } from './dto/user-register.dto';
66
import { Types } from 'mongoose';
7+
import { AuthEmailsService } from '../auth-emails/auth-emails.service';
78

89
@Injectable()
910
export class AuthService {
10-
constructor(private usersService: UsersService) {}
11+
constructor(
12+
private usersService: UsersService,
13+
private authEmailService: AuthEmailsService,
14+
) {}
15+
16+
private readonly logger = new Logger(AuthService.name);
1117

1218
async registerUser(data: UserRegisterDto): Promise<UserDocument> {
1319
const hash = await this.hashPassword(data.password);
@@ -18,6 +24,10 @@ export class AuthService {
1824
passwordHash: hash,
1925
});
2026

27+
await this.authEmailService.sendEmailConfirmation(user._id).catch((error) => {
28+
this.logger.error(`Failed to send initial confirmation email for ${user._id} - ${error}`);
29+
});
30+
2131
return user;
2232
}
2333

0 commit comments

Comments
 (0)