Adds a real (if simple) SMTP server to the test framework and tests handling of exceptions and rejected addresses. Fixes ticket #1314. - Legacy-Id: 7613 Note: SVN reference [7591] has been migrated to Git commit 54919f01343995ab154d27dcfaf7a60b15dd5eee
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
import os.path
|
|
from smtplib import SMTPRecipientsRefused
|
|
|
|
from django.conf import settings
|
|
from django.test import TestCase
|
|
|
|
from ietf.utils.management.commands import pyflakes
|
|
from ietf.utils.mail import send_mail_text, outbox, SMTPSomeRefusedRecipients, smtp_error_logging
|
|
|
|
class PyFlakesTestCase(TestCase):
|
|
|
|
def test_pyflakes(self):
|
|
path = os.path.join(settings.BASE_DIR)
|
|
warnings = []
|
|
warnings = pyflakes.checkPaths([path], verbosity=0)
|
|
self.assertEqual([str(w) for w in warnings], [])
|
|
|
|
class TestSMTPServer(TestCase):
|
|
|
|
def test_address_rejected(self):
|
|
|
|
def send_mail(to):
|
|
send_mail_text(None, to=to, frm=None, subject="Test for rejection", txt="dummy body")
|
|
|
|
with self.assertRaises(SMTPSomeRefusedRecipients):
|
|
send_mail('good@example.com,poison@example.com')
|
|
|
|
with self.assertRaises(SMTPRecipientsRefused):
|
|
send_mail('poison@example.com')
|
|
|
|
len_before = len(outbox)
|
|
with smtp_error_logging(send_mail) as send:
|
|
send('good@example.com,poison@example.com')
|
|
self.assertEqual(len(outbox),len_before+2)
|
|
self.assertTrue('Some recipients were refused' in outbox[-1]['Subject'])
|
|
|
|
len_before = len(outbox)
|
|
with smtp_error_logging(send_mail) as send:
|
|
send('poison@example.com')
|
|
self.assertEqual(len(outbox),len_before+2)
|
|
self.assertTrue('error while sending email' in outbox[-1]['Subject'])
|
|
|