Skip to content

Commit a1e7d65

Browse files
committed
Request-a-copy: Unit tests
1 parent 1fff3b5 commit a1e7d65

4 files changed

Lines changed: 55 additions & 9 deletions

File tree

src/app/core/data/item-request-data.service.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('ItemRequestDataService', () => {
4242
case 'request.item.grant.link.period':
4343
return createSuccessfulRemoteDataObject$(Object.assign(new ConfigurationProperty(), {
4444
name: 'request.item.grant.link.period',
45-
values: ['3600', '7200', '86400'],
45+
values: ['FOREVER', '+1DAY', '+1MONTH'],
4646
}));
4747
default:
4848
return createSuccessfulRemoteDataObject$(new ConfigurationProperty());
@@ -116,7 +116,7 @@ describe('ItemRequestDataService', () => {
116116
});
117117

118118
it('should send a PUT request containing the correct properties', (done) => {
119-
service.grant(itemRequest.token, email, true).subscribe(() => {
119+
service.grant(itemRequest.token, email, true, '+1DAY').subscribe(() => {
120120
expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({
121121
method: RestRequestMethod.PUT,
122122
href: `${restApiEndpoint}/${itemRequest.token}`,
@@ -125,7 +125,7 @@ describe('ItemRequestDataService', () => {
125125
responseMessage: email.message,
126126
subject: email.subject,
127127
suggestOpenAccess: true,
128-
accessPeriod: 0,
128+
accessPeriod: '+1DAY',
129129
}),
130130
options: jasmine.objectContaining({
131131
headers: jasmine.any(HttpHeaders),
@@ -153,7 +153,7 @@ describe('ItemRequestDataService', () => {
153153
responseMessage: email.message,
154154
subject: email.subject,
155155
suggestOpenAccess: false,
156-
accessPeriod: 0,
156+
accessPeriod: null,
157157
}),
158158
options: jasmine.objectContaining({
159159
headers: jasmine.any(HttpHeaders),
@@ -187,7 +187,7 @@ describe('ItemRequestDataService', () => {
187187
describe('getConfiguredAccessPeriods', () => {
188188
it('should return parsed integer values from config', () => {
189189
service.getConfiguredAccessPeriods().subscribe(periods => {
190-
expect(periods).toEqual([3600, 7200, 86400]);
190+
expect(periods).toEqual(['FOREVER', '+1DAY', '+1MONTH']);
191191
});
192192
});
193193
});

src/app/request-copy/email-request-copy/email-request-copy.component.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from '@angular/core/testing';
88
import { RouterTestingModule } from '@angular/router/testing';
99
import { TranslateModule } from '@ngx-translate/core';
10+
import { of } from 'rxjs';
1011

1112
import { VarDirective } from '../../shared/utils/var.directive';
1213
import { EmailRequestCopyComponent } from './email-request-copy.component';
@@ -33,6 +34,8 @@ describe('EmailRequestCopyComponent', () => {
3334
beforeEach(() => {
3435
fixture = TestBed.createComponent(EmailRequestCopyComponent);
3536
component = fixture.componentInstance;
37+
// Set validAccessPeriods$ before detectChanges calls ngOnInit
38+
component.validAccessPeriods$ = of(['FOREVER']);
3639
fixture.detectChanges();
3740
});
3841

@@ -45,7 +48,6 @@ describe('EmailRequestCopyComponent', () => {
4548
spyOn(component.send, 'emit').and.stub();
4649
component.subject = 'test-subject';
4750
component.message = 'test-message';
48-
component.validAccessPeriods = ['FOREVER'];
4951
component.submit();
5052
expect(component.send.emit).toHaveBeenCalledWith(new RequestCopyEmail('test-subject', 'test-message'));
5153
});

src/app/shared/file-download-link/file-download-link.component.spec.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { getItemModuleRoute } from '../../item-page/item-page-routing-paths';
2727
import { ActivatedRouteStub } from '../testing/active-router.stub';
2828
import { RouterLinkDirectiveStub } from '../testing/router-link-directive.stub';
2929
import { FileDownloadLinkComponent } from './file-download-link.component';
30+
import { ItemRequest } from '../../core/shared/item-request.model';
3031

3132
describe('FileDownloadLinkComponent', () => {
3233
let component: FileDownloadLinkComponent;
@@ -39,6 +40,13 @@ describe('FileDownloadLinkComponent', () => {
3940
let item: Item;
4041
let storeMock: any;
4142

43+
const itemRequestStub = Object.assign(new ItemRequest(), {
44+
token: 'item-request-token',
45+
requestName: 'requester name',
46+
accessToken: 'abc123',
47+
allfiles: true,
48+
});
49+
4250
function init() {
4351
authorizationService = jasmine.createSpyObj('authorizationService', {
4452
isAuthorized: cold('-a', { a: true }),
@@ -62,7 +70,8 @@ describe('FileDownloadLinkComponent', () => {
6270
});
6371
}
6472

65-
function initTestbed() {
73+
function initTestbed(itemRequest = null) {
74+
const activatedRoute = new ActivatedRouteStub({}, { itemRequest: itemRequest });
6675
TestBed.configureTestingModule({
6776
imports: [
6877
TranslateModule.forRoot(),
@@ -71,7 +80,7 @@ describe('FileDownloadLinkComponent', () => {
7180
providers: [
7281
RouterLinkDirectiveStub,
7382
{ provide: AuthorizationDataService, useValue: authorizationService },
74-
{ provide: ActivatedRoute, useValue: new ActivatedRouteStub() },
83+
{ provide: ActivatedRoute, useValue: activatedRoute },
7584
{ provide: Store, useValue: storeMock },
7685
{ provide: APP_DATA_SERVICES_MAP, useValue: {} },
7786
],
@@ -85,6 +94,9 @@ describe('FileDownloadLinkComponent', () => {
8594

8695
describe('init', () => {
8796
describe('getBitstreamPath', () => {
97+
98+
99+
88100
describe('when the user has download rights', () => {
89101
beforeEach(waitForAsync(() => {
90102
scheduler = getTestScheduler();
@@ -113,6 +125,7 @@ describe('FileDownloadLinkComponent', () => {
113125
expect(lock).toBeNull();
114126
});
115127
});
128+
116129
describe('when the user has no download rights but has the right to request a copy', () => {
117130
beforeEach(waitForAsync(() => {
118131
scheduler = getTestScheduler();
@@ -146,6 +159,7 @@ describe('FileDownloadLinkComponent', () => {
146159
expect(lock).toBeTruthy();
147160
});
148161
});
162+
149163
describe('when the user has no download rights and no request a copy rights', () => {
150164
beforeEach(waitForAsync(() => {
151165
scheduler = getTestScheduler();
@@ -165,7 +179,7 @@ describe('FileDownloadLinkComponent', () => {
165179
expect(component.canDownload$).toBeObservable(cold('--a', { a: false }));
166180

167181
});
168-
it('should init the component', () => {
182+
it('should init the component and show the locked icon', () => {
169183
scheduler.flush();
170184
fixture.detectChanges();
171185
const link = fixture.debugElement.query(By.css('a'));
@@ -174,6 +188,35 @@ describe('FileDownloadLinkComponent', () => {
174188
expect(lock).toBeTruthy();
175189
});
176190
});
191+
192+
describe('when the user has no (normal) download rights and request a copy rights via access token', () => {
193+
beforeEach(waitForAsync(() => {
194+
scheduler = getTestScheduler();
195+
init();
196+
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(cold('-a', { a: false }));
197+
initTestbed(itemRequestStub);
198+
}));
199+
beforeEach(() => {
200+
fixture = TestBed.createComponent(FileDownloadLinkComponent);
201+
component = fixture.componentInstance;
202+
component.bitstream = bitstream;
203+
component.item = item;
204+
fixture.detectChanges();
205+
});
206+
it('should return the bitstreamPath based on the access token and request-a-copy path', () => {
207+
expect(component.bitstreamPath$).toBeObservable(cold('-a', { a: { routerLink: new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString(), queryParams: { accessToken: 'abc123' } } }));
208+
expect(component.canDownload$).toBeObservable(cold('--a', { a: false }));
209+
210+
});
211+
it('should init the component and show an open lock', () => {
212+
scheduler.flush();
213+
fixture.detectChanges();
214+
const link = fixture.debugElement.query(By.css('a'));
215+
expect(link.injector.get(RouterLinkDirectiveStub).routerLink).toContain(new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString());
216+
const lock = fixture.debugElement.query(By.css('.fa-lock-open')).nativeElement;
217+
expect(lock).toBeTruthy();
218+
});
219+
});
177220
});
178221
});
179222
});

src/app/shared/testing/active-router.stub.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class ActivatedRouteStub {
6262
paramMap: convertToParamMap(this.params),
6363
queryParamMap: convertToParamMap(this.testParams),
6464
queryParams: {} as Params,
65+
data: this.testData,
6566
};
6667
}
6768
}

0 commit comments

Comments
 (0)