Skip to content

Commit

Permalink
squash 169
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbfled committed Dec 11, 2024
1 parent a26a029 commit de3ec97
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 47 deletions.
2 changes: 1 addition & 1 deletion notifications_utils/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
markdown_lists=False,
redact_missing_personalisation=False,
preview_mode=False,
is_letter_template=False
is_letter_template=False # TODO - remove
):
self.content = content
self.values = values
Expand Down
8 changes: 8 additions & 0 deletions notifications_utils/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,18 @@ def sms_encode(content):


def strip_html(value):
"""
Calls to bleach.clean escapes HTML. This function strips and escapes the input.
"""

return bleach.clean(value, tags=[], strip=True)


def escape_html(value):
"""
Calls to bleach.clean escapes HTML. This function escapes, but does not strip, the input.
"""

if not value:
return value
value = str(value).replace('<', '&lt;')
Expand Down
19 changes: 9 additions & 10 deletions notifications_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ def compose1(value, *fs):
Return the composition of functions applied to a single value.
"""

# print('COMPOSE', value) # TODO
return_value = value
for f in fs:
return_value = f(return_value)
# print('COMPOSE', return_value) # TODO
return return_value


Expand Down Expand Up @@ -369,12 +371,8 @@ def preheader(self):
return ' '.join(field)[:self.PREHEADER_LENGTH_IN_CHARACTERS].strip()

def __str__(self):

return self.jinja_template.render({
'body': get_html_email_body(
self.content, self.values, preview_mode=self.preview_mode
),
'preheader': self.preheader if not self.preview_mode else '',
'body': get_html_email_body(self.content, self.values, preview_mode=self.preview_mode),
'default_banner': self.default_banner,
'complete_html': self.complete_html,
'brand_logo': self.brand_logo,
Expand Down Expand Up @@ -460,16 +458,17 @@ def is_unicode(content):


def get_html_email_body(
template_content, template_values, redact_missing_personalisation=False, preview_mode=False
):
field = Field(
template_content, template_values, redact_missing_personalisation=False, preview_mode=False
) -> str:
field = str(Field(
template_content,
template_values,
html='escape',
html='passthrough', # notify_html_markdown will escape the input
markdown_lists=True,
redact_missing_personalisation=redact_missing_personalisation,
preview_mode=preview_mode
)
))

return compose1(
field,
unlink_govuk_escaped,
Expand Down
63 changes: 27 additions & 36 deletions tests/test_template_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,8 @@ def test_get_html_email_body_preview_with_placeholder_in_markdown_link(content,


def test_html_email_inserts_body():
assert 'the &lt;em&gt;quick&lt;/em&gt; brown fox' in str(HTMLEmailTemplate(
{'content': 'the <em>quick</em> brown fox', 'subject': ''}
))
content = 'the <em>quick</em> brown fox'
assert content in str(HTMLEmailTemplate({'content': content, 'subject': ''}))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -563,33 +562,37 @@ def test_makes_links_out_of_URLs(template_class, url, 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; color: #004795;"'
' href="https://service.example.com/accept_invite/a1b2c3d4">'
'https://service.example.com/accept_invite/a1b2c3d4'
'</a>'
),
),
@pytest.mark.parametrize(
'content, html_snippet',
(
(
'https://service.example.com/accept_invite/?a=b&c=d&'
(
'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; color: #004795;" target="_blank"'
' href="https://service.example.com/accept_invite/a1b2c3d4">'
'https://service.example.com/accept_invite/a1b2c3d4'
'</a>'
),
),
(
'<a style="word-wrap: break-word; color: #004795;"'
' href="https://service.example.com/accept_invite/?a=b&amp;c=d&amp;">'
'https://service.example.com/accept_invite/?a=b&amp;c=d&amp;'
'</a>'
(
'https://service.example.com/accept_invite/?a=b&c=d&'
),
(
'<a style="word-wrap: break-word; color: #004795;" target="_blank"'
' href="https://service.example.com/accept_invite/?a=b&amp;c=d&amp;">'
'https://service.example.com/accept_invite/?a=b&amp;c=d&amp;'
'</a>'
),
),
),
))
ids=['no_url_params', 'with_url_params']
)
def test_HTML_template_has_URLs_replaced_with_links(content, html_snippet):
assert html_snippet in str(HTMLEmailTemplate({'content': content, 'subject': ''}))

Expand Down Expand Up @@ -918,18 +921,6 @@ def test_smart_quotes_removed_from_long_template_in_under_a_second():
assert process_time() - start_time < 1


def test_basic_templates_return_markup():

template_dict = {'content': 'content', 'subject': 'subject'}

for output in [
str(Template(template_dict)),
str(WithSubjectTemplate(template_dict)),
WithSubjectTemplate(template_dict).subject,
]:
assert isinstance(output, Markup)


@pytest.mark.parametrize('template_instance, expected_placeholders', [
(
SMSMessageTemplate(
Expand Down

0 comments on commit de3ec97

Please sign in to comment.