forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrestricted-access.component.spec.ts
More file actions
316 lines (263 loc) · 11.2 KB
/
restricted-access.component.spec.ts
File metadata and controls
316 lines (263 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import {
DatePipe,
Location,
} from '@angular/common';
import {
ComponentFixture,
TestBed,
waitForAsync,
} from '@angular/core/testing';
import {
ActivatedRoute,
Router,
} from '@angular/router';
import { TranslateModule } from '@ngx-translate/core';
import { of as observableOf } from 'rxjs';
import { AuthService } from '../core/auth/auth.service';
import { AuthorizationDataService } from '../core/data/feature-authorization/authorization-data.service';
import { HardRedirectService } from '../core/services/hard-redirect.service';
import { ServerResponseService } from '../core/services/server-response.service';
import { Bitstream } from '../core/shared/bitstream.model';
import { FileService } from '../core/shared/file.service';
import { createSuccessfulRemoteDataObject } from '../shared/remote-data.utils';
import { RestrictedAccessComponent } from './restricted-access.component';
describe('RestrictedAccessComponent', () => {
let component: RestrictedAccessComponent;
let fixture: ComponentFixture<RestrictedAccessComponent>;
let authService: jasmine.SpyObj<AuthService>;
let authorizationService: jasmine.SpyObj<AuthorizationDataService>;
let fileService: jasmine.SpyObj<FileService>;
let hardRedirectService: jasmine.SpyObj<HardRedirectService>;
let serverResponseService: jasmine.SpyObj<ServerResponseService>;
let router: jasmine.SpyObj<Router>;
let location: jasmine.SpyObj<Location>;
let activatedRoute;
let bitstream: Bitstream;
function initBitstream(overrides: Partial<Bitstream> = {}): Bitstream {
return Object.assign(new Bitstream(), {
uuid: 'test-bitstream-uuid',
metadata: {
'dc.title': [{ value: 'test-file.pdf', language: null, authority: null, confidence: -1, place: 0 }],
},
_links: {
content: { href: 'bitstream-content-link' },
self: { href: 'bitstream-self-link' },
},
// Default in tests to "FOREVER" embargo
embargoRestriction: 'FOREVER',
...overrides,
});
}
function init(bitstreamOverrides: Partial<Bitstream> = {}) {
bitstream = initBitstream(bitstreamOverrides);
authService = jasmine.createSpyObj('AuthService', {
isAuthenticated: observableOf(false),
setRedirectUrl: {},
});
authorizationService = jasmine.createSpyObj('AuthorizationDataService', {
isAuthorized: observableOf(false),
});
fileService = jasmine.createSpyObj('FileService', {
retrieveFileDownloadLink: observableOf('content-url-with-headers'),
});
hardRedirectService = jasmine.createSpyObj('HardRedirectService', {
redirect: {},
});
serverResponseService = jasmine.createSpyObj('ServerResponseService', {
setUnauthorized: {},
setForbidden: {},
setNotFound: {},
setStatus: {},
});
router = jasmine.createSpyObj('Router', ['navigateByUrl']);
// Provide a url property for redirectOn4xx
(router as any).url = '/restricted-access/test-bitstream-uuid';
location = jasmine.createSpyObj('Location', ['back']);
activatedRoute = {
data: observableOf({
bitstream: createSuccessfulRemoteDataObject(bitstream),
}),
};
}
function initTestBed() {
void TestBed.configureTestingModule({
imports: [
TranslateModule.forRoot(),
RestrictedAccessComponent,
],
providers: [
{ provide: ActivatedRoute, useValue: activatedRoute },
{ provide: Router, useValue: router },
{ provide: AuthorizationDataService, useValue: authorizationService },
{ provide: AuthService, useValue: authService },
{ provide: FileService, useValue: fileService },
{ provide: HardRedirectService, useValue: hardRedirectService },
{ provide: ServerResponseService, useValue: serverResponseService },
{ provide: Location, useValue: location },
DatePipe,
],
}).compileComponents();
}
// Helper function for setting up anonymous tests with a specific embargo
// restriction
function setupAnonymous(bitstreamOverrides: Partial<Bitstream>) {
beforeEach(waitForAsync(() => {
init(bitstreamOverrides);
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(false));
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
initTestBed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RestrictedAccessComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
}
// Helper function verifying that a HTTP 401 Unauthorized status code is
// set, and that a redirect to the bitstream is not performed.
function verify401StatusCodeAndNoRedirectToDownload() {
it('should set 401 Unauthorized and not redirect to the file', () => {
expect(serverResponseService.setUnauthorized).toHaveBeenCalled();
expect(serverResponseService.setForbidden).not.toHaveBeenCalled();
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
});
}
describe('when the user is anonymous (not logged in)', () => {
describe('when embargoRestriction is FOREVER', () => {
setupAnonymous({ embargoRestriction: 'FOREVER' });
verify401StatusCodeAndNoRedirectToDownload();
it('should set the restrictedAccessMessage indicating the file is embargoed forever', () => {
expect(component.restrictedAccessMessage.value).toBe('bitstream.restricted-access.embargo.forever.message');
});
});
describe('when there is an embargo end date', () => {
setupAnonymous({ embargoRestriction: '2199-04-08' });
verify401StatusCodeAndNoRedirectToDownload();
it('should set the restrictedAccessMessage indicating an end date', () => {
expect(component.restrictedAccessMessage.value).toBe(
'bitstream.restricted-access.embargo.restricted-until.message');
});
});
describe('when embargoRestriction is NONE (embargo over, but file is restricted for another reason)', () => {
setupAnonymous({ embargoRestriction: 'NONE' });
verify401StatusCodeAndNoRedirectToDownload();
it('should set the restrictedAccessMessage to a simple "forbidden" message', () => {
expect(component.restrictedAccessMessage.value).toBe('bitstream.restricted-access.anonymous.forbidden.message');
});
});
describe('when file is restricted for non-embargo reasons (such as Campus IP restriction)', () => {
setupAnonymous({ embargoRestriction:null });
verify401StatusCodeAndNoRedirectToDownload();
it('should set the restrictedAccessMessage to a simple "forbidden" message', () => {
expect(component.restrictedAccessMessage.value).toBe('bitstream.restricted-access.anonymous.forbidden.message');
});
});
describe('but the user is authorized (even if there is an embargo)', () => {
beforeEach(waitForAsync(() => {
init();
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(false));
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(true));
initTestBed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RestrictedAccessComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should redirect to the content link', () => {
expect(hardRedirectService.redirect).toHaveBeenCalled();
});
it('should NOT call setUnauthorized', () => {
expect(serverResponseService.setUnauthorized).not.toHaveBeenCalled();
});
it('should NOT call setForbidden', () => {
expect(serverResponseService.setForbidden).not.toHaveBeenCalled();
});
});
});
describe('when the user is logged in', () => {
describe('returns 403 Forbidden when the user is not authorized to access the file', () => {
beforeEach(waitForAsync(() => {
init();
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(true));
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
initTestBed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RestrictedAccessComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should call setForbidden on ServerResponseService', () => {
expect(serverResponseService.setForbidden).toHaveBeenCalled();
});
it('should NOT call setUnauthorized on ServerResponseService', () => {
expect(serverResponseService.setUnauthorized).not.toHaveBeenCalled();
});
it('should NOT redirect to a download', () => {
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
});
it('should set the restrictedAccessHeader', () => {
expect(component.restrictedAccessHeader.value).toBe('bitstream.restricted-access.user.forbidden.header');
});
it('should set the restrictedAccessMessage', () => {
expect(component.restrictedAccessMessage.value).toBe(
'bitstream.restricted-access.user.forbidden.with_file.message');
});
});
describe('returns 403 Forbidden with a generic message when a filename is not provided', () => {
beforeEach(waitForAsync(() => {
init({ metadata: {} });
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(true));
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
initTestBed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RestrictedAccessComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should set the generic forbidden message', () => {
expect(component.restrictedAccessMessage.value).toBe(
'bitstream.restricted-access.user.forbidden.generic.message');
});
});
describe('allows access to the file when the user is authorized', () => {
beforeEach(waitForAsync(() => {
init();
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(true));
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(true));
initTestBed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RestrictedAccessComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should NOT call setUnauthorized', () => {
expect(serverResponseService.setUnauthorized).not.toHaveBeenCalled();
});
it('should NOT call setForbidden', () => {
expect(serverResponseService.setForbidden).not.toHaveBeenCalled();
});
it('should redirect to the file download link', () => {
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers');
});
});
});
describe('back()', () => {
beforeEach(waitForAsync(() => {
init();
initTestBed();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RestrictedAccessComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should call location.back()', () => {
component.back();
expect(location.back).toHaveBeenCalled();
});
});
});