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', () => {
let router: Router;
let route: ActivatedRouteSnapshot;
let ngZone: NgZone;
+ let mgrModuleService: MgrModuleService;
@Component({ selector: 'cd-foo', template: '' })
class FooComponent {}
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;
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);
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);
+ }));
});