-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_formatters.py
More file actions
1349 lines (1205 loc) · 48.7 KB
/
Copy pathtest_formatters.py
File metadata and controls
1349 lines (1205 loc) · 48.7 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 pytest
from flask import Markup
from notifications_utils.formatters import (
EMAIL_P_CLOSE_TAG,
EMAIL_P_OPEN_TAG,
add_language_divs,
add_trailing_newline,
escape_html,
escape_lang_tags,
formatted_list,
make_quotes_smart,
nl2li,
normalise_whitespace,
notify_email_markdown,
notify_letter_preview_markdown,
notify_plain_text_email_markdown,
remove_language_divs,
remove_nested_list_padding,
remove_smart_quotes_from_email_addresses,
remove_whitespace_before_punctuation,
replace_hyphens_with_en_dashes,
sms_encode,
strip_and_remove_obscure_whitespace,
strip_dvla_markup,
strip_pipes,
strip_unsupported_characters,
strip_whitespace,
tweak_dvla_list_markup,
unlink_govuk_escaped,
)
from notifications_utils.take import Take
from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate, SMSPreviewTemplate
@pytest.mark.parametrize(
"url",
[
"http://example.com",
"http://www.gov.uk/",
"https://www.gov.uk/",
"http://service.gov.uk",
"http://service.gov.uk/blah.ext?q=a%20b%20c&order=desc#fragment",
pytest.param("http://service.gov.uk/blah.ext?q=one two three", marks=pytest.mark.xfail),
],
)
def test_makes_links_out_of_URLs(url):
link = '<a style="word-wrap: break-word; word-break: break-word;" href="{}">{}</a>'.format(url, url)
assert notify_email_markdown(url) == (
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">' "{}" "</p>"
).format(link)
@pytest.mark.parametrize(
"input, output",
[
(
("this is some text with a link http://example.com in the middle"),
(
"this is some text with a link "
'<a style="word-wrap: break-word; word-break: break-word;" href="http://example.com">http://example.com</a>'
" in the middle"
),
),
(
("this link is in brackets (http://example.com)"),
(
"this link is in brackets "
'(<a style="word-wrap: break-word; word-break: break-word;" href="http://example.com">http://example.com</a>)'
),
),
],
)
def test_makes_links_out_of_URLs_in_context(input, output):
assert notify_email_markdown(input) == (
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">' "{}" "</p>"
).format(output)
@pytest.mark.parametrize(
"url",
[
"example.com",
"www.example.com",
"ftp://example.com",
"test@example.com",
"mailto:test@example.com",
'<a href="https://example.com">Example</a>',
],
)
def test_doesnt_make_links_out_of_invalid_urls(url):
assert notify_email_markdown(url) == (
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">' "{}" "</p>"
).format(url)
def test_handles_placeholders_in_urls():
assert notify_email_markdown("http://example.com/?token=<span class='placeholder'>((token))</span>&key=1") == (
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
'<a style="word-wrap: break-word; word-break: break-word;" href="http://example.com/?token=">'
"http://example.com/?token="
"</a>"
"<span class='placeholder'>((token))</span>&key=1"
"</p>"
)
@pytest.mark.parametrize(
"url, expected_html, expected_html_in_template",
[
(
"""https://example.com"onclick="alert('hi')""",
"""<a style="word-wrap: break-word; word-break: break-word;" href="https://example.com%22onclick=%22alert%28%27hi">https://example.com"onclick="alert('hi</a>')""", # noqa
"""<a style="word-wrap: break-word; word-break: break-word;" href="https://example.com%22onclick=%22alert%28%27hi">https://example.com"onclick="alert('hi</a>‘)""", # noqa
),
(
"""https://example.com"style='text-decoration:blink'""",
"""<a style="word-wrap: break-word; word-break: break-word;" href="https://example.com%22style=%27text-decoration:blink">https://example.com"style='text-decoration:blink</a>'""", # noqa
"""<a style="word-wrap: break-word; word-break: break-word;" href="https://example.com%22style=%27text-decoration:blink">https://example.com"style='text-decoration:blink</a>’""", # noqa
),
],
)
def test_URLs_get_escaped(url, expected_html, expected_html_in_template):
assert notify_email_markdown(url) == (
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">' "{}" "</p>"
).format(expected_html)
assert expected_html_in_template in str(HTMLEmailTemplate({"content": url, "subject": ""}))
def test_HTML_template_has_URLs_replaced_with_links():
assert (
'<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>"
) in str(
HTMLEmailTemplate(
{
"content": (
"You’ve been invited to a service. Click this link:\n"
"https://service.example.com/accept_invite/a1b2c3d4\n"
"\n"
"Thanks\n"
),
"subject": "",
}
)
)
@pytest.mark.parametrize(
"markdown_function, expected_output",
[
(
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
'<a style="word-wrap: break-word; word-break: break-word;" href="https://example.com">'
"https://example.com"
"</a>"
"</p>"
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"Next paragraph"
"</p>"
),
),
(notify_plain_text_email_markdown, ("\n" "\nhttps://example.com" "\n" "\nNext paragraph")),
],
)
def test_preserves_whitespace_when_making_links(markdown_function, expected_output):
assert markdown_function("https://example.com\n" "\n" "Next paragraph") == expected_output
@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": ""}))
@pytest.mark.parametrize(
"subject,expected",
[
("bonjour | hi", "bonjour | hi"),
("bonjour .", "bonjour."),
("double -- dash", "double \u2013 dash"),
],
)
def test_subject_is_cleaned_up(subject, expected):
assert expected == HTMLEmailTemplate({"content": "", "subject": subject}).subject
@pytest.mark.parametrize(
"prefix, body, expected",
[
("a", "b", "a: b"),
(None, "b", "b"),
],
)
def test_sms_message_adds_prefix(prefix, body, expected):
template = SMSMessageTemplate({"content": body})
template.prefix = prefix
template.sender = None
assert str(template) == expected
def test_sms_preview_adds_newlines():
template = SMSPreviewTemplate(
{
"content": """
the
quick
brown fox
"""
}
)
template.prefix = None
template.sender = None
assert "<br>" in str(template)
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, 'print("hello")'],
[notify_email_markdown, 'print("hello")'],
[notify_plain_text_email_markdown, 'print("hello")'],
),
)
def test_block_code(markdown_function, expected):
assert markdown_function('```\nprint("hello")\n```') == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<p>inset text</p>")],
[
notify_email_markdown,
(
"<blockquote "
'style="Margin: 0 0 20px 0; border-left: 10px solid #BFC1C3;'
"padding: 15px 0 0.1px 15px; font-size: 19px; line-height: 25px;"
'">'
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">inset text</p>'
"</blockquote>"
),
],
[
notify_plain_text_email_markdown,
("\n" "\ninset text"),
],
),
)
def test_block_quote(markdown_function, expected):
assert markdown_function("^ inset text") == expected
@pytest.mark.parametrize(
"heading",
(
"# heading",
"#heading",
),
)
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, "<h2>heading</h2>\n"],
[
notify_email_markdown,
(
'<h2 style="Margin: 0 0 20px 0; padding: 0; font-size: 27px; '
'line-height: 35px; font-weight: bold; color: #0B0C0C;">'
"heading"
"</h2>"
),
],
[
notify_plain_text_email_markdown,
("\n" "\n" "\nheading" "\n-----------------------------------------------------------------"),
],
),
)
def test_level_1_header(markdown_function, heading, expected):
assert markdown_function(heading) == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, "<p>inset text</p>"],
[
notify_email_markdown,
'<h3 style="Margin: 0 0 15px 0; padding: 0; line-height: 26px; color: #0B0C0C;'
'font-size: 24px; font-weight: bold;">inset text</h3>',
],
[
notify_plain_text_email_markdown,
("\n" "\ninset text" "\n-----------------------------------------------------------------"),
],
),
)
def test_level_2_header(markdown_function, expected):
assert markdown_function("## inset text") == (expected)
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<p>a</p>" '<div class="page-break"> </div>' "<p>b</p>")],
[
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">a</p>'
'<hr style="border: 0; height: 1px; background: #BFC1C3; Margin: 30px 0 30px 0;">'
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">b</p>'
),
],
[
notify_plain_text_email_markdown,
("\n" "\na" "\n" "\n=================================================================" "\n" "\nb"),
],
),
)
def test_hrule(markdown_function, expected):
assert markdown_function("a\n\n***\n\nb") == expected
assert markdown_function("a\n\n---\n\nb") == expected
@pytest.mark.parametrize(
"markdown_function, markdown_input, expected",
(
[
notify_letter_preview_markdown,
"1. one\n" "2. two\n" "3. three\n",
("<ol>\n" "<li>one</li>\n" "<li>two</li>\n" "<li>three</li>\n" "</ol>\n"),
],
[notify_letter_preview_markdown, "1.one\n" "2.two\n" "3.three\n", "<p>1.one<br>2.two<br>3.three</p>"],
[
notify_email_markdown,
"1. one\n" "2. two\n" "3. three\n",
(
'<table role="presentation" style="padding: 0 0 20px 0;">'
"<tr>"
'<td style="font-family: Helvetica, Arial, sans-serif;">'
'<ol style="margin: 0; padding: 0; list-style-type: decimal; margin-inline-start: 20px;">'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">one</li>'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">two</li>'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">three</li>'
"</ol>"
"</td>"
"</tr>"
"</table>"
),
],
[
notify_email_markdown,
"1.one\n" "2.two\n" "3.three\n",
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"1.one"
"<br />"
"2.two"
"<br />"
"3.three"
"</p>"
),
],
[
notify_plain_text_email_markdown,
"1. one\n" "2. two\n" "3. three\n",
("\n" "\n1. one" "\n2. two" "\n3. three"),
],
[
notify_plain_text_email_markdown,
"1.one\n" "2.two\n" "3.three\n",
"\n\n1.one\n2.two\n3.three",
],
),
)
def test_ordered_list(markdown_function, markdown_input, expected):
assert markdown_function(markdown_input) == expected
@pytest.mark.parametrize(
"markdown",
(
("* one\n" "* two\n" "* three\n"), # single space
("* one\n" "* two\n" "* three\n"), # two spaces
("* one\n" "* two\n" "* three\n"), # tab
("- one\n" "- two\n" "- three\n"), # dash as bullet
pytest.param(("+ one\n" "+ two\n" "+ three\n"), marks=pytest.mark.xfail(raises=AssertionError)), # plus as bullet
("• one\n" "• two\n" "• three\n"), # bullet as bullet
),
)
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<ul>\n" "<li>one</li>\n" "<li>two</li>\n" "<li>three</li>\n" "</ul>\n")],
[
notify_email_markdown,
(
'<table role="presentation" style="padding: 0 0 20px 0;">'
"<tr>"
'<td style="font-family: Helvetica, Arial, sans-serif;">'
'<ul style="margin: 0; padding: 0; list-style-type: disc; margin-inline-start: 20px;">'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">one</li>'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">two</li>'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">three</li>'
"</ul>"
"</td>"
"</tr>"
"</table>"
),
],
[
notify_plain_text_email_markdown,
("\n" "\n• one" "\n• two" "\n• three"),
],
),
)
def test_unordered_list(markdown, markdown_function, expected):
assert markdown_function(markdown) == expected
@pytest.mark.parametrize(
"markdown",
(("*one\n" "*two\n" "*three\n"),), # no space
)
@pytest.mark.parametrize(
"markdown_function, expected",
(
[
notify_email_markdown,
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;"><em>one</em>two<br />*three</p>',
],
[
notify_plain_text_email_markdown,
"\n\n_one_two\n*three",
],
),
)
def test_unordered_list_with_no_spaces(markdown, markdown_function, expected):
"""
This use case emulates formatting if someone would try to write a list without a
space after the bullet. The result would be italized text with line breaks."""
assert markdown_function(markdown) == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[
notify_letter_preview_markdown,
"<p>+ one</p><p>+ two</p><p>+ three</p>",
],
[
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">+ one</p>'
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">+ two</p>'
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">+ three</p>'
),
],
[
notify_plain_text_email_markdown,
("\n\n+ one" "\n\n+ two" "\n\n+ three"),
],
),
)
def test_pluses_dont_render_as_lists(markdown_function, expected):
assert markdown_function("+ one\n" "+ two\n" "+ three\n") == expected
@pytest.mark.parametrize(
"markdown_function, input, expected",
(
[
notify_email_markdown,
"**title**: description",
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"<strong>title</strong>: description</p>",
],
[
notify_email_markdown,
"**_title_**: description",
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"<strong><em>title</em></strong>: description</p>",
],
[
notify_email_markdown,
"* **title**: description",
'<table role="presentation" style="padding: 0 0 20px 0;">'
'<tr><td style="font-family: Helvetica, Arial, sans-serif;">'
'<ul style="margin: 0; padding: 0; list-style-type: disc; margin-inline-start: 20px;">'
'<li style="Margin: 5px 0 5px; padding: 0 0 0 5px; font-size: 19px; line-height: 25px; color: #0B0C0C; text-align:start;">'
"<strong>title</strong>: description</li></ul></td></tr></table>",
],
),
)
def test_list_and_bold_or_italic(markdown_function, input, expected):
assert markdown_function(input) == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<p>" "line one<br>" "line two" "</p>" "<p>" "new paragraph" "</p>")],
[
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">line one<br />'
"line two</p>"
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">new paragraph</p>'
),
],
[
notify_plain_text_email_markdown,
("\n" "\nline one" "\nline two" "\n" "\nnew paragraph"),
],
),
)
def test_paragraphs(markdown_function, expected):
assert markdown_function("line one\n" "line two\n" "\n" "new paragraph") == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<p>before</p>" "<p>after</p>")],
[
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">before</p>'
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">after</p>'
),
],
[
notify_plain_text_email_markdown,
("\n" "\nbefore" "\n" "\nafter"),
],
),
)
def test_multiple_newlines_get_truncated(markdown_function, expected):
assert markdown_function("before\n\n\n\n\n\nafter") == expected
def test_table_feature_off():
markdown_input = "col | col\n" "----|----\n" "val | val\n"
assert notify_letter_preview_markdown(markdown_input) == ""
# Tables are gated behind FF_EMAIL_TABLES — off by default (no app context)
email_result = notify_email_markdown(markdown_input)
assert "<table" not in email_result
assert "<th" not in email_result
assert "<td" not in email_result
plain_text_result = notify_plain_text_email_markdown(markdown_input)
assert "| col | col |" not in plain_text_result
assert "| --- | --- |" not in plain_text_result
assert "| val | val |" not in plain_text_result
def test_table_feature_on(app):
app.config["FF_EMAIL_TABLES"] = True
markdown_input = "col | col\n" "----|----\n" "val | val\n"
assert notify_letter_preview_markdown(markdown_input) == ""
email_result = notify_email_markdown(markdown_input)
assert "<table" in email_result
assert "<th" in email_result
assert "<td" in email_result
assert "background: #fffdf5" in email_result
assert "col" in email_result
assert "val" in email_result
plain_text_result = notify_plain_text_email_markdown(markdown_input)
assert "| col | col |" in plain_text_result
assert "| --- | --- |" in plain_text_result
assert "| val | val |" in plain_text_result
@pytest.mark.parametrize(
"markdown_function, link, expected",
(
[notify_letter_preview_markdown, "http://example.com", "<p><strong>example.com</strong></p>"],
[
notify_email_markdown,
"http://example.com",
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
'<a style="word-wrap: break-word; word-break: break-word;" href="http://example.com">http://example.com</a>'
"</p>"
),
],
[
notify_email_markdown,
"""https://example.com"onclick="alert('hi')""",
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
'<a style="word-wrap: break-word; word-break: break-word;" href="https://example.com%22onclick=%22alert%28%27hi">'
'https://example.com"onclick="alert(\'hi'
"</a>')"
"</p>"
),
],
[
notify_plain_text_email_markdown,
"http://example.com",
("\n" "\nhttp://example.com"),
],
),
)
def test_autolink(markdown_function, link, expected):
assert markdown_function(link) == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, "<p>variable called thing</p>"],
[
notify_email_markdown,
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">variable called thing</p>',
],
[
notify_plain_text_email_markdown,
"\n\nvariable called thing",
],
),
)
def test_codespan(markdown_function, expected):
assert markdown_function("variable called `thing`") == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, "<p>something important</p>"],
[
notify_email_markdown,
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"something <strong>important</strong></p>",
],
[
notify_plain_text_email_markdown,
"\n\nsomething **important**",
],
),
)
@pytest.mark.parametrize("emphasis_style", ["**", "__"])
def test_double_emphasis(markdown_function, expected, emphasis_style):
assert markdown_function(f"something {emphasis_style}important{emphasis_style}") == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, "<p>something important</p>"],
[
notify_email_markdown,
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"something <em>important</em></p>",
],
[
notify_plain_text_email_markdown,
"\n\nsomething _important_",
],
),
)
@pytest.mark.parametrize("emphasis_style", ["_", "*"])
def test_emphasis(markdown_function, expected, emphasis_style):
assert markdown_function(f"something {emphasis_style}important{emphasis_style}") == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[
notify_email_markdown,
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">'
"foo <strong><em>bar</em></strong></p>",
],
[
notify_plain_text_email_markdown,
"\n\nfoo **_bar_**",
],
),
)
def test_nested_emphasis(markdown_function, expected):
assert markdown_function("foo **_bar_**") == expected
@pytest.mark.parametrize(
"markdown_function", (notify_letter_preview_markdown, notify_email_markdown, notify_plain_text_email_markdown)
)
def test_image(markdown_function):
assert markdown_function("") == ("")
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<p>Example: <strong>example.com</strong></p>")],
[
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; '
'color: #0B0C0C;">'
'<a style="word-wrap: break-word; word-break: break-word;" href="http://example.com">Example</a>'
"</p>"
),
],
[
notify_plain_text_email_markdown,
("\n" "\nExample: http://example.com"),
],
),
)
def test_link(markdown_function, expected):
assert markdown_function("[Example](http://example.com)") == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, ("<p>Example: <strong>example.com</strong></p>")],
[
notify_email_markdown,
(
'<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; '
'color: #0B0C0C;">'
'<a style="word-wrap: break-word; word-break: break-word;" href="http://example.com" title="An example URL">'
"Example"
"</a>"
"</p>"
),
],
[
notify_plain_text_email_markdown,
("\n" "\nExample (An example URL): http://example.com"),
],
),
)
def test_link_with_title(markdown_function, expected):
assert markdown_function('[Example](http://example.com "An example URL")') == expected
@pytest.mark.parametrize(
"markdown_function, expected",
(
[notify_letter_preview_markdown, "<p>Strike</p>"],
[notify_email_markdown, '<p style="Margin: 0 0 20px 0; font-size: 19px; line-height: 25px; color: #0B0C0C;">Strike</p>'],
[notify_plain_text_email_markdown, "\n\nStrike"],
),
)
def test_strikethrough(markdown_function, expected):
assert markdown_function("~~Strike~~") == expected
def test_footnotes():
# Can’t work out how to test this
pass
def test_sms_encode():
assert sms_encode("aàá…") == "aàa..."
@pytest.mark.parametrize(
"items, kwargs, expected_output",
[
([1], {}, "‘1’"),
([1, 2], {}, "‘1’ and ‘2’"),
([1, 2, 3], {}, "‘1’, ‘2’ and ‘3’"),
([1, 2, 3], {"prefix": "foo", "prefix_plural": "bar"}, "bar ‘1’, ‘2’ and ‘3’"),
([1], {"prefix": "foo", "prefix_plural": "bar"}, "foo ‘1’"),
([1, 2, 3], {"before_each": "a", "after_each": "b"}, "a1b, a2b and a3b"),
([1, 2, 3], {"conjunction": "foo"}, "‘1’, ‘2’ foo ‘3’"),
(["&"], {"before_each": "<i>", "after_each": "</i>"}, "<i>&</i>"),
([1, 2, 3], {"before_each": "<i>", "after_each": "</i>"}, "<i>1</i>, <i>2</i> and <i>3</i>"),
],
)
def test_formatted_list(items, kwargs, expected_output):
assert formatted_list(items, **kwargs) == expected_output
def test_formatted_list_returns_markup():
assert isinstance(formatted_list([0]), Markup)
def test_removing_dvla_markup():
assert (
strip_dvla_markup(
(
"some words & some more <words>"
"<cr><h1><h2><p><normal><op><np><bul><tab>"
"<CR><H1><H2><P><NORMAL><OP><NP><BUL><TAB>"
"<tAb>"
)
)
== "some words & some more <words>"
)
def test_removing_pipes():
assert strip_pipes("|a|b|c") == "abc"
def test_bleach_doesnt_try_to_make_valid_html_before_cleaning():
assert escape_html("<to cancel daily cat facts reply 'cancel'>") == ("<to cancel daily cat facts reply 'cancel'>")
@pytest.mark.parametrize(
"dirty, clean",
[
("Hello ((name)) ,\n\nThis is a message", "Hello ((name)),\n\nThis is a message"),
("Hello Jo ,\n\nThis is a message", "Hello Jo,\n\nThis is a message"),
(
"\n \t , word",
"\n, word",
),
(
"bonjour | hi",
"bonjour | hi",
),
],
)
def test_removing_whitespace_before_punctuation(dirty, clean):
assert remove_whitespace_before_punctuation(dirty) == clean
@pytest.mark.parametrize(
"dirty, clean",
[
("Hello ((name)) .\n\nThis is a message", "Hello ((name)).\n\nThis is a message"),
("Hello Jo .\n\nThis is a message", "Hello Jo.\n\nThis is a message"),
(
"\n \t . word",
"\n. word",
),
],
)
def test_removing_whitespace_before_full_stops(dirty, clean):
assert remove_whitespace_before_punctuation(dirty) == clean
@pytest.mark.parametrize(
"dumb, smart",
[
(
"""And I said, "what about breakfast at Tiffany's"?""",
"""And I said, “what about breakfast at Tiffany’s”?""",
),
(
"""
<a href="http://example.com?q='foo'">http://example.com?q='foo'</a>
""",
"""
<a href="http://example.com?q='foo'">http://example.com?q='foo'</a>
""",
),
],
)
def test_smart_quotes(dumb, smart):
assert make_quotes_smart(dumb) == smart
@pytest.mark.parametrize(
"nasty, nice",
[
(
(
"The en dash - always with spaces in running text when, as "
"discussed in this section, indicating a parenthesis or "
"pause - and the spaced em dash both have a certain "
"technical advantage over the unspaced em dash. "
),
(
"The en dash \u2013 always with spaces in running text when, as "
"discussed in this section, indicating a parenthesis or "
"pause \u2013 and the spaced em dash both have a certain "
"technical advantage over the unspaced em dash. "
),
),
(
"double -- dash",
"double \u2013 dash",
),
(
"triple --- dash",
"triple \u2013 dash",
),
(
"quadruple ---- dash",
"quadruple ---- dash",
),
(
"em — dash",
"em – dash",
),
(
"already\u0020–\u0020correct", # \u0020 is a normal space character
"already\u0020–\u0020correct",
),
(
"2004-2008",
"2004-2008", # no replacement
),
(
"bonjour | hi",
"bonjour | hi",
),
],
)
def test_en_dashes(nasty, nice):
assert replace_hyphens_with_en_dashes(nasty) == nice
def test_unicode_dash_lookup():
en_dash_replacement_sequence = "\u0020\u2013"
hyphen = "-"
en_dash = "–"
space = " "
non_breaking_space = " "
assert en_dash_replacement_sequence == space + en_dash
assert non_breaking_space not in en_dash_replacement_sequence
assert hyphen not in en_dash_replacement_sequence
@pytest.mark.parametrize(
"markup, expected_fixed",
[
(
"a",
"a",
),
(
"before<p><cr><p><cr>after",
"before<p><cr>after",
),
(
"before<cr><cr><np>after",
"before<cr><np>after",
),
(
"before{}<np>after".format("<cr>" * 4),
"before{}<np>after".format("<cr>" * 3),
),
],
)
def test_tweaking_dvla_list_markup(markup, expected_fixed):
assert tweak_dvla_list_markup(markup) == expected_fixed
def test_make_list_from_linebreaks():
assert nl2li("a\n" "b\n" "c\n") == ("<ul>" "<li>a</li>" "<li>b</li>" "<li>c</li>" "</ul>")
@pytest.mark.parametrize(
"value",
[
"bar",
" bar ",
"""
\t bar
""",
" \u180e\u200b \u200c bar \u200d \u2060\ufeff ",
],
)
def test_strip_whitespace(value):
assert strip_whitespace(value) == "bar"
@pytest.mark.parametrize(
"value",
[
"notifications-email",
" \tnotifications-email \x0c ",
"\rn\u200coti\u200dfi\u200bcati\u2060ons-\u180eemai\ufeffl\ufeff",
],
)
def test_strip_and_remove_obscure_whitespace(value):
assert strip_and_remove_obscure_whitespace(value) == "notifications-email"