]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts
update ceph source to reef 18.2.0
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / module-status-guard.service.spec.ts
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';
6
7 import { of as observableOf, throwError } from 'rxjs';
8
9 import { configureTestBed } from '~/testing/unit-test-helper';
10 import { MgrModuleService } from '../api/mgr-module.service';
11 import { ModuleStatusGuardService } from './module-status-guard.service';
12
13 describe('ModuleStatusGuardService', () => {
14 let service: ModuleStatusGuardService;
15 let httpClient: HttpClient;
16 let router: Router;
17 let route: ActivatedRouteSnapshot;
18 let ngZone: NgZone;
19 let mgrModuleService: MgrModuleService;
20
21 @Component({ selector: 'cd-foo', template: '' })
22 class FooComponent {}
23
24 const fakeService = {
25 get: () => true
26 };
27
28 const routes: Routes = [{ path: '**', component: FooComponent }];
29
30 const testCanActivate = (
31 getResult: {},
32 activateResult: boolean,
33 urlResult: string,
34 backend = 'cephadm',
35 configOptPermission = true
36 ) => {
37 let result: boolean;
38 spyOn(httpClient, 'get').and.returnValue(observableOf(getResult));
39 const orchBackend = { orchestrator: backend };
40 const getConfigSpy = spyOn(mgrModuleService, 'getConfig');
41 configOptPermission
42 ? getConfigSpy.and.returnValue(observableOf(orchBackend))
43 : getConfigSpy.and.returnValue(throwError({}));
44 ngZone.run(() => {
45 service.canActivateChild(route).subscribe((resp) => {
46 result = resp;
47 });
48 });
49
50 tick();
51 expect(result).toBe(activateResult);
52 expect(router.url).toBe(urlResult);
53 };
54
55 configureTestBed({
56 imports: [RouterTestingModule.withRoutes(routes)],
57 providers: [ModuleStatusGuardService, { provide: HttpClient, useValue: fakeService }],
58 declarations: [FooComponent]
59 });
60
61 beforeEach(() => {
62 service = TestBed.inject(ModuleStatusGuardService);
63 httpClient = TestBed.inject(HttpClient);
64 mgrModuleService = TestBed.inject(MgrModuleService);
65 router = TestBed.inject(Router);
66 route = new ActivatedRouteSnapshot();
67 route.url = [];
68 route.data = {
69 moduleStatusGuardConfig: {
70 uiApiPath: 'bar',
71 redirectTo: '/foo',
72 backend: 'rook'
73 }
74 };
75 ngZone = TestBed.inject(NgZone);
76 });
77
78 it('should be created', () => {
79 expect(service).toBeTruthy();
80 });
81
82 it('should test canActivate with status available', fakeAsync(() => {
83 route.data.moduleStatusGuardConfig.redirectTo = 'foo';
84 testCanActivate({ available: true, message: 'foo' }, true, '/');
85 }));
86
87 it('should test canActivateChild with status unavailable', fakeAsync(() => {
88 testCanActivate({ available: false, message: null }, false, '/foo');
89 }));
90
91 it('should test canActivateChild with status unavailable', fakeAsync(() => {
92 testCanActivate(null, false, '/foo');
93 }));
94
95 it('should redirect normally if the backend provided matches the current backend', fakeAsync(() => {
96 testCanActivate({ available: true, message: 'foo' }, true, '/', 'rook');
97 }));
98
99 it('should redirect to the "redirectTo" link for user without sufficient permission', fakeAsync(() => {
100 testCanActivate({ available: true, message: 'foo' }, true, '/foo', 'rook', false);
101 }));
102 });