bgneal@819: """ bgneal@819: Unit tests for the contact application views. bgneal@819: bgneal@819: """ bgneal@1172: from mock import patch, Mock bgneal@819: from django.test import TestCase bgneal@1172: from django.conf import settings bgneal@819: from django.core.urlresolvers import reverse bgneal@819: from django.core import mail bgneal@819: bgneal@819: bgneal@819: class BaseTestCase(TestCase): bgneal@819: """Simple tests to ensure basic functionality.""" bgneal@819: bgneal@1172: def setUp(self): bgneal@1172: self.url = reverse('contact-form') bgneal@1172: bgneal@1172: @patch('contact.forms.requests.post') bgneal@1172: def test_usage(self, post_mock): bgneal@1172: post_response = Mock() bgneal@1172: post_response.json.return_value = {'success': True} bgneal@1172: post_mock.return_value = post_response bgneal@1172: bgneal@1172: response = self.client.get(self.url) bgneal@819: self.assertEqual(response.status_code, 200) bgneal@819: bgneal@819: post_data = { bgneal@819: 'name': 'John Doe', bgneal@819: 'email': 'jdoe@example.com', bgneal@819: 'subject': 'Test message', bgneal@819: 'message': 'Testing contact form.', bgneal@1172: 'g-recaptcha-response': 'crazy-google-string', bgneal@819: } bgneal@1172: response = self.client.post(self.url, data=post_data, follow=True) bgneal@819: self.assertRedirects(response, reverse('contact-thanks')) bgneal@819: bgneal@819: self.assertEqual(len(mail.outbox), 1) bgneal@819: email = mail.outbox[0] bgneal@819: self.assertEqual(len(email.recipients()), 1) bgneal@892: self.assertEqual(email.extra_headers['Reply-To'], post_data['email']) bgneal@892: self.assertEqual(email.from_email, 'no_reply@example.com') bgneal@819: self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com') bgneal@819: self.assertTrue(post_data['subject'] in email.subject) bgneal@819: msg = email.message().as_string() bgneal@819: self.assertTrue(post_data['name'] in msg) bgneal@819: self.assertTrue(post_data['email'] in msg) bgneal@819: self.assertTrue(post_data['message'] in msg) bgneal@819: bgneal@1172: post_mock.assert_called_once_with(settings.RECAPTCHA_URL, data={ bgneal@1172: 'secret': settings.RECAPTCHA_SECRET_KEY, bgneal@1172: 'response': 'crazy-google-string', bgneal@1172: 'remoteip': '127.0.0.1', bgneal@1172: }) bgneal@1172: bgneal@819: def test_honeypot(self): bgneal@819: post_data = { bgneal@819: 'name': 'John Doe', bgneal@819: 'email': 'jdoe@example.com', bgneal@819: 'subject': 'Test message', bgneal@819: 'message': 'Testing contact form.', bgneal@819: 'honeypot': 'some spam', bgneal@819: } bgneal@1172: response = self.client.post(self.url, data=post_data) bgneal@819: self.assertEqual(response.status_code, 200) bgneal@819: self.assertEqual(len(mail.outbox), 0) bgneal@1172: bgneal@1172: def test_no_captcha_response(self): bgneal@1172: post_data = { bgneal@1172: 'name': 'John Doe', bgneal@1172: 'email': 'jdoe@example.com', bgneal@1172: 'subject': 'Test message', bgneal@1172: 'message': 'Testing contact form.', bgneal@1172: } bgneal@1172: response = self.client.post(self.url, data=post_data, follow=True) bgneal@1172: self.assertEqual(response.status_code, 200) bgneal@1172: self.assertEqual(len(mail.outbox), 0) bgneal@1172: bgneal@1172: @patch('contact.forms.requests.post') bgneal@1172: def test_captcha_failure(self, post_mock): bgneal@1172: post_response = Mock() bgneal@1172: post_response.json.return_value = {'success': False} bgneal@1172: post_mock.return_value = post_response bgneal@1172: bgneal@1172: post_data = { bgneal@1172: 'name': 'John Doe', bgneal@1172: 'email': 'jdoe@example.com', bgneal@1172: 'subject': 'Test message', bgneal@1172: 'message': 'Testing contact form.', bgneal@1172: 'g-recaptcha-response': 'crazy-google-string', bgneal@1172: } bgneal@1172: response = self.client.post(self.url, data=post_data, follow=True) bgneal@1172: self.assertEqual(response.status_code, 200) bgneal@1172: self.assertEqual(len(mail.outbox), 0)