From 583342888154129190fc3da7a148f8c524cfb128 Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Mon, 8 Nov 2010 08:47:36 +0000 Subject: [PATCH] Merged [2639] from fenner@fenron.net: Add regression tests (and accompanying test mode) for utils.mail. Fixes #538 - Legacy-Id: 2645 Note: SVN reference [2639] has been migrated to Git commit 14de6870df426f18afaf90af2c74a5009cca3c1f --- ietf/templates/test/mail_body.txt | 1 + ietf/templates/test/mail_subject.txt | 3 +++ ietf/utils/mail.py | 28 +++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 ietf/templates/test/mail_body.txt create mode 100644 ietf/templates/test/mail_subject.txt diff --git a/ietf/templates/test/mail_body.txt b/ietf/templates/test/mail_body.txt new file mode 100644 index 000000000..9c4322ce7 --- /dev/null +++ b/ietf/templates/test/mail_body.txt @@ -0,0 +1 @@ +template {{ value }} rendered with a good {{ thing }} diff --git a/ietf/templates/test/mail_subject.txt b/ietf/templates/test/mail_subject.txt new file mode 100644 index 000000000..4d07132b1 --- /dev/null +++ b/ietf/templates/test/mail_subject.txt @@ -0,0 +1,3 @@ +you can have +{{ unsafe|safe }} +in your context diff --git a/ietf/utils/mail.py b/ietf/utils/mail.py index 35095b748..c191f4756 100644 --- a/ietf/utils/mail.py +++ b/ietf/utils/mail.py @@ -14,6 +14,19 @@ from ietf.utils import log import sys import time +# Testing mode: +# import ietf.utils.mail +# ietf.utils.mail.test_mode = True +# ... send some mail ... +# ... inspect ietf.utils.mail.outbox ... +# ... call ietf.utils.mail.empty_outbox() ... +test_mode=False +outbox=[] + +def empty_outbox(): + global outbox + outbox = [] + def add_headers(msg): if not(msg.has_key('Message-ID')): msg['Message-ID'] = make_msgid('idtracker') @@ -29,6 +42,9 @@ def send_smtp(msg, bcc=None): The destination list will be taken from the To:/Cc: headers in the Message. The From address will be used if present or will default to the django setting DEFAULT_FROM_EMAIL + + If someone has set test_mode=True, then just append the msg to + the outbox. ''' add_headers(msg) (fname, frm) = parseaddr(msg.get('From')) @@ -36,6 +52,9 @@ def send_smtp(msg, bcc=None): if bcc: addrlist += [bcc] to = [addr for name, addr in getaddresses(addrlist)] + if test_mode: + outbox.append((msg, to, msg.as_string())) + return server = None try: server = smtplib.SMTP() @@ -73,9 +92,6 @@ def copy_email(msg, to, toUser=False): Send a copy of the given email message to the given recipient. ''' add_headers(msg) - # Overwrite the From: header, so that the copy from a development or - # test server doesn't look like spam. - msg['From'] = settings.DEFAULT_FROM_EMAIL new = MIMEMultipart() # get info for first part. # Mode: if it's production, then "copy of a message", otherwise @@ -91,7 +107,9 @@ def copy_email(msg, to, toUser=False): explanation = "The attached message would have been sent, but the tracker is in %s mode.\nIt was not sent to anybody." % settings.SERVER_MODE new.attach(MIMEText(explanation + "\n\n")) new.attach(MIMEMessage(msg)) - new['From'] = msg['From'] + # Overwrite the From: header, so that the copy from a development or + # test server doesn't look like spam. + new['From'] = settings.DEFAULT_FROM_EMAIL new['Subject'] = '[Django %s] %s' % (settings.SERVER_MODE, msg.get('Subject', '[no subject]')) new['To'] = to send_smtp(new) @@ -148,7 +166,7 @@ def send_mail_mime(request, to, frm, subject, msg, cc=None, extra=None, toUser=N if extra: for k, v in extra.iteritems(): msg[k] = v - if settings.SERVER_MODE == 'production': + if test_mode or settings.SERVER_MODE == 'production': send_smtp(msg, bcc) elif settings.SERVER_MODE == 'test': if toUser: