From b8d282ebe4ba14765e2cb5d38817934e12f1bb98 Mon Sep 17 00:00:00 2001 From: Alexandre Vryghem Date: Sun, 11 Jun 2023 16:17:14 +0200 Subject: [PATCH] Fix proxy timeout error for browse by pages --- src/app/browse-by/browse-by-guard.spec.ts | 35 +++++++++++++++-- src/app/browse-by/browse-by-guard.ts | 48 ++++++++++++++--------- 2 files changed, 62 insertions(+), 21 deletions(-) diff --git a/src/app/browse-by/browse-by-guard.spec.ts b/src/app/browse-by/browse-by-guard.spec.ts index fc483d87e23..7f57c17ac13 100644 --- a/src/app/browse-by/browse-by-guard.spec.ts +++ b/src/app/browse-by/browse-by-guard.spec.ts @@ -1,10 +1,10 @@ import { first } from 'rxjs/operators'; import { BrowseByGuard } from './browse-by-guard'; import { of as observableOf } from 'rxjs'; -import { BrowseDefinitionDataService } from '../core/browse/browse-definition-data.service'; -import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; +import { createFailedRemoteDataObject$, createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; import { BrowseDefinition } from '../core/shared/browse-definition.model'; import { BrowseByDataType } from './browse-by-switcher/browse-by-decorator'; +import { RouterStub } from '../shared/testing/router.stub'; describe('BrowseByGuard', () => { describe('canActivate', () => { @@ -12,6 +12,7 @@ describe('BrowseByGuard', () => { let dsoService: any; let translateService: any; let browseDefinitionService: any; + let router: any; const name = 'An interesting DSO'; const title = 'Author'; @@ -34,7 +35,9 @@ describe('BrowseByGuard', () => { findById: () => createSuccessfulRemoteDataObject$(browseDefinition) }; - guard = new BrowseByGuard(dsoService, translateService, browseDefinitionService); + router = new RouterStub() as any; + + guard = new BrowseByGuard(dsoService, translateService, browseDefinitionService, router); }); it('should return true, and sets up the data correctly, with a scope and value', () => { @@ -64,6 +67,7 @@ describe('BrowseByGuard', () => { value: '"' + value + '"' }; expect(scopedRoute.data).toEqual(result); + expect(router.navigate).not.toHaveBeenCalled(); expect(canActivate).toEqual(true); } ); @@ -96,6 +100,7 @@ describe('BrowseByGuard', () => { value: '' }; expect(scopedNoValueRoute.data).toEqual(result); + expect(router.navigate).not.toHaveBeenCalled(); expect(canActivate).toEqual(true); } ); @@ -127,9 +132,33 @@ describe('BrowseByGuard', () => { value: '"' + value + '"' }; expect(route.data).toEqual(result); + expect(router.navigate).not.toHaveBeenCalled(); expect(canActivate).toEqual(true); } ); }); + + it('should return false, and sets up the data correctly, without a scope and with a value', () => { + jasmine.getEnv().allowRespy(true); + spyOn(browseDefinitionService, 'findById').and.returnValue(createFailedRemoteDataObject$()); + const scopedRoute = { + data: { + title: field, + }, + params: { + id, + }, + queryParams: { + scope, + value + } + }; + guard.canActivate(scopedRoute as any, undefined) + .pipe(first()) + .subscribe((canActivate) => { + expect(router.navigate).toHaveBeenCalled(); + expect(canActivate).toEqual(false); + }); + }); }); }); diff --git a/src/app/browse-by/browse-by-guard.ts b/src/app/browse-by/browse-by-guard.ts index e4582cb77a1..ed6a6275580 100644 --- a/src/app/browse-by/browse-by-guard.ts +++ b/src/app/browse-by/browse-by-guard.ts @@ -1,13 +1,15 @@ -import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router'; +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { Injectable } from '@angular/core'; import { DSpaceObjectDataService } from '../core/data/dspace-object-data.service'; import { hasNoValue, hasValue } from '../shared/empty.util'; import { map, switchMap } from 'rxjs/operators'; -import { getFirstSucceededRemoteData, getFirstSucceededRemoteDataPayload } from '../core/shared/operators'; +import { getFirstCompletedRemoteData, getFirstSucceededRemoteData, } from '../core/shared/operators'; import { TranslateService } from '@ngx-translate/core'; import { Observable, of as observableOf } from 'rxjs'; import { BrowseDefinitionDataService } from '../core/browse/browse-definition-data.service'; import { BrowseDefinition } from '../core/shared/browse-definition.model'; +import { RemoteData } from '../core/data/remote-data'; +import { PAGE_NOT_FOUND_PATH } from '../app-routing-paths'; @Injectable() /** @@ -17,15 +19,20 @@ export class BrowseByGuard implements CanActivate { constructor(protected dsoService: DSpaceObjectDataService, protected translate: TranslateService, - protected browseDefinitionService: BrowseDefinitionDataService) { + protected browseDefinitionService: BrowseDefinitionDataService, + protected router: Router, + ) { } - canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { const title = route.data.title; const id = route.params.id || route.queryParams.id || route.data.id; - let browseDefinition$: Observable; + let browseDefinition$: Observable; if (hasNoValue(route.data.browseDefinition) && hasValue(id)) { - browseDefinition$ = this.browseDefinitionService.findById(id).pipe(getFirstSucceededRemoteDataPayload()); + browseDefinition$ = this.browseDefinitionService.findById(id).pipe( + getFirstCompletedRemoteData(), + map((browseDefinitionRD: RemoteData) => browseDefinitionRD.payload), + ); } else { browseDefinition$ = observableOf(route.data.browseDefinition); } @@ -33,19 +40,24 @@ export class BrowseByGuard implements CanActivate { const value = route.queryParams.value; const metadataTranslated = this.translate.instant('browse.metadata.' + id); return browseDefinition$.pipe( - switchMap((browseDefinition) => { - if (hasValue(scope)) { - const dsoAndMetadata$ = this.dsoService.findById(scope).pipe(getFirstSucceededRemoteData()); - return dsoAndMetadata$.pipe( - map((dsoRD) => { - const name = dsoRD.payload.name; - route.data = this.createData(title, id, browseDefinition, name, metadataTranslated, value, route); - return true; - }) - ); + switchMap((browseDefinition: BrowseDefinition | undefined) => { + if (hasValue(browseDefinition)) { + if (hasValue(scope)) { + const dsoAndMetadata$ = this.dsoService.findById(scope).pipe(getFirstSucceededRemoteData()); + return dsoAndMetadata$.pipe( + map((dsoRD) => { + const name = dsoRD.payload.name; + route.data = this.createData(title, id, browseDefinition, name, metadataTranslated, value, route); + return true; + }) + ); + } else { + route.data = this.createData(title, id, browseDefinition, '', metadataTranslated, value, route); + return observableOf(true); + } } else { - route.data = this.createData(title, id, browseDefinition, '', metadataTranslated, value, route); - return observableOf(true); + void this.router.navigate([PAGE_NOT_FOUND_PATH]); + return observableOf(false); } }) );