]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts
import ceph 16.2.7
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / services / module-status-guard.service.spec.ts
index 0948fc878a9e0eb77bed08c016ba60d06e2bcc29..8cce3a8bf8ca115ed14838dfca13c033fb3c0e5b 100644 (file)
@@ -4,9 +4,10 @@ import { fakeAsync, TestBed, tick } from '@angular/core/testing';
 import { ActivatedRouteSnapshot, Router, Routes } from '@angular/router';
 import { RouterTestingModule } from '@angular/router/testing';
 
-import { of as observableOf } from 'rxjs';
+import { of as observableOf, throwError } from 'rxjs';
 
 import { configureTestBed } from '~/testing/unit-test-helper';
+import { MgrModuleService } from '../api/mgr-module.service';
 import { ModuleStatusGuardService } from './module-status-guard.service';
 
 describe('ModuleStatusGuardService', () => {
@@ -15,6 +16,7 @@ describe('ModuleStatusGuardService', () => {
   let router: Router;
   let route: ActivatedRouteSnapshot;
   let ngZone: NgZone;
+  let mgrModuleService: MgrModuleService;
 
   @Component({ selector: 'cd-foo', template: '' })
   class FooComponent {}
@@ -25,9 +27,20 @@ describe('ModuleStatusGuardService', () => {
 
   const routes: Routes = [{ path: '**', component: FooComponent }];
 
-  const testCanActivate = (getResult: {}, activateResult: boolean, urlResult: string) => {
+  const testCanActivate = (
+    getResult: {},
+    activateResult: boolean,
+    urlResult: string,
+    backend = 'cephadm',
+    configOptPermission = true
+  ) => {
     let result: boolean;
     spyOn(httpClient, 'get').and.returnValue(observableOf(getResult));
+    const orchBackend = { orchestrator: backend };
+    const getConfigSpy = spyOn(mgrModuleService, 'getConfig');
+    configOptPermission
+      ? getConfigSpy.and.returnValue(observableOf(orchBackend))
+      : getConfigSpy.and.returnValue(throwError({}));
     ngZone.run(() => {
       service.canActivateChild(route).subscribe((resp) => {
         result = resp;
@@ -48,13 +61,15 @@ describe('ModuleStatusGuardService', () => {
   beforeEach(() => {
     service = TestBed.inject(ModuleStatusGuardService);
     httpClient = TestBed.inject(HttpClient);
+    mgrModuleService = TestBed.inject(MgrModuleService);
     router = TestBed.inject(Router);
     route = new ActivatedRouteSnapshot();
     route.url = [];
     route.data = {
       moduleStatusGuardConfig: {
         apiPath: 'bar',
-        redirectTo: '/foo'
+        redirectTo: '/foo',
+        backend: 'rook'
       }
     };
     ngZone = TestBed.inject(NgZone);
@@ -76,4 +91,12 @@ describe('ModuleStatusGuardService', () => {
   it('should test canActivateChild with status unavailable', fakeAsync(() => {
     testCanActivate(null, false, '/foo');
   }));
+
+  it('should redirect normally if the backend provided matches the current backend', fakeAsync(() => {
+    testCanActivate({ available: true, message: 'foo' }, true, '/', 'rook');
+  }));
+
+  it('should redirect to the "redirectTo" link for user without sufficient permission', fakeAsync(() => {
+    testCanActivate({ available: true, message: 'foo' }, true, '/foo', 'rook', false);
+  }));
 });