-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtest_rest.py
More file actions
1159 lines (990 loc) · 38.9 KB
/
test_rest.py
File metadata and controls
1159 lines (990 loc) · 38.9 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
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import uuid
from datetime import date, datetime, timedelta
from unittest.mock import ANY
from zoneinfo import ZoneInfo
import pytest
from freezegun import freeze_time
import app.celery.tasks
from app.dao.templates_dao import dao_update_template
from app.enums import (
JobStatus,
KeyType,
NotificationStatus,
NotificationType,
TemplateType,
)
from app.utils import utc_now
from tests import create_admin_authorization_header
from tests.app.db import (
create_ft_notification_status,
create_job,
create_notification,
create_service,
create_template,
)
from tests.conftest import set_config
def test_get_job_with_invalid_service_id_returns404(client, sample_service):
path = f"/service/{sample_service.id}/job"
auth_header = create_admin_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 = f"/service/{service_id}/job/{'bad-id'}"
auth_header = create_admin_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 = f"/service/{service_id}/job/{fake_uuid}"
auth_header = create_admin_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": "No result found", "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 = f"/service/{service_id}/job/{job_id}/cancel"
auth_header = create_admin_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"] == JobStatus.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 = f"/service/{service_id}/job/{job_id}/cancel"
auth_header = create_admin_authorization_header()
response = client.post(path, headers=[auth_header])
assert response.status_code == 404
assert mock_update.call_count == 0
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",
},
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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)]), {"sender_id": None}, 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"] == JobStatus.PENDING
assert not resp_json["data"]["scheduled_for"]
assert resp_json["data"]["job_status"] == JobStatus.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,
},
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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)]),
{"sender_id": 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 = (utc_now() + 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",
},
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
"scheduled_for": scheduled_date,
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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=ZoneInfo("UTC")).isoformat()
)
assert resp_json["data"]["job_status"] == JobStatus.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_admin_authorization_header()
response = client.post(
f"/service/{sample_service.id}/job",
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",
(
{},
{"valid": "anything not the string True"},
),
)
def test_create_job_returns_400_if_file_is_invalid(
client,
fake_uuid,
sample_template,
mocker,
extra_metadata,
):
mock_job_dao = mocker.patch("app.dao.jobs_dao.dao_create_job")
auth_header = create_admin_authorization_header()
metadata = dict(
template_id=str(sample_template.id),
original_file_name="thisisatest.csv",
notification_count=1,
**extra_metadata,
)
mocker.patch("app.job.rest.get_job_metadata_from_s3", return_value=metadata)
data = {"id": fake_uuid}
response = client.post(
f"/service/{sample_template.service.id}/job",
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()
@freeze_time("2016-01-01 11:09:00.061258")
def test_should_not_create_scheduled_job_more_then_96_hours_in_the_future(
client, sample_template, mocker, fake_uuid
):
scheduled_date = (utc_now() + 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",
},
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
"scheduled_for": scheduled_date,
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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 96hrs 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 = (utc_now() - 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",
},
)
data = {
"id": fake_uuid,
"created_by": str(sample_template.created_by.id),
"scheduled_for": scheduled_date,
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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 = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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),
},
)
data = {
"id": fake_uuid,
"valid": "True",
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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 = f"/service/{sample_service.id}/job"
auth_header = create_admin_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 = f"/service/{random_id}/job"
auth_header = create_admin_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),
},
)
data = {
"id": fake_uuid,
"valid": "True",
}
path = f"/service/{sample_template.service.id}/job"
auth_header = create_admin_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 _ 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, mocker
):
mock_job = mocker.patch("app.job.rest.get_job_from_s3")
mock_job.return_value = None
mock_s3 = mocker.patch("app.job.rest.extract_phones")
mock_s3.return_value = {0: "15555555555", 1: "15555555555", 2: "15555555555"}
mock_s3_personalisation = mocker.patch("app.job.rest.extract_personalisation")
mock_s3_personalisation.return_value = {0: "", 1: "", 2: ""}
main_job = create_job(sample_template)
another_job = create_job(sample_template)
notification_1 = create_notification(job=main_job, to_field="1", job_row_number=1)
notification_2 = create_notification(job=main_job, to_field="2", job_row_number=2)
notification_3 = create_notification(job=main_job, to_field="3", job_row_number=3)
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
def test_get_recent_notifications_for_job_in_reverse_order_of_job_number(
admin_request, sample_template, mocker
):
mock_s3 = mocker.patch("app.job.rest.get_phone_number_from_s3")
mock_s3.return_value = "15555555555"
mock_s3_personalisation = mocker.patch("app.job.rest.get_personalisation_from_s3")
mock_s3_personalisation.return_value = {}
main_job = create_job(sample_template)
another_job = create_job(sample_template)
count = 1
for status in NotificationStatus:
create_notification(job=main_job, job_row_number=str(count), status=status)
count = count + 1
create_notification(job=another_job)
resp = admin_request.get(
"job.get_recent_notifications_for_service_job",
service_id=main_job.service_id,
job_id=main_job.id,
)
assert len(resp["notifications"]) == 13
assert resp["notifications"][0]["status"] == "virus-scan-failed"
assert resp["notifications"][0]["job_row_number"] == 13
query_string = {"status": "delivered"}
resp = admin_request.get(
"job.get_recent_notifications_for_service_job",
service_id=main_job.service_id,
job_id=main_job.id,
**query_string,
)
assert len(resp["notifications"]) == 1
assert resp["notifications"][0]["status"] == "delivered"
assert resp["notifications"][0]["job_row_number"] == 5
@pytest.mark.parametrize(
"expected_notification_count, status_args, expected_phones, expected_personalisation",
[
(1, [NotificationStatus.CREATED], {0: "15555555555"}, {0: ""}),
(0, [NotificationStatus.SENDING], {}, {}),
(
1,
[NotificationStatus.CREATED, NotificationStatus.SENDING],
{0: "15555555555"},
{0: ""},
),
(0, [NotificationStatus.SENDING, NotificationStatus.DELIVERED], {}, {}),
],
)
def test_get_all_notifications_for_job_filtered_by_status(
admin_request,
sample_job,
expected_notification_count,
status_args,
expected_phones,
expected_personalisation,
mocker,
):
mock_job = mocker.patch("app.job.rest.get_job_from_s3")
mock_job.return_value = None
mock_s3 = mocker.patch("app.job.rest.extract_phones")
mock_s3.return_value = expected_phones
mock_s3_personalisation = mocker.patch("app.job.rest.extract_personalisation")
mock_s3_personalisation.return_value = expected_personalisation
sample_job.job_row_number = 0
create_notification(job=sample_job, to_field="1", status=NotificationStatus.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, mocker
):
mock_job = mocker.patch("app.job.rest.get_job_from_s3")
mock_job.return_value = None
mock_s3 = mocker.patch("app.job.rest.extract_phones")
mock_s3.return_value = {0: "15555555555"}
mock_s3_personalisation = mocker.patch("app.job.rest.extract_personalisation")
mock_s3_personalisation.return_value = {0: ""}
sample_notification_with_job.job_row_number = 0
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_notification_count_for_job_id(admin_request, mocker, sample_job):
mock_dao = mocker.patch(
"app.job.rest.dao_get_notification_count_for_job_id", return_value=3
)
response = admin_request.get(
"job.get_notification_count_for_job_id",
service_id=sample_job.service_id,
job_id=sample_job.id,
)
mock_dao.assert_called_once_with(job_id=str(sample_job.id))
assert response["count"] == 3
def test_get_notification_count_for_job_id_for_wrong_service_id(
admin_request, sample_job
):
service_id = uuid.uuid4()
response = admin_request.get(
"job.get_notification_count_for_job_id",
service_id=service_id,
job_id=sample_job.id,
_expected_status=404,
)
assert response["message"] == "No result found"
def test_get_notification_count_for_job_id_for_wrong_job_id(
admin_request, sample_service
):
job_id = uuid.uuid4()
response = admin_request.get(
"job.get_notification_count_for_job_id",
service_id=sample_service.id,
job_id=job_id,
_expected_status=404,
)
assert response["message"] == "No result found"
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
create_notification(job=sample_job, status=NotificationStatus.CREATED)
create_notification(job=sample_job, status=NotificationStatus.CREATED)
create_notification(job=sample_job, status=NotificationStatus.CREATED)
create_notification(job=sample_job, status=NotificationStatus.SENDING)
create_notification(job=sample_job, status=NotificationStatus.FAILED)
create_notification(job=sample_job, status=NotificationStatus.FAILED)
create_notification(job=sample_job, status=NotificationStatus.FAILED)
create_notification(job=sample_job, status=NotificationStatus.TECHNICAL_FAILURE)
create_notification(job=sample_job, status=NotificationStatus.TEMPORARY_FAILURE)
create_notification(job=sample_job, status=NotificationStatus.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": NotificationStatus.CREATED,
"count": 3,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.SENDING,
"count": 1,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.FAILED,
"count": 3,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.TECHNICAL_FAILURE,
"count": 1,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.TEMPORARY_FAILURE,
"count": 2,
} in resp_json[
"data"
]["statistics"]
assert resp_json["data"]["created_by"]["name"] == "Test User"
def test_get_job_by_id_with_stats_for_old_job_where_notifications_have_been_purged(
admin_request, sample_template
):
old_job = create_job(
sample_template,
notification_count=10,
created_at=utc_now() - timedelta(days=9),
job_status=JobStatus.FINISHED,
)
def __create_ft_status(job, status, count):
create_ft_notification_status(
local_date=job.created_at.date(),
notification_type=NotificationType.SMS,
service=job.service,
job=job,
template=job.template,
key_type=KeyType.NORMAL,
notification_status=status,
count=count,
)
__create_ft_status(old_job, NotificationStatus.CREATED, 3)
__create_ft_status(old_job, NotificationStatus.SENDING, 1)
__create_ft_status(old_job, NotificationStatus.FAILED, 3)
__create_ft_status(old_job, NotificationStatus.TECHNICAL_FAILURE, 1)
__create_ft_status(old_job, NotificationStatus.TEMPORARY_FAILURE, 2)
resp_json = admin_request.get(
"job.get_job_by_service_and_job_id",
service_id=old_job.service_id,
job_id=old_job.id,
)
assert resp_json["data"]["id"] == str(old_job.id)
assert {
"status": NotificationStatus.CREATED,
"count": 3,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.SENDING,
"count": 1,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.FAILED,
"count": 3,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.TECHNICAL_FAILURE,
"count": 1,
} in resp_json[
"data"
]["statistics"]
assert {
"status": NotificationStatus.TEMPORARY_FAILURE,
"count": 2,
} in resp_json[
"data"
]["statistics"]
assert resp_json["data"]["created_by"]["name"] == "Test User"
@freeze_time("2017-07-17 07:17")
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
assert resp_json["data"][0] == {
"archived": False,
"created_at": "2017-07-17T07:17:00+00:00",
"created_by": {
"id": ANY,
"name": "Test User",
},
"id": ANY,
"job_status": JobStatus.PENDING,
"notification_count": 1,
"original_file_name": "some.csv",
"processing_finished": None,
"processing_started": None,
"scheduled_for": None,
"service": str(sample_template.service.id),
"service_name": {"name": sample_template.service.name},
"statistics": [],
"template": str(sample_template.id),
"template_name": sample_template.name,
"template_type": TemplateType.SMS,
"template_version": 1,
"updated_at": None,
}
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 = utc_now()
earlier = utc_now() - timedelta(days=1)
job_1 = create_job(sample_template, processing_started=earlier)
job_2 = create_job(sample_template, processing_started=now)
create_notification(job=job_1, status=NotificationStatus.CREATED)
create_notification(job=job_1, status=NotificationStatus.CREATED)
create_notification(job=job_1, status=NotificationStatus.CREATED)
create_notification(job=job_2, status=NotificationStatus.SENDING)
create_notification(job=job_2, status=NotificationStatus.SENDING)
create_notification(job=job_2, status=NotificationStatus.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": NotificationStatus.SENDING,
"count": 3,
} in resp_json["data"][
0
]["statistics"]
assert resp_json["data"][1]["id"] == str(job_1.id)
assert {
"status": NotificationStatus.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 = utc_now()
earlier = utc_now() - 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+00:00"
assert resp_json["data"][1]["created_at"] == "2015-01-01T09:00:00+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", "prev"}
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+00:00"
assert resp_json["data"][1]["created_at"] == "2015-01-01T07:00:00+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",
[
("", list(JobStatus)),
("pending", [JobStatus.PENDING]),
(
"pending, in progress, finished, sending limits exceeded, scheduled, cancelled, ready to send, sent to dvla, error", # noqa
list(JobStatus),
),
# 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=JobStatus.PENDING)
create_job(sample_template, job_status=JobStatus.IN_PROGRESS)
create_job(sample_template, job_status=JobStatus.FINISHED)
create_job(sample_template, job_status=JobStatus.SENDING_LIMITS_EXCEEDED)
create_job(sample_template, job_status=JobStatus.SCHEDULED)
create_job(sample_template, job_status=JobStatus.CANCELLED)
create_job(sample_template, job_status=JobStatus.READY_TO_SEND)
create_job(sample_template, job_status=JobStatus.SENT_TO_DVLA)
create_job(sample_template, job_status=JobStatus.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, mocker
):
mock_job = mocker.patch("app.job.rest.get_job_from_s3")
mock_job.return_value = None
mock_s3 = mocker.patch("app.job.rest.extract_phones")
mock_s3.return_value = {0: "15555555555"}
mock_s3_personalisation = mocker.patch("app.job.rest.extract_personalisation")
mock_s3_personalisation.return_value = {0: ""}
sample_notification_with_job.job_row_number = 0
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",
"carrier",
"provider_response",
"status",
"row_number",
"recipient",
"client_reference",
}
@freeze_time("2017-06-10 00:00")
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, 6, 23, 59, 59)
not_quite_three_days_ago = just_three_days_ago + timedelta(seconds=1)
job_1 = create_job(