1
0

[DSC-287] use CanActivateChild in order to check every time the rest server availability

This commit is contained in:
Giuseppe Digilio
2021-12-14 15:22:55 +01:00
parent ddcb1ecdf2
commit 44fc86c9fe
3 changed files with 13 additions and 8 deletions

View File

@@ -36,7 +36,9 @@ import { ServerCheckGuard } from './core/server-check/server-check.guard';
RouterModule.forRoot([
{ path: INTERNAL_SERVER_ERROR, component: ThemedPageInternalServerErrorComponent },
{
path: '', canActivate: [ServerCheckGuard, AuthBlockingGuard],
path: '',
canActivate: [AuthBlockingGuard],
canActivateChild: [ServerCheckGuard],
children: [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{

View File

@@ -13,7 +13,8 @@ describe('ServerCheckGuard', () => {
let rootDataServiceStub: SpyObj<RootDataService>;
rootDataServiceStub = jasmine.createSpyObj('RootDataService', {
findRoot: jasmine.createSpy('findRoot')
findRoot: jasmine.createSpy('findRoot'),
invalidateRootCache: jasmine.createSpy('invalidateRootCache')
});
router = jasmine.createSpyObj('Router', {
navigateByUrl: jasmine.createSpy('navigateByUrl')
@@ -37,10 +38,11 @@ describe('ServerCheckGuard', () => {
});
it('should not redirect to error page', () => {
guard.canActivate({} as any, {} as any).pipe(
guard.canActivateChild({} as any, {} as any).pipe(
take(1)
).subscribe((canActivate: boolean) => {
expect(canActivate).toEqual(true);
expect(rootDataServiceStub.invalidateRootCache).not.toHaveBeenCalled();
expect(router.navigateByUrl).not.toHaveBeenCalled();
});
});
@@ -52,10 +54,11 @@ describe('ServerCheckGuard', () => {
});
it('should redirect to error page', () => {
guard.canActivate({} as any, {} as any).pipe(
guard.canActivateChild({} as any, {} as any).pipe(
take(1)
).subscribe((canActivate: boolean) => {
expect(canActivate).toEqual(false);
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalled();
expect(router.navigateByUrl).toHaveBeenCalledWith(getPageInternalServerErrorRoute());
});
});

View File

@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
import { ActivatedRouteSnapshot, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
@@ -16,18 +16,18 @@ import { getFirstCompletedRemoteData } from '../shared/operators';
* A guard that checks if root api endpoint is reachable.
* If not redirect to 500 error page
*/
export class ServerCheckGuard implements CanActivate {
export class ServerCheckGuard implements CanActivateChild {
constructor(private router: Router, private rootDataService: RootDataService) {
}
/**
* True when root api endpoint is reachable.
*/
canActivate(
canActivateChild(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> {
return this.rootDataService.findRoot().pipe(
return this.rootDataService.findRoot(false).pipe(
getFirstCompletedRemoteData(),
map((res: RemoteData<any>) => res.hasSucceeded),
tap((hasSucceeded: boolean) => {