-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_rest.py
823 lines (678 loc) · 32.6 KB
/
test_rest.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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
import json
import uuid
from datetime import date, datetime, timedelta
import pytest
import pytz
from freezegun import freeze_time
import app.celery.tasks
from app.dao.templates_dao import dao_update_template
from app.models import JOB_STATUS_PENDING, JOB_STATUS_TYPES
from tests import create_authorization_header
from tests.app.db import (
create_ft_notification_status,
create_job,
create_notification,
save_notification,
)
from tests.conftest import set_config
def test_get_job_with_invalid_service_id_returns404(client, sample_service):
path = "/service/{}/job".format(sample_service.id)
auth_header = create_authorization_header()
response = client.get(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert len(resp_json["data"]) == 0
def test_get_job_with_invalid_job_id_returns404(client, sample_template):
service_id = sample_template.service.id
path = "/service/{}/job/{}".format(service_id, "bad-id")
auth_header = create_authorization_header()
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["result"] == "error"
assert resp_json["message"] == "No result found"
def test_get_job_with_unknown_id_returns404(client, sample_template, fake_uuid):
service_id = sample_template.service.id
path = "/service/{}/job/{}".format(service_id, fake_uuid)
auth_header = create_authorization_header()
response = client.get(path, headers=[auth_header])
assert response.status_code == 404
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json == {"message": "Job not found in database", "result": "error"}
def test_cancel_job(client, sample_scheduled_job):
job_id = str(sample_scheduled_job.id)
service_id = sample_scheduled_job.service.id
path = "/service/{}/job/{}/cancel".format(service_id, job_id)
auth_header = create_authorization_header()
response = client.post(path, headers=[auth_header])
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["data"]["id"] == job_id
assert resp_json["data"]["job_status"] == "cancelled"
def test_cant_cancel_normal_job(client, sample_job, mocker):
job_id = str(sample_job.id)
service_id = sample_job.service.id
mock_update = mocker.patch("app.dao.jobs_dao.dao_update_job")
path = "/service/{}/job/{}/cancel".format(service_id, job_id)
auth_header = create_authorization_header()
response = client.post(path, headers=[auth_header])
assert response.status_code == 404
assert mock_update.call_count == 0
@pytest.mark.skip(reason="Letter tests")
@freeze_time("2019-06-13 13:00")
def test_cancel_letter_job_updates_notifications_and_job_to_cancelled(sample_letter_template, admin_request, mocker):
job = create_job(template=sample_letter_template, notification_count=1, job_status="finished")
save_notification(create_notification(template=job.template, job=job, status="created"))
mock_get_job = mocker.patch("app.job.rest.dao_get_job_by_service_id_and_job_id", return_value=job)
mock_can_letter_job_be_cancelled = mocker.patch("app.job.rest.can_letter_job_be_cancelled", return_value=(True, None))
mock_dao_cancel_letter_job = mocker.patch("app.job.rest.dao_cancel_letter_job", return_value=1)
response = admin_request.post(
"job.cancel_letter_job",
service_id=job.service_id,
job_id=job.id,
)
mock_get_job.assert_called_once_with(job.service_id, str(job.id))
mock_can_letter_job_be_cancelled.assert_called_once_with(job)
mock_dao_cancel_letter_job.assert_called_once_with(job)
assert response == 1
@pytest.mark.skip(reason="Letter tests")
@freeze_time("2019-06-13 13:00")
def test_cancel_letter_job_does_not_call_cancel_if_can_letter_job_be_cancelled_returns_False(
sample_letter_template, admin_request, mocker
):
job = create_job(template=sample_letter_template, notification_count=2, job_status="finished")
save_notification(create_notification(template=job.template, job=job, status="sending"))
save_notification(create_notification(template=job.template, job=job, status="created"))
mock_get_job = mocker.patch("app.job.rest.dao_get_job_by_service_id_and_job_id", return_value=job)
error_message = "Sorry, it's too late, letters have already been sent."
mock_can_letter_job_be_cancelled = mocker.patch(
"app.job.rest.can_letter_job_be_cancelled", return_value=(False, error_message)
)
mock_dao_cancel_letter_job = mocker.patch("app.job.rest.dao_cancel_letter_job")
response = admin_request.post(
"job.cancel_letter_job",
service_id=job.service_id,
job_id=job.id,
_expected_status=400,
)
mock_get_job.assert_called_once_with(job.service_id, str(job.id))
mock_can_letter_job_be_cancelled.assert_called_once_with(job)
mock_dao_cancel_letter_job.assert_not_called
assert response["message"] == "Sorry, it's too late, letters have already been sent."
def test_create_unscheduled_job(client, sample_template, mocker, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
"original_file_name": "thisisatest.csv",
"notification_count": "1",
"valid": "True",
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
assert response.status_code == 201
app.celery.tasks.process_job.apply_async.assert_called_once_with(([str(fake_uuid)]), queue="job-tasks")
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["data"]["id"] == fake_uuid
assert resp_json["data"]["statistics"] == []
assert resp_json["data"]["job_status"] == "pending"
assert not resp_json["data"]["scheduled_for"]
assert resp_json["data"]["job_status"] == "pending"
assert resp_json["data"]["template"] == str(sample_template.id)
assert resp_json["data"]["original_file_name"] == "thisisatest.csv"
assert resp_json["data"]["notification_count"] == 1
def test_create_unscheduled_job_with_sender_id_in_metadata(client, sample_template, mocker, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
"original_file_name": "thisisatest.csv",
"notification_count": "1",
"valid": "True",
"sender_id": fake_uuid,
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
assert response.status_code == 201
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["data"]["sender_id"] == fake_uuid
app.celery.tasks.process_job.apply_async.assert_called_once_with(([str(fake_uuid)]), queue="job-tasks")
@freeze_time("2016-01-01 12:00:00.000000")
def test_create_scheduled_job(client, sample_template, mocker, fake_uuid):
scheduled_date = (datetime.utcnow() + timedelta(hours=95, minutes=59)).isoformat()
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
"original_file_name": "thisisatest.csv",
"notification_count": "1",
"valid": "True",
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
"scheduled_for": scheduled_date,
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
assert response.status_code == 201
app.celery.tasks.process_job.apply_async.assert_not_called()
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["data"]["id"] == fake_uuid
assert resp_json["data"]["scheduled_for"] == datetime(2016, 1, 5, 11, 59, 0, tzinfo=pytz.UTC).isoformat(
timespec="microseconds"
)
assert resp_json["data"]["job_status"] == "scheduled"
assert resp_json["data"]["template"] == str(sample_template.id)
assert resp_json["data"]["original_file_name"] == "thisisatest.csv"
assert resp_json["data"]["notification_count"] == 1
def test_create_job_returns_403_if_service_is_not_active(client, fake_uuid, sample_service, mocker):
sample_service.active = False
mock_job_dao = mocker.patch("app.dao.jobs_dao.dao_create_job")
auth_header = create_authorization_header()
response = client.post(
"/service/{}/job".format(sample_service.id),
data="",
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 403
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["result"] == "error"
assert resp_json["message"] == "Create job is not allowed: service is inactive "
mock_job_dao.assert_not_called()
@pytest.mark.parametrize("extra_metadata, test_run", [({}, 1), ({"valid": "anything not the string True"}, 2)])
def test_create_job_returns_400_if_file_is_invalid(client, fake_uuid, sample_template, mocker, extra_metadata, test_run):
mock_job_dao = mocker.patch("app.dao.jobs_dao.dao_create_job")
auth_header = create_authorization_header()
metadata = dict(
template_id=str(sample_template.id),
original_file_name=f"thisisatest{test_run}.csv",
notification_count=1,
**extra_metadata,
)
mocker.patch("app.job.rest.get_job_metadata_from_s3", return_value=metadata)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {"id": fake_uuid}
response = client.post(
"/service/{}/job".format(sample_template.service.id),
data=json.dumps(data),
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 400
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["result"] == "error"
assert resp_json["message"] == "File is not valid, can't create job"
mock_job_dao.assert_not_called()
@pytest.mark.skip(reason="Letter tests")
def test_create_job_returns_403_if_letter_template_type_and_service_in_trial(
client, fake_uuid, sample_trial_letter_template, mocker
):
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_trial_letter_template.id),
"original_file_name": "thisisatest.csv",
"notification_count": "1",
},
)
data = {
"id": fake_uuid,
"created_by": str(sample_trial_letter_template.created_by.id),
}
mock_job_dao = mocker.patch("app.dao.jobs_dao.dao_create_job")
auth_header = create_authorization_header()
response = client.post(
"/service/{}/job".format(sample_trial_letter_template.service.id),
data=json.dumps(data),
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 403
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["result"] == "error"
assert resp_json["message"] == "Create letter job is not allowed for service in trial mode "
mock_job_dao.assert_not_called()
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_not_create_scheduled_job_too_far_in_the_future(client, sample_template, mocker, fake_uuid):
scheduled_date = (datetime.utcnow() + timedelta(hours=96, minutes=1)).isoformat()
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
"original_file_name": "thisisatest.csv",
"notification_count": "1",
"valid": "True",
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
"scheduled_for": scheduled_date,
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
assert response.status_code == 400
app.celery.tasks.process_job.apply_async.assert_not_called()
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["result"] == "error"
assert "scheduled_for" in resp_json["message"]
assert resp_json["message"]["scheduled_for"] == ["Date cannot be more than 96 hours in the future"]
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_not_create_scheduled_job_in_the_past(client, sample_template, mocker, fake_uuid):
scheduled_date = (datetime.utcnow() - timedelta(minutes=1)).isoformat()
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
"original_file_name": "thisisatest.csv",
"notification_count": "1",
"valid": "True",
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
"scheduled_for": scheduled_date,
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
assert response.status_code == 400
app.celery.tasks.process_job.apply_async.assert_not_called()
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json["result"] == "error"
assert "scheduled_for" in resp_json["message"]
assert resp_json["message"]["scheduled_for"] == ["Date cannot be in the past"]
def test_create_job_returns_400_if_missing_id(client, sample_template, mocker):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
},
)
data = {}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json["result"] == "error"
assert "Missing data for required field." in resp_json["message"]["id"]
def test_create_job_returns_400_if_missing_data(client, sample_template, mocker, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"valid": "True",
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json["result"] == "error"
assert "Missing data for required field." in resp_json["message"]["original_file_name"]
assert "Missing data for required field." in resp_json["message"]["notification_count"]
def test_create_job_returns_404_if_template_does_not_exist(client, sample_service, mocker, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_service.id),
},
)
data = {
"id": fake_uuid,
}
path = "/service/{}/job".format(sample_service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json["result"] == "error"
assert resp_json["message"] == "No result found"
def test_create_job_returns_404_if_missing_service(client, sample_template, mocker):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
},
)
random_id = str(uuid.uuid4())
data = {}
path = "/service/{}/job".format(random_id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json["result"] == "error"
assert resp_json["message"] == "No result found"
def test_create_job_returns_400_if_archived_template(client, sample_template, mocker, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
sample_template.archived = True
dao_update_template(sample_template)
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": str(sample_template.id),
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="phone number\r\n6502532222",
)
data = {
"id": fake_uuid,
"valid": "True",
}
path = "/service/{}/job".format(sample_template.service.id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]
response = client.post(path, data=json.dumps(data), headers=headers)
resp_json = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
app.celery.tasks.process_job.apply_async.assert_not_called()
assert resp_json["result"] == "error"
assert "Template has been deleted" in resp_json["message"]["template"]
def _setup_jobs(template, number_of_jobs=5):
for i in range(number_of_jobs):
create_job(template=template)
def test_get_all_notifications_for_job_in_order_of_job_number(admin_request, sample_template):
main_job = create_job(sample_template)
another_job = create_job(sample_template)
notification_1 = save_notification(create_notification(job=main_job, to_field="1", job_row_number=1))
notification_2 = save_notification(create_notification(job=main_job, to_field="2", job_row_number=2))
notification_3 = save_notification(create_notification(job=main_job, to_field="3", job_row_number=3))
save_notification(create_notification(job=another_job))
resp = admin_request.get(
"job.get_all_notifications_for_service_job",
service_id=main_job.service_id,
job_id=main_job.id,
)
assert len(resp["notifications"]) == 3
assert resp["notifications"][0]["to"] == notification_1.to
assert resp["notifications"][0]["job_row_number"] == notification_1.job_row_number
assert resp["notifications"][1]["to"] == notification_2.to
assert resp["notifications"][1]["job_row_number"] == notification_2.job_row_number
assert resp["notifications"][2]["to"] == notification_3.to
assert resp["notifications"][2]["job_row_number"] == notification_3.job_row_number
@pytest.mark.parametrize(
"expected_notification_count, status_args",
[
(1, ["created"]),
(0, ["sending"]),
(1, ["created", "sending"]),
(0, ["sending", "delivered"]),
],
)
def test_get_all_notifications_for_job_filtered_by_status(admin_request, sample_job, expected_notification_count, status_args):
save_notification(create_notification(job=sample_job, to_field="1", status="created"))
resp = admin_request.get(
"job.get_all_notifications_for_service_job",
service_id=sample_job.service_id,
job_id=sample_job.id,
status=status_args,
)
assert len(resp["notifications"]) == expected_notification_count
def test_get_all_notifications_for_job_returns_correct_format(admin_request, sample_notification_with_job):
service_id = sample_notification_with_job.service_id
job_id = sample_notification_with_job.job_id
resp = admin_request.get(
"job.get_all_notifications_for_service_job",
service_id=service_id,
job_id=job_id,
)
assert len(resp["notifications"]) == 1
assert resp["notifications"][0]["id"] == str(sample_notification_with_job.id)
assert resp["notifications"][0]["status"] == sample_notification_with_job.status
def test_get_job_by_id(admin_request, sample_job):
job_id = str(sample_job.id)
service_id = sample_job.service.id
resp_json = admin_request.get("job.get_job_by_service_and_job_id", service_id=service_id, job_id=job_id)
assert resp_json["data"]["id"] == job_id
assert resp_json["data"]["statistics"] == []
assert resp_json["data"]["created_by"]["name"] == "Test User"
def test_get_job_by_id_should_return_summed_statistics(admin_request, sample_job):
job_id = str(sample_job.id)
service_id = sample_job.service.id
save_notification(create_notification(job=sample_job, status="created"))
save_notification(create_notification(job=sample_job, status="created"))
save_notification(create_notification(job=sample_job, status="created"))
save_notification(create_notification(job=sample_job, status="sending"))
save_notification(create_notification(job=sample_job, status="failed"))
save_notification(create_notification(job=sample_job, status="failed"))
save_notification(create_notification(job=sample_job, status="failed"))
save_notification(create_notification(job=sample_job, status="technical-failure"))
save_notification(create_notification(job=sample_job, status="temporary-failure"))
save_notification(create_notification(job=sample_job, status="temporary-failure"))
resp_json = admin_request.get("job.get_job_by_service_and_job_id", service_id=service_id, job_id=job_id)
assert resp_json["data"]["id"] == job_id
assert {"status": "created", "count": 3} in resp_json["data"]["statistics"]
assert {"status": "sending", "count": 1} in resp_json["data"]["statistics"]
assert {"status": "failed", "count": 3} in resp_json["data"]["statistics"]
assert {"status": "technical-failure", "count": 1} in resp_json["data"]["statistics"]
assert {"status": "temporary-failure", "count": 2} in resp_json["data"]["statistics"]
assert resp_json["data"]["created_by"]["name"] == "Test User"
def test_get_jobs(admin_request, sample_template):
_setup_jobs(sample_template)
service_id = sample_template.service.id
resp_json = admin_request.get("job.get_jobs_by_service", service_id=service_id)
assert len(resp_json["data"]) == 5
def test_get_jobs_with_limit_days(admin_request, sample_template):
for time in [
"Sunday 1st July 2018 22:59",
"Sunday 2nd July 2018 23:00", # beginning of monday morning
"Monday 3rd July 2018 12:00",
]:
with freeze_time(time):
create_job(template=sample_template)
with freeze_time("Monday 9th July 2018 12:00"):
resp_json = admin_request.get(
"job.get_jobs_by_service",
service_id=sample_template.service_id,
limit_days=7,
)
assert len(resp_json["data"]) == 2
def test_get_jobs_should_return_statistics(admin_request, sample_template):
now = datetime.utcnow()
earlier = datetime.utcnow() - timedelta(days=1)
job_1 = create_job(sample_template, processing_started=earlier)
job_2 = create_job(sample_template, processing_started=now)
save_notification(create_notification(job=job_1, status="created"))
save_notification(create_notification(job=job_1, status="created"))
save_notification(create_notification(job=job_1, status="created"))
save_notification(create_notification(job=job_2, status="sending"))
save_notification(create_notification(job=job_2, status="sending"))
save_notification(create_notification(job=job_2, status="sending"))
resp_json = admin_request.get("job.get_jobs_by_service", service_id=sample_template.service_id)
assert len(resp_json["data"]) == 2
assert resp_json["data"][0]["id"] == str(job_2.id)
assert {"status": "sending", "count": 3} in resp_json["data"][0]["statistics"]
assert resp_json["data"][1]["id"] == str(job_1.id)
assert {"status": "created", "count": 3} in resp_json["data"][1]["statistics"]
def test_get_jobs_should_return_no_stats_if_no_rows_in_notifications(admin_request, sample_template):
now = datetime.utcnow()
earlier = datetime.utcnow() - timedelta(days=1)
job_1 = create_job(sample_template, created_at=earlier)
job_2 = create_job(sample_template, created_at=now)
resp_json = admin_request.get("job.get_jobs_by_service", service_id=sample_template.service_id)
assert len(resp_json["data"]) == 2
assert resp_json["data"][0]["id"] == str(job_2.id)
assert resp_json["data"][0]["statistics"] == []
assert resp_json["data"][1]["id"] == str(job_1.id)
assert resp_json["data"][1]["statistics"] == []
def test_get_jobs_should_paginate(admin_request, sample_template):
create_10_jobs(sample_template)
with set_config(admin_request.app, "PAGE_SIZE", 2):
resp_json = admin_request.get("job.get_jobs_by_service", service_id=sample_template.service_id)
assert resp_json["data"][0]["created_at"] == "2015-01-01T10:00:00.000000+00:00"
assert resp_json["data"][1]["created_at"] == "2015-01-01T09:00:00.000000+00:00"
assert resp_json["page_size"] == 2
assert resp_json["total"] == 10
assert "links" in resp_json
assert set(resp_json["links"].keys()) == {"next", "last"}
def test_get_jobs_accepts_page_parameter(admin_request, sample_template):
create_10_jobs(sample_template)
with set_config(admin_request.app, "PAGE_SIZE", 2):
resp_json = admin_request.get("job.get_jobs_by_service", service_id=sample_template.service_id, page=2)
assert resp_json["data"][0]["created_at"] == "2015-01-01T08:00:00.000000+00:00"
assert resp_json["data"][1]["created_at"] == "2015-01-01T07:00:00.000000+00:00"
assert resp_json["page_size"] == 2
assert resp_json["total"] == 10
assert "links" in resp_json
assert set(resp_json["links"].keys()) == {"prev", "next", "last"}
@pytest.mark.parametrize(
"statuses_filter, expected_statuses",
[
("", JOB_STATUS_TYPES),
("pending", [JOB_STATUS_PENDING]),
(
"pending, in progress, finished, sending limits exceeded, scheduled, cancelled, ready to send, sent to dvla, error", # noqa
JOB_STATUS_TYPES,
),
# bad statuses are accepted, just return no data
("foo", []),
],
)
def test_get_jobs_can_filter_on_statuses(admin_request, sample_template, statuses_filter, expected_statuses):
create_job(sample_template, job_status="pending")
create_job(sample_template, job_status="in progress")
create_job(sample_template, job_status="finished")
create_job(sample_template, job_status="sending limits exceeded")
create_job(sample_template, job_status="scheduled")
create_job(sample_template, job_status="cancelled")
create_job(sample_template, job_status="ready to send")
create_job(sample_template, job_status="sent to dvla")
create_job(sample_template, job_status="error")
resp_json = admin_request.get(
"job.get_jobs_by_service",
service_id=sample_template.service_id,
statuses=statuses_filter,
)
assert {x["job_status"] for x in resp_json["data"]} == set(expected_statuses)
def create_10_jobs(template):
with freeze_time("2015-01-01T00:00:00") as the_time:
for _ in range(10):
the_time.tick(timedelta(hours=1))
create_job(template)
def test_get_all_notifications_for_job_returns_csv_format(admin_request, sample_notification_with_job):
resp = admin_request.get(
"job.get_all_notifications_for_service_job",
service_id=sample_notification_with_job.service_id,
job_id=sample_notification_with_job.job_id,
format_for_csv=True,
)
assert len(resp["notifications"]) == 1
assert set(resp["notifications"][0].keys()) == {
"created_at",
"created_by_name",
"created_by_email_address",
"template_type",
"template_name",
"job_name",
"status",
"row_number",
"recipient",
}
@freeze_time("2017-06-10 4:00")
# This test assumes the local timezone is EST
def test_get_jobs_should_retrieve_from_ft_notification_status_for_old_jobs(admin_request, sample_template):
# it's the 10th today, so 3 days should include all of 7th, 8th, 9th, and some of 10th.
just_three_days_ago = datetime(2017, 6, 7, 3, 59, 59)
not_quite_three_days_ago = just_three_days_ago + timedelta(seconds=1)
job_1 = create_job(
sample_template,
created_at=just_three_days_ago,
processing_started=just_three_days_ago,
)
job_2 = create_job(
sample_template,
created_at=just_three_days_ago,
processing_started=not_quite_three_days_ago,
)
# is old but hasn't started yet (probably a scheduled job). We don't have any stats for this job yet.
job_3 = create_job(sample_template, created_at=just_three_days_ago, processing_started=None)
# some notifications created more than three days ago, some created after the midnight cutoff
create_ft_notification_status(date(2017, 6, 6), job=job_1, notification_status="delivered", count=2)
create_ft_notification_status(date(2017, 6, 7), job=job_1, notification_status="delivered", count=4)
# job2's new enough
save_notification(create_notification(job=job_2, status="created", created_at=not_quite_three_days_ago))
# this isn't picked up because the job is too new
create_ft_notification_status(date(2017, 6, 7), job=job_2, notification_status="delivered", count=8)
# this isn't picked up - while the job is old, it started in last 3 days so we look at notification table instead
create_ft_notification_status(date(2017, 6, 7), job=job_3, notification_status="delivered", count=16)
# this isn't picked up because we're using the ft status table for job_1 as it's old
save_notification(create_notification(job=job_1, status="created", created_at=not_quite_three_days_ago))
resp_json = admin_request.get("job.get_jobs_by_service", service_id=sample_template.service_id)
assert resp_json["data"][0]["id"] == str(job_3.id)
assert resp_json["data"][0]["statistics"] == []
assert resp_json["data"][1]["id"] == str(job_2.id)
assert resp_json["data"][1]["statistics"] == [{"status": "created", "count": 1}]
assert resp_json["data"][2]["id"] == str(job_1.id)
assert resp_json["data"][2]["statistics"] == [{"status": "delivered", "count": 6}]