Skip to content

Commit af8daa0

Browse files
committed
Patch DNS_NAME used in mail module by default
DNS lookups can be really slow, and it is good to skip them in general, and have a more stable header in the mails.
1 parent cb8ac4b commit af8daa0

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

pytest_django/plugin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,14 +440,25 @@ def _dj_autoclear_mailbox():
440440

441441

442442
@pytest.fixture(scope='function')
443-
def mailoutbox(monkeypatch, _dj_autoclear_mailbox):
443+
def mailoutbox(monkeypatch, django_mail_patch_dns, _dj_autoclear_mailbox):
444444
if not django_settings_is_configured():
445445
return
446446

447447
from django.core import mail
448448
return mail.outbox
449449

450450

451+
@pytest.fixture(scope='function')
452+
def django_mail_patch_dns(monkeypatch, django_mail_dnsname):
453+
from django.core import mail
454+
monkeypatch.setattr(mail.message, 'DNS_NAME', django_mail_dnsname)
455+
456+
457+
@pytest.fixture(scope='function')
458+
def django_mail_dnsname(monkeypatch):
459+
return 'fake-tests.example.com'
460+
461+
451462
@pytest.fixture(autouse=True, scope='function')
452463
def _django_set_urlconf(request):
453464
"""Apply the @pytest.mark.urls marker, internal to pytest-django."""

tests/test_fixtures.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,3 +499,65 @@ def test_mail(mailoutbox):
499499

500500
def test_mail_again(mailoutbox):
501501
test_mail(mailoutbox)
502+
503+
504+
def test_mail_message_uses_mocked_DNS_NAME(mailoutbox):
505+
mail.send_mail('subject', 'body', '[email protected]', ['[email protected]'])
506+
m = mailoutbox[0]
507+
message = m.message()
508+
assert message['Message-ID'].endswith('@fake-tests.example.com>')
509+
510+
511+
def test_mail_message_uses_django_mail_dnsname_fixture(django_testdir):
512+
django_testdir.create_test_module("""
513+
from django.core import mail
514+
import pytest
515+
516+
@pytest.fixture
517+
def django_mail_dnsname():
518+
return 'from.django_mail_dnsname'
519+
520+
def test_mailbox_inner(mailoutbox):
521+
mail.send_mail('subject', 'body', '[email protected]',
522+
523+
m = mailoutbox[0]
524+
message = m.message()
525+
assert message['Message-ID'].endswith('@from.django_mail_dnsname>')
526+
""")
527+
result = django_testdir.runpytest_subprocess('--tb=short', '-v')
528+
result.stdout.fnmatch_lines(['*test_mailbox_inner*PASSED*'])
529+
assert result.ret == 0
530+
531+
532+
def test_mail_message_dns_patching_can_be_skipped(django_testdir):
533+
django_testdir.create_test_module("""
534+
from django.core import mail
535+
import pytest
536+
537+
@pytest.fixture
538+
def django_mail_dnsname():
539+
raise Exception('should not get called')
540+
541+
@pytest.fixture
542+
def django_mail_patch_dns():
543+
print('\\ndjango_mail_dnsname_mark')
544+
545+
def test_mailbox_inner(mailoutbox, monkeypatch):
546+
def mocked_make_msgid(*args, **kwargs):
547+
mocked_make_msgid.called += [(args, kwargs)]
548+
mocked_make_msgid.called = []
549+
550+
monkeypatch.setattr(mail.message, 'make_msgid', mocked_make_msgid)
551+
mail.send_mail('subject', 'body', '[email protected]',
552+
553+
m = mailoutbox[0]
554+
assert len(mocked_make_msgid.called) == 1
555+
556+
assert mocked_make_msgid.called[0][1]['domain'] is mail.DNS_NAME
557+
""")
558+
result = django_testdir.runpytest_subprocess('--tb=short', '-vv', '-s')
559+
result.stdout.fnmatch_lines([
560+
'*test_mailbox_inner*',
561+
'django_mail_dnsname_mark',
562+
'PASSED*'])
563+
assert result.ret == 0

0 commit comments

Comments
 (0)