Request-a-copy improv: Changes to bitstream page to support tests

This commit is contained in:
Kim Shepherd
2025-02-13 14:59:40 +01:00
parent 0de6481c24
commit 60bbcf3420
2 changed files with 50 additions and 14 deletions

View File

@@ -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']);

View File

@@ -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<string> = 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,21 +115,50 @@ 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) {
} 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 (!isAuthorized && !isLoggedIn) {
} else if (!isLoggedIn) {
this.auth.setRedirectUrl(this.router.url);
this.router.navigateByUrl('login');
}
}
});
}