annotate contact/tests/test_views.py @ 1200:b9514abc2a67

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