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:
@@ -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>(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',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user