diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2009-11-03 12:53:26 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2009-11-03 12:53:26 +0000 |
| commit | aba5389326372be43b2a3bdcda16646fd197e807 (patch) | |
| tree | a296f56691d3cb6da4dcbc3a99e16a7eab9a7c43 /tests/regressiontests/mail | |
| parent | 8287c27b1895ba56c6680295ff3d202fc7a4b64e (diff) | |
Fixed #10355 -- Added an API for pluggable e-mail backends.
Thanks to Andi Albrecht for his work on this patch, and to everyone else that contributed during design and development.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11709 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/mail')
| -rw-r--r-- | tests/regressiontests/mail/custombackend.py | 15 | ||||
| -rw-r--r-- | tests/regressiontests/mail/tests.py | 223 |
2 files changed, 236 insertions, 2 deletions
diff --git a/tests/regressiontests/mail/custombackend.py b/tests/regressiontests/mail/custombackend.py new file mode 100644 index 0000000000..6b0e15ad0a --- /dev/null +++ b/tests/regressiontests/mail/custombackend.py @@ -0,0 +1,15 @@ +"""A custom backend for testing.""" + +from django.core.mail.backends.base import BaseEmailBackend + + +class EmailBackend(BaseEmailBackend): + + def __init__(self, *args, **kwargs): + super(EmailBackend, self).__init__(*args, **kwargs) + self.test_outbox = [] + + def send_messages(self, email_messages): + # Messages are stored in a instance variable for testing. + self.test_outbox.extend(email_messages) + return len(email_messages) diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index e90d77366f..5033581a09 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -1,10 +1,18 @@ # coding: utf-8 + r""" # Tests for the django.core.mail. +>>> import os +>>> import shutil +>>> import tempfile +>>> from StringIO import StringIO >>> from django.conf import settings >>> from django.core import mail >>> from django.core.mail import EmailMessage, mail_admins, mail_managers, EmailMultiAlternatives +>>> from django.core.mail import send_mail, send_mass_mail +>>> from django.core.mail.backends.base import BaseEmailBackend +>>> from django.core.mail.backends import console, dummy, locmem, filebased, smtp >>> from django.utils.translation import ugettext_lazy # Test normal ascii character case: @@ -85,8 +93,6 @@ BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection T >>> mail_managers('hi','there') >>> len(mail.outbox) 1 ->>> settings.ADMINS = old_admins ->>> settings.MANAGERS = old_managers # Make sure we can manually set the From header (#9214) @@ -138,4 +144,217 @@ Content-Disposition: attachment; filename="an attachment.pdf" JVBERi0xLjQuJS4uLg== ... +# Make sure that the console backend writes to stdout by default +>>> connection = console.EmailBackend() +>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> connection.send_messages([email]) +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: Subject +From: from@example.com +To: to@example.com +Date: ... +Message-ID: ... + +Content +------------------------------------------------------------------------------- +1 + +# Test that the console backend can be pointed at an arbitrary stream +>>> s = StringIO() +>>> connection = mail.get_connection('django.core.mail.backends.console', stream=s) +>>> send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection) +1 +>>> print s.getvalue() +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: Subject +From: from@example.com +To: to@example.com +Date: ... +Message-ID: ... + +Content +------------------------------------------------------------------------------- + +# Make sure that dummy backends returns correct number of sent messages +>>> connection = dummy.EmailBackend() +>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> connection.send_messages([email, email, email]) +3 + +# Make sure that locmen backend populates the outbox +>>> mail.outbox = [] +>>> connection = locmem.EmailBackend() +>>> email1 = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> email2 = EmailMessage('Subject 2', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> connection.send_messages([email1, email2]) +2 +>>> len(mail.outbox) +2 +>>> mail.outbox[0].subject +'Subject' +>>> mail.outbox[1].subject +'Subject 2' + +# Make sure that multiple locmem connections share mail.outbox +>>> mail.outbox = [] +>>> connection1 = locmem.EmailBackend() +>>> connection2 = locmem.EmailBackend() +>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> connection1.send_messages([email]) +1 +>>> connection2.send_messages([email]) +1 +>>> len(mail.outbox) +2 + +# Make sure that the file backend write to the right location +>>> tmp_dir = tempfile.mkdtemp() +>>> connection = filebased.EmailBackend(file_path=tmp_dir) +>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> connection.send_messages([email]) +1 +>>> len(os.listdir(tmp_dir)) +1 +>>> print open(os.path.join(tmp_dir, os.listdir(tmp_dir)[0])).read() +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: Subject +From: from@example.com +To: to@example.com +Date: ... +Message-ID: ... + +Content +------------------------------------------------------------------------------- + +>>> connection2 = filebased.EmailBackend(file_path=tmp_dir) +>>> connection2.send_messages([email]) +1 +>>> len(os.listdir(tmp_dir)) +2 +>>> connection.send_messages([email]) +1 +>>> len(os.listdir(tmp_dir)) +2 +>>> email.connection = filebased.EmailBackend(file_path=tmp_dir) +>>> connection_created = connection.open() +>>> num_sent = email.send() +>>> len(os.listdir(tmp_dir)) +3 +>>> num_sent = email.send() +>>> len(os.listdir(tmp_dir)) +3 +>>> connection.close() +>>> shutil.rmtree(tmp_dir) + +# Make sure that get_connection() accepts arbitrary keyword that might be +# used with custom backends. +>>> c = mail.get_connection(fail_silently=True, foo='bar') +>>> c.fail_silently +True + +# Test custom backend defined in this suite. +>>> conn = mail.get_connection('regressiontests.mail.custombackend') +>>> hasattr(conn, 'test_outbox') +True +>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'}) +>>> conn.send_messages([email]) +1 +>>> len(conn.test_outbox) +1 + +# Test backend argument of mail.get_connection() +>>> isinstance(mail.get_connection('django.core.mail.backends.smtp'), smtp.EmailBackend) +True +>>> isinstance(mail.get_connection('django.core.mail.backends.locmem'), locmem.EmailBackend) +True +>>> isinstance(mail.get_connection('django.core.mail.backends.dummy'), dummy.EmailBackend) +True +>>> isinstance(mail.get_connection('django.core.mail.backends.console'), console.EmailBackend) +True +>>> tmp_dir = tempfile.mkdtemp() +>>> isinstance(mail.get_connection('django.core.mail.backends.filebased', file_path=tmp_dir), filebased.EmailBackend) +True +>>> shutil.rmtree(tmp_dir) +>>> isinstance(mail.get_connection(), locmem.EmailBackend) +True + +# Test connection argument of send_mail() et al +>>> connection = mail.get_connection('django.core.mail.backends.console') +>>> send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection) +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: Subject +From: from@example.com +To: to@example.com +Date: ... +Message-ID: ... + +Content +------------------------------------------------------------------------------- +1 + +>>> send_mass_mail([ +... ('Subject1', 'Content1', 'from1@example.com', ['to1@example.com']), +... ('Subject2', 'Content2', 'from2@example.com', ['to2@example.com']) +... ], connection=connection) +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: Subject1 +From: from1@example.com +To: to1@example.com +Date: ... +Message-ID: ... + +Content1 +------------------------------------------------------------------------------- +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: Subject2 +From: from2@example.com +To: to2@example.com +Date: ... +Message-ID: ... + +Content2 +------------------------------------------------------------------------------- +2 + +>>> mail_admins('Subject', 'Content', connection=connection) +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: [Django] Subject +From: root@localhost +To: nobody@example.com +Date: ... +Message-ID: ... + +Content +------------------------------------------------------------------------------- + +>>> mail_managers('Subject', 'Content', connection=connection) +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +Subject: [Django] Subject +From: root@localhost +To: nobody@example.com +Date: ... +Message-ID: ... + +Content +------------------------------------------------------------------------------- + +>>> settings.ADMINS = old_admins +>>> settings.MANAGERS = old_managers + """ |
