forked from cds-snc/notification-api
-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_rest.py
1405 lines (1155 loc) · 49.2 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
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 base64
import json
import random
import string
import uuid
from datetime import datetime, timedelta
import botocore
import pytest
import requests_mock
from PyPDF2.utils import PdfReadError
from freezegun import freeze_time
from notifications_utils import SMS_CHAR_COUNT_LIMIT
from app.models import (
EMAIL_TYPE,
LETTER_TYPE,
SMS_TYPE,
Template,
TemplateHistory
)
from app.dao.templates_dao import dao_get_template_by_id, dao_redact_template
from app.dao.service_permissions_dao import dao_add_service_permission
from tests import create_authorization_header
from tests.app.conftest import sample_template as create_sample_template
from tests.app.db import (
create_service, create_letter_contact, create_template, create_notification,
create_template_folder,
)
from tests.conftest import set_config_values
@pytest.mark.parametrize('template_type, subject', [
(SMS_TYPE, None),
(EMAIL_TYPE, 'subject'),
(LETTER_TYPE, 'subject'),
])
def test_should_create_a_new_template_for_a_service(
client, sample_user, template_type, subject
):
service = create_service(service_permissions=[template_type])
data = {
'name': 'my template',
'template_type': template_type,
'content': 'template <b>content</b>',
'service': str(service.id),
'created_by': str(sample_user.id)
}
if subject:
data.update({'subject': subject})
if template_type == LETTER_TYPE:
data.update({'postage': 'first'})
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 201
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['data']['name'] == 'my template'
assert json_resp['data']['template_type'] == template_type
assert json_resp['data']['content'] == 'template <b>content</b>'
assert json_resp['data']['service'] == str(service.id)
assert json_resp['data']['id']
assert json_resp['data']['version'] == 1
assert json_resp['data']['process_type'] == 'normal'
assert json_resp['data']['created_by'] == str(sample_user.id)
if subject:
assert json_resp['data']['subject'] == 'subject'
else:
assert not json_resp['data']['subject']
if template_type == LETTER_TYPE:
assert json_resp['data']['postage'] == 'first'
else:
assert not json_resp['data']['postage']
template = Template.query.get(json_resp['data']['id'])
from app.schemas import template_schema
assert sorted(json_resp['data']) == sorted(template_schema.dump(template).data)
def test_create_a_new_template_for_a_service_adds_folder_relationship(
client, sample_service
):
parent_folder = create_template_folder(service=sample_service, name='parent folder')
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template <b>content</b>',
'service': str(sample_service.id),
'created_by': str(sample_service.users[0].id),
'parent_folder_id': str(parent_folder.id)
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 201
template = Template.query.filter(Template.name == 'my template').first()
assert template.folder == parent_folder
@pytest.mark.parametrize("template_type, expected_postage", [
(SMS_TYPE, None), (EMAIL_TYPE, None), (LETTER_TYPE, "second")
])
def test_create_a_new_template_for_a_service_adds_postage_for_letters_only(
client, sample_service, template_type, expected_postage
):
dao_add_service_permission(service_id=sample_service.id, permission=LETTER_TYPE)
data = {
'name': 'my template',
'template_type': template_type,
'content': 'template <b>content</b>',
'service': str(sample_service.id),
'created_by': str(sample_service.users[0].id)
}
if template_type in [EMAIL_TYPE, LETTER_TYPE]:
data["subject"] = "Hi, I have good news"
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 201
template = Template.query.filter(Template.name == 'my template').first()
assert template.postage == expected_postage
def test_create_template_should_return_400_if_folder_is_for_a_different_service(
client, sample_service
):
service2 = create_service(service_name='second service')
parent_folder = create_template_folder(service=service2)
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template <b>content</b>',
'service': str(sample_service.id),
'created_by': str(sample_service.users[0].id),
'parent_folder_id': str(parent_folder.id)
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 400
assert json.loads(response.get_data(as_text=True))['message'] == 'parent_folder_id not found'
def test_create_template_should_return_400_if_folder_does_not_exist(
client, sample_service
):
data = {
'name': 'my template',
'template_type': 'sms',
'content': 'template <b>content</b>',
'service': str(sample_service.id),
'created_by': str(sample_service.users[0].id),
'parent_folder_id': str(uuid.uuid4())
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 400
assert json.loads(response.get_data(as_text=True))['message'] == 'parent_folder_id not found'
def test_should_raise_error_if_service_does_not_exist_on_create(client, sample_user, fake_uuid):
data = {
'name': 'my template',
'template_type': SMS_TYPE,
'content': 'template content',
'service': fake_uuid,
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(fake_uuid),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'No result found'
@pytest.mark.parametrize('permissions, template_type, subject, expected_error', [
([EMAIL_TYPE], SMS_TYPE, None, {'template_type': ['Creating text message templates is not allowed']}),
([SMS_TYPE], EMAIL_TYPE, 'subject', {'template_type': ['Creating email templates is not allowed']}),
([SMS_TYPE], LETTER_TYPE, 'subject', {'template_type': ['Creating letter templates is not allowed']}),
])
def test_should_raise_error_on_create_if_no_permission(
client, sample_user, permissions, template_type, subject, expected_error):
service = create_service(service_permissions=permissions)
data = {
'name': 'my template',
'template_type': template_type,
'content': 'template content',
'service': str(service.id),
'created_by': str(sample_user.id)
}
if subject:
data.update({'subject': subject})
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 403
assert json_resp['result'] == 'error'
assert json_resp['message'] == expected_error
@pytest.mark.parametrize('template_type, permissions, expected_error', [
(SMS_TYPE, [EMAIL_TYPE], {'template_type': ['Updating text message templates is not allowed']}),
(EMAIL_TYPE, [LETTER_TYPE], {'template_type': ['Updating email templates is not allowed']}),
(LETTER_TYPE, [SMS_TYPE], {'template_type': ['Updating letter templates is not allowed']})
])
def test_should_be_error_on_update_if_no_permission(
client,
sample_user,
notify_db_session,
template_type,
permissions,
expected_error,
):
service = create_service(service_permissions=permissions)
template_without_permission = create_template(service, template_type=template_type)
data = {
'content': 'new template content',
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header()
update_response = client.post(
'/service/{}/template/{}'.format(
template_without_permission.service_id, template_without_permission.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(update_response.get_data(as_text=True))
assert update_response.status_code == 403
assert json_resp['result'] == 'error'
assert json_resp['message'] == expected_error
def test_should_error_if_created_by_missing(client, sample_user, sample_service):
service_id = str(sample_service.id)
data = {
'name': 'my template',
'template_type': SMS_TYPE,
'content': 'template content',
'service': service_id
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(service_id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 400
assert json_resp["errors"][0]["error"] == 'ValidationError'
assert json_resp["errors"][0]["message"] == 'created_by is a required property'
def test_should_be_error_if_service_does_not_exist_on_update(client, fake_uuid):
data = {
'name': 'my template'
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template/{}'.format(fake_uuid, fake_uuid),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert response.status_code == 404
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'No result found'
@pytest.mark.parametrize('template_type', [EMAIL_TYPE, LETTER_TYPE])
def test_must_have_a_subject_on_an_email_or_letter_template(client, sample_user, sample_service, template_type):
data = {
'name': 'my template',
'template_type': template_type,
'content': 'template content',
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['errors'][0]['error'] == "ValidationError"
assert json_resp['errors'][0]["message"] == 'subject is a required property'
def test_update_should_update_a_template(client, sample_user):
service = create_service(service_permissions=[LETTER_TYPE])
template = create_template(service, template_type="letter", postage="second")
data = {
'content': 'my template has new content, swell!',
'created_by': str(sample_user.id),
'postage': 'first'
}
data = json.dumps(data)
auth_header = create_authorization_header()
update_response = client.post(
'/service/{}/template/{}'.format(service.id, template.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert update_response.status_code == 200
update_json_resp = json.loads(update_response.get_data(as_text=True))
assert update_json_resp['data']['content'] == (
'my template has new content, swell!'
)
assert update_json_resp['data']['postage'] == 'first'
assert update_json_resp['data']['name'] == template.name
assert update_json_resp['data']['template_type'] == template.template_type
assert update_json_resp['data']['version'] == 2
def test_should_be_able_to_archive_template(client, sample_template):
data = {
'name': sample_template.name,
'template_type': sample_template.template_type,
'content': sample_template.content,
'archived': True,
'service': str(sample_template.service.id),
'created_by': str(sample_template.created_by.id)
}
json_data = json.dumps(data)
auth_header = create_authorization_header()
resp = client.post(
'/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=json_data
)
assert resp.status_code == 200
assert Template.query.first().archived
def test_get_precompiled_template_for_service(
client,
notify_user,
sample_service,
):
assert len(sample_service.templates) == 0
response = client.get(
'/service/{}/template/precompiled'.format(sample_service.id),
headers=[create_authorization_header()],
)
assert response.status_code == 200
assert len(sample_service.templates) == 1
data = json.loads(response.get_data(as_text=True))
assert data['name'] == 'Pre-compiled PDF'
assert data['hidden'] is True
def test_get_precompiled_template_for_service_when_service_has_existing_precompiled_template(
client,
notify_user,
sample_service,
):
create_template(
sample_service,
template_name='Exisiting precompiled template',
template_type=LETTER_TYPE,
hidden=True)
assert len(sample_service.templates) == 1
response = client.get(
'/service/{}/template/precompiled'.format(sample_service.id),
headers=[create_authorization_header()],
)
assert response.status_code == 200
assert len(sample_service.templates) == 1
data = json.loads(response.get_data(as_text=True))
assert data['name'] == 'Exisiting precompiled template'
assert data['hidden'] is True
def test_should_be_able_to_get_all_templates_for_a_service(client, sample_user, sample_service):
data = {
'name': 'my template 1',
'template_type': EMAIL_TYPE,
'subject': 'subject 1',
'content': 'template content',
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data_1 = json.dumps(data)
data = {
'name': 'my template 2',
'template_type': EMAIL_TYPE,
'subject': 'subject 2',
'content': 'template content',
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data_2 = json.dumps(data)
auth_header = create_authorization_header()
client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data_1
)
auth_header = create_authorization_header()
client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data_2
)
auth_header = create_authorization_header()
response = client.get(
'/service/{}/template'.format(sample_service.id),
headers=[auth_header]
)
assert response.status_code == 200
update_json_resp = json.loads(response.get_data(as_text=True))
assert update_json_resp['data'][0]['name'] == 'my template 1'
assert update_json_resp['data'][0]['version'] == 1
assert update_json_resp['data'][0]['created_at']
assert update_json_resp['data'][1]['name'] == 'my template 2'
assert update_json_resp['data'][1]['version'] == 1
assert update_json_resp['data'][1]['created_at']
def test_should_get_only_templates_for_that_service(admin_request, notify_db_session):
service_1 = create_service(service_name='service_1')
service_2 = create_service(service_name='service_2')
id_1 = create_template(service_1).id
id_2 = create_template(service_1).id
id_3 = create_template(service_2).id
json_resp_1 = admin_request.get('template.get_all_templates_for_service', service_id=service_1.id)
json_resp_2 = admin_request.get('template.get_all_templates_for_service', service_id=service_2.id)
assert {template['id'] for template in json_resp_1['data']} == {str(id_1), str(id_2)}
assert {template['id'] for template in json_resp_2['data']} == {str(id_3)}
@pytest.mark.parametrize(
"subject, content, template_type", [
(
'about your ((thing))',
'hello ((name)) we’ve received your ((thing))',
EMAIL_TYPE
),
(
None,
'hello ((name)) we’ve received your ((thing))',
SMS_TYPE
),
(
'about your ((thing))',
'hello ((name)) we’ve received your ((thing))',
LETTER_TYPE
)
]
)
def test_should_get_a_single_template(
notify_db,
client,
sample_user,
service_factory,
subject,
content,
template_type
):
template = create_sample_template(
notify_db, notify_db.session, subject_line=subject, content=content, template_type=template_type
)
response = client.get(
'/service/{}/template/{}'.format(template.service.id, template.id),
headers=[create_authorization_header()]
)
data = json.loads(response.get_data(as_text=True))['data']
assert response.status_code == 200
assert data['content'] == content
assert data['subject'] == subject
assert data['process_type'] == 'normal'
assert not data['redact_personalisation']
@pytest.mark.parametrize(
"subject, content, path, expected_subject, expected_content, expected_error", [
(
'about your thing',
'hello user we’ve received your thing',
'/service/{}/template/{}/preview',
'about your thing',
'hello user we’ve received your thing',
None
),
(
'about your ((thing))',
'hello ((name)) we’ve received your ((thing))',
'/service/{}/template/{}/preview?name=Amala&thing=document',
'about your document',
'hello Amala we’ve received your document',
None
),
(
'about your ((thing))',
'hello ((name)) we’ve received your ((thing))',
'/service/{}/template/{}/preview?eman=Amala&gniht=document',
None, None,
'Missing personalisation: thing, name'
),
(
'about your ((thing))',
'hello ((name)) we’ve received your ((thing))',
'/service/{}/template/{}/preview?name=Amala&thing=document&foo=bar',
'about your document',
'hello Amala we’ve received your document',
None,
)
]
)
def test_should_preview_a_single_template(
notify_db,
client,
sample_user,
service_factory,
subject,
content,
path,
expected_subject,
expected_content,
expected_error
):
template = create_sample_template(
notify_db, notify_db.session, subject_line=subject, content=content, template_type=EMAIL_TYPE
)
response = client.get(
path.format(template.service.id, template.id),
headers=[create_authorization_header()]
)
content = json.loads(response.get_data(as_text=True))
if expected_error:
assert response.status_code == 400
assert content['message']['template'] == [expected_error]
else:
assert response.status_code == 200
assert content['content'] == expected_content
assert content['subject'] == expected_subject
def test_should_return_empty_array_if_no_templates_for_service(client, sample_service):
auth_header = create_authorization_header()
response = client.get(
'/service/{}/template'.format(sample_service.id),
headers=[auth_header]
)
assert response.status_code == 200
json_resp = json.loads(response.get_data(as_text=True))
assert len(json_resp['data']) == 0
def test_should_return_404_if_no_templates_for_service_with_id(client, sample_service, fake_uuid):
auth_header = create_authorization_header()
response = client.get(
'/service/{}/template/{}'.format(sample_service.id, fake_uuid),
headers=[auth_header]
)
assert response.status_code == 404
json_resp = json.loads(response.get_data(as_text=True))
assert json_resp['result'] == 'error'
assert json_resp['message'] == 'No result found'
def test_create_400_for_over_limit_content(client, notify_api, sample_user, sample_service, fake_uuid):
content = ''.join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(SMS_CHAR_COUNT_LIMIT + 1) # nosec
)
data = {
'name': 'too big template',
'template_type': SMS_TYPE,
'content': content,
'service': str(sample_service.id),
'created_by': str(sample_user.id)
}
data = json.dumps(data)
auth_header = create_authorization_header()
response = client.post(
'/service/{}/template'.format(sample_service.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=data
)
assert response.status_code == 400
json_resp = json.loads(response.get_data(as_text=True))
assert (
'Content has a character count greater than the limit of {}'
).format(SMS_CHAR_COUNT_LIMIT) in json_resp['message']['content']
def test_update_400_for_over_limit_content(client, notify_api, sample_user, sample_template):
json_data = json.dumps({
'content': ''.join(
random.choice(string.ascii_uppercase + string.digits) for _ in range(SMS_CHAR_COUNT_LIMIT + 1) # nosec
),
'created_by': str(sample_user.id)
})
auth_header = create_authorization_header()
resp = client.post(
'/service/{}/template/{}'.format(sample_template.service.id, sample_template.id),
headers=[('Content-Type', 'application/json'), auth_header],
data=json_data
)
assert resp.status_code == 400
json_resp = json.loads(resp.get_data(as_text=True))
assert (
'Content has a character count greater than the limit of {}'
).format(SMS_CHAR_COUNT_LIMIT) in json_resp['message']['content']
def test_should_return_all_template_versions_for_service_and_template_id(client, sample_template):
original_content = sample_template.content
from app.dao.templates_dao import dao_update_template
sample_template.content = original_content + '1'
dao_update_template(sample_template)
sample_template.content = original_content + '2'
dao_update_template(sample_template)
auth_header = create_authorization_header()
resp = client.get('/service/{}/template/{}/versions'.format(sample_template.service_id, sample_template.id),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 200
resp_json = json.loads(resp.get_data(as_text=True))['data']
assert len(resp_json) == 3
for x in resp_json:
if x['version'] == 1:
assert x['content'] == original_content
elif x['version'] == 2:
assert x['content'] == original_content + '1'
else:
assert x['content'] == original_content + '2'
def test_update_does_not_create_new_version_when_there_is_no_change(client, sample_template):
auth_header = create_authorization_header()
data = {
'template_type': sample_template.template_type,
'content': sample_template.content,
}
resp = client.post('/service/{}/template/{}'.format(sample_template.service_id, sample_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 200
template = dao_get_template_by_id(sample_template.id)
assert template.version == 1
def test_update_set_process_type_on_template(client, sample_template):
auth_header = create_authorization_header()
data = {
'process_type': 'priority'
}
resp = client.post('/service/{}/template/{}'.format(sample_template.service_id, sample_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 200
template = dao_get_template_by_id(sample_template.id)
assert template.process_type == 'priority'
def test_create_a_template_with_reply_to(admin_request, sample_user):
service = create_service(service_permissions=['letter'])
letter_contact = create_letter_contact(service, "Edinburgh, ED1 1AA")
data = {
'name': 'my template',
'subject': 'subject',
'template_type': 'letter',
'content': 'template <b>content</b>',
'service': str(service.id),
'created_by': str(sample_user.id),
'reply_to': str(letter_contact.id),
}
json_resp = admin_request.post('template.create_template', service_id=service.id, _data=data, _expected_status=201)
assert json_resp['data']['template_type'] == 'letter'
assert json_resp['data']['reply_to'] == str(letter_contact.id)
assert json_resp['data']['reply_to_text'] == letter_contact.contact_block
template = Template.query.get(json_resp['data']['id'])
from app.schemas import template_schema
assert sorted(json_resp['data']) == sorted(template_schema.dump(template).data)
th = TemplateHistory.query.filter_by(id=template.id, version=1).one()
assert th.service_letter_contact_id == letter_contact.id
def test_create_a_template_with_foreign_service_reply_to(admin_request, sample_user):
service = create_service(service_permissions=['letter'])
service2 = create_service(service_name='test service', email_from='test@example.com',
service_permissions=['letter'])
letter_contact = create_letter_contact(service2, "Edinburgh, ED1 1AA")
data = {
'name': 'my template',
'subject': 'subject',
'template_type': 'letter',
'content': 'template <b>content</b>',
'service': str(service.id),
'created_by': str(sample_user.id),
'reply_to': str(letter_contact.id),
}
json_resp = admin_request.post('template.create_template', service_id=service.id, _data=data, _expected_status=400)
assert json_resp['message'] == "letter_contact_id {} does not exist in database for service id {}".format(
str(letter_contact.id), str(service.id)
)
@pytest.mark.parametrize('template_default, service_default',
[('template address', 'service address'),
(None, 'service address'),
('template address', None),
(None, None)
])
def test_get_template_reply_to(client, sample_service, template_default, service_default):
auth_header = create_authorization_header()
if service_default:
create_letter_contact(
service=sample_service, contact_block=service_default, is_default=True
)
if template_default:
template_default_contact = create_letter_contact(
service=sample_service, contact_block=template_default, is_default=False
)
reply_to_id = str(template_default_contact.id) if template_default else None
template = create_template(service=sample_service, template_type='letter', reply_to=reply_to_id)
resp = client.get('/service/{}/template/{}'.format(template.service_id, template.id),
headers=[auth_header])
assert resp.status_code == 200, resp.get_data(as_text=True)
json_resp = json.loads(resp.get_data(as_text=True))
assert 'service_letter_contact_id' not in json_resp['data']
assert json_resp['data']['reply_to'] == reply_to_id
assert json_resp['data']['reply_to_text'] == template_default
def test_update_template_reply_to(client, sample_letter_template):
auth_header = create_authorization_header()
letter_contact = create_letter_contact(sample_letter_template.service, "Edinburgh, ED1 1AA")
data = {
'reply_to': str(letter_contact.id),
}
resp = client.post('/service/{}/template/{}'.format(sample_letter_template.service_id, sample_letter_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 200, resp.get_data(as_text=True)
template = dao_get_template_by_id(sample_letter_template.id)
assert template.service_letter_contact_id == letter_contact.id
th = TemplateHistory.query.filter_by(id=sample_letter_template.id, version=2).one()
assert th.service_letter_contact_id == letter_contact.id
def test_update_template_reply_to_set_to_blank(client, notify_db_session):
auth_header = create_authorization_header()
service = create_service(service_permissions=['letter'])
letter_contact = create_letter_contact(service, "Edinburgh, ED1 1AA")
template = create_template(service=service, template_type='letter', reply_to=letter_contact.id)
data = {
'reply_to': None,
}
resp = client.post('/service/{}/template/{}'.format(template.service_id, template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 200, resp.get_data(as_text=True)
template = dao_get_template_by_id(template.id)
assert template.service_letter_contact_id is None
th = TemplateHistory.query.filter_by(id=template.id, version=2).one()
assert th.service_letter_contact_id is None
def test_update_template_with_foreign_service_reply_to(client, sample_letter_template):
auth_header = create_authorization_header()
service2 = create_service(service_name='test service', email_from='test@example.com',
service_permissions=['letter'])
letter_contact = create_letter_contact(service2, "Edinburgh, ED1 1AA")
data = {
'reply_to': str(letter_contact.id),
}
resp = client.post('/service/{}/template/{}'.format(sample_letter_template.service_id, sample_letter_template.id),
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400, resp.get_data(as_text=True)
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['message'] == "letter_contact_id {} does not exist in database for service id {}".format(
str(letter_contact.id), str(sample_letter_template.service_id)
)
def test_update_redact_template(admin_request, sample_template):
assert sample_template.redact_personalisation is False
data = {
'redact_personalisation': True,
'created_by': str(sample_template.created_by_id)
}
dt = datetime.now()
with freeze_time(dt):
resp = admin_request.post(
'template.update_template',
service_id=sample_template.service_id,
template_id=sample_template.id,
_data=data
)
assert resp is None
assert sample_template.redact_personalisation is True
assert sample_template.template_redacted.updated_by_id == sample_template.created_by_id
assert sample_template.template_redacted.updated_at == dt
assert sample_template.version == 1
def test_update_redact_template_ignores_other_properties(admin_request, sample_template):
data = {
'name': 'Foo',
'redact_personalisation': True,
'created_by': str(sample_template.created_by_id)
}
admin_request.post(
'template.update_template',
service_id=sample_template.service_id,
template_id=sample_template.id,
_data=data
)
assert sample_template.redact_personalisation is True
assert sample_template.name != 'Foo'
def test_update_redact_template_does_nothing_if_already_redacted(admin_request, sample_template):
dt = datetime.now()
with freeze_time(dt):
dao_redact_template(sample_template, sample_template.created_by_id)
data = {
'redact_personalisation': True,
'created_by': str(sample_template.created_by_id)
}
with freeze_time(dt + timedelta(days=1)):
resp = admin_request.post(
'template.update_template',
service_id=sample_template.service_id,
template_id=sample_template.id,
_data=data
)
assert resp is None
assert sample_template.redact_personalisation is True
# make sure that it hasn't been updated
assert sample_template.template_redacted.updated_at == dt
def test_update_redact_template_400s_if_no_created_by(admin_request, sample_template):
original_updated_time = sample_template.template_redacted.updated_at
resp = admin_request.post(
'template.update_template',
service_id=sample_template.service_id,
template_id=sample_template.id,
_data={'redact_personalisation': True},
_expected_status=400
)
assert resp == {
'result': 'error',
'message': {'created_by': ['Field is required']}
}
assert sample_template.redact_personalisation is False
assert sample_template.template_redacted.updated_at == original_updated_time
def test_preview_letter_template_by_id_invalid_file_type(
sample_letter_notification,
admin_request):
resp = admin_request.get(
'template.preview_letter_template_by_notification_id',
service_id=sample_letter_notification.service_id,
template_id=sample_letter_notification.template_id,
notification_id=sample_letter_notification.id,
file_type='doc',
_expected_status=400
)
assert ['file_type must be pdf or png'] == resp['message']['content']
@freeze_time('2012-12-12')
@pytest.mark.parametrize('file_type', ('png', 'pdf'))
def test_preview_letter_template_by_id_valid_file_type(
notify_api,
sample_letter_notification,
admin_request,