Skip to content

Commit

Permalink
Integration tests for email sending.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinabrahms committed Mar 22, 2016
1 parent a3bfa46 commit 1897270
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 9 deletions.
25 changes: 16 additions & 9 deletions StarCellBio/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<h1>You must POST your contact form.</h1>', status=400)

form = ContactForm(request.POST)
if form.is_valid():
clean = form.cleaned_data
try:
send_mail(
'WebFeedback- StarCellBio: {0}...'.format(
clean['note'][:10]
Expand All @@ -428,13 +432,16 @@ def contact(request, **kwargs):
'{0} <{1}>'.format(clean['name'], clean['email']),
[getattr(settings, 'FEEDBACK_EMAIL', '[email protected]')],
)
return HttpResponse('<h1>Thank you for your feedback.')
else:
except Exception:
return HttpResponse(
'<h1>Errors in contact form</h1> {}'.format(
form.errors
)
'<h1>Unable to send the email. Sorry!</h1>', status=500)
return HttpResponse('<h1>Thank you for your feedback.</h1>')
else:
return HttpResponse(
'<h1>Errors in contact form</h1> {}'.format(
form.errors
)
)


# yapf: disable
Expand Down
54 changes: 54 additions & 0 deletions StarCellBio/views_test.py
Original file line number Diff line number Diff line change
@@ -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(['[email protected]'], 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)


1 change: 1 addition & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pudb
yapf
selenium==2.35.0
bok-choy==0.4.10
mock
-r requirements.txt
21 changes: 21 additions & 0 deletions test_contact_form.sh
Original file line number Diff line number Diff line change
@@ -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 protected]\
&report=This is a test for errors\'
&note=This is my note" "$URL"

0 comments on commit 1897270

Please sign in to comment.