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:
parent
29cbbb1d08
commit
3716dfe611
@ -1,12 +1,24 @@
|
|||||||
|
import { BadRequestException } from '@nestjs/common';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { AuthController } from './auth.controller';
|
import { AuthController } from './auth.controller';
|
||||||
|
import { AuthService } from './auth.service';
|
||||||
|
import { UserService } from '../user/user.service';
|
||||||
|
|
||||||
describe('AuthController', () => {
|
describe('AuthController', () => {
|
||||||
let controller: AuthController;
|
let controller: AuthController;
|
||||||
|
const authServiceMock = {
|
||||||
|
verifyCreatePasswordToken: jest.fn(),
|
||||||
|
createPasswordWithToken: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks();
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
controllers: [AuthController],
|
controllers: [AuthController],
|
||||||
|
providers: [
|
||||||
|
{ provide: AuthService, useValue: authServiceMock },
|
||||||
|
{ provide: UserService, useValue: {} },
|
||||||
|
],
|
||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
controller = module.get<AuthController>(AuthController);
|
controller = module.get<AuthController>(AuthController);
|
||||||
@ -15,4 +27,47 @@ describe('AuthController', () => {
|
|||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(controller).toBeDefined();
|
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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,12 +1,41 @@
|
|||||||
|
import { NotFoundException } from '@nestjs/common';
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
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 { 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;
|
let service: AuthService;
|
||||||
|
const usersRepoMock = {
|
||||||
|
findOne: jest.fn(),
|
||||||
|
createQueryBuilder: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
jest.clearAllMocks();
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
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();
|
}).compile();
|
||||||
|
|
||||||
service = module.get<AuthService>(AuthService);
|
service = module.get<AuthService>(AuthService);
|
||||||
@ -15,4 +44,21 @@ describe('AuthService', () => {
|
|||||||
it('should be defined', () => {
|
it('should be defined', () => {
|
||||||
expect(service).toBeDefined();
|
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',
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user