From c057bf428e6111bb9a63e2cf8ec69a0b9a03010a Mon Sep 17 00:00:00 2001 From: Taylor Osler Date: Wed, 21 Aug 2024 15:00:56 -0700 Subject: [PATCH] fix(email): add tests for email character escaping --- .../utils/email/test_message_builder.py | 25 +++++++++++++++++++ .../templates/escaping_plaintext_test.txt | 1 + 2 files changed, 26 insertions(+) create mode 100644 tests/sentry/utils/email/testdata/templates/escaping_plaintext_test.txt diff --git a/tests/sentry/utils/email/test_message_builder.py b/tests/sentry/utils/email/test_message_builder.py index fc9724200826fa..0f260c5d92e2e4 100644 --- a/tests/sentry/utils/email/test_message_builder.py +++ b/tests/sentry/utils/email/test_message_builder.py @@ -1,8 +1,12 @@ import functools +import os +from copy import deepcopy from unittest.mock import patch +from django.conf import settings from django.core import mail from django.core.mail.message import EmailMultiAlternatives +from django.test import override_settings from sentry import options from sentry.models.groupemailthread import GroupEmailThread @@ -18,6 +22,14 @@ class MessageBuilderTest(TestCase): + def _override_templates(self): + templates = deepcopy(settings.TEMPLATES) + for template in templates: + template["DIRS"] = [os.path.abspath(os.path.dirname(__file__)) + "/testdata/templates"] + template["APP_DIRS"] = False + + return override_settings(TEMPLATES=templates) + def test_raw_content(self): msg = MessageBuilder( subject="Test", @@ -333,3 +345,16 @@ def test_adds_type_to_headers(self): json_xsmtpapi_data = json.loads(out.extra_headers["X-SMTPAPI"]) assert json_xsmtpapi_data["category"] == "test_email.type" + + def test_doesnt_escape_plaintext(self): + context = {"name": "Bob's Hardware & Paint "} + with self._override_templates(): + msg = MessageBuilder( + subject="Congratulations! You won!", + template="escaping_plaintext_test.txt", + context=context, + ) + msg.send(["foo@example.com"]) + + assert len(mail.outbox) == 1 + assert context["name"] in mail.outbox[0].body diff --git a/tests/sentry/utils/email/testdata/templates/escaping_plaintext_test.txt b/tests/sentry/utils/email/testdata/templates/escaping_plaintext_test.txt new file mode 100644 index 00000000000000..d00551fa5f0ee6 --- /dev/null +++ b/tests/sentry/utils/email/testdata/templates/escaping_plaintext_test.txt @@ -0,0 +1 @@ +You're our winner, {{ name }}! Congratulations!