]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | import { Component, NgZone } from '@angular/core'; |
2 | import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
b3b6e05e | 3 | import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, Routes } from '@angular/router'; |
9f95a23c TL |
4 | import { RouterTestingModule } from '@angular/router/testing'; |
5 | ||
f67539c2 | 6 | import { configureTestBed } from '~/testing/unit-test-helper'; |
9f95a23c TL |
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; | |
b3b6e05e TL |
14 | let route: ActivatedRouteSnapshot; |
15 | let state: RouterStateSnapshot; | |
9f95a23c TL |
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(() => { | |
f67539c2 TL |
29 | service = TestBed.inject(ChangePasswordGuardService); |
30 | authStorageService = TestBed.inject(AuthStorageService); | |
31 | ngZone = TestBed.inject(NgZone); | |
9f95a23c TL |
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); | |
b3b6e05e | 40 | expect(service.canActivate(route, state)).toBeTruthy(); |
9f95a23c TL |
41 | }); |
42 | ||
43 | it('should do nothing (SSO enabled)', () => { | |
44 | spyOn(authStorageService, 'isLoggedIn').and.returnValue(true); | |
45 | spyOn(authStorageService, 'isSSO').and.returnValue(true); | |
b3b6e05e | 46 | expect(service.canActivate(route, state)).toBeTruthy(); |
9f95a23c TL |
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); | |
b3b6e05e | 52 | expect(service.canActivate(route, state)).toBeTruthy(); |
9f95a23c TL |
53 | }); |
54 | ||
b3b6e05e TL |
55 | it('should redirect to change password page by preserving the query params', fakeAsync(() => { |
56 | route = null; | |
57 | state = { url: '/host', root: null }; | |
9f95a23c TL |
58 | spyOn(authStorageService, 'isLoggedIn').and.returnValue(true); |
59 | spyOn(authStorageService, 'isSSO').and.returnValue(false); | |
60 | spyOn(authStorageService, 'getPwdUpdateRequired').and.returnValue(true); | |
f67539c2 | 61 | const router = TestBed.inject(Router); |
9f95a23c | 62 | ngZone.run(() => { |
b3b6e05e | 63 | expect(service.canActivate(route, state)).toBeFalsy(); |
9f95a23c TL |
64 | }); |
65 | tick(); | |
b3b6e05e | 66 | expect(router.url).toBe('/login-change-password?returnUrl=%2Fhost'); |
9f95a23c TL |
67 | })); |
68 | }); |