From 1897270d23561f9b5891d15b616e1ba110a1a2d4 Mon Sep 17 00:00:00 2001 From: Justin Abrahms Date: Mon, 14 Mar 2016 16:13:44 -0400 Subject: [PATCH] Integration tests for email sending. --- StarCellBio/views.py | 25 +++++++++++------- StarCellBio/views_test.py | 54 +++++++++++++++++++++++++++++++++++++++ requirements.dev.txt | 1 + test_contact_form.sh | 21 +++++++++++++++ 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 StarCellBio/views_test.py create mode 100755 test_contact_form.sh diff --git a/StarCellBio/views.py b/StarCellBio/views.py index 51a31895..cccf9295 100644 --- a/StarCellBio/views.py +++ b/StarCellBio/views.py @@ -416,10 +416,14 @@ def post_state(request, **kwargs): def contact(request, **kwargs): """Contact form handler.""" - if request.method == "POST": - form = ContactForm(request.POST) - if form.is_valid(): - clean = form.cleaned_data + if request.method != "POST": + return HttpResponse( + '

You must POST your contact form.

', status=400) + + form = ContactForm(request.POST) + if form.is_valid(): + clean = form.cleaned_data + try: send_mail( 'WebFeedback- StarCellBio: {0}...'.format( clean['note'][:10] @@ -428,13 +432,16 @@ def contact(request, **kwargs): '{0} <{1}>'.format(clean['name'], clean['email']), [getattr(settings, 'FEEDBACK_EMAIL', 'star@mit.edu')], ) - return HttpResponse('

Thank you for your feedback.') - else: + except Exception: return HttpResponse( - '

Errors in contact form

{}'.format( - form.errors - ) + '

Unable to send the email. Sorry!

', status=500) + return HttpResponse('

Thank you for your feedback.

') + else: + return HttpResponse( + '

Errors in contact form

{}'.format( + form.errors ) + ) # yapf: disable diff --git a/StarCellBio/views_test.py b/StarCellBio/views_test.py new file mode 100644 index 00000000..a3f1a773 --- /dev/null +++ b/StarCellBio/views_test.py @@ -0,0 +1,54 @@ +from django.core.urlresolvers import reverse +from django.test import TestCase, RequestFactory +from django.core import mail +import mock + +from .views import contact + + +class ContactTests(TestCase): + def test_400_on_GET(self): + factory = RequestFactory() + request = factory.get(reverse('contact')) + response = contact(request) + + self.assertEqual(response.status_code, 400) + self.assertEqual(len(mail.outbox), 0) + + def test_error_on_invalid_form(self): + factory = RequestFactory() + request = factory.post(reverse('contact')) + response = contact(request) + + self.assertContains(response, 'Errors in contact form') + self.assertEqual(len(mail.outbox), 0) + + def test_sends_email_on_valid_form(self): + factory = RequestFactory() + request = factory.post(reverse('contact'), data={ + 'report': 'There was an error', + 'note': 'this is my note', + }) + response = contact(request) + + self.assertContains(response, 'Thank you for your feedback') + self.assertEqual(len(mail.outbox), 1) + email = mail.outbox[0] + self.assertIn('WebFeedback', email.subject) + self.assertEqual(['star@mit.edu'], email.to) + + def test_sends_error_on_sendmail_exception(self): + factory = RequestFactory() + request = factory.post(reverse('contact'), data={ + 'report': 'There was an error', + 'note': 'this is my note', + }) + with mock.patch('StarCellBio.views.send_mail') as mailer: + mailer.side_effect = Exception() + + response = contact(request) + + self.assertEqual(500, response.status_code) + self.assertTrue('Unable to send the email' in response.content) + + diff --git a/requirements.dev.txt b/requirements.dev.txt index 8e9f4ed5..29889792 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -6,4 +6,5 @@ pudb yapf selenium==2.35.0 bok-choy==0.4.10 +mock -r requirements.txt diff --git a/test_contact_form.sh b/test_contact_form.sh new file mode 100755 index 00000000..154c1e16 --- /dev/null +++ b/test_contact_form.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# Ensures that the contact form works. Provide it the url of the +# contact form, or the server itself, and we'll test the contact form +# works. +set -euf -o pipefail +URL=${1:-} + +if [[ -z "$URL" ]]; then + >&2 echo "usage: $0 URL" + exit 1 +fi + +if [[ "$URL" != *"contact"* ]]; then + # Assuming bare URL (eg: localhost:8000) was passed + URL="$URL/scb/contact" +fi + +curl -v -X POST -d "name=Justin\ +&email=abrahms@mit.edu\ +&report=This is a test for errors\' +¬e=This is my note" "$URL"