Skip to content

Commit

Permalink
fix: strip html from preheader for templates where html is allowed (#103
Browse files Browse the repository at this point in the history
)

* fix the bug where html shows up in the email preheader

* added tests and updated version number
  • Loading branch information
smcmurtry committed Aug 27, 2021
1 parent 5375abd commit 8d8a407
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion notifications_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "46.1.0"
__version__ = "46.2.0"
# GDS version '34.0.1'
22 changes: 22 additions & 0 deletions tests/test_template_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<div>Hello World</div>", "Hello World"),
(False, "<div>Hello World</div>", "&lt;div&gt;Hello World&lt;/div&gt;"),
(True, '<div><img src="file.png" />Hello World</div>', "Hello World"),
(False, '<div><img src="file.png" />Hello World</div>', "&lt;div&gt;&lt;img src=”file.png” /&gt;Hello World&lt;/div&gt;"),
],
)
@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",
[
Expand Down

0 comments on commit 8d8a407

Please sign in to comment.