Skip to content

Commit

Permalink
fix(email): add tests for email character escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
Taylor Osler authored and Taylor Osler committed Aug 21, 2024
1 parent 9edbb44 commit c057bf4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/sentry/utils/email/test_message_builder.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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 <LTD>"}
with self._override_templates():
msg = MessageBuilder(
subject="Congratulations! You won!",
template="escaping_plaintext_test.txt",
context=context,
)
msg.send(["[email protected]"])

assert len(mail.outbox) == 1
assert context["name"] in mail.outbox[0].body
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You're our winner, {{ name }}! Congratulations!

0 comments on commit c057bf4

Please sign in to comment.