comparison contact/tests/test_views.py @ 1172:b957e4829a03

Add reCAPTCHA to contact form
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Apr 2018 13:53:05 -0500
parents 79a71b9d0a2a
children
comparison
equal deleted inserted replaced
1170:b213e4b333bb 1172:b957e4829a03
1 """ 1 """
2 Unit tests for the contact application views. 2 Unit tests for the contact application views.
3 3
4 """ 4 """
5 from mock import patch, Mock
5 from django.test import TestCase 6 from django.test import TestCase
7 from django.conf import settings
6 from django.core.urlresolvers import reverse 8 from django.core.urlresolvers import reverse
7 from django.core import mail 9 from django.core import mail
8 10
9 11
10 class BaseTestCase(TestCase): 12 class BaseTestCase(TestCase):
11 """Simple tests to ensure basic functionality.""" 13 """Simple tests to ensure basic functionality."""
12 14
13 def test_usage(self): 15 def setUp(self):
14 url = reverse('contact-form') 16 self.url = reverse('contact-form')
15 response = self.client.get(url) 17
18 @patch('contact.forms.requests.post')
19 def test_usage(self, post_mock):
20 post_response = Mock()
21 post_response.json.return_value = {'success': True}
22 post_mock.return_value = post_response
23
24 response = self.client.get(self.url)
16 self.assertEqual(response.status_code, 200) 25 self.assertEqual(response.status_code, 200)
17 26
18 post_data = { 27 post_data = {
19 'name': 'John Doe', 28 'name': 'John Doe',
20 'email': 'jdoe@example.com', 29 'email': 'jdoe@example.com',
21 'subject': 'Test message', 30 'subject': 'Test message',
22 'message': 'Testing contact form.', 31 'message': 'Testing contact form.',
32 'g-recaptcha-response': 'crazy-google-string',
23 } 33 }
24 response = self.client.post(url, data=post_data, follow=True) 34 response = self.client.post(self.url, data=post_data, follow=True)
25 self.assertRedirects(response, reverse('contact-thanks')) 35 self.assertRedirects(response, reverse('contact-thanks'))
26 36
27 self.assertEqual(len(mail.outbox), 1) 37 self.assertEqual(len(mail.outbox), 1)
28 email = mail.outbox[0] 38 email = mail.outbox[0]
29 self.assertEqual(len(email.recipients()), 1) 39 self.assertEqual(len(email.recipients()), 1)
34 msg = email.message().as_string() 44 msg = email.message().as_string()
35 self.assertTrue(post_data['name'] in msg) 45 self.assertTrue(post_data['name'] in msg)
36 self.assertTrue(post_data['email'] in msg) 46 self.assertTrue(post_data['email'] in msg)
37 self.assertTrue(post_data['message'] in msg) 47 self.assertTrue(post_data['message'] in msg)
38 48
49 post_mock.assert_called_once_with(settings.RECAPTCHA_URL, data={
50 'secret': settings.RECAPTCHA_SECRET_KEY,
51 'response': 'crazy-google-string',
52 'remoteip': '127.0.0.1',
53 })
54
39 def test_honeypot(self): 55 def test_honeypot(self):
40 url = reverse('contact-form')
41 post_data = { 56 post_data = {
42 'name': 'John Doe', 57 'name': 'John Doe',
43 'email': 'jdoe@example.com', 58 'email': 'jdoe@example.com',
44 'subject': 'Test message', 59 'subject': 'Test message',
45 'message': 'Testing contact form.', 60 'message': 'Testing contact form.',
46 'honeypot': 'some spam', 61 'honeypot': 'some spam',
47 } 62 }
48 response = self.client.post(url, data=post_data) 63 response = self.client.post(self.url, data=post_data)
49 self.assertEqual(response.status_code, 200) 64 self.assertEqual(response.status_code, 200)
50 self.assertEqual(len(mail.outbox), 0) 65 self.assertEqual(len(mail.outbox), 0)
66
67 def test_no_captcha_response(self):
68 post_data = {
69 'name': 'John Doe',
70 'email': 'jdoe@example.com',
71 'subject': 'Test message',
72 'message': 'Testing contact form.',
73 }
74 response = self.client.post(self.url, data=post_data, follow=True)
75 self.assertEqual(response.status_code, 200)
76 self.assertEqual(len(mail.outbox), 0)
77
78 @patch('contact.forms.requests.post')
79 def test_captcha_failure(self, post_mock):
80 post_response = Mock()
81 post_response.json.return_value = {'success': False}
82 post_mock.return_value = post_response
83
84 post_data = {
85 'name': 'John Doe',
86 'email': 'jdoe@example.com',
87 'subject': 'Test message',
88 'message': 'Testing contact form.',
89 'g-recaptcha-response': 'crazy-google-string',
90 }
91 response = self.client.post(self.url, data=post_data, follow=True)
92 self.assertEqual(response.status_code, 200)
93 self.assertEqual(len(mail.outbox), 0)