test(auth): couvrir verify-token/create-password pour le ticket #118

Ajoute des tests unitaires sur le flux API de création de mot de passe: forwarding verify-token, rejet confirmation mismatch et validation du comportement lien invalide/expiré côté service.

Made-with: Cursor
This commit is contained in:
2026-04-23 11:53:34 +02:00
parent 29cbbb1d08
commit 3716dfe611
2 changed files with 103 additions and 2 deletions
@@ -1,12 +1,24 @@
import { BadRequestException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { UserService } from '../user/user.service';
describe('AuthController', () => {
let controller: AuthController;
const authServiceMock = {
verifyCreatePasswordToken: jest.fn(),
createPasswordWithToken: jest.fn(),
};
beforeEach(async () => {
jest.clearAllMocks();
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [
{ provide: AuthService, useValue: authServiceMock },
{ provide: UserService, useValue: {} },
],
}).compile();
controller = module.get<AuthController>(AuthController);
@@ -15,4 +27,47 @@ describe('AuthController', () => {
it('should be defined', () => {
expect(controller).toBeDefined();
});
it('forwards token verification to auth service', async () => {
authServiceMock.verifyCreatePasswordToken.mockResolvedValue({
valid: true,
message: 'Token valide',
});
await expect(controller.verifyCreatePasswordToken('tok-123')).resolves.toEqual({
valid: true,
message: 'Token valide',
});
expect(authServiceMock.verifyCreatePasswordToken).toHaveBeenCalledWith('tok-123');
});
it('rejects create-password when confirmation mismatches', async () => {
await expect(
controller.createPassword({
token: 'tok-123',
password: 'Password1',
password_confirmation: 'Password2',
}),
).rejects.toThrow(BadRequestException);
expect(authServiceMock.createPasswordWithToken).not.toHaveBeenCalled();
});
it('calls auth service when create-password payload is valid', async () => {
authServiceMock.createPasswordWithToken.mockResolvedValue({
message: 'Mot de passe créé avec succès. Vous pouvez maintenant vous connecter.',
userId: 'user-1',
});
await expect(
controller.createPassword({
token: 'tok-123',
password: 'Password1',
password_confirmation: 'Password1',
}),
).resolves.toEqual({
message: 'Mot de passe créé avec succès. Vous pouvez maintenant vous connecter.',
userId: 'user-1',
});
expect(authServiceMock.createPasswordWithToken).toHaveBeenCalledWith('tok-123', 'Password1');
});
});