Request-a-copy: Unit tests

This commit is contained in:
Kim Shepherd
2025-03-24 13:10:47 +01:00
parent 1fff3b5b86
commit a1e7d653f2
4 changed files with 55 additions and 9 deletions

View File

@@ -42,7 +42,7 @@ describe('ItemRequestDataService', () => {
case 'request.item.grant.link.period': case 'request.item.grant.link.period':
return createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), { return createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), {
name: 'request.item.grant.link.period', name: 'request.item.grant.link.period',
values: ['3600', '7200', '86400'], values: ['FOREVER', '+1DAY', '+1MONTH'],
})); }));
default: default:
return createSuccessfulRemoteDataObject$(new ConfigurationProperty()); return createSuccessfulRemoteDataObject$(new ConfigurationProperty());
@@ -116,7 +116,7 @@ describe('ItemRequestDataService', () => {
}); });
it('should send a PUT request containing the correct properties', (done) => { 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({ expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({
method: RestRequestMethod.PUT, method: RestRequestMethod.PUT,
href: `${restApiEndpoint}/${itemRequest.token}`, href: `${restApiEndpoint}/${itemRequest.token}`,
@@ -125,7 +125,7 @@ describe('ItemRequestDataService', () => {
responseMessage: email.message, responseMessage: email.message,
subject: email.subject, subject: email.subject,
suggestOpenAccess: true, suggestOpenAccess: true,
accessPeriod: 0, accessPeriod: '+1DAY',
}), }),
options: jasmine.objectContaining({ options: jasmine.objectContaining({
headers: jasmine.any(HttpHeaders), headers: jasmine.any(HttpHeaders),
@@ -153,7 +153,7 @@ describe('ItemRequestDataService', () => {
responseMessage: email.message, responseMessage: email.message,
subject: email.subject, subject: email.subject,
suggestOpenAccess: false, suggestOpenAccess: false,
accessPeriod: 0, accessPeriod: null,
}), }),
options: jasmine.objectContaining({ options: jasmine.objectContaining({
headers: jasmine.any(HttpHeaders), headers: jasmine.any(HttpHeaders),
@@ -187,7 +187,7 @@ describe('ItemRequestDataService', () => {
describe('getConfiguredAccessPeriods', () => { describe('getConfiguredAccessPeriods', () => {
it('should return parsed integer values from config', () => { it('should return parsed integer values from config', () => {
service.getConfiguredAccessPeriods().subscribe(periods => { service.getConfiguredAccessPeriods().subscribe(periods => {
expect(periods).toEqual([3600, 7200, 86400]); expect(periods).toEqual(['FOREVER', '+1DAY', '+1MONTH']);
}); });
}); });
}); });

View File

@@ -7,6 +7,7 @@ import {
} from '@angular/core/testing'; } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing'; import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import { VarDirective } from '../../shared/utils/var.directive'; import { VarDirective } from '../../shared/utils/var.directive';
import { EmailRequestCopyComponent } from './email-request-copy.component'; import { EmailRequestCopyComponent } from './email-request-copy.component';
@@ -33,6 +34,8 @@ describe('EmailRequestCopyComponent', () => {
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(EmailRequestCopyComponent); fixture = TestBed.createComponent(EmailRequestCopyComponent);
component = fixture.componentInstance; component = fixture.componentInstance;
// Set validAccessPeriods$ before detectChanges calls ngOnInit
component.validAccessPeriods$ = of(['FOREVER']);
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -45,7 +48,6 @@ describe('EmailRequestCopyComponent', () => {
spyOn(component.send, 'emit').and.stub(); spyOn(component.send, 'emit').and.stub();
component.subject = 'test-subject'; component.subject = 'test-subject';
component.message = 'test-message'; component.message = 'test-message';
component.validAccessPeriods = ['FOREVER'];
component.submit(); component.submit();
expect(component.send.emit).toHaveBeenCalledWith(new RequestCopyEmail('test-subject', 'test-message')); expect(component.send.emit).toHaveBeenCalledWith(new RequestCopyEmail('test-subject', 'test-message'));
}); });

View File

@@ -27,6 +27,7 @@ import { getItemModuleRoute } from '../../item-page/item-page-routing-paths';
import { ActivatedRouteStub } from '../testing/active-router.stub'; import { ActivatedRouteStub } from '../testing/active-router.stub';
import { RouterLinkDirectiveStub } from '../testing/router-link-directive.stub'; import { RouterLinkDirectiveStub } from '../testing/router-link-directive.stub';
import { FileDownloadLinkComponent } from './file-download-link.component'; import { FileDownloadLinkComponent } from './file-download-link.component';
import { ItemRequest } from '../../core/shared/item-request.model';
describe('FileDownloadLinkComponent', () => { describe('FileDownloadLinkComponent', () => {
let component: FileDownloadLinkComponent; let component: FileDownloadLinkComponent;
@@ -39,6 +40,13 @@ describe('FileDownloadLinkComponent', () => {
let item: Item; let item: Item;
let storeMock: any; let storeMock: any;
const itemRequestStub = Object.assign(new ItemRequest(), {
token: 'item-request-token',
requestName: 'requester name',
accessToken: 'abc123',
allfiles: true,
});
function init() { function init() {
authorizationService = jasmine.createSpyObj('authorizationService', { authorizationService = jasmine.createSpyObj('authorizationService', {
isAuthorized: cold('-a', { a: true }), 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({ TestBed.configureTestingModule({
imports: [ imports: [
TranslateModule.forRoot(), TranslateModule.forRoot(),
@@ -71,7 +80,7 @@ describe('FileDownloadLinkComponent', () => {
providers: [ providers: [
RouterLinkDirectiveStub, RouterLinkDirectiveStub,
{ provide: AuthorizationDataService, useValue: authorizationService }, { provide: AuthorizationDataService, useValue: authorizationService },
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() }, { provide: ActivatedRoute, useValue: activatedRoute },
{ provide: Store, useValue: storeMock }, { provide: Store, useValue: storeMock },
{ provide: APP_DATA_SERVICES_MAP, useValue: {} }, { provide: APP_DATA_SERVICES_MAP, useValue: {} },
], ],
@@ -85,6 +94,9 @@ describe('FileDownloadLinkComponent', () => {
describe('init', () => { describe('init', () => {
describe('getBitstreamPath', () => { describe('getBitstreamPath', () => {
describe('when the user has download rights', () => { describe('when the user has download rights', () => {
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
scheduler = getTestScheduler(); scheduler = getTestScheduler();
@@ -113,6 +125,7 @@ describe('FileDownloadLinkComponent', () => {
expect(lock).toBeNull(); expect(lock).toBeNull();
}); });
}); });
describe('when the user has no download rights but has the right to request a copy', () => { describe('when the user has no download rights but has the right to request a copy', () => {
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
scheduler = getTestScheduler(); scheduler = getTestScheduler();
@@ -146,6 +159,7 @@ describe('FileDownloadLinkComponent', () => {
expect(lock).toBeTruthy(); expect(lock).toBeTruthy();
}); });
}); });
describe('when the user has no download rights and no request a copy rights', () => { describe('when the user has no download rights and no request a copy rights', () => {
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
scheduler = getTestScheduler(); scheduler = getTestScheduler();
@@ -165,7 +179,7 @@ describe('FileDownloadLinkComponent', () => {
expect(component.canDownload$).toBeObservable(cold('--a', { a: false })); 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(); scheduler.flush();
fixture.detectChanges(); fixture.detectChanges();
const link = fixture.debugElement.query(By.css('a')); const link = fixture.debugElement.query(By.css('a'));
@@ -174,6 +188,35 @@ describe('FileDownloadLinkComponent', () => {
expect(lock).toBeTruthy(); 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();
});
});
}); });
}); });
}); });

View File

@@ -62,6 +62,7 @@ export class ActivatedRouteStub {
paramMap: convertToParamMap(this.params), paramMap: convertToParamMap(this.params),
queryParamMap: convertToParamMap(this.testParams), queryParamMap: convertToParamMap(this.testParams),
queryParams: {} as Params, queryParams: {} as Params,
data: this.testData,
}; };
} }
} }