Skip to content

Commit

Permalink
Modify test case to include newline characters in TestCase summary
Browse files Browse the repository at this point in the history
these will end up in email Subject: header and that causes a traceback:

django/core/mail/message.py in forbid_multi_line_headers at line 60:

def forbid_multi_line_headers(name, val, encoding):

    """Forbid multi-line headers to prevent header injection."""

    encoding = encoding or settings.DEFAULT_CHARSET

    val = str(val)  # val may be lazy

    if "\n" in val or "\r" in val:

        raise BadHeaderError(

            "Header values can't contain newlines (got %r for header %r)" % (val, name)

        )
  • Loading branch information
atodorov committed Jun 28, 2024
1 parent 962dc16 commit 85e1b40
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tcms/testcases/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,19 @@ def setUpTestData(cls):

@patch("tcms.core.utils.mailto.send_mail")
def test_send_mail_to_case_author(self, send_mail):
self.case.summary = "New summary for running test"
# note: includes \n and \r characters
self.case.summary = "New summary\n for\r running test"
self.case.save()

expected_subject, expected_body = history_email_for(
self.case, self.case.summary
)
recipients = get_case_notification_recipients(self.case)

# make sure subject doesn't result in a multi-line header
self.assertEqual(expected_subject.find("\n"), -1)
self.assertEqual(expected_subject.find("\r"), -1)

# Verify notification mail
send_mail.assert_called_once_with(
settings.EMAIL_SUBJECT_PREFIX + expected_subject,
Expand Down

0 comments on commit 85e1b40

Please sign in to comment.