From d999b5fa73507830dd3540fe250453ed36840495 Mon Sep 17 00:00:00 2001
From: echoduck <bekaluteshome@gmail.com>
Date: Oct 26 2018 21:23:45 +0000
Subject: [PATCH 1/2] Fixed failing outbox tests by replacing send_html_mail Celery task with a Celery email backend


---

diff --git a/happinesspackets/messaging/models.py b/happinesspackets/messaging/models.py
index c11d1a0..e7890f9 100644
--- a/happinesspackets/messaging/models.py
+++ b/happinesspackets/messaging/models.py
@@ -80,7 +80,7 @@ class Message(TimeStampedModel):
         subject = ' '.join(subject.splitlines())
         body_txt = render_to_string('messaging/sender_confirmation_mail.txt', context)
         body_html = render_to_string('messaging/sender_confirmation_mail.html', context)
-        send_html_mail.delay(subject, body_txt, body_html, self.sender_email)
+        send_html_mail(subject, body_txt, body_html, self.sender_email)
         self.save()
 
     def send_to_recipient(self, use_https, domain):
@@ -103,7 +103,7 @@ class Message(TimeStampedModel):
         subject = ' '.join(subject.splitlines())
         body_txt = render_to_string('messaging/recipient_mail.txt', context)
         body_html = render_to_string('messaging/recipient_mail.html', context)
-        send_html_mail.delay(subject, body_txt, body_html, self.recipient_email)
+        send_html_mail(subject, body_txt, body_html, self.recipient_email)
         self.save()
 
 
diff --git a/happinesspackets/settings/base.py b/happinesspackets/settings/base.py
index 249badd..c7269a4 100644
--- a/happinesspackets/settings/base.py
+++ b/happinesspackets/settings/base.py
@@ -50,7 +50,6 @@ MESSAGE_TAGS = {
     messages.ERROR: 'danger',
 }
 
-
 MEDIA_ROOT = PROJECT_DIR.child('media')
 MEDIA_URL = '/media/'
 STATIC_ROOT = PROJECT_DIR.child('static')
@@ -116,8 +115,12 @@ INSTALLED_APPS = [
     'django_extensions',
     'crispy_forms',
     'happinesspackets.messaging',
+    'djcelery_email',
 ]
 
+
+EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'
+
 AUTHENTICATION_BACKENDS = (
     'happinesspackets.messaging.auth.OIDC',
 )
diff --git a/happinesspackets/settings/dev.py b/happinesspackets/settings/dev.py
index a7ccc0b..7b37258 100644
--- a/happinesspackets/settings/dev.py
+++ b/happinesspackets/settings/dev.py
@@ -15,7 +15,7 @@ DATABASES = {
     }
 }
 
-EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" 
+CELERY_EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" 
 
 
 SECRET_KEY = 'only-for-testing'
diff --git a/happinesspackets/tasks.py b/happinesspackets/tasks.py
index 989bab0..396a924 100644
--- a/happinesspackets/tasks.py
+++ b/happinesspackets/tasks.py
@@ -1,12 +1,10 @@
 import logging
 
-from happinesspackets._celery import app
 from email.mime.image import MIMEImage
 from django.conf import settings
 from django.core.mail import EmailMultiAlternatives
 
 
-@app.task
 def send_html_mail(subject, body_txt, body_html, recipient):
     message = EmailMultiAlternatives(subject, body_txt, settings.DEFAULT_FROM_EMAIL, [recipient])
     message.attach_alternative(body_html, 'text/html')
diff --git a/requirements/base.txt b/requirements/base.txt
index 77fcef5..31cec1a 100644
--- a/requirements/base.txt
+++ b/requirements/base.txt
@@ -35,3 +35,4 @@ opbeat==3.3
 mozilla-django-oidc==1.0.0
 fedmsg==1.1.1
 celery[redis]==4.2.1
+django-celery-email==2.0.1

From 07b8fa04d532280b422c18b37e3519e55d4eddc2 Mon Sep 17 00:00:00 2001
From: echoduck <bekaluteshome@gmail.com>
Date: Oct 26 2018 21:23:45 +0000
Subject: [PATCH 2/2] Changed testing backend to ModelBackend to prevent 302 redirects, and fixed ratelimiting and blacklisting validation


---

diff --git a/happinesspackets/messaging/forms.py b/happinesspackets/messaging/forms.py
index 8b296e8..0861170 100644
--- a/happinesspackets/messaging/forms.py
+++ b/happinesspackets/messaging/forms.py
@@ -34,6 +34,7 @@ class MessageSendForm(forms.ModelForm):
                   'sender_named', 'sender_approved_public', 'sender_approved_public_named']
 
     def __init__(self, *args, **kwargs):
