feat : maj du mot de passe
This commit is contained in:
@@ -1,50 +1,44 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
|
||||
import { authGuard } from './auth.guard';
|
||||
import { CanActivateFn, Router } from '@angular/router';
|
||||
import { CanActivateFn, Router, UrlTree } from '@angular/router';
|
||||
import { AuthRepository } from '@app/domain/authentification/auth.repository';
|
||||
import { ProfileRepository } from '@app/domain/profiles/profile.repository';
|
||||
import { AUTH_REPOSITORY_TOKEN } from '@app/infrastructure/authentification/auth-repository.token';
|
||||
import { PROFILE_REPOSITORY_TOKEN } from '@app/infrastructure/profiles/profile-repository.token';
|
||||
import { AuthFacade } from '@app/ui/authentification/auth.facade';
|
||||
|
||||
describe('authGuard', () => {
|
||||
let mockRouter: Partial<Router>;
|
||||
|
||||
let mockAuthRepository: jest.Mocked<Partial<AuthRepository>>;
|
||||
let mockProfileRepo: jest.Mocked<Partial<ProfileRepository>>;
|
||||
// 1. Définition des variables pour les Mocks
|
||||
let mockRouter: { parseUrl: jest.Mock };
|
||||
let mockAuthFacade: {
|
||||
verifyEmail: jest.Mock;
|
||||
verifyAuthenticatedUser: jest.Mock;
|
||||
isAuthenticated: jest.Mock;
|
||||
isEmailVerified: jest.Mock;
|
||||
};
|
||||
|
||||
// 2. Fonction helper pour exécuter le guard dans le contexte d'injection
|
||||
const executeGuard: CanActivateFn = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => authGuard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
// 3. Initialisation des Mocks
|
||||
mockRouter = {
|
||||
parseUrl: jest.fn(),
|
||||
};
|
||||
|
||||
mockAuthRepository = {
|
||||
get: jest.fn(),
|
||||
login: jest.fn(),
|
||||
sendVerificationEmail: jest.fn(),
|
||||
logout: jest.fn(),
|
||||
isAuthenticated: jest.fn(),
|
||||
isEmailVerified: jest.fn(),
|
||||
register: jest.fn(),
|
||||
resetPassword: jest.fn(),
|
||||
sendPasswordResetEmail: jest.fn(),
|
||||
};
|
||||
|
||||
mockProfileRepo = {
|
||||
create: jest.fn(),
|
||||
list: jest.fn(),
|
||||
update: jest.fn(),
|
||||
getByUserId: jest.fn(),
|
||||
mockAuthFacade = {
|
||||
verifyEmail: jest.fn(),
|
||||
verifyAuthenticatedUser: jest.fn(),
|
||||
isAuthenticated: jest.fn(), // On changera la valeur de retour selon le test
|
||||
isEmailVerified: jest.fn(), // On changera la valeur de retour selon le test
|
||||
};
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
{ provide: Router, useValue: mockRouter },
|
||||
{ provide: AUTH_REPOSITORY_TOKEN, useValue: mockAuthRepository },
|
||||
{ provide: PROFILE_REPOSITORY_TOKEN, useValue: mockProfileRepo },
|
||||
{ provide: AuthFacade, useValue: mockAuthFacade }, // On fournit la Facade, pas le Repo
|
||||
],
|
||||
});
|
||||
});
|
||||
@@ -53,26 +47,50 @@ describe('authGuard', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
|
||||
/*it('should allow access if user is valid', () => {
|
||||
const mockRoute = {} as any;
|
||||
const mockState = {} as any;
|
||||
// --- SCÉNARIO 1 : L'utilisateur a tout bon ---
|
||||
it('should return true if user is authenticated AND email is verified', () => {
|
||||
// Setup : Tout est OK
|
||||
mockAuthFacade.isAuthenticated.mockReturnValue(true);
|
||||
mockAuthFacade.isEmailVerified.mockReturnValue(true);
|
||||
|
||||
const result = TestBed.runInInjectionContext(() => executeGuard(mockRoute, mockState));
|
||||
expect(result).toEqual(true);
|
||||
const result = executeGuard({} as any, {} as any); // On passe des fausses routes/state
|
||||
|
||||
// Vérifications
|
||||
expect(result).toBe(true);
|
||||
// On vérifie aussi que le guard a bien lancé les vérifications
|
||||
expect(mockAuthFacade.verifyEmail).toHaveBeenCalled();
|
||||
expect(mockAuthFacade.verifyAuthenticatedUser).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should redirect to /auth if user is not valid', () => {
|
||||
mockFacade.isAuthenticated();
|
||||
mockFacade.isEmailVerified();
|
||||
// --- SCÉNARIO 2 : L'utilisateur n'est pas connecté ---
|
||||
it('should redirect to /auth if user is NOT authenticated', () => {
|
||||
// Setup : Pas connecté
|
||||
mockAuthFacade.isAuthenticated.mockReturnValue(false);
|
||||
mockAuthFacade.isEmailVerified.mockReturnValue(true); // Peu importe ici
|
||||
|
||||
const mockRoute = {} as any;
|
||||
const mockState = {} as any;
|
||||
// On prépare le router pour qu'il renvoie une fausse UrlTree
|
||||
const dummyUrlTree = {} as UrlTree;
|
||||
mockRouter.parseUrl.mockReturnValue(dummyUrlTree);
|
||||
|
||||
(mockRouter.parseUrl as jest.Mock).mockReturnValue('/auth');
|
||||
const result = executeGuard({} as any, {} as any);
|
||||
|
||||
const result = TestBed.runInInjectionContext(() => executeGuard(mockRoute, mockState));
|
||||
|
||||
expect(result).toEqual('/auth' as any);
|
||||
// Le guard doit retourner la redirection (UrlTree)
|
||||
expect(result).toBe(dummyUrlTree);
|
||||
expect(mockRouter.parseUrl).toHaveBeenCalledWith('/auth');
|
||||
});*/
|
||||
});
|
||||
|
||||
// --- SCÉNARIO 3 : Connecté mais email non vérifié ---
|
||||
it('should redirect to /auth if user is authenticated but email is NOT verified', () => {
|
||||
// Setup : Connecté mais mail pas bon
|
||||
mockAuthFacade.isAuthenticated.mockReturnValue(true);
|
||||
mockAuthFacade.isEmailVerified.mockReturnValue(false);
|
||||
|
||||
const dummyUrlTree = {} as UrlTree;
|
||||
mockRouter.parseUrl.mockReturnValue(dummyUrlTree);
|
||||
|
||||
const result = executeGuard({} as any, {} as any);
|
||||
|
||||
expect(result).toBe(dummyUrlTree);
|
||||
expect(mockRouter.parseUrl).toHaveBeenCalledWith('/auth');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user