Skip to content

Commit 9996d43

Browse files
authored
Merge pull request pytest-dev#503 from blueyed/patch-mail-dnsname
Patch DNS_NAME used in mail module by default
2 parents 34f7060 + 18de4c9 commit 9996d43

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

docs/changelog.rst

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,24 @@ Changelog
44
3.3.0 (unreleased)
55
------------------
66

7-
* Fix test for classmethod with Django TestCases (#597, #598).
8-
* Fix RemovedInPytest4Warning: MarkInfo objects are deprecated (#596, #603)
7+
Features
8+
^^^^^^^^
9+
10+
* Added new fixtures ``django_mail_dnsname`` and ``django_mail_patch_dns``,
11+
used by ``mailoutbox`` to monkeypatch the ``DNS_NAME`` used in
12+
:py:mod:`django.core.mail` to improve performance and
13+
reproducibility.
14+
15+
Bug fixes
16+
^^^^^^^^^
17+
18+
* Fixed test for classmethod with Django TestCases (#597, #598).
19+
* Fixed RemovedInPytest4Warning: MarkInfo objects are deprecated (#596, #603)
920

1021
3.2.1
1122
-----
1223

13-
Fix automatic deployment to PyPI.
24+
* Fixed automatic deployment to PyPI.
1425

1526
3.2.0
1627
-----

docs/helpers.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Example
264264

265265

266266
``mailoutbox``
267-
~~~~~~~~~~~~~~~~~~~~~~~~~
267+
~~~~~~~~~~~~~~
268268

269269
A clean email outbox to which Django-generated emails are sent.
270270

@@ -285,6 +285,12 @@ Example
285285
assert list(m.to) == ['[email protected]']
286286

287287

288+
This uses the ``django_mail_patch_dns`` fixture, which patches
289+
``DNS_NAME`` used by :py:mod:`django.core.mail` with the value from
290+
the ``django_mail_dnsname`` fixture, which defaults to
291+
"fake-tests.example.com".
292+
293+
288294
Automatic cleanup
289295
-----------------
290296

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)