import pytest from notifications_utils.markdown import ( notify_email_markdown, notify_letter_preview_markdown, notify_plain_text_email_markdown, ) from notifications_utils.template import HTMLEmailTemplate @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 = '{}'.format( url, url ) assert notify_email_markdown(url) == ( '

' "{}" "

" ).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 " 'http://example.com' " in the middle" ), ), ( ("this link is in brackets (http://example.com)"), ( "this link is in brackets " '(http://example.com)' ), ), ], ) def test_makes_links_out_of_URLs_in_context(input, output): assert notify_email_markdown(input) == ( '

' "{}" "

" ).format(output) @pytest.mark.parametrize( "url", [ "example.com", "www.example.com", "ftp://example.com", "test@example.com", "mailto:test@example.com", 'Example', ], ) def test_doesnt_make_links_out_of_invalid_urls(url): assert notify_email_markdown(url) == ( '

' "{}" "

" ).format(url) def test_handles_placeholders_in_urls(): assert notify_email_markdown( "http://example.com/?token=((token))&key=1" ) == ( '

' '' "http://example.com/?token=" "" "((token))&key=1" "

" ) @pytest.mark.parametrize( ("url", "expected_html", "expected_html_in_template"), [ ( """https://example.com"onclick="alert('hi')""", """https://example.com"onclick="alert('hi')""", # noqa """https://example.com"onclick="alert('hi‘)""", # noqa ), ( """https://example.com"style='text-decoration:blink'""", """https://example.com"style='text-decoration:blink'""", # noqa """https://example.com"style='text-decoration:blink’""", # noqa ), ], ) def test_URLs_get_escaped(url, expected_html, expected_html_in_template): assert notify_email_markdown(url) == ( '

' "{}" "

" ).format(expected_html) assert expected_html_in_template in str( HTMLEmailTemplate( { "content": url, "subject": "", "template_type": "email", } ) ) @pytest.mark.parametrize( ("markdown_function", "expected_output"), [ ( notify_email_markdown, ( '

' '' "https://example.com" "" "

" '

' "Next paragraph" "

" ), ), ( 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( ("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, ("

inset text

")), ( notify_email_markdown, ( "
' '

inset text

' "
" ), ), ( 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, "

heading

\n"), ( notify_email_markdown, ( '

' "heading" "

" ), ), ( 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, "

inset text

"), ( notify_email_markdown, '

inset text

', ), ( notify_plain_text_email_markdown, ("\n" "\ninset text"), ), ], ) def test_level_2_header(markdown_function, expected): assert markdown_function("## inset text") == (expected) @pytest.mark.parametrize( ("markdown_function", "expected"), [ ( notify_letter_preview_markdown, ("

a

" '
 
' "

b

"), ), ( notify_email_markdown, ( '

a

' '
' '

b

' ), ), ( 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", "expected"), [ ( notify_letter_preview_markdown, ("
    \n" "
  1. one
  2. \n" "
  3. two
  4. \n" "
  5. three
  6. \n" "
\n"), ), ( notify_email_markdown, ( '' "" '" "" "
' '
    ' '
  1. one
  2. ' '
  3. two
  4. ' '
  5. three
  6. ' "
" "
" ), ), ( notify_plain_text_email_markdown, ("\n" "\n1. one" "\n2. two" "\n3. three"), ), ], ) def test_ordered_list(markdown_function, expected): assert markdown_function("1. one\n" "2. two\n" "3. three\n") == expected assert markdown_function("1.one\n" "2.two\n" "3.three\n") == expected @pytest.mark.parametrize( "markdown", [ ("*one\n" "*two\n" "*three\n"), # no space ("* one\n" "* two\n" "* three\n"), # single space ("* one\n" "* two\n" "* three\n"), # two spaces ("- one\n" "- two\n" "- three\n"), # dash as bullet pytest.param( ("+ one\n" "+ two\n" "+ three\n"), # plus as bullet marks=pytest.mark.xfail(raises=AssertionError), ), ("• one\n" "• two\n" "• three\n"), # bullet as bullet ], ) @pytest.mark.parametrize( ("markdown_function", "expected"), [ ( notify_letter_preview_markdown, ("\n"), ), ( notify_email_markdown, ( '' "" '" "" "
' '
    ' '
  • one
  • ' '
  • two
  • ' '
  • three
  • ' "
" "
" ), ), ( 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_function", "expected"), [ ( notify_letter_preview_markdown, "

+ one

+ two

+ three

", ), ( notify_email_markdown, ( '

+ one

' '

+ two

' '

+ three

' ), ), ( 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", "expected"), [ ( notify_letter_preview_markdown, ("

" "line one
" "line two" "

" "

" "new paragraph" "

"), ), ( notify_email_markdown, ( '

line one
' "line two

" '

new paragraph

' ), ), ( 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, ("

before

" "

after

")), ( notify_email_markdown, ( '

before

' '

after

' ), ), ( 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 @pytest.mark.parametrize( "markdown_function", [ notify_letter_preview_markdown, notify_email_markdown, notify_plain_text_email_markdown, ], ) def test_table(markdown_function): assert markdown_function("col | col\n" "----|----\n" "val | val\n") == ("") @pytest.mark.parametrize( ("markdown_function", "link", "expected"), [ ( notify_letter_preview_markdown, "http://example.com", "

example.com

", ), ( notify_email_markdown, "http://example.com", ( '

' 'http://example.com' "

" ), ), ( notify_email_markdown, """https://example.com"onclick="alert('hi')""", ( '

' '' 'https://example.com"onclick="alert(\'hi' "')" "

" ), ), ( 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, "

variable called `thing`

"), ( notify_email_markdown, '

variable called `thing`

', # noqa E501 ), ( 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, "

something **important**

"), ( notify_email_markdown, '

something **important**

', # noqa E501 ), ( notify_plain_text_email_markdown, "\n\nsomething **important**", ), ], ) def test_double_emphasis(markdown_function, expected): assert markdown_function("something **important**") == expected @pytest.mark.parametrize( ("markdown_function", "text", "expected"), [ ( notify_letter_preview_markdown, "something *important*", "

something *important*

", ), ( notify_email_markdown, "something *important*", '

something *important*

', # noqa E501 ), ( notify_plain_text_email_markdown, "something *important*", "\n\nsomething *important*", ), ( notify_plain_text_email_markdown, "something _important_", "\n\nsomething _important_", ), ( notify_plain_text_email_markdown, "before*after", "\n\nbefore*after", ), ( notify_plain_text_email_markdown, "before_after", "\n\nbefore_after", ), ], ) def test_emphasis(markdown_function, text, expected): assert markdown_function(text) == expected @pytest.mark.parametrize( ("markdown_function", "expected"), [ ( notify_email_markdown, '

foo ****** bar

', ), ( 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("![alt text](http://example.com/image.png)") == ("") @pytest.mark.parametrize( ("markdown_function", "expected"), [ ( notify_letter_preview_markdown, ("

Example: example.com

"), ), ( notify_email_markdown, ( '

' 'Example' "

" ), ), ( 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, ("

Example: example.com

"), ), ( notify_email_markdown, ( '

' '' "Example" "" "

" ), ), ( 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, "

~~Strike~~

"), ( notify_email_markdown, '

~~Strike~~

', ), (notify_plain_text_email_markdown, "\n\n~~Strike~~"), ], ) def test_strikethrough(markdown_function, expected): assert markdown_function("~~Strike~~") == expected def test_footnotes(): # Can’t work out how to test this pass