use fake mail everywhere when SEND_REAL_EMAIL=False (bug 768916)
This commit is contained in:
Родитель
24903c76b7
Коммит
cfc50e509f
|
@ -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))
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче