Skip to content

Commit 736fd80

Browse files
committed
add auth email controller tests
1 parent 85734b5 commit 736fd80

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,71 @@
11
import { Test, TestingModule } from '@nestjs/testing';
22
import { AuthEmailsController } from './auth-emails.controller';
3+
import { AuthEmailsService } from './auth-emails.service';
4+
import { mockUserRequest } from '../mocks/mock-user-request';
5+
import { Types } from 'mongoose';
6+
import { IResetPasswordDto } from 'shared-types';
37

48
describe('AuthEmailsController', () => {
59
let controller: AuthEmailsController;
610

11+
const mockAuthEmailsService = {
12+
sendEmailConfirmation: jest.fn(),
13+
sendPasswordResetEmail: jest.fn(),
14+
confirmEmailWithToken: jest.fn(),
15+
resetPasswordWithToken: jest.fn(),
16+
};
17+
18+
const sendEmailConfirmationSpy = jest.spyOn(mockAuthEmailsService, 'sendEmailConfirmation');
19+
const sendPasswordResetEmailSpy = jest.spyOn(mockAuthEmailsService, 'sendPasswordResetEmail');
20+
const confirmEmailWithTokenSpy = jest.spyOn(mockAuthEmailsService, 'confirmEmailWithToken');
21+
const resetPasswordWithTokenSpy = jest.spyOn(mockAuthEmailsService, 'resetPasswordWithToken');
22+
723
beforeEach(async () => {
824
const module: TestingModule = await Test.createTestingModule({
925
controllers: [AuthEmailsController],
10-
}).compile();
26+
providers: [AuthEmailsService],
27+
})
28+
.overrideProvider(AuthEmailsService)
29+
.useValue(mockAuthEmailsService)
30+
.compile();
1131

1232
controller = module.get<AuthEmailsController>(AuthEmailsController);
1333
});
1434

35+
afterEach(() => {
36+
jest.clearAllMocks();
37+
});
38+
1539
it('should be defined', () => {
1640
expect(controller).toBeDefined();
1741
});
42+
43+
it('should start email confirmation', async () => {
44+
await controller.startEmailConfirmation(mockUserRequest);
45+
expect(sendEmailConfirmationSpy).toBeCalledWith(expect.any(Types.ObjectId));
46+
});
47+
48+
it('should confirm email', async () => {
49+
const userId = new Types.ObjectId();
50+
await controller.confirmEmail(userId, 'TOKEN');
51+
expect(confirmEmailWithTokenSpy).toBeCalledWith(userId, 'TOKEN');
52+
});
53+
54+
it('should password reset', async () => {
55+
await controller.startPasswordReset('[email protected]');
56+
expect(sendPasswordResetEmailSpy).toBeCalledWith('[email protected]');
57+
});
58+
59+
it('should reset password', async () => {
60+
const userId = new Types.ObjectId();
61+
const dto: IResetPasswordDto = {
62+
user: userId.toString(),
63+
token: 'TOKEN',
64+
password: 'password',
65+
};
66+
67+
await controller.resetPassword(dto);
68+
69+
expect(resetPasswordWithTokenSpy).toBeCalledWith(userId, 'TOKEN', 'password');
70+
});
1871
});

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export class AuthEmailsController {
2424
async confirmEmail(
2525
@Query('user', ParseObjectIdPipe) userId: Types.ObjectId,
2626
@Query('token') token: string,
27-
): Promise<PrivateUserDto> {
28-
const user = await this.authEmailsService.confirmEmailWithToken(userId, token);
29-
return User.toPrivateDto(user);
27+
): Promise<any> {
28+
await this.authEmailsService.confirmEmailWithToken(userId, token);
29+
return { statusCode: 200 };
3030
}
3131

3232
@Post('reset-password/start')

0 commit comments

Comments
 (0)