]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/change-password-guard.service.spec.ts
import ceph 16.2.6
[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 { ActivatedRouteSnapshot, Router, RouterStateSnapshot, 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 let route: ActivatedRouteSnapshot;
15 let state: RouterStateSnapshot;
16
17 @Component({ selector: 'cd-login-password-form', template: '' })
18 class LoginPasswordFormComponent {}
19
20 const routes: Routes = [{ path: 'login-change-password', component: LoginPasswordFormComponent }];
21
22 configureTestBed({
23 imports: [RouterTestingModule.withRoutes(routes)],
24 providers: [ChangePasswordGuardService, AuthStorageService],
25 declarations: [LoginPasswordFormComponent]
26 });
27
28 beforeEach(() => {
29 service = TestBed.inject(ChangePasswordGuardService);
30 authStorageService = TestBed.inject(AuthStorageService);
31 ngZone = TestBed.inject(NgZone);
32 });
33
34 it('should be created', () => {
35 expect(service).toBeTruthy();
36 });
37
38 it('should do nothing (not logged in)', () => {
39 spyOn(authStorageService, 'isLoggedIn').and.returnValue(false);
40 expect(service.canActivate(route, state)).toBeTruthy();
41 });
42
43 it('should do nothing (SSO enabled)', () => {
44 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
45 spyOn(authStorageService, 'isSSO').and.returnValue(true);
46 expect(service.canActivate(route, state)).toBeTruthy();
47 });
48
49 it('should do nothing (no update pwd required)', () => {
50 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
51 spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(false);
52 expect(service.canActivate(route, state)).toBeTruthy();
53 });
54
55 it('should redirect to change password page by preserving the query params', fakeAsync(() => {
56 route = null;
57 state = { url: '/host', root: null };
58 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
59 spyOn(authStorageService, 'isSSO').and.returnValue(false);
60 spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(true);
61 const router = TestBed.inject(Router);
62 ngZone.run(() => {
63 expect(service.canActivate(route, state)).toBeFalsy();
64 });
65 tick();
66 expect(router.url).toBe('/login-change-password?returnUrl=%2Fhost');
67 }));
68 });