1 import { HttpClient } from '@angular/common/http';
2 import { Component, NgZone } from '@angular/core';
3 import { fakeAsync, TestBed, tick } from '@angular/core/testing';
4 import { ActivatedRouteSnapshot, Router, Routes } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
7 import { of as observableOf, throwError } from 'rxjs';
9 import { configureTestBed } from '~/testing/unit-test-helper';
10 import { MgrModuleService } from '../api/mgr-module.service';
11 import { ModuleStatusGuardService } from './module-status-guard.service';
13 describe('ModuleStatusGuardService', () => {
14 let service: ModuleStatusGuardService;
15 let httpClient: HttpClient;
17 let route: ActivatedRouteSnapshot;
19 let mgrModuleService: MgrModuleService;
21 @Component({ selector: 'cd-foo', template: '' })
28 const routes: Routes = [{ path: '**', component: FooComponent }];
30 const testCanActivate = (
32 activateResult: boolean,
35 configOptPermission = true
38 spyOn(httpClient, 'get').and.returnValue(observableOf(getResult));
39 const orchBackend = { orchestrator: backend };
40 const getConfigSpy = spyOn(mgrModuleService, 'getConfig');
42 ? getConfigSpy.and.returnValue(observableOf(orchBackend))
43 : getConfigSpy.and.returnValue(throwError({}));
45 service.canActivateChild(route).subscribe((resp) => {
51 expect(result).toBe(activateResult);
52 expect(router.url).toBe(urlResult);
56 imports: [RouterTestingModule.withRoutes(routes)],
57 providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }],
58 declarations: [FooComponent]
62 service = TestBed.inject(ModuleStatusGuardService);
63 httpClient = TestBed.inject(HttpClient);
64 mgrModuleService = TestBed.inject(MgrModuleService);
65 router = TestBed.inject(Router);
66 route = new ActivatedRouteSnapshot();
69 moduleStatusGuardConfig: {
75 ngZone = TestBed.inject(NgZone);
78 it('should be created', () => {
79 expect(service).toBeTruthy();
82 it('should test canActivate with status available', fakeAsync(() => {
83 route.data.moduleStatusGuardConfig.redirectTo = 'foo';
84 testCanActivate({ available: true, message: 'foo' }, true, '/');
87 it('should test canActivateChild with status unavailable', fakeAsync(() => {
88 testCanActivate({ available: false, message: null }, false, '/foo');
91 it('should test canActivateChild with status unavailable', fakeAsync(() => {
92 testCanActivate(null, false, '/foo');
95 it('should redirect normally if the backend provided matches the current backend', fakeAsync(() => {
96 testCanActivate({ available: true, message: 'foo' }, true, '/', 'rook');
99 it('should redirect to the "redirectTo" link for user without sufficient permission', fakeAsync(() => {
100 testCanActivate({ available: true, message: 'foo' }, true, '/foo', 'rook', false);