-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
624 lines (524 loc) · 17.9 KB
/
index.test.js
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
const HTTP_STATUS = require('http-status-codes');
const supertest = require('supertest');
const getPort = require('get-port');
const db = require('../../../app/models');
const CONFIG = require('../../../app/config');
const {listen} = require('../../../app');
CONFIG.set('env', 'test');
const {username, password} = CONFIG.get('testing');
const BASE_URI = '/api/germline-small-mutation-reports';
jest.mock('../../../app/middleware/auth.js');
let server;
let request;
let testUser;
const mockData = require('../../testData/mockGermlineReportData.json');
const NON_AUTHORIZED_GROUP = 'NON AUTHORIZED GROUP';
const NON_PROD_ACCESS = 'non-production access';
const UNREVIEWED_ACCESS = 'unreviewed access';
const GERMLINE_ACCESS = 'germline access';
const CREATE_DATA = {
normalLibrary: 'test library',
sourceVersion: 'v1.0.0',
sourcePath: '/some/random/source/path',
exported: false,
patientId: 'TESTPAT01',
biopsyName: 'TEST123',
};
const UPDATE_DATA = {
normalLibrary: 'updated library',
sourceVersion: 'v2.0.0',
sourcePath: '/some/random/source/path/updated',
patientId: 'TESTPAT012345',
biopsyName: 'TEST12345',
};
const UPLOAD_DATA = {
normalLibrary: mockData.normalLibrary,
sourceVersion: mockData.version,
sourcePath: mockData.source,
patientId: mockData.patientId,
biopsyName: mockData.biopsyName,
};
const germlineReportProperties = [
'ident', 'createdAt', 'updatedAt', 'biopsyName', 'normalLibrary', 'sourceVersion',
'sourcePath', 'exported', 'biofxAssigned', 'projects', 'users', 'variants', 'reviews',
'patientId',
];
const checkGermlineReport = (reportObject) => {
germlineReportProperties.forEach((element) => {
expect(reportObject).toHaveProperty(element);
});
expect(reportObject).toEqual(expect.not.objectContaining({
id: expect.any(Number),
deletedAt: expect.any(String),
}));
};
const checkGermlineReports = (reports) => {
reports.forEach((report) => {
checkGermlineReport(report);
});
};
const hasNonProdReport = (reports) => {
return reports.some((report) => {
return report.state === 'nonproduction';
});
};
// Template of a GET all reports query for tests
const checkGermlineReportList = expect.objectContaining({
total: expect.any(Number),
reports: expect.any(Array),
});
// Start API
beforeAll(async () => {
const port = await getPort({port: CONFIG.get('web:port')});
server = await listen(port);
request = supertest(server);
// get test user
testUser = await db.models.user.findOne({
where: {username},
});
});
describe('/germline-small-mutation-reports', () => {
let report;
let nonProdReport;
let realProject;
let fakeProject;
const reports = [];
const projects = [];
beforeAll(async () => {
// create a bunch of reports to be used in tests
report = await db.models.germlineSmallMutation.create({
...CREATE_DATA, biofxAssignedId: testUser.id,
});
reports.push(report);
// Create projects
fakeProject = await db.models.project.create({
name: 'Fake project',
});
projects.push(fakeProject);
realProject = await db.models.project.create({
name: 'Real project',
});
projects.push(realProject);
// Create a bunch of reports
const report2 = await db.models.germlineSmallMutation.create({
...CREATE_DATA,
biofxAssignedId: testUser.id,
patientId: 'Patient002',
biopsyName: 'biopsy002',
});
reports.push(report2);
await db.models.germlineReportsToProjects.create({
germlineReportId: report2.id,
projectId: fakeProject.id,
});
await db.models.germlineSmallMutationReview.create({
reviewerId: testUser.id,
germlineReportId: report2.id,
type: 'biofx',
});
const report3 = await db.models.germlineSmallMutation.create({
...CREATE_DATA,
biofxAssignedId: testUser.id,
patientId: 'Client001',
biopsyName: 'biopsy003',
});
reports.push(report3);
await db.models.germlineReportsToProjects.create({
germlineReportId: report3.id,
projectId: fakeProject.id,
});
await db.models.germlineSmallMutationReview.create({
reviewerId: testUser.id,
germlineReportId: report3.id,
type: 'projects',
});
const report4 = await db.models.germlineSmallMutation.create({
...CREATE_DATA,
biofxAssignedId: testUser.id,
patientId: 'Client002',
biopsyName: 'sample001',
});
reports.push(report4);
await db.models.germlineReportsToProjects.create({
germlineReportId: report4.id,
projectId: realProject.id,
});
await db.models.germlineSmallMutationReview.create({
reviewerId: testUser.id,
germlineReportId: report4.id,
type: 'diff',
});
const report5 = await db.models.germlineSmallMutation.create({
...CREATE_DATA,
biofxAssignedId: testUser.id,
patientId: 'Admin001',
biopsyName: 'sample002',
});
reports.push(report5);
await db.models.germlineReportsToProjects.create({
germlineReportId: report5.id,
projectId: realProject.id,
});
await db.models.germlineSmallMutationReview.create({
reviewerId: testUser.id,
germlineReportId: report5.id,
type: 'biofx',
});
nonProdReport = await db.models.germlineSmallMutation.create({
...CREATE_DATA,
biofxAssignedId: testUser.id,
patientId: 'Admin001',
biopsyName: 'sample002',
state: 'nonproduction',
});
reports.push(nonProdReport);
await db.models.germlineReportsToProjects.create({
germlineReportId: nonProdReport.id,
projectId: realProject.id,
});
await db.models.germlineSmallMutationReview.create({
reviewerId: testUser.id,
germlineReportId: nonProdReport.id,
type: 'biofx',
});
});
describe('GET', () => {
test('/ - 200 Success', async () => {
const res = await request
.get(BASE_URI)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
checkGermlineReports(res.body.reports);
});
test('/ - 403 Forbidden', async () => {
await request
.get(BASE_URI)
.query({
groups: [{name: NON_PROD_ACCESS}, {name: UNREVIEWED_ACCESS}],
projects: [{name: realProject.name, ident: realProject.ident}],
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.FORBIDDEN);
});
test('/ - patient query - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({patientId: 'Patient'})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports).toEqual(expect.arrayContaining([
expect.objectContaining({patientId: expect.stringContaining('Patient')}),
]));
});
test('/ - biopsy name query - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({biopsyName: 'biopsy'})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports).toEqual(expect.arrayContaining([
expect.objectContaining({biopsyName: expect.stringContaining('biopsy')}),
]));
});
test('/ - project query - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({project: 'Real project'})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports).toEqual(expect.arrayContaining([
expect.objectContaining({
projects: expect.arrayContaining([
expect.objectContaining({name: expect.stringContaining('Real project')}),
]),
}),
]));
});
test('/ - limit and offset - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({limit: 3, offset: 1})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports.length).toEqual(3);
const {body: {reports: germReports}} = res;
// Test offset
const results = await db.models.germlineSmallMutation.scope('public').findAll({limit: 3, offset: 1});
for (let i = 0; i < results.length; i++) {
expect(germReports[i].ident).toBe(results[i].ident);
}
});
test('/ - exported query - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({exported: false})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports).toEqual(expect.arrayContaining([
expect.objectContaining({exported: false}),
]));
});
test('/ - reviewType string query - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({reviewType: 'biofx'})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports).toEqual(expect.arrayContaining([
expect.objectContaining({
reviews: expect.arrayContaining([
expect.objectContaining({type: expect.stringContaining('biofx')}),
]),
}),
]));
});
test('/ - reviewType array query - 200 success', async () => {
const res = await request
.get(BASE_URI)
.query({reviewType: 'biofx,projects'})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
expect(res.body).toEqual(checkGermlineReportList);
expect(res.body.reports).toEqual(expect.arrayContaining([
expect.objectContaining({
reviews: expect.arrayContaining([
expect.objectContaining({type: expect.stringMatching(/(biofx|projects)/)}),
]),
}),
]));
});
test('/ - 200 NOT GET non-production reports with non-authorized group', async () => {
const res = await request
.get(BASE_URI)
.query({
groups: [{name: NON_AUTHORIZED_GROUP}, {name: GERMLINE_ACCESS}],
projects: [{name: realProject.name, ident: realProject.ident}],
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
checkGermlineReports(res.body.reports);
expect(hasNonProdReport(res.body.reports)).not.toBeTruthy();
});
test('/ - 200 GET non-production reports with authorized group', async () => {
// CacheKey variable added in order to not trigger the cache.
// Cache is being triggered by mocking the permissions,
// Making it hard for the api to differenciate queries
const res = await request
.get(BASE_URI)
.query({
groups: [{name: NON_PROD_ACCESS}, {name: UNREVIEWED_ACCESS}, {name: GERMLINE_ACCESS}],
projects: [
{name: realProject.name, ident: realProject.ident},
],
cacheKey: '00',
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
checkGermlineReports(res.body.reports);
expect(hasNonProdReport(res.body.reports)).toBeTruthy();
});
test('/{gsm_report} - 200 Success', async () => {
const res = await request
.get(`${BASE_URI}/${report.ident}`)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
checkGermlineReport(res.body);
expect(res.body).toEqual(expect.objectContaining(CREATE_DATA));
});
test('/{gsm_report} - error on non-production ident with wrong group', async () => {
await request
.get(`${BASE_URI}/${nonProdReport.ident}`)
.query({
groups: [{name: NON_AUTHORIZED_GROUP}, {name: GERMLINE_ACCESS}],
projects: [{name: realProject.name, ident: realProject.ident}],
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.FORBIDDEN);
});
test('/{gsm_report} - allow query with correct group', async () => {
const res = await request
.get(`${BASE_URI}/${nonProdReport.ident}`)
.query({
groups: [{name: NON_PROD_ACCESS}, {name: UNREVIEWED_ACCESS}, {name: GERMLINE_ACCESS}],
projects: [{name: realProject.name, ident: realProject.ident}],
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
checkGermlineReport(res.body);
});
test('/{gsm_report} - not allow query with no germline access', async () => {
await request
.get(`${BASE_URI}/${nonProdReport.ident}`)
.query({
groups: [{name: NON_PROD_ACCESS}, {name: UNREVIEWED_ACCESS}],
projects: [{name: realProject.name, ident: realProject.ident}],
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.FORBIDDEN);
});
});
describe('POST', () => {
test('/ - 201 Created', async () => {
const res = await request
.post(BASE_URI)
.send(mockData)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.CREATED);
checkGermlineReport(res.body);
expect(res.body).toEqual(expect.objectContaining(UPLOAD_DATA));
// Check that record was created in the db
const result = await db.models.germlineSmallMutation.findOne({where: {ident: res.body.ident}});
expect(result).not.toBeNull();
reports.push(result);
});
test('/ - project is required - 400 Bad Request', async () => {
const {project, ...testData} = mockData;
await request
.post(BASE_URI)
.auth(username, password)
.type('json')
.send(testData)
.expect(HTTP_STATUS.BAD_REQUEST);
});
test('/ - source is required - 400 Bad Request', async () => {
const {source, ...testData} = mockData;
await request
.post(BASE_URI)
.auth(username, password)
.type('json')
.send(testData)
.expect(HTTP_STATUS.BAD_REQUEST);
});
test('/ - version is required - 400 Bad Request', async () => {
const {version, ...testData} = mockData;
await request
.post(BASE_URI)
.auth(username, password)
.type('json')
.send(testData)
.expect(HTTP_STATUS.BAD_REQUEST);
});
});
describe('PUT', () => {
let germlineReportUpdate;
beforeEach(async () => {
germlineReportUpdate = await db.models.germlineSmallMutation.create({
...CREATE_DATA, biofxAssignedId: testUser.id,
});
});
afterEach(async () => {
await db.models.germlineSmallMutation.destroy({where: {ident: germlineReportUpdate.ident}, force: true});
});
test('/{gsm_report} - 200 Success', async () => {
const res = await request
.put(`${BASE_URI}/${germlineReportUpdate.ident}`)
.send({...UPDATE_DATA, assignToMe: true})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
checkGermlineReport(res.body);
expect(res.body).toEqual(expect.objectContaining(UPDATE_DATA));
});
test('/{gsm_report} - 400 Bad Request Failed Validation', async () => {
await request
.put(`${BASE_URI}/${germlineReportUpdate.ident}`)
.send({...UPDATE_DATA, id: 6})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.BAD_REQUEST);
});
test('/{gsm_report} - 404 Not Found no variant data to update', async () => {
// First soft-delete record
await db.models.germlineSmallMutation.destroy({where: {ident: germlineReportUpdate.ident}});
await request
.put(`${BASE_URI}/${germlineReportUpdate.ident}`)
.send(UPDATE_DATA)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.NOT_FOUND);
});
});
describe('DELETE', () => {
let germlineReportDelete;
beforeEach(async () => {
germlineReportDelete = await db.models.germlineSmallMutation.create({
...CREATE_DATA, biofxAssignedId: testUser.id,
});
});
afterEach(async () => {
await db.models.germlineSmallMutation.destroy({
where: {ident: germlineReportDelete.ident}, force: true,
});
});
test('/{gsm_report} - 204 No content', async () => {
await request
.delete(`${BASE_URI}/${germlineReportDelete.ident}`)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.NO_CONTENT);
// Verify that the record was deleted
const result = await db.models.germlineSmallMutation.findOne({where: {id: germlineReportDelete.id}});
expect(result).toBeNull();
});
test('/{gsm_report} - 400 Bad Request', async () => {
await request
.delete(`${BASE_URI}/NOT_AN_EXISTING_RECORD`)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.BAD_REQUEST);
});
test('/{gsm_report} - 404 Not Found no germline report to delete', async () => {
// First soft-delete record
await db.models.germlineSmallMutation.destroy({where: {ident: germlineReportDelete.ident}});
await request
.delete(`${BASE_URI}/${germlineReportDelete.ident}`)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.NOT_FOUND);
});
});
// delete reports and projects
afterAll(async () => {
// delete newly created reports and all of their components
await db.models.germlineSmallMutation.destroy({
where: {
ident: reports.map((rep) => {return rep.ident;}),
},
force: true,
});
// Delete projects
await db.models.project.destroy({
where: {
id: projects.map((project) => {return project.id;}),
},
force: true,
});
});
});
afterAll(async () => {
global.gc && global.gc();
await server.close();
});