/
test_routes.py
555 lines (484 loc) · 24.2 KB
/
test_routes.py
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
import json
from copy import deepcopy
from microsetta_admin.tests.base import TestBase
DUMMY_DAK_ORDER = {'contact_phone_number': '(858) 555-1212',
'projects': ['1', '32'],
'dak_article_code': '350101',
'description': '',
'fedex_ref_1': '',
'fedex_ref_2': '',
'fedex_ref_3': '',
'addresses_file': None}
class DummyResponse(object):
def __init__(self, status_code, output_dict):
self.status_code = status_code
self.text = json.dumps(output_dict)
self.json = lambda: output_dict
class RouteTests(TestBase):
PROJ_LIST = [{'additional_contact_name': None,
'alias': None,
'bank_samples': False,
'branding_associated_instructions': None,
'branding_status': None,
'collection': None,
'computed_stats': {
'num_fully_returned_kits': 1,
'num_kits': 5,
'num_kits_w_problems': 0,
'num_no_associated_source': 0,
'num_no_collection_info': 0,
'num_no_registered_account': 0,
'num_partially_returned_kits': 1,
'num_received_unknown_validity': 0,
'num_sample_is_valid': 4,
'num_samples': 20,
'num_samples_received': 4,
'num_unique_sources': 4},
'contact_email': None,
'contact_name': None,
'coordination': None,
'deadlines': None,
'disposition_comments': None,
'do_16s': None,
'do_mass_spec': None,
'do_metatranscriptomics': None,
'do_other': None,
'do_rt_qpcr': None,
'do_serology': None,
'do_shallow_shotgun': None,
'do_shotgun': None,
'is_blood': None,
'is_fecal': None,
'is_microsetta': False,
'is_other': None,
'is_saliva': None,
'is_skin': None,
'mass_spec_comments': None,
'mass_spec_contact_email': None,
'mass_spec_contact_name': None,
'num_subjects': None,
'num_timepoints': None,
'plating_start_date': None,
'project_id': 8,
'project_name': 'Project - %u[zGmÅq=g',
'sponsor': None,
'start_date': None,
'subproject_name': None},
{'additional_contact_name': None,
'alias': None,
'bank_samples': False,
'branding_associated_instructions': None,
'branding_status': None,
'collection': None,
'computed_stats': {
'num_fully_returned_kits': 4,
'num_kits': 5,
'num_kits_w_problems': 1,
'num_no_associated_source': 1,
'num_no_collection_info': 0,
'num_no_registered_account': 0,
'num_partially_returned_kits': 1,
'num_received_unknown_validity': 0,
'num_sample_is_valid': 2,
'num_samples': 12,
'num_samples_received': 5,
'num_unique_sources': 2},
'contact_email': None,
'contact_name': None,
'coordination': None,
'deadlines': None,
'disposition_comments': None,
'do_16s': None,
'do_mass_spec': None,
'do_metatranscriptomics': None,
'do_other': None,
'do_rt_qpcr': None,
'do_serology': None,
'do_shallow_shotgun': None,
'do_shotgun': None,
'is_blood': None,
'is_fecal': None,
'is_microsetta': False,
'is_other': None,
'is_saliva': None,
'is_skin': None,
'mass_spec_comments': None,
'mass_spec_contact_email': None,
'mass_spec_contact_name': None,
'num_subjects': None,
'num_timepoints': None,
'plating_start_date': None,
'project_id': 12,
'project_name': 'New proj',
'sponsor': None,
'start_date': None,
'subproject_name': "sub sub"}
]
def test_home_simple(self):
response = self.app.get('/', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<title>Microsetta Admin</title>', response.data)
def test_search_simple(self):
response = self.app.get('/search', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<h3>Microsetta Search</h3>', response.data)
def test_search_specific_barcode(self):
# server side issues a GET to the API
self.mock_get.return_value.status_code = 200
response = self.app.post('/search/sample',
data={'search_samples': '000004216'},
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<h3>Microsetta Search</h3>', response.data)
def test_search_missing_barcode(self):
# server side issues a GET to the API
self.mock_get.return_value = DummyResponse(404, {})
response = self.app.post('/search/sample',
data={'search_samples': 'missing'},
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Query not found', response.data)
def test_scan_simple(self):
response = self.app.get('/scan', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<h3>Microsetta Scan</h3>', response.data)
def test_scan_specific_no_warnings(self):
resp1 = {
"barcode_info": {"barcode": "000004216"},
"projects_info": [],
"scans_info": [],
"latest_scan": None,
"sample": {'site': 'baz'},
"source": {'name': 'a source a name',
'source_type': 'human',
'source_data': {'description': None}},
"account": {'id': 'd8592c74-9694-2135-e040-8a80115d6401'}
}
api_get_1 = DummyResponse(200, resp1)
api_get_2 = DummyResponse(200, [])
self.mock_get.side_effect = [api_get_1, api_get_2]
response = self.app.get('/scan?sample_barcode=000004216',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<td>000004216</td>', response.data)
self.assertNotIn(b'Status Warnings:', response.data)
def test_scan_specific_no_collection_info_warning(self):
resp1 = {
"barcode_info": {"barcode": "000004216"},
"projects_info": [{
"project": "American Gut Project",
"is_microsetta": True,
"bank_samples": False,
"plating_start_date": None
}],
"scans_info": [],
"latest_scan": None,
"sample": {'datetime_collected': None},
"account": {'id': "ThizIzNotReal"},
"source": {'name': 'a source a name',
'source_type': 'human',
'source_data': {'description': None}},
}
api_get_1 = DummyResponse(200, resp1)
api_get_2 = DummyResponse(200, [])
self.mock_get.side_effect = [api_get_1, api_get_2]
response = self.app.get('/scan?sample_barcode=000004216',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<td>000004216</td>', response.data)
self.assertIn(b'Status Warning: no-collection-info', response.data)
def test_scan_specific_no_associated_source_warning(self):
resp1 = {"barcode_info": {"barcode": "000004216"},
"projects_info": [{
"project": "American Gut Project",
"is_microsetta": True,
"bank_samples": False,
"plating_start_date": None
}],
"scans_info": [],
"latest_scan": None,
"sample": None,
"account": {"id": "foo"},
"source": None}
api_get_1 = DummyResponse(200, resp1)
api_get_2 = DummyResponse(200, [])
self.mock_get.side_effect = [api_get_1, api_get_2]
response = self.app.get('/scan?sample_barcode=000004216',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<td>000004216</td>', response.data)
self.assertIn(b'Status Warning: no-associated-source', response.data)
def test_scan_specific_no_registered_account_warning(self):
resp = {"barcode_info": {"barcode": "000004216"},
"projects_info": [{
"project": "American Gut Project",
"is_microsetta": True,
"bank_samples": False,
"plating_start_date": None
}],
"scans_info": [],
"latest_scan": None,
"sample": None,
"account": None,
"source": None}
self.mock_get.return_value = DummyResponse(200, resp)
response = self.app.get('/scan?sample_barcode=000004216',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<td>000004216</td>', response.data)
self.assertIn(b'Status Warning: no-registered-account', response.data)
def test_scan_specific_received_unknown_validity_warning(self):
resp = {"barcode_info": {"barcode": "000004216"},
"projects_info": [],
"scans_info": [],
"latest_scan": None,
"sample": None,
"account": None,
"source": None}
self.mock_get.return_value = DummyResponse(200, resp)
response = self.app.get('/scan?sample_barcode=000004216',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<td>000004216</td>', response.data)
self.assertIn(b'Status Warning: received-unknown-validity',
response.data)
def test_create_kits_get_success(self):
proj_list = deepcopy(self.PROJ_LIST)
self.mock_get.return_value = DummyResponse(200, proj_list)
response = self.app.get('/create_kits', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<option value=\'12\'>New proj</option>',
response.data)
def test_create_kits_get_fail(self):
self.mock_get.return_value = DummyResponse(400, "")
response = self.app.get('/create_kits', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Unable to load project list', response.data)
def test_create_project_success(self):
api_post_dummy = DummyResponse(201, {})
api_get_dummy = DummyResponse(200, self.PROJ_LIST)
self.mock_post.return_value = api_post_dummy
self.mock_get.return_value = api_get_dummy
create_input = deepcopy(self.PROJ_LIST[1])
create_input["project_id"] = ""
create_input.pop('computed_stats')
response = self.app.post('/manage_projects',
data=create_input,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
# if the below is on the page, the table of projects is shown
self.assertIn(b'<th>Flight Sub-Project Name</th>', response.data)
def test_create_project_fail(self):
self.mock_post.return_value = DummyResponse(401, {})
create_input = deepcopy(self.PROJ_LIST[1])
create_input["project_id"] = ""
create_input.pop('computed_stats')
response = self.app.post('/manage_projects',
data=create_input,
follow_redirects=True)
# the api call failed, but the admin page call succeeds--in returning
# a page reporting the error :)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Unable to create project.', response.data)
def test_update_project_success(self):
self.mock_put.return_value = DummyResponse(204, {})
self.mock_get.return_value = DummyResponse(200, self.PROJ_LIST)
create_input = deepcopy(self.PROJ_LIST[1])
create_input.pop('computed_stats')
response = self.app.post('/manage_projects',
data=create_input,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
# if the below is on the page, the table of projects is shown
self.assertIn(b'<th>Flight Sub-Project Name</th>', response.data)
def test_update_project_fail(self):
self.mock_put.return_value = DummyResponse(400, {})
create_input = deepcopy(self.PROJ_LIST[1])
create_input.pop('computed_stats')
response = self.app.post('/manage_projects',
data=create_input,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
# the api call failed, but the admin page call succeeds--in returning
# a page reporting the error :)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Unable to update project.', response.data)
def test_manage_projects_success(self):
self.mock_get.return_value = DummyResponse(200, self.PROJ_LIST)
response = self.app.get('/manage_projects', follow_redirects=True)
self.assertEqual(response.status_code, 200)
# if the below is on the page, the table of projects is shown
self.assertIn(b'<th>Flight Sub-Project Name</th>', response.data)
def test_manage_projects_fail(self):
self.mock_get.return_value = DummyResponse(
400, {"message": "hideous error"})
response = self.app.get('/manage_projects', follow_redirects=True)
# the api call failed, but the admin page call succeeds--in returning
# a page reporting the error :)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Unable to load project list.', response.data)
def test_get_submit_daklapack_order_success(self):
# server side issues two GETs to the API
dak_article = {'dak_article_code': 350100,
'short_description': 'TMI 1 tube',
'num_2point5ml_etoh_tubes': 1,
'num_7ml_etoh_tube': 0,
'num_neoteryx_kit': 0,
'outer_sleeve': 'Microsetta',
'box': 'Microsetta',
'return_label': 'Microsetta',
'compartment_bag': 'Microsetta',
'num_stool_collector': 0,
'instructions': 'Fv1',
'registration_card': 'Microsetta',
'swabs': '1x bag of two',
'rigid_safety_bag': 'yes'}
a_project = {'project_name': 'test_proj', 'is_microsetta': True,
'bank_samples': False, 'plating_start_date': None,
'contact_name': 'Jane Doe',
'contact_email': 'jd@test.com',
'additional_contact_name': 'John Doe',
'deadlines': 'Spring 2021', 'num_subjects': 'Variable',
'num_timepoints': '4', 'start_date': 'Fall 2020',
'disposition_comments': 'Store', 'collection': 'AGP',
'is_fecal': 'X', 'is_saliva': '', 'is_skin': '?',
'is_blood': 'X', 'is_other': 'Nares, mouth',
'do_16s': '', 'do_shallow_shotgun': 'Subset',
'do_shotgun': 'X', 'do_rt_qpcr': '', 'do_serology': '',
'do_metatranscriptomics': 'X', 'do_mass_spec': 'X',
'mass_spec_comments': 'Dorrestein',
'mass_spec_contact_name': 'Ted Doe',
'mass_spec_contact_email': 'td@test.com',
'do_other': '',
'branding_associated_instructions': 'branding_doc.pdf',
'branding_status': 'In Review',
'subproject_name': 'IBL SIBL',
'alias': 'Healthy Sitting', 'sponsor': 'Crowdfunded',
'coordination': 'TMI', 'is_active': True,
'project_id': 8}
api_get_1 = DummyResponse(200, [dak_article])
api_get_2 = DummyResponse(200, [a_project])
self.mock_get.side_effect = [api_get_1, api_get_2]
response = self.app.get('/submit_daklapack_order',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'<strong>HOLD FULFILLMENT</strong>', response.data)
def test_get_submit_daklapack_order_fail_articles(self):
# server side issues two GETs to the API
dak_article = {'error_msg': 'hit a problem'}
a_project = {'project_name': 'test_proj', 'is_microsetta': True,
'bank_samples': False, 'plating_start_date': None,
'contact_name': 'Jane Doe',
'contact_email': 'jd@test.com',
'additional_contact_name': 'John Doe',
'deadlines': 'Spring 2021', 'num_subjects': 'Variable',
'num_timepoints': '4', 'start_date': 'Fall 2020',
'disposition_comments': 'Store', 'collection': 'AGP',
'is_fecal': 'X', 'is_saliva': '', 'is_skin': '?',
'is_blood': 'X', 'is_other': 'Nares, mouth',
'do_16s': '', 'do_shallow_shotgun': 'Subset',
'do_shotgun': 'X', 'do_rt_qpcr': '', 'do_serology': '',
'do_metatranscriptomics': 'X', 'do_mass_spec': 'X',
'mass_spec_comments': 'Dorrestein',
'mass_spec_contact_name': 'Ted Doe',
'mass_spec_contact_email': 'td@test.com',
'do_other': '',
'branding_associated_instructions': 'branding_doc.pdf',
'branding_status': 'In Review',
'subproject_name': 'IBL SIBL',
'alias': 'Healthy Sitting', 'sponsor': 'Crowdfunded',
'coordination': 'TMI', 'is_active': True,
'project_id': 8}
api_get_1 = DummyResponse(400, [dak_article])
api_get_2 = DummyResponse(200, [a_project])
self.mock_get.side_effect = [api_get_1, api_get_2]
response = self.app.get('/submit_daklapack_order',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Unable to load daklapack articles list.',
response.data)
def test_get_submit_daklapack_order_fail_projects(self):
# server side issues two GETs to the API
dak_article = {'dak_article_code': 350100,
'short_description': 'TMI 1 tube',
'num_2point5ml_etoh_tubes': 1,
'num_7ml_etoh_tube': 0,
'num_neoteryx_kit': 0,
'outer_sleeve': 'Microsetta',
'box': 'Microsetta',
'return_label': 'Microsetta',
'compartment_bag': 'Microsetta',
'num_stool_collector': 0,
'instructions': 'Fv1',
'registration_card': 'Microsetta',
'swabs': '1x bag of two',
'rigid_safety_bag': 'yes'}
a_project = {'error_message': 'no projects for you'}
api_get_1 = DummyResponse(200, [dak_article])
api_get_2 = DummyResponse(400, [a_project])
self.mock_get.side_effect = [api_get_1, api_get_2]
response = self.app.get('/submit_daklapack_order',
follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Unable to load project list.', response.data)
def _test_post_submit_daklapack_order(self, addresses_filename=None):
if addresses_filename is None:
addresses_filename = "order_addresses_sample.xlsx"
addresses_fp = self.get_data_path(addresses_filename)
with open(addresses_fp, "rb") as addresses_file:
request_form = deepcopy(DUMMY_DAK_ORDER)
request_form['addresses_file'] = addresses_file
response = self.app.post('/submit_daklapack_order',
data=request_form,
follow_redirects=True)
self.assertEqual(response.status_code, 200)
return response
def test_post_submit_daklapack_order_success(self):
# server side issues one POST to the API
api_post_1 = DummyResponse(201, {'order_id': '11211',
'email_success': None})
self.mock_post.side_effect = [api_post_1]
response = self._test_post_submit_daklapack_order()
self.assertIn(b'Order 11211 submitted.', response.data)
def test_post_submit_daklapack_order_success_but_email_fail(self):
# server side issues one POST to the API
api_post_1 = DummyResponse(201, {'order_id': '11211',
'email_success': False})
self.mock_post.side_effect = [api_post_1]
response = self._test_post_submit_daklapack_order()
self.assertIn(b'Order 11211 submitted.', response.data)
self.assertIn(b'HOWEVER, fulfillment hold', response.data)
def test_post_submit_daklapack_order_fail_api(self):
# server side issues one POST to the API
api_post_1 = DummyResponse(400, {
"isValid": False,
"errors": [
{
"propertyName": "orderId",
"errorMessage": "invalid format",
"severity": "Error",
"errorCode": "12",
"formattedMessagePlaceholderValues": {}
}
],
"ruleSetsExecuted": []
})
self.mock_post.side_effect = [api_post_1]
response = self._test_post_submit_daklapack_order(
"order_addresses_sample.xlsx")
self.assertIn(b'formattedMessagePlaceholderValues', response.data)
def test_post_submit_daklapack_order_fail_xlsx_format(self):
# actually code shouldn't make it to private api call, but in case:
api_post_1 = DummyResponse(201, {'order_id': '11211',
'email_success': None})
self.mock_post.side_effect = [api_post_1]
response = self._test_post_submit_daklapack_order("empty.txt")
self.assertIn(b'Could not parse addresses file', response.data)
def test_post_submit_daklapack_order_fail_xlsx_headers(self):
# actually code shouldn't make it to private api call, but in case:
api_post_1 = DummyResponse(201, {'order_id': '11211',
'email_success': None})
self.mock_post.side_effect = [api_post_1]
response = self._test_post_submit_daklapack_order(
"order_addresses_malformed.xlsx")
self.assertIn(b'do not match expected column names', response.data)