]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/change-password-guard.service.spec.ts
5bc8d751fd4d974417adcd0e8e369d7c4f043669
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / change-password-guard.service.spec.ts
1 import { Component, NgZone } from '@angular/core';
2 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
3 import { Router, Routes } from '@angular/router';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { configureTestBed } from '~/testing/unit-test-helper';
7 import { AuthStorageService } from './auth-storage.service';
8 import { ChangePasswordGuardService } from './change-password-guard.service';
9
10 describe('ChangePasswordGuardService', () => {
11 let service: ChangePasswordGuardService;
12 let authStorageService: AuthStorageService;
13 let ngZone: NgZone;
14
15 @Component({ selector: 'cd-login-password-form', template: '' })
16 class LoginPasswordFormComponent {}
17
18 const routes: Routes = [{ path: 'login-change-password', component: LoginPasswordFormComponent }];
19
20 configureTestBed({
21 imports: [RouterTestingModule.withRoutes(routes)],
22 providers: [ChangePasswordGuardService, AuthStorageService],
23 declarations: [LoginPasswordFormComponent]
24 });
25
26 beforeEach(() => {
27 service = TestBed.inject(ChangePasswordGuardService);
28 authStorageService = TestBed.inject(AuthStorageService);
29 ngZone = TestBed.inject(NgZone);
30 });
31
32 it('should be created', () => {
33 expect(service).toBeTruthy();
34 });
35
36 it('should do nothing (not logged in)', () => {
37 spyOn(authStorageService, 'isLoggedIn').and.returnValue(false);
38 expect(service.canActivate()).toBeTruthy();
39 });
40
41 it('should do nothing (SSO enabled)', () => {
42 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
43 spyOn(authStorageService, 'isSSO').and.returnValue(true);
44 expect(service.canActivate()).toBeTruthy();
45 });
46
47 it('should do nothing (no update pwd required)', () => {
48 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
49 spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(false);
50 expect(service.canActivate()).toBeTruthy();
51 });
52
53 it('should redirect to change password page', fakeAsync(() => {
54 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
55 spyOn(authStorageService, 'isSSO').and.returnValue(false);
56 spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(true);
57 const router = TestBed.inject(Router);
58 ngZone.run(() => {
59 expect(service.canActivate()).toBeFalsy();
60 });
61 tick();
62 expect(router.url).toBe('/login-change-password');
63 }));
64 });