Calling the send_mail function independently will cause a BadHeaderError exception due to the newline in the subject.
I expect this test_newline_causes_exception to fail as well, but it does not. This is in Django 1.3. Any ideas?
from django.core.mail import send_mail
from django.utils import unittest
class EmailTestCase(unittest.TestCase):
def test_newline_causes_exception(self):
send_mail('Header\nInjection', 'Here is the message.', 'from@example.com',
['to@example.com'], fail_silently=False)
EDIT: This new test shows that the header checking code (django.core.mail.message.forbid_multi_line_headers) is not called when send_mail is used in tests.
from django.core.mail import send_mail, BadHeaderError, outbox
from django.utils import unittest
class EmailTestCase(unittest.TestCase):
def test_newline_in_subject_should_raise_exception(self):
try:
send_mail('Subject\nhere', 'Here is the message.',
'from@example.com', ['to@example.com'], fail_silently=False)
except BadHeaderError:
raise Exception
self.assertEqual(len(outbox), 1)
self.assertEqual(outbox[0].subject, 'Subject here')
Result:
AssertionError: 'Subject\nhere' != 'Subject here'