From a1e7d653f2e90e47376d5e4c708f6318d92e8c00 Mon Sep 17 00:00:00 2001 From: Kim Shepherd Date: Mon, 24 Mar 2025 13:10:47 +0100 Subject: [PATCH] Request-a-copy: Unit tests --- .../data/item-request-data.service.spec.ts | 10 ++-- .../email-request-copy.component.spec.ts | 4 +- .../file-download-link.component.spec.ts | 49 +++++++++++++++++-- src/app/shared/testing/active-router.stub.ts | 1 + 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/app/core/data/item-request-data.service.spec.ts b/src/app/core/data/item-request-data.service.spec.ts index b9cfe689fa..59a497777f 100644 --- a/src/app/core/data/item-request-data.service.spec.ts +++ b/src/app/core/data/item-request-data.service.spec.ts @@ -42,7 +42,7 @@ describe('ItemRequestDataService', () => { case 'request.item.grant.link.period': return createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { name: 'request.item.grant.link.period', - values: ['3600', '7200', '86400'], + values: ['FOREVER', '+1DAY', '+1MONTH'], })); default: return createSuccessfulRemoteDataObject$(new ConfigurationProperty()); @@ -116,7 +116,7 @@ describe('ItemRequestDataService', () => { }); it('should send a PUT request containing the correct properties', (done) => { - service.grant(itemRequest.token, email, true).subscribe(() => { + service.grant(itemRequest.token, email, true, '+1DAY').subscribe(() => { expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ method: RestRequestMethod.PUT, href: `${restApiEndpoint}/${itemRequest.token}`, @@ -125,7 +125,7 @@ describe('ItemRequestDataService', () => { responseMessage: email.message, subject: email.subject, suggestOpenAccess: true, - accessPeriod: 0, + accessPeriod: '+1DAY', }), options: jasmine.objectContaining({ headers: jasmine.any(HttpHeaders), @@ -153,7 +153,7 @@ describe('ItemRequestDataService', () => { responseMessage: email.message, subject: email.subject, suggestOpenAccess: false, - accessPeriod: 0, + accessPeriod: null, }), options: jasmine.objectContaining({ headers: jasmine.any(HttpHeaders), @@ -187,7 +187,7 @@ describe('ItemRequestDataService', () => { describe('getConfiguredAccessPeriods', () => { it('should return parsed integer values from config', () => { service.getConfiguredAccessPeriods().subscribe(periods => { - expect(periods).toEqual([3600, 7200, 86400]); + expect(periods).toEqual(['FOREVER', '+1DAY', '+1MONTH']); }); }); }); diff --git a/src/app/request-copy/email-request-copy/email-request-copy.component.spec.ts b/src/app/request-copy/email-request-copy/email-request-copy.component.spec.ts index 9fa32be58b..e212de9d11 100644 --- a/src/app/request-copy/email-request-copy/email-request-copy.component.spec.ts +++ b/src/app/request-copy/email-request-copy/email-request-copy.component.spec.ts @@ -7,6 +7,7 @@ import { } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { TranslateModule } from '@ngx-translate/core'; +import { of } from 'rxjs'; import { VarDirective } from '../../shared/utils/var.directive'; import { EmailRequestCopyComponent } from './email-request-copy.component'; @@ -33,6 +34,8 @@ describe('EmailRequestCopyComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(EmailRequestCopyComponent); component = fixture.componentInstance; + // Set validAccessPeriods$ before detectChanges calls ngOnInit + component.validAccessPeriods$ = of(['FOREVER']); fixture.detectChanges(); }); @@ -45,7 +48,6 @@ describe('EmailRequestCopyComponent', () => { spyOn(component.send, 'emit').and.stub(); component.subject = 'test-subject'; component.message = 'test-message'; - component.validAccessPeriods = ['FOREVER']; component.submit(); expect(component.send.emit).toHaveBeenCalledWith(new RequestCopyEmail('test-subject', 'test-message')); }); diff --git a/src/app/shared/file-download-link/file-download-link.component.spec.ts b/src/app/shared/file-download-link/file-download-link.component.spec.ts index 2a4dfe2a36..f66863e3a7 100644 --- a/src/app/shared/file-download-link/file-download-link.component.spec.ts +++ b/src/app/shared/file-download-link/file-download-link.component.spec.ts @@ -27,6 +27,7 @@ import { getItemModuleRoute } from '../../item-page/item-page-routing-paths'; import { ActivatedRouteStub } from '../testing/active-router.stub'; import { RouterLinkDirectiveStub } from '../testing/router-link-directive.stub'; import { FileDownloadLinkComponent } from './file-download-link.component'; +import { ItemRequest } from '../../core/shared/item-request.model'; describe('FileDownloadLinkComponent', () => { let component: FileDownloadLinkComponent; @@ -39,6 +40,13 @@ describe('FileDownloadLinkComponent', () => { let item: Item; let storeMock: any; + const itemRequestStub = Object.assign(new ItemRequest(), { + token: 'item-request-token', + requestName: 'requester name', + accessToken: 'abc123', + allfiles: true, + }); + function init() { authorizationService = jasmine.createSpyObj('authorizationService', { isAuthorized: cold('-a', { a: true }), @@ -62,7 +70,8 @@ describe('FileDownloadLinkComponent', () => { }); } - function initTestbed() { + function initTestbed(itemRequest = null) { + const activatedRoute = new ActivatedRouteStub({}, { itemRequest: itemRequest }); TestBed.configureTestingModule({ imports: [ TranslateModule.forRoot(), @@ -71,7 +80,7 @@ describe('FileDownloadLinkComponent', () => { providers: [ RouterLinkDirectiveStub, { provide: AuthorizationDataService, useValue: authorizationService }, - { provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, + { provide: ActivatedRoute, useValue: activatedRoute }, { provide: Store, useValue: storeMock }, { provide: APP_DATA_SERVICES_MAP, useValue: {} }, ], @@ -85,6 +94,9 @@ describe('FileDownloadLinkComponent', () => { describe('init', () => { describe('getBitstreamPath', () => { + + + describe('when the user has download rights', () => { beforeEach(waitForAsync(() => { scheduler = getTestScheduler(); @@ -113,6 +125,7 @@ describe('FileDownloadLinkComponent', () => { expect(lock).toBeNull(); }); }); + describe('when the user has no download rights but has the right to request a copy', () => { beforeEach(waitForAsync(() => { scheduler = getTestScheduler(); @@ -146,6 +159,7 @@ describe('FileDownloadLinkComponent', () => { expect(lock).toBeTruthy(); }); }); + describe('when the user has no download rights and no request a copy rights', () => { beforeEach(waitForAsync(() => { scheduler = getTestScheduler(); @@ -165,7 +179,7 @@ describe('FileDownloadLinkComponent', () => { expect(component.canDownload$).toBeObservable(cold('--a', { a: false })); }); - it('should init the component', () => { + it('should init the component and show the locked icon', () => { scheduler.flush(); fixture.detectChanges(); const link = fixture.debugElement.query(By.css('a')); @@ -174,6 +188,35 @@ describe('FileDownloadLinkComponent', () => { expect(lock).toBeTruthy(); }); }); + + describe('when the user has no (normal) download rights and request a copy rights via access token', () => { + beforeEach(waitForAsync(() => { + scheduler = getTestScheduler(); + init(); + (authorizationService.isAuthorized as jasmine.Spy).and.returnValue(cold('-a', { a: false })); + initTestbed(itemRequestStub); + })); + beforeEach(() => { + fixture = TestBed.createComponent(FileDownloadLinkComponent); + component = fixture.componentInstance; + component.bitstream = bitstream; + component.item = item; + fixture.detectChanges(); + }); + it('should return the bitstreamPath based on the access token and request-a-copy path', () => { + expect(component.bitstreamPath$).toBeObservable(cold('-a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: { accessToken: 'abc123' } } })); + expect(component.canDownload$).toBeObservable(cold('--a', { a: false })); + + }); + it('should init the component and show an open lock', () => { + scheduler.flush(); + fixture.detectChanges(); + const link = fixture.debugElement.query(By.css('a')); + expect(link.injector.get(RouterLinkDirectiveStub).routerLink).toContain(new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString()); + const lock = fixture.debugElement.query(By.css('.fa-lock-open')).nativeElement; + expect(lock).toBeTruthy(); + }); + }); }); }); }); diff --git a/src/app/shared/testing/active-router.stub.ts b/src/app/shared/testing/active-router.stub.ts index 4d58d61769..a9c1f0edd9 100644 --- a/src/app/shared/testing/active-router.stub.ts +++ b/src/app/shared/testing/active-router.stub.ts @@ -62,6 +62,7 @@ export class ActivatedRouteStub { paramMap: convertToParamMap(this.params), queryParamMap: convertToParamMap(this.testParams), queryParams: {} as Params, + data: this.testData, }; } }