From cfc50e509f8fcdac4834c0184550568b50c082a3 Mon Sep 17 00:00:00 2001 From: Chris Van Date: Fri, 20 Jul 2012 01:29:59 -0700 Subject: [PATCH] use fake mail everywhere when SEND_REAL_EMAIL=False (bug 768916) --- apps/addons/management/commands/jetpackers.py | 7 ++-- apps/amo/mail.py | 19 ++++++----- apps/amo/models.py | 7 ++++ apps/amo/tests/test_send_mail.py | 33 ++++++++++++------- apps/amo/utils.py | 4 ++- apps/reviews/views.py | 4 +-- 6 files changed, 45 insertions(+), 29 deletions(-) diff --git a/apps/addons/management/commands/jetpackers.py b/apps/addons/management/commands/jetpackers.py index fefbecad59..0784261b13 100644 --- a/apps/addons/management/commands/jetpackers.py +++ b/apps/addons/management/commands/jetpackers.py @@ -15,11 +15,10 @@ class Command(BaseCommand): help = "Send the email for bug 662571" def handle(self, *args, **options): - cxn = amo.utils.get_email_backend() - sendmail(cxn) + sendmail() -def sendmail(cxn): +def sendmail(): addrs = set(UserProfile.objects.values_list('email', flat=True) # whoa .filter(addons__versions__files__jetpack_version__isnull=False)) @@ -28,7 +27,7 @@ def sendmail(cxn): for addr in addrs: count += 1 try: - mail.send_mail(SUBJECT, MSG, FROM, [addr], connection=cxn) + mail.send_mail(SUBJECT, MSG, FROM, [addr]) log.info('%s. DONE: %s' % (count, addr)) except Exception, e: log.info('%s. FAIL: %s (%s)' % (count, addr, e)) diff --git a/apps/amo/mail.py b/apps/amo/mail.py index b0f866cd1d..d3038fca71 100644 --- a/apps/amo/mail.py +++ b/apps/amo/mail.py @@ -3,28 +3,29 @@ import logging from django.core.mail.backends.base import BaseEmailBackend from django.db import models -from amo.models import ModelBase +from amo.models import FakeEmail log = logging.getLogger('z.amo.mail') -class FakeEmail(ModelBase): - message = models.TextField() - - class Meta: - db_table = 'fake_email' - - class FakeEmailBackend(BaseEmailBackend): + """ + Used for development environments when we don't want to send out + real emails. This gets swapped in as the email backend when + `settings.SEND_REAL_EMAIL` is disabled. + """ def send_messages(self, messages): + """Sends a list of messages (saves `FakeEmail` objects).""" log.debug('Sending fake mail.') for msg in messages: FakeEmail.objects.create(message=msg.message().as_string()) + return len(messages) def view_all(self): + """Useful for displaying messages in admin panel.""" return (FakeEmail.objects.values_list('message', flat=True) - .order_by('id')) + .order_by('-id')) def clear(self): return FakeEmail.objects.all().delete() diff --git a/apps/amo/models.py b/apps/amo/models.py index 440b47f498..cf201963f6 100644 --- a/apps/amo/models.py +++ b/apps/amo/models.py @@ -379,3 +379,10 @@ class BlobField(models.Field): def db_type(self, **kw): return 'blob' + + +class FakeEmail(ModelBase): + message = models.TextField() + + class Meta: + db_table = 'fake_email' diff --git a/apps/amo/tests/test_send_mail.py b/apps/amo/tests/test_send_mail.py index 6c2dac66e7..3c972e2ad1 100644 --- a/apps/amo/tests/test_send_mail.py +++ b/apps/amo/tests/test_send_mail.py @@ -7,12 +7,13 @@ from django.utils import translation import mock from nose.tools import eq_ +from amo.models import FakeEmail from amo.utils import send_mail from users.models import UserProfile, UserNotification import users.notifications -class SendMailTest(test.TestCase): +class TestSendMail(test.TestCase): fixtures = ['base/users'] def setUp(self): @@ -82,7 +83,7 @@ class SendMailTest(test.TestCase): eq_(len(mail.outbox), 1) def test_user_mandatory(self): - """ Make sure there's no unsubscribe link in mandatory emails. """ + # Make sure there's no unsubscribe link in mandatory emails. user = UserProfile.objects.all()[0] to = user.email n = users.notifications.NOTIFICATIONS_BY_SHORT['individual_contact'] @@ -106,7 +107,7 @@ class SendMailTest(test.TestCase): UserNotification.objects.get_or_create(notification_id=n.id, user=user, enabled=False) - # Confirm we're reading from the database + # Confirm we're reading from the database. eq_(UserNotification.objects.filter(notification_id=n.id).count(), 1) success = send_mail('test subject', 'test body', perm_setting='reply', @@ -115,16 +116,24 @@ class SendMailTest(test.TestCase): assert success, "Email wasn't sent" eq_(len(mail.outbox), 0) - def test_success(self): - to = 'nobody@mozilla.org' - settings.EMAIL_BLACKLIST = () - success = send_mail('test subject', 'test body', - recipient_list=[to], fail_silently=False) - - assert success + @mock.patch.object(settings, 'EMAIL_BLACKLIST', ()) + def test_success_real_mail(self): + assert send_mail('test subject', 'test body', + recipient_list=['nobody@mozilla.org'], + fail_silently=False) eq_(len(mail.outbox), 1) - assert mail.outbox[0].subject.find('test subject') == 0 - assert mail.outbox[0].body.find('test body') == 0 + eq_(mail.outbox[0].subject.find('test subject'), 0) + eq_(mail.outbox[0].body.find('test body'), 0) + + @mock.patch.object(settings, 'EMAIL_BLACKLIST', ()) + @mock.patch.object(settings, 'SEND_REAL_EMAIL', False) + def test_success_fake_mail(self): + assert send_mail('test subject', 'test body', + recipient_list=['nobody@mozilla.org'], + fail_silently=False) + eq_(len(mail.outbox), 0) + eq_(FakeEmail.objects.count(), 1) + eq_(FakeEmail.objects.get().message.endswith('test body'), True) @mock.patch('amo.utils.Context') def test_dont_localize(self, fake_Context): diff --git a/apps/amo/utils.py b/apps/amo/utils.py index ea5bbe7402..5377917352 100644 --- a/apps/amo/utils.py +++ b/apps/amo/utils.py @@ -139,7 +139,7 @@ def paginate(request, queryset, per_page=20, count=None): def send_mail(subject, message, from_email=None, recipient_list=None, fail_silently=False, use_blacklist=True, perm_setting=None, - manage_url=None, headers=None, connection=None, cc=None): + manage_url=None, headers=None, cc=None): """ A wrapper around django.core.mail.EmailMessage. @@ -150,6 +150,8 @@ def send_mail(subject, message, from_email=None, recipient_list=None, if not recipient_list: return True + connection = get_email_backend() + if not from_email: from_email = settings.DEFAULT_FROM_EMAIL diff --git a/apps/reviews/views.py b/apps/reviews/views.py index 838805d315..eb6d880a2f 100644 --- a/apps/reviews/views.py +++ b/apps/reviews/views.py @@ -27,12 +27,10 @@ addon_view = addon_view_factory(qs=Addon.objects.valid) def send_mail(template, subject, emails, context, perm_setting): - cxn = amo.utils.get_email_backend() template = loader.get_template(template) amo.utils.send_mail(subject, template.render(Context(context, autoescape=False)), - recipient_list=emails, perm_setting=perm_setting, - connection=cxn) + recipient_list=emails, perm_setting=perm_setting) @addon_view