Author: jacob Date: 2010-10-11 10:11:55 -0500 (Mon, 11 Oct 2010) New Revision: 14143
Modified: django/trunk/tests/regressiontests/mail/tests.py Log: Fixed #14440 - Converted mail doctests to unittests. Thanks to Rob Hudson for the patch and also to andialbrecht who filed a similar patch that I didn't use. Modified: django/trunk/tests/regressiontests/mail/tests.py =================================================================== --- django/trunk/tests/regressiontests/mail/tests.py 2010-10-11 14:36:20 UTC (rev 14142) +++ django/trunk/tests/regressiontests/mail/tests.py 2010-10-11 15:11:55 UTC (rev 14143) @@ -1,446 +1,345 @@ # coding: utf-8 +import email +import os +import shutil +import sys +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.core.mail.message import BadHeaderError +from django.test import TestCase +from django.utils.translation import ugettext_lazy -r""" -# Tests for the django.core.mail. +class MailTests(TestCase): ->>> 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 + def test_ascii(self): + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com']) + message = email.message() + self.assertEqual(message['Subject'].encode(), 'Subject') + self.assertEqual(message.get_payload(), 'Content') + self.assertEqual(message['From'], 'f...@example.com') + self.assertEqual(message['To'], 't...@example.com') -# Test normal ascii character case: + def test_multiple_recipients(self): + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com','ot...@example.com']) + message = email.message() + self.assertEqual(message['Subject'].encode(), 'Subject') + self.assertEqual(message.get_payload(), 'Content') + self.assertEqual(message['From'], 'f...@example.com') + self.assertEqual(message['To'], 't...@example.com, ot...@example.com') ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com']) ->>> message = email.message() ->>> message['Subject'].encode() -'Subject' ->>> message.get_payload() -'Content' ->>> message['From'] -'f...@example.com' ->>> message['To'] -'t...@example.com' + def test_cc(self): + """Regression test for #7722""" + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com'], cc=['c...@example.com']) + message = email.message() + self.assertEqual(message['Cc'], 'c...@example.com') + self.assertEqual(email.recipients(), ['t...@example.com', 'c...@example.com']) -# Test multiple-recipient case + # Verify headers + old_stdout = sys.stdout + sys.stdout = StringIO() + connection = console.EmailBackend() + connection.send_messages([email]) + self.assertTrue(sys.stdout.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: f...@example.com\nto: t...@example.com\ncc: c...@example.com\ndate: ')) + sys.stdout = old_stdout ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com','ot...@example.com']) ->>> message = email.message() ->>> message['Subject'].encode() -'Subject' ->>> message.get_payload() -'Content' ->>> message['From'] -'f...@example.com' ->>> message['To'] -'t...@example.com, ot...@example.com' + # Test multiple CC with multiple To + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com', 'ot...@example.com'], cc=['c...@example.com', 'cc.ot...@example.com']) + message = email.message() + self.assertEqual(message['Cc'], 'c...@example.com, cc.ot...@example.com') + self.assertEqual(email.recipients(), ['t...@example.com', 'ot...@example.com', 'c...@example.com', 'cc.ot...@example.com']) -# Test for header injection + # Testing with Bcc + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com', 'ot...@example.com'], cc=['c...@example.com', 'cc.ot...@example.com'], bcc=['b...@example.com']) + message = email.message() + self.assertEqual(message['Cc'], 'c...@example.com, cc.ot...@example.com') + self.assertEqual(email.recipients(), ['t...@example.com', 'ot...@example.com', 'c...@example.com', 'cc.ot...@example.com', 'b...@example.com']) ->>> email = EmailMessage('Subject\nInjection Test', 'Content', 'f...@example.com', ['t...@example.com']) ->>> message = email.message() -Traceback (most recent call last): - ... -BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection Test' for header 'Subject') + def test_header_injection(self): + email = EmailMessage('Subject\nInjection Test', 'Content', 'f...@example.com', ['t...@example.com']) + self.assertRaises(BadHeaderError, email.message) + email = EmailMessage(ugettext_lazy('Subject\nInjection Test'), 'Content', 'f...@example.com', ['t...@example.com']) + self.assertRaises(BadHeaderError, email.message) ->>> email = EmailMessage(ugettext_lazy('Subject\nInjection Test'), 'Content', 'f...@example.com', ['t...@example.com']) ->>> message = email.message() -Traceback (most recent call last): - ... -BadHeaderError: Header values can't contain newlines (got u'Subject\nInjection Test' for header 'Subject') + def test_space_continuation(self): + """ + Test for space continuation character in long (ascii) subject headers (#7747) + """ + email = EmailMessage('Long subject lines that get wrapped should use a space continuation character to get expected behaviour in Outlook and Thunderbird', 'Content', 'f...@example.com', ['t...@example.com']) + message = email.message() + self.assertEqual(message['Subject'], 'Long subject lines that get wrapped should use a space continuation\n character to get expected behaviour in Outlook and Thunderbird') -# Test for space continuation character in long (ascii) subject headers (#7747) + def test_message_header_overrides(self): + """ + Specifying dates or message-ids in the extra headers overrides the + default values (#9233) + """ + headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} + email = EmailMessage('subject', 'content', 'f...@example.com', ['t...@example.com'], headers=headers) + self.assertEqual(email.message().as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: subject\nFrom: f...@example.com\nto: t...@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent') ->>> email = EmailMessage('Long subject lines that get wrapped should use a space continuation character to get expected behaviour in Outlook and Thunderbird', 'Content', 'f...@example.com', ['t...@example.com']) ->>> message = email.message() ->>> message.as_string() -'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Long subject lines that get wrapped should use a space continuation\n character to get expected behaviour in Outlook and Thunderbird\nFrom: f...@example.com\nto: t...@example.com\ndate: ...\nMessage-ID: <...>\n\nContent' + def test_empty_admins(self): + """ + Test that mail_admins/mail_managers doesn't connect to the mail server + if there are no recipients (#9383) + """ + old_admins = settings.ADMINS + old_managers = settings.MANAGERS -# Specifying dates or message-ids in the extra headers overrides the defaul -# values (#9233). + settings.ADMINS = settings.MANAGERS = [('nobody','nob...@example.com')] + mail.outbox = [] + mail_admins('hi', 'there') + self.assertEqual(len(mail.outbox), 1) + mail.outbox = [] + mail_managers('hi', 'there') + self.assertEqual(len(mail.outbox), 1) ->>> headers = {"date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} ->>> email = EmailMessage('subject', 'content', 'f...@example.com', ['t...@example.com'], headers=headers) ->>> email.message().as_string() -'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: subject\nFrom: f...@example.com\nto: t...@example.com\ndate: Fri, 09 Nov 2001 01:08:47 -0000\nMessage-ID: foo\n\ncontent' + settings.ADMINS = settings.MANAGERS = [] + mail.outbox = [] + mail_admins('hi', 'there') + self.assertEqual(len(mail.outbox), 0) + mail.outbox = [] + mail_managers('hi', 'there') + self.assertEqual(len(mail.outbox), 0) -# Test that mail_admins/mail_managers doesn't connect to the mail server if there are no recipients (#9383) + settings.ADMINS = old_admins + settings.MANAGERS = old_managers ->>> old_admins = settings.ADMINS ->>> old_managers = settings.MANAGERS ->>> settings.ADMINS = [] ->>> settings.MANAGERS = [] ->>> mail.outbox = [] ->>> mail_admins('hi','there') ->>> len(mail.outbox) -0 ->>> mail.outbox = [] ->>> mail_managers('hi','there') ->>> len(mail.outbox) -0 ->>> settings.ADMINS = settings.MANAGERS = [('nobody','nob...@example.com')] ->>> mail.outbox = [] ->>> mail_admins('hi','there') ->>> len(mail.outbox) -1 ->>> mail.outbox = [] ->>> mail_managers('hi','there') ->>> len(mail.outbox) -1 + def test_from_header(self): + """ + Make sure we can manually set the From header (#9214) + """ + email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + message = email.message() + self.assertEqual(message['From'], 'f...@example.com') -# Make sure we can manually set the From header (#9214) + def test_multiple_message_call(self): + """ + Regression for #13259 - Make sure that headers are not changed when + calling EmailMessage.message() + """ + email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + message = email.message() + self.assertEqual(message['From'], 'f...@example.com') + message = email.message() + self.assertEqual(message['From'], 'f...@example.com') ->>> email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) ->>> message = email.message() ->>> message['From'] -'f...@example.com' + def test_unicode_header(self): + """ + Regression for #11144 - When a to/from/cc header contains unicode, + make sure the email addresses are parsed correctly (especially with + regards to commas) + """ + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['"Firstname Sürname" <t...@example.com>','ot...@example.com']) + self.assertEqual(email.message()['To'], '=?utf-8?q?Firstname_S=C3=BCrname?= <t...@example.com>, ot...@example.com') + email = EmailMessage('Subject', 'Content', 'f...@example.com', ['"Sürname, Firstname" <t...@example.com>','ot...@example.com']) + self.assertEqual(email.message()['To'], '=?utf-8?q?S=C3=BCrname=2C_Firstname?= <t...@example.com>, ot...@example.com') -# Regression for #13259 - Make sure that headers are not changed -# when calling EmailMessage.message() ->>> email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) ->>> message = email.message() ->>> message['From'] -'f...@example.com' ->>> message = email.message() ->>> message['From'] -'f...@example.com' + def test_safe_mime_multipart(self): + """ + Make sure headers can be set with a different encoding than utf-8 in + SafeMIMEMultipart as well + """ + headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} + subject, from_email, to = 'hello', 'f...@example.com', '"Sürname, Firstname" <t...@example.com>' + text_content = 'This is an important message.' + html_content = '<p>This is an <strong>important</strong> message.</p>' + msg = EmailMultiAlternatives('Message from Firstname Sürname', text_content, from_email, [to], headers=headers) + msg.attach_alternative(html_content, "text/html") + msg.encoding = 'iso-8859-1' + self.assertEqual(msg.message()['To'], '=?iso-8859-1?q?S=FCrname=2C_Firstname?= <t...@example.com>') + self.assertEqual(msg.message()['Subject'].encode(), u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?=') -# Regression for #11144 - When a to/from/cc header contains unicode, -# make sure the email addresses are parsed correctly (especially -# with regards to commas) ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['"Firstname Sürname" <t...@example.com>','ot...@example.com']) ->>> email.message()['To'] -'=?utf-8?q?Firstname_S=C3=BCrname?= <t...@example.com>, ot...@example.com' + def test_encoding(self): + """ + Regression for #12791 - Encode body correctly with other encodings + than utf-8 + """ + email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'f...@example.com', ['ot...@example.com']) + email.encoding = 'iso-8859-1' + message = email.message() + self.assertTrue(message.as_string().startswith('Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: f...@example.com\nto: ot...@example.com')) + self.assertEqual(message.get_payload(), 'Firstname S=FCrname is a great guy.') ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['"Sürname, Firstname" <t...@example.com>','ot...@example.com']) ->>> email.message()['To'] -'=?utf-8?q?S=C3=BCrname=2C_Firstname?= <t...@example.com>, ot...@example.com' + # Make sure MIME attachments also works correctly with other encodings than utf-8 + text_content = 'Firstname Sürname is a great guy.' + html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>' + msg = EmailMultiAlternatives('Subject', text_content, 'f...@example.com', ['t...@example.com']) + msg.encoding = 'iso-8859-1' + msg.attach_alternative(html_content, "text/html") + self.assertEqual(msg.message().get_payload(0).as_string(), 'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.') + self.assertEqual(msg.message().get_payload(1).as_string(), 'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>') -# Regression for #6918 - When a header contains unicode, -# make sure headers can be set with a different encoding than utf-8 ->>> email = EmailMessage('Message from Firstname Sürname', 'Content', 'f...@example.com', ['"Sürname, Firstname" <t...@example.com>','ot...@example.com']) ->>> email.encoding = 'iso-8859-1' ->>> email.message()['To'] -'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <t...@example.com>, ot...@example.com' ->>> email.message()['Subject'].encode() -u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?=' + def test_attachments(self): + """Regression test for #9367""" + headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} + subject, from_email, to = 'hello', 'f...@example.com', 't...@example.com' + text_content = 'This is an important message.' + html_content = '<p>This is an <strong>important</strong> message.</p>' + msg = EmailMultiAlternatives(subject, text_content, from_email, [to], headers=headers) + msg.attach_alternative(html_content, "text/html") + msg.attach("an attachment.pdf", "%PDF-1.4.%...", mimetype="application/pdf") + msg_str = msg.message().as_string() + message = email.message_from_string(msg_str) + self.assertTrue(message.is_multipart()) + self.assertEqual(message.get_content_type(), 'multipart/mixed') + self.assertEqual(message.get_default_type(), 'text/plain') + payload = message.get_payload() + self.assertEqual(payload[0].get_content_type(), 'multipart/alternative') + self.assertEqual(payload[1].get_content_type(), 'application/pdf') -# Make sure headers can be set with a different encoding than utf-8 in SafeMIMEMultipart as well ->>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} ->>> subject, from_email, to = 'hello', 'f...@example.com', '"Sürname, Firstname" <t...@example.com>' ->>> text_content = 'This is an important message.' ->>> html_content = '<p>This is an <strong>important</strong> message.</p>' ->>> msg = EmailMultiAlternatives('Message from Firstname Sürname', text_content, from_email, [to], headers=headers) ->>> msg.attach_alternative(html_content, "text/html") ->>> msg.encoding = 'iso-8859-1' ->>> msg.message()['To'] -'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <t...@example.com>' ->>> msg.message()['Subject'].encode() -u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?=' + def test_arbitrary_stream(self): + """ + Test that the console backend can be pointed at an arbitrary stream. + """ + s = StringIO() + connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s) + send_mail('Subject', 'Content', 'f...@example.com', ['t...@example.com'], connection=connection) + self.assertTrue(s.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: f...@example.com\nto: t...@example.com\ndate: ')) -# Regression for #12791 - Encode body correctly with other encodings than utf-8 ->>> email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'f...@example.com', ['ot...@example.com']) ->>> email.encoding = 'iso-8859-1' ->>> message = email.message() ->>> message.as_string() -'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: f...@example.com\nto: ot...@example.com\ndate: ...\nMessage-ID: <...>\n\nFirstname S=FCrname is a great guy.' + def test_stdout(self): + """Make sure that the console backend writes to stdout by default""" + old_stdout = sys.stdout + sys.stdout = StringIO() + connection = console.EmailBackend() + email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + connection.send_messages([email]) + self.assertTrue(sys.stdout.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: f...@example.com\nto: t...@example.com\ndate: ')) + sys.stdout = old_stdout -# Make sure MIME attachments also works correctly with other encodings than utf-8 ->>> text_content = 'Firstname Sürname is a great guy.' ->>> html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>' ->>> msg = EmailMultiAlternatives('Subject', text_content, 'f...@example.com', ['t...@example.com']) ->>> msg.encoding = 'iso-8859-1' ->>> msg.attach_alternative(html_content, "text/html") ->>> msg.message().get_payload(0).as_string() -'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.' ->>> msg.message().get_payload(1).as_string() -'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>' + def test_dummy(self): + """ + Make sure that dummy backends returns correct number of sent messages + """ + connection = dummy.EmailBackend() + email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + self.assertEqual(connection.send_messages([email, email, email]), 3) -# Handle attachments within an multipart/alternative mail correctly (#9367) -# (test is not as precise/clear as it could be w.r.t. email tree structure, -# but it's good enough.) ->>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"} ->>> subject, from_email, to = 'hello', 'f...@example.com', 't...@example.com' ->>> text_content = 'This is an important message.' ->>> html_content = '<p>This is an <strong>important</strong> message.</p>' ->>> msg = EmailMultiAlternatives(subject, text_content, from_email, [to], headers=headers) ->>> msg.attach_alternative(html_content, "text/html") ->>> msg.attach("an attachment.pdf", "%PDF-1.4.%...", mimetype="application/pdf") ->>> print msg.message().as_string() -Content-Type: multipart/mixed; boundary="..." -MIME-Version: 1.0 -Subject: hello -From: f...@example.com -To: t...@example.com -Date: Fri, 09 Nov 2001 01:08:47 -0000 -Message-ID: foo -... -Content-Type: multipart/alternative;... -... -Content-Type: text/plain; charset="utf-8" -MIME-Version: 1.0 -Content-Transfer-Encoding: quoted-printable -... -This is an important message. -... -Content-Type: text/html; charset="utf-8" -MIME-Version: 1.0 -Content-Transfer-Encoding: quoted-printable -... -<p>This is an <strong>important</strong> message.</p> -... -... -Content-Type: application/pdf -MIME-Version: 1.0 -Content-Transfer-Encoding: base64 -Content-Disposition: attachment; filename="an attachment.pdf" -... -JVBERi0xLjQuJS4uLg== -... + def test_locmem(self): + """ + Make sure that the locmen backend populates the outbox. + """ + mail.outbox = [] + connection = locmem.EmailBackend() + email1 = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + email2 = EmailMessage('Subject 2', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + connection.send_messages([email1, email2]) + self.assertEqual(len(mail.outbox), 2) + self.assertEqual(mail.outbox[0].subject, 'Subject') + self.assertEqual(mail.outbox[1].subject, 'Subject 2') + + # Make sure that multiple locmem connections share mail.outbox + mail.outbox = [] + connection2 = locmem.EmailBackend() + email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + connection.send_messages([email]) + connection2.send_messages([email]) + self.assertEqual(len(mail.outbox), 2) -# Make sure that the console backend writes to stdout by default ->>> connection = console.EmailBackend() ->>> email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@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: f...@example.com -To: t...@example.com -Date: ... -Message-ID: ... + def test_file_backend(self): + tmp_dir = tempfile.mkdtemp() + connection = filebased.EmailBackend(file_path=tmp_dir) + email1 = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + connection.send_messages([email1]) + self.assertEqual(len(os.listdir(tmp_dir)), 1) + message = email.message_from_file(open(os.path.join(tmp_dir, os.listdir(tmp_dir)[0]))) + self.assertEqual(message.get_content_type(), 'text/plain') + self.assertEqual(message.get('subject'), 'Subject') + self.assertEqual(message.get('from'), 'f...@example.com') + self.assertEqual(message.get('to'), 't...@example.com') + connection2 = filebased.EmailBackend(file_path=tmp_dir) + connection2.send_messages([email1]) + self.assertEqual(len(os.listdir(tmp_dir)), 2) + connection.send_messages([email1]) + self.assertEqual(len(os.listdir(tmp_dir)), 2) + email1.connection = filebased.EmailBackend(file_path=tmp_dir) + connection_created = connection.open() + email1.send() + self.assertEqual(len(os.listdir(tmp_dir)), 3) + email1.send() + self.assertEqual(len(os.listdir(tmp_dir)), 3) + connection.close() + shutil.rmtree(tmp_dir) -Content -------------------------------------------------------------------------------- -1 + def test_arbitrary_keyword(self): + """ + Make sure that get_connection() accepts arbitrary keyword that might be + used with custom backends. + """ + c = mail.get_connection(fail_silently=True, foo='bar') + self.assertTrue(c.fail_silently) -# Test that the console backend can be pointed at an arbitrary stream ->>> s = StringIO() ->>> connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s) ->>> send_mail('Subject', 'Content', 'f...@example.com', ['t...@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: f...@example.com -To: t...@example.com -Date: ... -Message-ID: ... + def test_custom_backend(self): + """Test custom backend defined in this suite.""" + conn = mail.get_connection('regressiontests.mail.custombackend.EmailBackend') + self.assertTrue(hasattr(conn, 'test_outbox')) + email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) + conn.send_messages([email]) + self.assertEqual(len(conn.test_outbox), 1) -Content -------------------------------------------------------------------------------- + def test_backend_arg(self): + """Test backend argument of mail.get_connection()""" + self.assertTrue(isinstance(mail.get_connection('django.core.mail.backends.smtp.EmailBackend'), smtp.EmailBackend)) + self.assertTrue(isinstance(mail.get_connection('django.core.mail.backends.locmem.EmailBackend'), locmem.EmailBackend)) + self.assertTrue(isinstance(mail.get_connection('django.core.mail.backends.dummy.EmailBackend'), dummy.EmailBackend)) + self.assertTrue(isinstance(mail.get_connection('django.core.mail.backends.console.EmailBackend'), console.EmailBackend)) + tmp_dir = tempfile.mkdtemp() + self.assertTrue(isinstance(mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir), filebased.EmailBackend)) + shutil.rmtree(tmp_dir) + self.assertTrue(isinstance(mail.get_connection(), locmem.EmailBackend)) -# Make sure that dummy backends returns correct number of sent messages ->>> connection = dummy.EmailBackend() ->>> email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) ->>> connection.send_messages([email, email, email]) -3 + def test_connection_arg(self): + """Test connection argument to send_mail(), et. al.""" + connection = mail.get_connection('django.core.mail.backends.locmem.EmailBackend') -# Make sure that locmen backend populates the outbox ->>> mail.outbox = [] ->>> connection = locmem.EmailBackend() ->>> email1 = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) ->>> email2 = EmailMessage('Subject 2', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) ->>> connection.send_messages([email1, email2]) -2 ->>> len(mail.outbox) -2 ->>> mail.outbox[0].subject -'Subject' ->>> mail.outbox[1].subject -'Subject 2' + mail.outbox = [] + send_mail('Subject', 'Content', 'f...@example.com', ['t...@example.com'], connection=connection) + self.assertEqual(len(mail.outbox), 1) + message = mail.outbox[0] + self.assertEqual(message.subject, 'Subject') + self.assertEqual(message.from_email, 'f...@example.com') + self.assertEqual(message.to, ['t...@example.com']) -# Make sure that multiple locmem connections share mail.outbox ->>> mail.outbox = [] ->>> connection1 = locmem.EmailBackend() ->>> connection2 = locmem.EmailBackend() ->>> email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@example.com'}) ->>> connection1.send_messages([email]) -1 ->>> connection2.send_messages([email]) -1 ->>> len(mail.outbox) -2 + mail.outbox = [] + send_mass_mail([ + ('Subject1', 'Content1', 'fr...@example.com', ['t...@example.com']), + ('Subject2', 'Content2', 'fr...@example.com', ['t...@example.com']) + ], connection=connection) + self.assertEqual(len(mail.outbox), 2) + message = mail.outbox[0] + self.assertEqual(message.subject, 'Subject1') + self.assertEqual(message.from_email, 'fr...@example.com') + self.assertEqual(message.to, ['t...@example.com']) + message = mail.outbox[1] + self.assertEqual(message.subject, 'Subject2') + self.assertEqual(message.from_email, 'fr...@example.com') + self.assertEqual(message.to, ['t...@example.com']) -# 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', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@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: f...@example.com -To: t...@example.com -Date: ... -Message-ID: ... + old_admins = settings.ADMINS + old_managers = settings.MANAGERS + settings.ADMINS = settings.MANAGERS = [('nobody','nob...@example.com')] -Content -------------------------------------------------------------------------------- + mail.outbox = [] + mail_admins('Subject', 'Content', connection=connection) + self.assertEqual(len(mail.outbox), 1) + message = mail.outbox[0] + self.assertEqual(message.subject, '[Django] Subject') + self.assertEqual(message.from_email, 'r...@localhost') + self.assertEqual(message.to, ['nob...@example.com']) ->>> 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) + mail.outbox = [] + mail_managers('Subject', 'Content', connection=connection) + self.assertEqual(len(mail.outbox), 1) + message = mail.outbox[0] + self.assertEqual(message.subject, '[Django] Subject') + self.assertEqual(message.from_email, 'r...@localhost') + self.assertEqual(message.to, ['nob...@example.com']) -# 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 + settings.ADMINS = old_admins + settings.MANAGERS = old_managers -# Test custom backend defined in this suite. ->>> conn = mail.get_connection('regressiontests.mail.custombackend.EmailBackend') ->>> hasattr(conn, 'test_outbox') -True ->>> email = EmailMessage('Subject', 'Content', 'bou...@example.com', ['t...@example.com'], headers={'From': 'f...@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.EmailBackend'), smtp.EmailBackend) -True ->>> isinstance(mail.get_connection('django.core.mail.backends.locmem.EmailBackend'), locmem.EmailBackend) -True ->>> isinstance(mail.get_connection('django.core.mail.backends.dummy.EmailBackend'), dummy.EmailBackend) -True ->>> isinstance(mail.get_connection('django.core.mail.backends.console.EmailBackend'), console.EmailBackend) -True ->>> tmp_dir = tempfile.mkdtemp() ->>> isinstance(mail.get_connection('django.core.mail.backends.filebased.EmailBackend', 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.EmailBackend') ->>> send_mail('Subject', 'Content', 'f...@example.com', ['t...@example.com'], connection=connection) -Content-Type: text/plain; charset="utf-8" -MIME-Version: 1.0 -Content-Transfer-Encoding: quoted-printable -Subject: Subject -From: f...@example.com -To: t...@example.com -Date: ... -Message-ID: ... - -Content -------------------------------------------------------------------------------- -1 - ->>> send_mass_mail([ -... ('Subject1', 'Content1', 'fr...@example.com', ['t...@example.com']), -... ('Subject2', 'Content2', 'fr...@example.com', ['t...@example.com']) -... ], connection=connection) -Content-Type: text/plain; charset="utf-8" -MIME-Version: 1.0 -Content-Transfer-Encoding: quoted-printable -Subject: Subject1 -From: fr...@example.com -To: t...@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: fr...@example.com -To: t...@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: r...@localhost -To: nob...@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: r...@localhost -To: nob...@example.com -Date: ... -Message-ID: ... - -Content -------------------------------------------------------------------------------- - ->>> settings.ADMINS = old_admins ->>> settings.MANAGERS = old_managers - -# Add Cc to the email argument list (#7722) - ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com'], cc=['c...@example.com']) ->>> message = email.message() ->>> message['Cc'] -'c...@example.com' ->>> email.recipients() -['t...@example.com', 'c...@example.com'] - ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com','ot...@example.com'], cc=['c...@example.com', 'cc.ot...@example.com']) ->>> message = email.message() ->>> message['Cc'] -'c...@example.com, cc.ot...@example.com' ->>> email.recipients() -['t...@example.com', 'ot...@example.com', 'c...@example.com', 'cc.ot...@example.com'] - ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com','ot...@example.com'], cc=['c...@example.com', 'cc.ot...@example.com'], bcc=['b...@example.com']) ->>> message = email.message() ->>> email.recipients() -['t...@example.com', 'ot...@example.com', 'c...@example.com', 'cc.ot...@example.com', 'b...@example.com'] - ->>> email = EmailMessage('Subject', 'Content', 'f...@example.com', ['t...@example.com'], cc=['c...@example.com']) ->>> message = email.message() ->>> message.as_string() -'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: f...@example.com\nto: t...@example.com\ncc: c...@example.com\ndate: ...\nMessage-ID: <...>\n\nContent' - -""" -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.