From aafabb0d0d75442e0e1800a32e4686048b6b72f6 Mon Sep 17 00:00:00 2001 From: "David J. Kalbfleisch" <1.21e9W@protonmail.com> Date: Tue, 10 Dec 2024 18:03:37 -0500 Subject: [PATCH] squash 169 --- notifications_utils/template.py | 7 ++-- tests/test_template_types.py | 61 ++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/notifications_utils/template.py b/notifications_utils/template.py index 9c2db5ad..ac8b2bb5 100644 --- a/notifications_utils/template.py +++ b/notifications_utils/template.py @@ -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, @@ -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'), @@ -241,7 +240,7 @@ def __str__(self): nl2br, autolink_sms, ) - })) + }))) class WithSubjectTemplate(Template): diff --git a/tests/test_template_types.py b/tests/test_template_types.py index 7fec138a..6547b75b 100644 --- a/tests/test_template_types.py +++ b/tests/test_template_types.py @@ -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, 'ht&ml', "b", (Markup("b"), 'ht&ml')), - (SMSPreviewTemplate, 'ht&ml', "b", (Markup("b"), '<em>ht&ml</em>')), - ] + "template_class, prefix, body, expected", + [ + ( + SMSMessageTemplate, + 'a', + 'b', + 'a: b', + ), + ( + SMSMessageTemplate, + None, + 'b', + 'b', + ), + ( + SMSMessageTemplate, + 'ht&ml', + 'b', + 'ht&ml: b', + ), + ( + SMSPreviewTemplate, + 'a', + 'b', + '\n\n
\n a: b\n
', + ), + ( + SMSPreviewTemplate, + None, + 'b', + '\n\n
\n b\n
', + ), + ( + SMSPreviewTemplate, + 'ht&ml', + 'b', + '\n\n
\n <em>ht&ml</em>: b\n
', + ), + ], + 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")),