-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_template_types.py
More file actions
2345 lines (2154 loc) · 74 KB
/
Copy pathtest_template_types.py
File metadata and controls
2345 lines (2154 loc) · 74 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 datetime
import os
from functools import partial
from time import process_time
from unittest import mock
import pytest
from bs4 import BeautifulSoup
from flask import Markup
from freezegun import freeze_time
from notifications_utils.formatters import unlink_govuk_escaped
from notifications_utils.template import (
EmailPreviewTemplate,
HTMLEmailTemplate,
LetterImageTemplate,
LetterPreviewTemplate,
LetterPrintTemplate,
PlainTextEmailTemplate,
SMSMessageTemplate,
SMSPreviewTemplate,
Template,
WithSubjectTemplate,
_render_conditional_email_markdown,
)
def test_pass_through_renderer():
message = """
the
quick brown
fox
"""
assert str(Template({"content": message})) == message
def test_html_email_inserts_body():
assert "the <em>quick</em> brown fox" in str(
HTMLEmailTemplate({"content": "the <em>quick</em> brown fox", "subject": ""})
)
@pytest.mark.parametrize("content", ("DOCTYPE", "html", "body", "hello world"))
def test_default_template(content):
assert content in str(HTMLEmailTemplate({"content": "hello world", "subject": ""}))
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
@pytest.mark.parametrize("show_banner", (True, False))
def test_fip_banner_english(renderer, show_banner):
email = renderer({"content": "hello world", "subject": ""})
email.fip_banner_english = show_banner
if show_banner:
assert "gc-logo-en.png" in str(email)
assert "canada-logo.png" in str(email)
else:
assert "gc-logo-en.png" not in str(email)
assert "canada-logo.png" not in str(email)
@pytest.mark.parametrize("lang", ["en", "fr"])
@pytest.mark.parametrize("asset_domain", [None, "assets.example.com"])
def test_custom_asset_domain(lang, asset_domain):
expected_domain = asset_domain or "assets.notification.canada.ca"
email = EmailPreviewTemplate(
{
"content": "hello world",
"subject": "",
},
fip_banner_english=lang == "en",
fip_banner_french=lang == "fr",
asset_domain=asset_domain,
)
assert f"https://{expected_domain}/gc-logo-{lang}.png" in str(email)
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
@pytest.mark.parametrize("show_banner", (True, False))
def test_fip_banner_french(renderer, show_banner):
email = renderer({"content": "hello world", "subject": ""})
email.fip_banner_english = False
email.fip_banner_french = show_banner
if show_banner:
assert "gc-logo-fr.png" in str(email)
assert "canada-logo.png" in str(email)
else:
assert "gc-logo-fr.png" not in str(email)
assert "canada-logo.png" not in str(email)
def test_html_email_lang_defaults_to_english():
rendered = str(HTMLEmailTemplate({"content": "hello world", "subject": ""}))
assert '<html lang="en">' in rendered
@pytest.mark.parametrize(
"lang,expected",
[
("fr", '<html lang="fr">'),
("fr-CA", '<html lang="fr-CA">'),
("en-CA", '<html lang="en-CA">'),
("und", '<html lang="und">'),
],
)
def test_html_email_lang_is_threaded_into_html_tag(lang, expected):
rendered = str(HTMLEmailTemplate({"content": "hello world", "subject": ""}, lang=lang))
assert expected in rendered
def test_html_email_lang_falls_back_to_english_when_none():
rendered = str(HTMLEmailTemplate({"content": "hello world", "subject": ""}, lang=None))
assert '<html lang="en">' in rendered
def test_html_email_bilingual_blocks_get_inline_lang_attributes():
# add_language_divs already wraps [[en]]/[[fr]] blocks with lang attributes;
# for bilingual content the document-level lang should be set by the caller
# (e.g. to "und") so screen readers pick up the lang on the inline divs.
bilingual_content = "[[en]]\nHello\n[[/en]]\n[[fr]]\nBonjour\n[[/fr]]"
rendered = str(HTMLEmailTemplate({"content": bilingual_content, "subject": ""}, lang="und"))
assert '<html lang="und">' in rendered
assert '<div lang="en-ca">' in rendered
assert '<div lang="fr-ca">' in rendered
def test_logo_with_background_colour_shows():
email = str(
HTMLEmailTemplate(
{"content": "hello world", "subject": ""},
logo_with_background_colour=True,
fip_banner_english=False,
brand_colour="#eee",
)
)
assert "gc-logo-en.png" not in email
assert 'bgcolor="#eee"' in email
assert "background: linear-gradient(#eee, #eee);" in email
@pytest.mark.parametrize(
"brand_logo, brand_text, brand_colour",
[
("http://example.com/image.png", "Example", "red"),
("http://example.com/image.png", "Example", "#f00"),
("http://example.com/image.png", "Example", None),
("http://example.com/image.png", "", "#f00"),
(None, "Example", "#f00"),
],
)
def test_brand_data_shows(brand_logo, brand_text, brand_colour):
email = str(
HTMLEmailTemplate(
{"content": "hello world", "subject": ""},
logo_with_background_colour=True,
fip_banner_english=False,
brand_logo=brand_logo,
brand_text=brand_text,
brand_colour=brand_colour,
)
)
assert "Government of Canada" not in email
if brand_logo:
assert brand_logo in email
if brand_text:
assert brand_text in email
if brand_colour:
assert 'bgcolor="{}"'.format(brand_colour) in email
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
def test_alt_text_with_brand_text_and_fip_banner_english_shown(renderer):
email = str(
renderer(
{"content": "hello world", "subject": ""},
fip_banner_english=True,
brand_logo="http://example.com/image.png",
brand_text="Example",
logo_with_background_colour=True,
brand_name="Notify Logo",
alt_text_en="alt_text_en",
alt_text_fr="alt_text_fr",
)
)
assert 'alt="alt_text_en / alt_text_fr"' in email
assert 'alt="Notify Logo"' not in email
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
def test_alt_text_with_no_brand_text_and_fip_banner_english_shown(renderer):
email = str(
renderer(
{"content": "hello world", "subject": ""},
fip_banner_english=True,
brand_logo="http://example.com/image.png",
brand_text=None,
logo_with_background_colour=True,
brand_name="Notify Logo",
alt_text_en="alt_text_en",
alt_text_fr="alt_text_fr",
)
)
assert 'alt="Symbol of the Government of Canada / Symbole du gouvernement du Canada"' in email
assert 'alt="alt_text_en / alt_text_fr"' in email
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
def test_alt_text_with_no_brand_text_and_fip_banner_french_shown(renderer):
email = str(
renderer(
{"content": "hello world", "subject": ""},
fip_banner_english=False,
fip_banner_french=True,
brand_logo="http://example.com/image.png",
brand_text=None,
logo_with_background_colour=True,
brand_name="Notify Logo",
)
)
assert 'alt="Symbol of the Government of Canada / Symbole du gouvernement du Canada"' in email
assert 'alt="Notify Logo"' in email
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
@pytest.mark.parametrize(
"logo_with_background_colour, brand_text, alt_text_en, alt_text_fr, expected_alt_text",
[
(True, None, None, None, 'alt="Notify Logo"'),
(True, "Example", "alt_text_en", "alt_text_fr", 'alt="alt_text_en / alt_text_fr"'),
(False, "Example", None, None, 'alt="Notify Logo"'),
(False, None, "alt_text_en", "alt_text_fr", 'alt="alt_text_en / alt_text_fr"'),
],
)
def test_alt_text_with_no_fip_banner(
logo_with_background_colour, brand_text, alt_text_en, alt_text_fr, expected_alt_text, renderer
):
email = str(
renderer(
{"content": "hello world", "subject": ""},
fip_banner_english=False,
brand_logo="http://example.com/image.png",
brand_text=brand_text,
logo_with_background_colour=logo_with_background_colour,
brand_name="Notify Logo",
alt_text_en=alt_text_en,
alt_text_fr=alt_text_fr,
)
)
assert expected_alt_text in email
@pytest.mark.parametrize("renderer", [HTMLEmailTemplate, EmailPreviewTemplate])
@pytest.mark.parametrize(
"content, allow_html, expected",
[
("Hello World", True, "Hello World"),
("Hello World", False, "Hello World"),
("<div>Hello World</div>", True, "<div>Hello World</div>"),
("<div>Hello World</div>", False, "<div>Hello World</div>"),
],
)
def test_allow_html_works(content: str, allow_html: bool, expected: str, renderer):
email = str(
renderer(
{"content": content, "subject": ""},
fip_banner_english=True,
allow_html=allow_html,
)
)
assert expected in email
@pytest.mark.parametrize("complete_html", (True, False))
@pytest.mark.parametrize(
"branding_should_be_present, brand_logo, brand_text, brand_colour",
[
(True, "http://example.com/image.png", "Example", "#f00"),
(True, "http://example.com/image.png", "Example", None),
(True, "http://example.com/image.png", "", None),
(False, None, "Example", "#f00"),
(False, "http://example.com/image.png", None, "#f00"),
],
)
@pytest.mark.parametrize("content", ("DOCTYPE", "html", "body"))
def test_complete_html(complete_html, branding_should_be_present, brand_logo, brand_text, brand_colour, content):
email = str(
HTMLEmailTemplate(
{"content": "hello world", "subject": ""},
complete_html=complete_html,
brand_logo=brand_logo,
brand_text=brand_text,
brand_colour=brand_colour,
)
)
if complete_html:
assert content in email
else:
assert content not in email
if branding_should_be_present:
assert brand_logo in email
assert brand_text in email
if brand_colour:
assert brand_colour in email
assert "##" not in email
def test_subject_is_page_title():
email = BeautifulSoup(
str(
HTMLEmailTemplate(
{"content": "", "subject": "this is the subject", "template_type": "email"},
)
),
features="html.parser",
)
assert email.select_one("title").text == "this is the subject" # type: ignore
def test_preheader_is_at_start_of_html_emails():
assert (
'<body style="font-family: Helvetica, Arial, sans-serif;font-size: 16px;margin: 0;color:#0b0c0c;">\n'
"\n"
'<span style="display: none;font-size: 1px;color: #fff; max-height: 0;">content…</span>'
) in str(HTMLEmailTemplate({"content": "content", "subject": "subject"}))
@pytest.mark.parametrize(
"content, values, expected_preheader",
[
(
(
"Hello (( name ))\n"
"\n"
'# This - is a "heading"\n'
"\n"
"My favourite websites' URLs are:\n"
"- GOV.UK\n"
"- https://www.example.com\n"
),
{"name": "Jo"},
"Hello Jo This – is a “heading” My favourite websites’ URLs are: • GOV.UK • https://www.example.com",
),
(
("[Markdown link](https://www.example.com)\n"),
{},
"Markdown link",
),
(
"""
Lorem Ipsum is simply dummy text of the printing and
typesetting industry.
Lorem Ipsum has been the industry’s standard dummy text
ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book.
Lorem Ipsum is simply dummy text of the printing and
typesetting industry.
Lorem Ipsum has been the industry’s standard dummy text
ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book.
""",
{},
(
"Lorem Ipsum is simply dummy text of the printing and "
"typesetting industry. Lorem Ipsum has been the industry’s "
"standard dummy text ever since the 1500s, when an unknown "
"printer took a galley of type and scrambled it to make a "
"type specimen book. Lorem Ipsu"
),
),
(
"short email",
{},
"short email",
),
],
)
@mock.patch("notifications_utils.template.HTMLEmailTemplate.jinja_template.render", return_value="mocked")
def test_content_of_preheader_in_html_emails(
mock_jinja_template,
content,
values,
expected_preheader,
):
assert str(HTMLEmailTemplate({"content": content, "subject": "subject"}, values)) == "mocked"
assert mock_jinja_template.call_args[0][0]["preheader"] == expected_preheader
@pytest.mark.parametrize(
"allow_html, content, expected_preheader",
[
(True, "Hello World", "Hello World"),
(False, "Hello World", "Hello World"),
(True, "<div>Hello World</div>", "Hello World"),
(False, "<div>Hello World</div>", "<div>Hello World</div>"),
(True, '<div><img src="file.png" />Hello World</div>', "Hello World"),
(False, '<div><img src="file.png" />Hello World</div>', "<div><img src=”file.png” />Hello World</div>"),
],
)
@mock.patch("notifications_utils.template.HTMLEmailTemplate.jinja_template.render", return_value="mocked")
def test_content_of_preheader_in_html_emails_with_allow_html(
mock_jinja_template,
allow_html: bool,
content: str,
expected_preheader: str,
):
assert str(HTMLEmailTemplate({"content": content, "subject": "subject"}, allow_html=allow_html)) == "mocked"
assert mock_jinja_template.call_args[0][0]["preheader"] == expected_preheader
@pytest.mark.parametrize(
"template_class, extra_args, result, markdown_renderer",
[
[
HTMLEmailTemplate,
{},
("the quick brown fox\n" "\n" "jumped over the lazy dog\n"),
"notifications_utils.template.notify_email_markdown",
],
[
LetterPreviewTemplate,
{},
("the quick brown fox\n" "\n" "jumped over the lazy dog\n"),
"notifications_utils.template.notify_letter_preview_markdown",
],
],
)
def test_markdown_in_templates(
template_class,
extra_args,
result,
markdown_renderer,
):
with mock.patch(markdown_renderer, return_value="") as mock_markdown_renderer:
str(
template_class(
{"content": ("the quick ((colour)) ((animal))\n" "\n" "jumped over the lazy dog"), "subject": "animal story"},
{"animal": "fox", "colour": "brown"},
**extra_args,
)
)
mock_markdown_renderer.assert_called_once_with(result)
@pytest.mark.parametrize(
"template_class",
[
HTMLEmailTemplate,
EmailPreviewTemplate,
SMSPreviewTemplate,
],
)
@pytest.mark.parametrize(
"url, url_with_entities_replaced",
[
("http://example.com", "http://example.com"),
("http://www.gov.uk/", "http://www.gov.uk/"),
("https://www.gov.uk/", "https://www.gov.uk/"),
("http://service.gov.uk", "http://service.gov.uk"),
(
"http://service.gov.uk/blah.ext?q=a%20b%20c&order=desc#fragment",
"http://service.gov.uk/blah.ext?q=a%20b%20c&order=desc#fragment",
),
pytest.param("example.com", "example.com", marks=pytest.mark.xfail),
pytest.param("www.example.com", "www.example.com", marks=pytest.mark.xfail),
pytest.param(
"http://service.gov.uk/blah.ext?q=one two three",
"http://service.gov.uk/blah.ext?q=one two three",
marks=pytest.mark.xfail,
),
pytest.param("ftp://example.com", "ftp://example.com", marks=pytest.mark.xfail),
pytest.param("mailto:test@example.com", "mailto:test@example.com", marks=pytest.mark.xfail),
],
)
def test_makes_links_out_of_URLs(template_class, url, url_with_entities_replaced):
assert '<a style="word-wrap: break-word; word-break: break-word;" href="{}">{}</a>'.format(
url_with_entities_replaced, url_with_entities_replaced
) in str(template_class({"content": url, "subject": ""}))
@pytest.mark.parametrize(
"content, html_snippet",
(
(
(
"You’ve been invited to a service. Click this link:\n"
"https://service.example.com/accept_invite/a1b2c3d4\n"
"\n"
"Thanks\n"
),
(
'<a style="word-wrap: break-word; word-break: break-word;"'
' href="https://service.example.com/accept_invite/a1b2c3d4">'
"https://service.example.com/accept_invite/a1b2c3d4"
"</a>"
),
),
(
("https://service.example.com/accept_invite/?a=b&c=d&"),
(
'<a style="word-wrap: break-word; word-break: break-word;"'
' href="https://service.example.com/accept_invite/?a=b&c=d&">'
"https://service.example.com/accept_invite/?a=b&c=d&"
"</a>"
),
),
),
)
def test_HTML_template_has_URLs_replaced_with_links(content, html_snippet):
assert html_snippet in str(HTMLEmailTemplate({"content": content, "subject": ""}))
@pytest.mark.parametrize(
"template_content,expected",
[
("gov.uk", "gov.\u200buk"),
("GOV.UK", "GOV.\u200bUK"),
("Gov.uk", "Gov.\u200buk"),
("https://gov.uk", "https://gov.uk"),
("https://www.gov.uk", "https://www.gov.uk"),
("www.gov.uk", "www.gov.uk"),
("gov.uk/register-to-vote", "gov.uk/register-to-vote"),
("gov.uk?q=", "gov.uk?q="),
],
)
def test_escaping_govuk_in_email_templates(template_content, expected):
assert unlink_govuk_escaped(template_content) == expected
assert expected in str(PlainTextEmailTemplate({"content": template_content, "subject": ""}))
assert expected in str(HTMLEmailTemplate({"content": template_content, "subject": ""}))
def test_stripping_of_unsupported_characters_in_email_templates():
template_content = "line one\u2028line two"
expected = "line oneline two"
assert expected in str(PlainTextEmailTemplate({"content": template_content, "subject": ""}))
assert expected in str(HTMLEmailTemplate({"content": template_content, "subject": ""}))
@mock.patch("notifications_utils.template.add_prefix", return_value="")
@pytest.mark.parametrize(
"template_class, prefix, body, expected_call",
[
(SMSMessageTemplate, "a", "b", (Markup("b"), "a")),
(SMSPreviewTemplate, "a", "b", (Markup("b"), "a")),
(SMSMessageTemplate, None, "b", (Markup("b"), None)),
(SMSPreviewTemplate, None, "b", (Markup("b"), None)),
(SMSMessageTemplate, "<em>ht&ml</em>", "b", (Markup("b"), "<em>ht&ml</em>")),
(SMSPreviewTemplate, "<em>ht&ml</em>", "b", (Markup("b"), "<em>ht&ml</em>")),
],
)
def test_sms_message_adds_prefix(add_prefix, template_class, prefix, body, expected_call):
template = template_class({"content": body})
template.prefix = prefix
template.sender = None
str(template)
add_prefix.assert_called_once_with(*expected_call)
@mock.patch("notifications_utils.template.add_prefix", return_value="")
@pytest.mark.parametrize("template_class", [SMSMessageTemplate, SMSPreviewTemplate])
@pytest.mark.parametrize(
"show_prefix, prefix, body, sender, expected_call",
[
(False, "a", "b", "c", (Markup("b"), None)),
(True, "a", "b", None, (Markup("b"), "a")),
(True, "a", "b", False, (Markup("b"), "a")),
],
)
def test_sms_message_adds_prefix_only_if_asked_to(
add_prefix,
show_prefix,
prefix,
body,
sender,
expected_call,
template_class,
):
template = template_class(
{"content": body},
prefix=prefix,
show_prefix=show_prefix,
sender=sender,
)
str(template)
add_prefix.assert_called_once_with(*expected_call)
@pytest.mark.parametrize("content_to_look_for", ["GOVUK", "sms-message-sender"])
@pytest.mark.parametrize(
"show_sender",
[
True,
pytest.param(False, marks=pytest.mark.xfail),
],
)
def test_sms_message_preview_shows_sender(
show_sender,
content_to_look_for,
):
assert content_to_look_for in str(
SMSPreviewTemplate(
{"content": "foo"},
sender="GOVUK",
show_sender=show_sender,
)
)
def test_sms_message_preview_hides_sender_by_default():
assert SMSPreviewTemplate({"content": "foo"}).show_sender is False
@mock.patch("notifications_utils.template.sms_encode", return_value="downgraded")
@pytest.mark.parametrize("template_class", [SMSMessageTemplate, SMSPreviewTemplate])
def test_sms_messages_downgrade_non_sms(mock_sms_encode, template_class):
template = str(template_class({"content": "Message"}, prefix="Service name"))
assert "downgraded" in str(template)
mock_sms_encode.assert_called_once_with("Service name: Message")
@mock.patch("notifications_utils.template.sms_encode", return_value="downgraded")
def test_sms_messages_dont_downgrade_non_sms_if_setting_is_false(mock_sms_encode):
template = str(
SMSPreviewTemplate(
{"content": "😎"},
prefix="👉",
downgrade_non_sms_characters=False,
)
)
assert "👉: 😎" in str(template)
assert mock_sms_encode.called is False
@mock.patch("notifications_utils.template.nl2br")
def test_sms_preview_adds_newlines(nl2br):
content = "the\nquick\n\nbrown fox"
str(SMSPreviewTemplate({"content": content}))
nl2br.assert_called_once_with(content)
@pytest.mark.parametrize(
"content",
[
("one newline\n" "two newlines\n" "\n" "end"), # Unix-style
("one newline\r\n" "two newlines\r\n" "\r\n" "end"), # Windows-style
("one newline\r" "two newlines\r" "\r" "end"), # Mac Classic style
("\t\t\n\r one newline\xa0\n" "two newlines\r" "\r\n" "end\n\n \r \n \t "), # A mess
],
)
def test_sms_message_normalises_newlines(content):
assert repr(str(SMSMessageTemplate({"content": content}))) == repr("one newline\n" "two newlines\n" "\n" "end")
@pytest.mark.skip(reason="not in use")
@freeze_time("2012-12-12 12:12:12")
@mock.patch("notifications_utils.template.LetterPreviewTemplate.jinja_template.render")
@mock.patch("notifications_utils.template.remove_empty_lines", return_value="123 Street")
@mock.patch("notifications_utils.template.unlink_govuk_escaped")
@mock.patch("notifications_utils.template.notify_letter_preview_markdown", return_value="Bar")
@mock.patch("notifications_utils.template.strip_pipes", side_effect=lambda x: x)
@pytest.mark.parametrize(
"values, expected_address",
[
(
{},
Markup(
"<span class='placeholder-no-brackets'>[address line 1]</span>\n"
"<span class='placeholder-no-brackets'>[address line 2]</span>\n"
"<span class='placeholder-no-brackets'>[address line 3]</span>\n"
"<span class='placeholder-no-brackets'>[address line 4]</span>\n"
"<span class='placeholder-no-brackets'>[address line 5]</span>\n"
"<span class='placeholder-no-brackets'>[address line 6]</span>\n"
"<span class='placeholder-no-brackets'>[postcode]</span>"
),
),
(
{
"address line 1": "123 Fake Street",
"address line 6": "United Kingdom",
},
Markup(
"123 Fake Street\n"
"<span class='placeholder-no-brackets'>[address line 2]</span>\n"
"<span class='placeholder-no-brackets'>[address line 3]</span>\n"
"<span class='placeholder-no-brackets'>[address line 4]</span>\n"
"<span class='placeholder-no-brackets'>[address line 5]</span>\n"
"United Kingdom\n"
"<span class='placeholder-no-brackets'>[postcode]</span>"
),
),
(
{
"address line 1": "123 Fake Street",
"address line 2": "City of Town",
"postcode": "SW1A 1AA",
},
Markup("123 Fake Street\n" "City of Town\n" "\n" "\n" "\n" "\n" "SW1A 1AA"),
),
],
)
@pytest.mark.parametrize(
"contact_block, expected_rendered_contact_block",
[
(None, ""),
("", ""),
(
"""
The Pension Service
Mail Handling Site A
Wolverhampton WV9 1LU
Telephone: 0845 300 0168
Email: fpc.customercare@dwp.gsi.gov.uk
Monday - Friday 8am - 6pm
www.gov.uk
""",
(
"The Pension Service<br>"
"Mail Handling Site A<br>"
"Wolverhampton WV9 1LU<br>"
"<br>"
"Telephone: 0845 300 0168<br>"
"Email: fpc.customercare@dwp.gsi.gov.uk<br>"
"Monday - Friday 8am - 6pm<br>"
"www.gov.uk"
),
),
],
)
@pytest.mark.parametrize(
"extra_args, expected_logo_file_name, expected_logo_class",
[
({}, None, None),
({"logo_file_name": "example.foo"}, "example.foo", "foo"),
],
)
@pytest.mark.parametrize(
"additional_extra_args, expected_date",
[
({}, "12 December 2012"),
({"date": None}, "12 December 2012"),
({"date": datetime.date.fromtimestamp(0)}, "1 January 1970"),
],
)
def test_letter_preview_renderer(
strip_pipes,
letter_markdown,
unlink_govuk,
remove_empty_lines,
jinja_template,
values,
expected_address,
contact_block,
expected_rendered_contact_block,
extra_args,
expected_logo_file_name,
expected_logo_class,
additional_extra_args,
expected_date,
):
extra_args.update(additional_extra_args)
str(LetterPreviewTemplate({"content": "Foo", "subject": "Subject"}, values, contact_block=contact_block, **extra_args))
remove_empty_lines.assert_called_once_with(expected_address)
jinja_template.assert_called_once_with(
{
"address": "<ul><li>123 Street</li></ul>",
"subject": "Subject",
"message": "Bar",
"date": expected_date,
"contact_block": expected_rendered_contact_block,
"admin_base_url": "http://localhost:6012",
"logo_file_name": expected_logo_file_name,
"logo_class": expected_logo_class,
}
)
letter_markdown.assert_called_once_with(Markup("Foo\n"))
unlink_govuk.assert_not_called()
assert strip_pipes.call_args_list == [
mock.call("Subject"),
mock.call("Foo"),
mock.call(expected_address),
mock.call(expected_rendered_contact_block),
]
@freeze_time("2001-01-01 12:00:00.000000")
@mock.patch("notifications_utils.template.LetterPreviewTemplate.jinja_template.render")
def test_letter_preview_renderer_without_mocks(jinja_template):
str(
LetterPreviewTemplate(
{"content": "Foo", "subject": "Subject"},
{"addressline1": "name", "addressline2": "street", "postcode": "SW1 1AA"},
contact_block="",
)
)
jinja_template_locals = jinja_template.call_args_list[0][0][0]
assert jinja_template_locals["address"] == ("<ul>" "<li>name</li>" "<li>street</li>" "<li>SW1 1AA</li>" "</ul>")
assert jinja_template_locals["subject"] == "Subject"
assert jinja_template_locals["message"] == "<p>Foo</p>"
assert jinja_template_locals["date"] == "1 January 2001"
assert jinja_template_locals["contact_block"] == ""
assert jinja_template_locals["admin_base_url"] == "http://localhost:6012"
assert jinja_template_locals["logo_file_name"] is None
@freeze_time("2012-12-12 12:12:12")
@mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render")
@pytest.mark.parametrize(
"page_count, expected_oversized, expected_page_numbers",
[
(
1,
False,
[1],
),
(
5,
False,
[1, 2, 3, 4, 5],
),
(
10,
False,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
),
(
11,
True,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
),
(
99,
True,
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
),
],
)
@pytest.mark.parametrize(
"postage_args, expected_postage",
(
pytest.param({}, "second"),
pytest.param({"postage": "first"}, "first"),
pytest.param({"postage": "second"}, "second"),
pytest.param({"postage": "third"}, "third", marks=pytest.mark.xfail(raises=TypeError)),
),
)
def test_letter_image_renderer(
jinja_template,
page_count,
expected_page_numbers,
expected_oversized,
postage_args,
expected_postage,
):
str(
LetterImageTemplate(
{"content": "Content", "subject": "Subject"},
image_url="http://example.com/endpoint.png",
page_count=page_count,
contact_block="10 Downing Street",
**postage_args,
)
)
jinja_template.assert_called_once_with(
{
"image_url": "http://example.com/endpoint.png",
"page_numbers": expected_page_numbers,
"too_many_pages": expected_oversized,
"address": (
"<ul>"
"<li><span class='placeholder-no-brackets'>[address line 1]</span></li>"
"<li><span class='placeholder-no-brackets'>[address line 2]</span></li>"
"<li><span class='placeholder-no-brackets'>[address line 3]</span></li>"
"<li><span class='placeholder-no-brackets'>[address line 4]</span></li>"
"<li><span class='placeholder-no-brackets'>[address line 5]</span></li>"
"<li><span class='placeholder-no-brackets'>[address line 6]</span></li>"
"<li><span class='placeholder-no-brackets'>[postcode]</span></li>"
"</ul>"
),
"contact_block": "10 Downing Street",
"date": "12 December 2012",
"subject": "Subject",
"message": "<p>Content</p>",
"postage": expected_postage,
}
)
@pytest.mark.parametrize(
"page_image_url",
[
pytest.param("http://example.com/endpoint.png?page=0", marks=pytest.mark.xfail),
"http://example.com/endpoint.png?page=1",
"http://example.com/endpoint.png?page=2",
"http://example.com/endpoint.png?page=3",
pytest.param("http://example.com/endpoint.png?page=4", marks=pytest.mark.xfail),
],
)
def test_letter_image_renderer_pagination(page_image_url):
assert page_image_url in str(
LetterImageTemplate(
{"content": "", "subject": ""},
image_url="http://example.com/endpoint.png",
page_count=3,
)
)
@pytest.mark.parametrize(
"partial_call, expected_exception",
[
(partial(LetterImageTemplate), TypeError),
(partial(LetterImageTemplate, page_count=1), TypeError),
(partial(LetterImageTemplate, image_url="foo"), TypeError),
(partial(LetterImageTemplate, image_url="foo", page_count="foo"), ValueError),
],
)
def test_letter_image_renderer_requires_arguments(partial_call, expected_exception):
with pytest.raises(expected_exception):
partial_call({"content": "", "subject": ""})
def test_sets_subject():
assert WithSubjectTemplate({"content": "", "subject": "Your tax is due"}).subject == "Your tax is due"
def test_subject_line_gets_applied_to_correct_template_types():
for cls in [
EmailPreviewTemplate,
HTMLEmailTemplate,
PlainTextEmailTemplate,
LetterPreviewTemplate,
LetterImageTemplate,
]:
assert issubclass(cls, WithSubjectTemplate)
for cls in [
SMSMessageTemplate,
SMSPreviewTemplate,
]:
assert not issubclass(cls, WithSubjectTemplate)
def test_subject_line_gets_replaced():
template = WithSubjectTemplate({"content": "", "subject": "((name))"})
assert template.subject == Markup("<mark class='placeholder'>((name))</mark>")
template.values = {"name": "Jo"}
assert template.subject == "Jo"
@pytest.mark.parametrize(
"template_class, extra_args, expected_field_calls",
[
(
Template,
{},
[
mock.call("content", {}, html="escape", redact_missing_personalisation=False),
],
),
(
WithSubjectTemplate,
{},
[
mock.call("content", {}, html="passthrough", redact_missing_personalisation=False, markdown_lists=True),
],
),
(PlainTextEmailTemplate, {}, [mock.call("content", {}, html="passthrough", markdown_lists=True)]),
(
HTMLEmailTemplate,
{},
[
mock.call("subject", {}, html="escape", redact_missing_personalisation=False),
mock.call(
"content",
{},
html="escape",
markdown_lists=True,
redact_missing_personalisation=False,
markdown_renderer=_render_conditional_email_markdown,
),
mock.call("content", {}, html="escape", markdown_lists=True),
],
),
(
EmailPreviewTemplate,
{},
[
mock.call(
"content",
{},
html="escape",