From 60bbcf342021fe8175fb3ad74d90245c07b1105d Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Thu, 13 Feb 2025 14:59:40 +0100 Subject: [PATCH] Request-a-copy improv: Changes to bitstream page to support tests --- .../bitstream-download-page.component.spec.ts | 8 +-- .../bitstream-download-page.component.ts | 56 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.spec.ts b/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.spec.ts index 0cc293c6f7..b32510cba7 100644 --- a/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.spec.ts +++ b/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.spec.ts @@ -73,16 +73,16 @@ describe('BitstreamDownloadPageComponent', () => { self: { href: 'bitstream-self-link' }, }, }); - activatedRoute = { data: observableOf({ - bitstream: createSuccessfulRemoteDataObject( - bitstream, - ), + bitstream: createSuccessfulRemoteDataObject(bitstream), }), params: observableOf({ id: 'testid', }), + queryParams: observableOf({ + accessToken: undefined, + }), }; router = jasmine.createSpyObj('router', ['navigateByUrl']); diff --git a/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.ts b/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.ts index ee329df16e..9a4ee2df97 100644 --- a/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.ts +++ b/src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.ts @@ -11,6 +11,7 @@ import { } from '@angular/core'; import { ActivatedRoute, + Params, Router, } from '@angular/router'; import { TranslateModule } from '@ngx-translate/core'; @@ -83,11 +84,16 @@ export class BitstreamDownloadPageComponent implements OnInit { } ngOnInit(): void { + const accessToken$: Observable = this.route.queryParams.pipe( + map((queryParams: Params) => queryParams?.accessToken || null), + take(1), + ); this.bitstreamRD$ = this.route.data.pipe( map((data) => data.bitstream)); this.bitstream$ = this.bitstreamRD$.pipe( + // TODO: this redirect was commented out earlier... redirectOn4xx(this.router, this.auth), getRemoteDataPayload(), ); @@ -95,12 +101,13 @@ export class BitstreamDownloadPageComponent implements OnInit { this.bitstream$.pipe( switchMap((bitstream: Bitstream) => { const isAuthorized$ = this.authorizationService.isAuthorized(FeatureID.CanDownload, isNotEmpty(bitstream) ? bitstream.self : undefined); + // TODO isAuthorizedByToken check here so we already know if this token is going to be valid? const isLoggedIn$ = this.auth.isAuthenticated(); - return observableCombineLatest([isAuthorized$, isLoggedIn$, observableOf(bitstream)]); + return observableCombineLatest([isAuthorized$, isLoggedIn$, accessToken$, observableOf(bitstream)]); }), - filter(([isAuthorized, isLoggedIn, bitstream]: [boolean, boolean, Bitstream]) => hasValue(isAuthorized) && hasValue(isLoggedIn)), + filter(([isAuthorized, isLoggedIn, accessToken, bitstream]: [boolean, boolean, string, Bitstream]) => (hasValue(isAuthorized) && hasValue(isLoggedIn)) || hasValue(accessToken)), take(1), - switchMap(([isAuthorized, isLoggedIn, bitstream]: [boolean, boolean, Bitstream]) => { + switchMap(([isAuthorized, isLoggedIn, accessToken, bitstream]: [boolean, boolean, string, Bitstream]) => { if (isAuthorized && isLoggedIn) { return this.fileService.retrieveFileDownloadLink(bitstream._links.content.href).pipe( filter((fileLink) => hasValue(fileLink)), @@ -108,20 +115,49 @@ export class BitstreamDownloadPageComponent implements OnInit { map((fileLink) => { return [isAuthorized, isLoggedIn, bitstream, fileLink]; })); + } else if (hasValue(accessToken)) { + // We aren't authorized or logged in, but we might have temp access via the access token + console.log('RETRIEVE WITH ACCESS TOKEN'); + console.log('BUT - we dont want to retrieve the link with access token eh bro'); + // return this.fileService.retrieveFileDownloadLinkWithAccessToken(bitstream._links.content.href, accessToken).pipe( + // filter((fileLink) => hasValue(fileLink)), + // take(1), + // map((fileLink) => { + // return [isAuthorized, isLoggedIn, bitstream, fileLink]; + // })); + return [[isAuthorized, !isLoggedIn, bitstream, '', accessToken]]; } else { return [[isAuthorized, isLoggedIn, bitstream, '']]; } }), - ).subscribe(([isAuthorized, isLoggedIn, bitstream, fileLink]: [boolean, boolean, Bitstream, string]) => { + ).subscribe(([isAuthorized, isLoggedIn, bitstream, fileLink, accessToken]: [boolean, boolean, Bitstream, string, string]) => { + // if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) { + // this.hardRedirectService.redirect(fileLink); + // } else if (isAuthorized && !isLoggedIn) { + // this.hardRedirectService.redirect(bitstream._links.content.href); + // } else if (!isAuthorized && isLoggedIn) { + // this.router.navigateByUrl(getForbiddenRoute(), {skipLocationChange: true}); + // } else if (!isAuthorized && !isLoggedIn) { + // this.auth.setRedirectUrl(this.router.url); + // this.router.navigateByUrl('login'); + // } + if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) { this.hardRedirectService.redirect(fileLink); - } else if (isAuthorized && !isLoggedIn) { + } else if (isAuthorized && !isLoggedIn && !hasValue(accessToken)) { this.hardRedirectService.redirect(bitstream._links.content.href); - } else if (!isAuthorized && isLoggedIn) { - this.router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true }); - } else if (!isAuthorized && !isLoggedIn) { - this.auth.setRedirectUrl(this.router.url); - this.router.navigateByUrl('login'); + } else if (!isAuthorized) { + // Either we have an access token, or we are logged in, or we are not logged in. + // For now, the access token does not care if we are logged in or not. + if (hasValue(accessToken)) { + this.hardRedirectService.redirect(bitstream._links.content.href + '?accessToken=' + accessToken); + // this.router.navigateByUrl(getForbiddenRoute(), {skipLocationChange: true}); + } else if (isLoggedIn) { + this.router.navigateByUrl(getForbiddenRoute(), { skipLocationChange: true }); + } else if (!isLoggedIn) { + this.auth.setRedirectUrl(this.router.url); + this.router.navigateByUrl('login'); + } } }); }