diff 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
line wrap: on
line diff
--- a/contact/tests/test_views.py	Mon Jan 01 10:39:48 2018 -0600
+++ b/contact/tests/test_views.py	Sat Apr 14 13:53:05 2018 -0500
@@ -2,7 +2,9 @@
 Unit tests for the contact application views.
 
 """
+from mock import patch, Mock
 from django.test import TestCase
+from django.conf import settings
 from django.core.urlresolvers import reverse
 from django.core import mail
 
@@ -10,9 +12,16 @@
 class BaseTestCase(TestCase):
     """Simple tests to ensure basic functionality."""
 
-    def test_usage(self):
-        url = reverse('contact-form')
-        response = self.client.get(url)
+    def setUp(self):
+        self.url = reverse('contact-form')
+
+    @patch('contact.forms.requests.post')
+    def test_usage(self, post_mock):
+        post_response = Mock()
+        post_response.json.return_value = {'success': True}
+        post_mock.return_value = post_response
+
+        response = self.client.get(self.url)
         self.assertEqual(response.status_code, 200)
 
         post_data = {
@@ -20,8 +29,9 @@
             'email': 'jdoe@example.com',
             'subject': 'Test message',
             'message': 'Testing contact form.',
+            'g-recaptcha-response': 'crazy-google-string',
         }
-        response = self.client.post(url, data=post_data, follow=True)
+        response = self.client.post(self.url, data=post_data, follow=True)
         self.assertRedirects(response, reverse('contact-thanks'))
 
         self.assertEqual(len(mail.outbox), 1)
@@ -36,8 +46,13 @@
         self.assertTrue(post_data['email'] in msg)
         self.assertTrue(post_data['message'] in msg)
 
+        post_mock.assert_called_once_with(settings.RECAPTCHA_URL, data={
+            'secret': settings.RECAPTCHA_SECRET_KEY,
+            'response': 'crazy-google-string',
+            'remoteip': '127.0.0.1',
+        })
+
     def test_honeypot(self):
-        url = reverse('contact-form')
         post_data = {
             'name': 'John Doe',
             'email': 'jdoe@example.com',
@@ -45,6 +60,34 @@
             'message': 'Testing contact form.',
             'honeypot': 'some spam',
         }
-        response = self.client.post(url, data=post_data)
+        response = self.client.post(self.url, data=post_data)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(len(mail.outbox), 0)
+
+    def test_no_captcha_response(self):
+        post_data = {
+            'name': 'John Doe',
+            'email': 'jdoe@example.com',
+            'subject': 'Test message',
+            'message': 'Testing contact form.',
+        }
+        response = self.client.post(self.url, data=post_data, follow=True)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(mail.outbox), 0)
+
+    @patch('contact.forms.requests.post')
+    def test_captcha_failure(self, post_mock):
+        post_response = Mock()
+        post_response.json.return_value = {'success': False}
+        post_mock.return_value = post_response
+
+        post_data = {
+            'name': 'John Doe',
+            'email': 'jdoe@example.com',
+            'subject': 'Test message',
+            'message': 'Testing contact form.',
+            'g-recaptcha-response': 'crazy-google-string',
+        }
+        response = self.client.post(self.url, data=post_data, follow=True)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(mail.outbox), 0)