bgneal@819: """ bgneal@819: Unit tests for the contact application views. bgneal@819: bgneal@819: """ bgneal@819: from django.test import TestCase 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@819: def test_usage(self): bgneal@819: url = reverse('contact-form') bgneal@819: response = self.client.get(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@819: } bgneal@819: response = self.client.post(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@819: def test_honeypot(self): bgneal@819: url = reverse('contact-form') 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@819: response = self.client.post(url, data=post_data) bgneal@819: self.assertEqual(response.status_code, 200) bgneal@819: self.assertEqual(len(mail.outbox), 0)