send_mail 関数を個別に呼び出すと、件名の改行が原因で BadHeaderError 例外が発生します。
この test_newline_causes_exception も失敗すると思いますが、そうではありません。これは Django 1.3 にあります。何か案は?
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)
編集:この新しいテストは、テストで send_mail が使用されている場合、ヘッダー チェック コード (django.core.mail.message.forbid_multi_line_headers) が呼び出されないことを示しています。
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')
結果:
AssertionError: 'Subject\nhere' != 'Subject here'