diff --git a/backend/src/routes/auth/auth.controller.spec.ts b/backend/src/routes/auth/auth.controller.spec.ts index 27a31e6..e1b11aa 100644 --- a/backend/src/routes/auth/auth.controller.spec.ts +++ b/backend/src/routes/auth/auth.controller.spec.ts @@ -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); @@ -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'); + }); }); diff --git a/backend/src/routes/auth/auth.service.spec.ts b/backend/src/routes/auth/auth.service.spec.ts index 800ab66..03812b4 100644 --- a/backend/src/routes/auth/auth.service.spec.ts +++ b/backend/src/routes/auth/auth.service.spec.ts @@ -1,12 +1,41 @@ +import { NotFoundException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; +import { JwtService } from '@nestjs/jwt'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { ConfigService } from '@nestjs/config'; import { AuthService } from './auth.service'; +import { UserService } from '../user/user.service'; +import { AppConfigService } from 'src/modules/config/config.service'; +import { MailService } from 'src/modules/mail/mail.service'; +import { NumeroDossierService } from 'src/modules/numero-dossier/numero-dossier.service'; +import { Parents } from 'src/entities/parents.entity'; +import { Users } from 'src/entities/users.entity'; +import { Children } from 'src/entities/children.entity'; +import { AssistanteMaternelle } from 'src/entities/assistantes_maternelles.entity'; -describe('AuthService', () => { +describe('AuthService (#118 create-password API)', () => { let service: AuthService; + const usersRepoMock = { + findOne: jest.fn(), + createQueryBuilder: jest.fn(), + }; beforeEach(async () => { + jest.clearAllMocks(); const module: TestingModule = await Test.createTestingModule({ - providers: [AuthService], + providers: [ + AuthService, + { provide: UserService, useValue: {} }, + { provide: JwtService, useValue: {} }, + { provide: ConfigService, useValue: { get: jest.fn() } }, + { provide: AppConfigService, useValue: {} }, + { provide: MailService, useValue: {} }, + { provide: NumeroDossierService, useValue: {} }, + { provide: getRepositoryToken(Parents), useValue: {} }, + { provide: getRepositoryToken(Users), useValue: usersRepoMock }, + { provide: getRepositoryToken(Children), useValue: {} }, + { provide: getRepositoryToken(AssistanteMaternelle), useValue: {} }, + ], }).compile(); service = module.get(AuthService); @@ -15,4 +44,21 @@ describe('AuthService', () => { it('should be defined', () => { expect(service).toBeDefined(); }); + + it('returns 404-like error when verify token is missing', async () => { + await expect(service.verifyCreatePasswordToken('')).rejects.toThrow(NotFoundException); + }); + + it('returns valid=true when token exists and is not expired', async () => { + usersRepoMock.findOne.mockResolvedValue({ + id: 'u1', + password: null, + token_creation_mdp_expire_le: new Date(Date.now() + 60_000), + }); + + await expect(service.verifyCreatePasswordToken('tok-123')).resolves.toEqual({ + valid: true, + message: 'Token valide', + }); + }); });