+        self.user = kwargs.pop('user')
         super(MessageSendForm, self).__init__(*args, **kwargs)
         self.helper = FormHelper()
         self.helper.form_class = 'form-horizontal'
@@ -66,7 +67,7 @@ class MessageSendForm(forms.ModelForm):
             self.add_error('sender_approved_public_named', "If you want us to publish the message including your names, "
                                                            "you must also check 'I agree to publish this message and"
                                                            "display it publicly in the Happiness Archive'")
-
+        validate_email(self.user.email)
 
 class MessageRecipientForm(forms.ModelForm):
     class Meta:
diff --git a/happinesspackets/messaging/tests/test_views.py b/happinesspackets/messaging/tests/test_views.py
index e104640..2d1c364 100644
--- a/happinesspackets/messaging/tests/test_views.py
+++ b/happinesspackets/messaging/tests/test_views.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
 
 from django.conf import settings
 from django.core import mail
+from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
 from django.test import TestCase
 from django.utils.crypto import salted_hmac
@@ -155,6 +156,9 @@ class SendViewTest(TestCase):
             'sender_approved_public': True,
             'sender_approved_public_named': True,
         }
+        self.user = User.objects.create_user("erikio", "sender@erik.io", "helloworld", first_name="Erik",last_name="Doe")
+        self.user.save()
+        self.client.login(username = "erikio", password = "helloworld")
 
     def test_renders(self):
         response = self.client.get(self.url)
@@ -179,14 +183,14 @@ class SendViewTest(TestCase):
         self.assertEqual(len(mail.outbox), 0)
 
     def test_post_blacklisted_sender(self):
-        BlacklistedEmailFactory(email='sender@erik.io', stripped_email='sender@erikio')
+        BlacklistedEmailFactory(email='SEN.DER+FOOBAR@erik.io', stripped_email='sender@erikio')
         response = self.client.post(self.url, self.post_data)
         self.assertRedirects(response, reverse('messaging:sender_confirmation_sent'))
         self.assertEqual(len(mail.outbox), 0)
 
     def test_post_ratelimited_sender(self):
         for i in range(settings.MAX_MESSAGES + 1):
-            MessageModelFactory(sender_email='sender@erik.io', sender_email_stripped='sender@erikio')
+            MessageModelFactory(sender_email='SEN.DER+FOOBAR@erik.io', sender_email_stripped='sender@erikio')
         response = self.client.post(self.url, self.post_data)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(response.context['form'].errors), 1)
@@ -194,7 +198,7 @@ class SendViewTest(TestCase):
 
     def test_post_ratelimited_recipient(self):
         for i in range(settings.MAX_MESSAGES + 1):
-            MessageModelFactory(recipient_email='sender@erik.io', recipient_email_stripped='recipient@erikio')
+            MessageModelFactory(recipient_email='recipient@erik.io', recipient_email_stripped='recipient@erikio')
         response = self.client.post(self.url, self.post_data)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(response.context['form'].errors), 1)
diff --git a/happinesspackets/messaging/views.py b/happinesspackets/messaging/views.py
index 92754a4..3789893 100644
--- a/happinesspackets/messaging/views.py
+++ b/happinesspackets/messaging/views.py
@@ -90,6 +90,11 @@ class MessageSendView(LoginRequiredMixin, FormView):
     def dispatch(self, *args, **kwargs):
         return super(MessageSendView, self).dispatch(*args, **kwargs)
 
+    def get_form_kwargs(self):
+        kwargs = super(MessageSendView, self).get_form_kwargs()
+        kwargs['user'] = self.request.user
+        return kwargs
+
     def form_valid(self, form):
         message = form.save(commit=False)
         message.sender_ip = self.request.META['REMOTE_ADDR']
diff --git a/happinesspackets/settings/tsting.py b/happinesspackets/settings/tsting.py
index 1421d8c..0ec3596 100644
--- a/happinesspackets/settings/tsting.py
+++ b/happinesspackets/settings/tsting.py
@@ -9,3 +9,8 @@ from .dev import *  # noqa
 PASSWORD_HASHERS = (
     'django.contrib.auth.hashers.MD5PasswordHasher',
 )
+
+AUTHENTICATION_BACKENDS = (
+    'django.contrib.auth.backends.ModelBackend',
+)
+

