@@ -499,3 +499,65 @@ def test_mail(mailoutbox):
499
499
500
500
def test_mail_again (mailoutbox ):
501
501
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