-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration tests for email sending.
- Loading branch information
1 parent
a3bfa46
commit 1897270
Showing
4 changed files
with
92 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ pudb | |
yapf | ||
selenium==2.35.0 | ||
bok-choy==0.4.10 | ||
mock | ||
-r requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\' | ||
¬e=This is my note" "$URL" |