Commit 2880adb5 authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

Authorize access when allow access => DONE, WIP: tests

parent 14c4b7e6
......@@ -9,19 +9,26 @@ const routes: Routes = [
{
path: 'search',
loadChildren: () => import('./search/search.module').then(m => m.SearchModule),
canLoad: [NavigationGuard]
// canLoad: [NavigationGuard]
canActivate: [NavigationGuard]
},
{
path: 'search-multiple',
loadChildren: () => import('./search-multiple/search-multiple.module').then(m => m.SearchMultipleModule)
loadChildren: () => import('./search-multiple/search-multiple.module').then(m => m.SearchMultipleModule),
// canLoad: [NavigationGuard]
canActivate: [NavigationGuard]
},
{
path: 'detail/:dname/:objectSelected',
loadChildren: () => import('./detail/detail.module').then(m => m.DetailModule)
loadChildren: () => import('./detail/detail.module').then(m => m.DetailModule),
// canLoad: [NavigationGuard]
canActivate: [NavigationGuard]
},
{
path: 'documentation',
loadChildren: () => import('./documentation/documentation.module').then(m => m.DocumentationModule)
loadChildren: () => import('./documentation/documentation.module').then(m => m.DocumentationModule),
// canLoad: [NavigationGuard]
canActivate: [NavigationGuard]
},
{ path: '**', component: NotFoundPageComponent }
];
......
......@@ -58,7 +58,7 @@ describe('[Core] Component: NavComponent', () => {
const template = fixture.nativeElement;
expect(template.querySelector('#search_link')).toBeTruthy();
expect(template.querySelector('#search_multiple_link')).toBeTruthy();
expect(template.querySelector('#documentation_link')).toBeTruthy();
expect(template.querySelector('#documentation_link')).toBeNull();
});
it('should not display search, search multiple and documentation links if instance config don\'t allows it', () => {
......
import { TestBed } from '@angular/core/testing';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { NavigationGuard } from './navigation.guard';
import * as fromMetamodel from '../metamodel/reducers';
import { INSTANCE } from '../../settings/test-data/instance';
fdescribe('NavigationGuardGuard', () => {
let guard: NavigationGuard;
let store: MockStore;
const initialState = { metamodel: {
...fromMetamodel,
instance: { instance: INSTANCE }
} };
describe('NavigationGuardGuard', () => {
let guard: NavigationGuard;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockStore({ initialState })
]
});
guard = TestBed.inject(NavigationGuard);
});
beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(NavigationGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
it('#isModuleAllowed() should return if module is allowed or not', () => {
guard.isModuleAllowed('search').subscribe(allowed => expect(allowed).toBeTruthy());
guard.isModuleAllowed('search-multiple').subscribe(allowed => expect(allowed).toBeTruthy());
guard.isModuleAllowed('documentation').subscribe(allowed => expect(allowed).toBeFalsy());
});
});
import { Injectable } from '@angular/core';
import { CanLoad, Route, UrlSegment } from '@angular/router';
import {
ActivatedRouteSnapshot,
CanActivate,
CanLoad,
Route,
RouterStateSnapshot,
UrlSegment,
UrlTree
} from '@angular/router';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Actions } from '@ngrx/effects';
import { SearchService } from '../search/store/search.service';
import { ToastrService } from 'ngx-toastr';
import { Store } from '@ngrx/store';
import * as fromRouter from '@ngrx/router-store';
import * as utils from '../shared/utils';
import * as fromSearch from '../search/store/search.reducer';
import { Observable } from 'rxjs';
import { filter, map, take } from 'rxjs/operators';
import * as fromMetamodel from '../metamodel/reducers';
import * as fromConeSearch from '../shared/cone-search/store/cone-search.reducer';
import { Instance } from '../metamodel/model';
@Injectable({
providedIn: 'root'
})
export class NavigationGuard implements CanLoad {
export class NavigationGuard implements CanLoad, CanActivate {
constructor(private store$: Store<{ metamodel: fromMetamodel.State }>) { }
canLoad(
route: Route,
segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
// console.error(route.path);
switch (route.path) {
case 'search':
console.error(this.isSearchAllowed());
return this.isSearchAllowed();
break;
// default:
// return false;
}
const module: string = route.path;
return this.isModuleAllowed(module).pipe(take(1));
}
waitForDataToLoad(): Observable<Instance> {
return this.store$.select(state => state.metamodel.instance.instance);
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
const module: string = next.routeConfig.path;
return this.isModuleAllowed(module);
}
isSearchAllowed(): Observable<boolean> {
return this.waitForDataToLoad().pipe(
switchMap(instance => {
// console.error(instance.config.search);
return of(instance.config.search);
})
)
isModuleAllowed(module: string): Observable<boolean> {
return this.store$.select(state => state.metamodel.instance.instance)
.pipe(
filter( instance => instance !== null),
map(instance => {
if (module === 'search-multiple') {
return instance.config.search_multiple.allowed;
} else {
return instance.config[module];
}
})
);
}
}
......@@ -12,6 +12,6 @@ export const INSTANCE: Instance = {
allowed: true,
all_datasets_selected: true,
},
documentation: true
documentation: false
}
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment