From 8d8a407b6ed6afb133914536f47c0ba08997b636 Mon Sep 17 00:00:00 2001 From: Stephen McMurtry Date: Fri, 27 Aug 2021 11:35:01 -0600 Subject: [PATCH] fix: strip html from preheader for templates where html is allowed (#103) * fix the bug where html shows up in the email preheader * added tests and updated version number --- notifications_utils/template.py | 2 +- notifications_utils/version.py | 2 +- tests/test_template_types.py | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/notifications_utils/template.py b/notifications_utils/template.py index 946d978c..38e7b53b 100644 --- a/notifications_utils/template.py +++ b/notifications_utils/template.py @@ -378,7 +378,7 @@ def preheader(self): Field( self.content, self.values, - html="escape", + html="strip" if self.allow_html else "escape", markdown_lists=True, ) ) diff --git a/notifications_utils/version.py b/notifications_utils/version.py index 39d67450..763bc1df 100644 --- a/notifications_utils/version.py +++ b/notifications_utils/version.py @@ -1,2 +1,2 @@ -__version__ = "46.1.0" +__version__ = "46.2.0" # GDS version '34.0.1' diff --git a/tests/test_template_types.py b/tests/test_template_types.py index 708b7e53..ffbefaae 100644 --- a/tests/test_template_types.py +++ b/tests/test_template_types.py @@ -338,6 +338,28 @@ def test_content_of_preheader_in_html_emails( 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, "
Hello World
", "Hello World"), + (False, "
Hello World
", "<div>Hello World</div>"), + (True, '
Hello World
', "Hello World"), + (False, '
Hello World
', "<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", [