comparison contact/tests/test_views.py @ 819:38db6ec61af3

Contact form can prepopulate subject field now. Also updated code for PEP-8 and added tests.
author Brian Neal <bgneal@gmail.com>
date Sat, 20 Sep 2014 14:05:52 -0500
parents
children 79a71b9d0a2a
comparison
equal deleted inserted replaced
818:cf486a8e8b43 819:38db6ec61af3
1 """
2 Unit tests for the contact application views.
3
4 """
5 from django.test import TestCase
6 from django.core.urlresolvers import reverse
7 from django.core import mail
8
9
10 class BaseTestCase(TestCase):
11 """Simple tests to ensure basic functionality."""
12
13 def test_usage(self):
14 url = reverse('contact-form')
15 response = self.client.get(url)
16 self.assertEqual(response.status_code, 200)
17
18 post_data = {
19 'name': 'John Doe',
20 'email': 'jdoe@example.com',
21 'subject': 'Test message',
22 'message': 'Testing contact form.',
23 }
24 response = self.client.post(url, data=post_data, follow=True)
25 self.assertRedirects(response, reverse('contact-thanks'))
26
27 self.assertEqual(len(mail.outbox), 1)
28 email = mail.outbox[0]
29 self.assertEqual(len(email.recipients()), 1)
30 self.assertEqual(email.from_email, post_data['email'])
31 self.assertEqual(email.recipients()[0], 'admin@surfguitar101.com')
32 self.assertTrue(post_data['subject'] in email.subject)
33 msg = email.message().as_string()
34 self.assertTrue(post_data['name'] in msg)
35 self.assertTrue(post_data['email'] in msg)
36 self.assertTrue(post_data['message'] in msg)
37
38 def test_honeypot(self):
39 url = reverse('contact-form')
40 post_data = {
41 'name': 'John Doe',
42 'email': 'jdoe@example.com',
43 'subject': 'Test message',
44 'message': 'Testing contact form.',
45 'honeypot': 'some spam',
46 }
47 response = self.client.post(url, data=post_data)
48 self.assertEqual(response.status_code, 200)
49 self.assertEqual(len(mail.outbox), 0)