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';
getResult: {},
activateResult: boolean,
urlResult: string,
- backend = 'cephadm'
+ backend = 'cephadm',
+ configOptPermission = true
) => {
let result: boolean;
spyOn(httpClient, 'get').and.returnValue(observableOf(getResult));
- const test = { orchestrator: backend };
- spyOn(mgrModuleService, 'getConfig').and.returnValue(observableOf(test));
+ 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;
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);
+ }));
});
const config = route.data['moduleStatusGuardConfig'];
let backendCheck = false;
if (config.backend) {
- this.mgrModuleService.getConfig('orchestrator').subscribe((resp) => {
- backendCheck = config.backend === resp['orchestrator'];
- });
+ this.mgrModuleService.getConfig('orchestrator').subscribe(
+ (resp) => {
+ backendCheck = config.backend === resp['orchestrator'];
+ },
+ () => {
+ this.router.navigate([config.redirectTo]);
+ return observableOf(false);
+ }
+ );
}
return this.http.get(`api/${config.apiPath}/status`).pipe(
map((resp: any) => {