Skip to content

Commit

Permalink
squash 169
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbfled committed Dec 10, 2024
1 parent c0e34f2 commit aafabb0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
7 changes: 3 additions & 4 deletions notifications_utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ def __init__(
self.redact_missing_personalisation = redact_missing_personalisation
self.jinja_template = self.template_env.get_template('sms_preview_template.jinja2')

def __str__(self):

def __str__(self) -> str:
field = Field(
self.content,
self.values,
Expand All @@ -229,7 +228,7 @@ def __str__(self):
)
field = add_prefix(field, escape_html(self.prefix) if self.show_prefix else None)

return Markup(self.jinja_template.render({
return str(Markup(self.jinja_template.render({
'sender': self.sender,
'show_sender': self.show_sender,
'recipient': Field('((phone number))', self.values, with_brackets=False, html='escape'),
Expand All @@ -241,7 +240,7 @@ def __str__(self):
nl2br,
autolink_sms,
)
}))
})))


class WithSubjectTemplate(Template):
Expand Down
61 changes: 45 additions & 16 deletions tests/test_template_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,31 +639,60 @@ def test_stripping_of_unsupported_characters_in_email_templates():
assert expected in str(HTMLEmailTemplate({'content': template_content, 'subject': ''}))


@mock.patch('notifications_utils.template.add_prefix', return_value='')
@pytest.mark.parametrize(
"template_class, prefix, body, expected_call", [
(SMSMessageTemplate, "a", "b", (Markup("b"), "a")),
(SMSPreviewTemplate, "a", "b", (Markup("b"), "a")),
(SMSMessageTemplate, None, "b", (Markup("b"), None)),
(SMSPreviewTemplate, None, "b", (Markup("b"), None)),
(SMSMessageTemplate, '<em>ht&ml</em>', "b", (Markup("b"), '<em>ht&ml</em>')),
(SMSPreviewTemplate, '<em>ht&ml</em>', "b", (Markup("b"), '&lt;em&gt;ht&amp;ml&lt;/em&gt;')),
]
"template_class, prefix, body, expected",
[
(
SMSMessageTemplate,
'a',
'b',
'a: b',
),
(
SMSMessageTemplate,
None,
'b',
'b',
),
(
SMSMessageTemplate,
'<em>ht&ml</em>',
'b',
'<em>ht&ml</em>: b',
),
(
SMSPreviewTemplate,
'a',
'b',
'\n\n<div class="sms-message-wrapper">\n a: b\n</div>',
),
(
SMSPreviewTemplate,
None,
'b',
'\n\n<div class="sms-message-wrapper">\n b\n</div>',
),
(
SMSPreviewTemplate,
'<em>ht&ml</em>',
'b',
'\n\n<div class="sms-message-wrapper">\n &lt;em&gt;ht&amp;ml&lt;/em&gt;: b\n</div>',
),
],
ids=['message_a', 'message_none', 'message_html', 'preview_a', 'preview_none', 'preview_html']
)
def test_sms_message_adds_prefix(add_prefix, template_class, prefix, body, expected_call):
def test_sms_templates_add_prefix(template_class, prefix, body, expected):
template = template_class({'content': body})
template.prefix = prefix
template.sender = None
str(template)
add_prefix.assert_called_once_with(*expected_call)
assert str(template) == expected


@mock.patch('notifications_utils.template.add_prefix', return_value='')
@pytest.mark.parametrize('template_class', [SMSMessageTemplate, SMSPreviewTemplate])
@pytest.mark.parametrize(
'template_class', [SMSMessageTemplate, SMSPreviewTemplate]
)
@pytest.mark.parametrize(
"show_prefix, prefix, body, sender, expected_call", [
"show_prefix, prefix, body, sender, expected_call",
[
(False, "a", "b", "c", (Markup("b"), None)),
(True, "a", "b", None, (Markup("b"), "a")),
(True, "a", "b", False, (Markup("b"), "a")),
Expand Down

0 comments on commit aafabb0

Please sign in to comment.