]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts
796b42b4373ae5a4e6ffb00c77e4ebf6f20aec22
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / auth-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 { AuthGuardService } from './auth-guard.service';
8 import { AuthStorageService } from './auth-storage.service';
9
10 describe('AuthGuardService', () => {
11 let service: AuthGuardService;
12 let authStorageService: AuthStorageService;
13 let ngZone: NgZone;
14 let route: ActivatedRouteSnapshot;
15 let state: RouterStateSnapshot;
16
17 @Component({ selector: 'cd-login', template: '' })
18 class LoginComponent {}
19
20 const routes: Routes = [{ path: 'login', component: LoginComponent }];
21
22 configureTestBed({
23 imports: [RouterTestingModule.withRoutes(routes)],
24 providers: [AuthGuardService, AuthStorageService],
25 declarations: [LoginComponent]
26 });
27
28 beforeEach(() => {
29 service = TestBed.get(AuthGuardService);
30 authStorageService = TestBed.get(AuthStorageService);
31 ngZone = TestBed.get(NgZone);
32 });
33
34 it('should be created', () => {
35 expect(service).toBeTruthy();
36 });
37
38 it('should allow the user if loggedIn', () => {
39 route = null;
40 state = { url: '/', root: null };
41 spyOn(authStorageService, 'isLoggedIn').and.returnValue(true);
42 expect(service.canActivate(route, state)).toBe(true);
43 });
44
45 it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => {
46 const router = TestBed.get(Router);
47 state = { url: '/pool', root: null };
48 ngZone.run(() => {
49 expect(service.canActivate(route, state)).toBe(false);
50 });
51 tick();
52 expect(router.url).toBe('/login?returnUrl=%2Fpool');
53 }));
54 });