Mercurial > public > madeira
comparison email_list/tests/view_tests.py @ 71:e2868ad47a1e
For Django 1.4, using the new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 14 Apr 2012 16:40:29 -0500 |
parents | madeira/email_list/tests/view_tests.py@7f9e76e7eb4d |
children |
comparison
equal
deleted
inserted
replaced
70:f26cdda0ad8b | 71:e2868ad47a1e |
---|---|
1 """ | |
2 View tests for the email_list application. | |
3 | |
4 """ | |
5 from django.test import TestCase | |
6 from django.core.urlresolvers import reverse | |
7 from django.core import mail | |
8 | |
9 from email_list.models import Subscriber | |
10 import email_list.forms | |
11 | |
12 | |
13 SUB_PARAMS = { | |
14 'name': 'John Doe', | |
15 'email': 'j.doe@example.com', | |
16 'location': 'USA', | |
17 'option': 'sub' | |
18 } | |
19 | |
20 UNSUB_PARAMS = { | |
21 'name': '', | |
22 'email': SUB_PARAMS['email'], | |
23 'location': '', | |
24 'option': 'unsub' | |
25 } | |
26 | |
27 class EmailListTestCase(TestCase): | |
28 | |
29 def test_already_subscribed(self): | |
30 """ | |
31 Test that subscribing twice fails with a form error. | |
32 | |
33 """ | |
34 sub = Subscriber(email=SUB_PARAMS['email']) | |
35 sub.set_active() | |
36 sub.save() | |
37 | |
38 # Post a subscribe request | |
39 | |
40 response = self.client.post( | |
41 reverse('email_list-main'), | |
42 SUB_PARAMS, | |
43 follow=True) | |
44 | |
45 self.assertTrue(response.status_code, 200) | |
46 self.assertEqual(len(response.redirect_chain), 0) | |
47 self.assertContains(response, email_list.forms.ALREADY_SUBSCRIBED) | |
48 | |
49 def test_not_subscribed(self): | |
50 """ | |
51 Test that unsubscribing without being subscribed fails with a form error. | |
52 | |
53 """ | |
54 # Post a unsubscribe request | |
55 | |
56 response = self.client.post( | |
57 reverse('email_list-main'), | |
58 UNSUB_PARAMS, | |
59 follow=True) | |
60 | |
61 self.assertTrue(response.status_code, 200) | |
62 self.assertEqual(len(response.redirect_chain), 0) | |
63 self.assertContains(response, email_list.forms.NOT_SUBSCRIBED) | |
64 | |
65 def test_normal_cycle(self): | |
66 """ | |
67 Test a normal subscribe and unsubscribe cycle. | |
68 | |
69 """ | |
70 self.do_test_subscribe() | |
71 self.do_test_unsubscribe() | |
72 | |
73 def test_subscribe_if_pending(self): | |
74 """ | |
75 Ensure you can subscribe if you are already pending. | |
76 | |
77 """ | |
78 sub = Subscriber(email=SUB_PARAMS['email']) | |
79 sub.set_pending() | |
80 sub.save() | |
81 self.do_test_subscribe() | |
82 | |
83 def test_subscribe_if_leaving(self): | |
84 """ | |
85 Ensure you can subscribe if you are leaving. | |
86 | |
87 """ | |
88 sub = Subscriber(email=SUB_PARAMS['email']) | |
89 sub.set_leaving() | |
90 sub.save() | |
91 self.do_test_subscribe() | |
92 | |
93 def test_unsubscribe_if_leaving(self): | |
94 """ | |
95 Ensure you can unsubscribe if you are already leaving. | |
96 | |
97 """ | |
98 sub = Subscriber(email=SUB_PARAMS['email']) | |
99 sub.set_leaving() | |
100 sub.save() | |
101 self.do_test_unsubscribe() | |
102 | |
103 def do_test_subscribe(self): | |
104 # Get the form view | |
105 response = self.client.get(reverse('email_list-main')) | |
106 self.assertEqual(response.status_code, 200) | |
107 self.assertTemplateUsed(response, 'email_list/subscribe_form.html') | |
108 | |
109 # Post a subscribe request | |
110 | |
111 response = self.client.post( | |
112 reverse('email_list-main'), | |
113 SUB_PARAMS, | |
114 follow=True) | |
115 | |
116 self.assertTrue(response.status_code, 200) | |
117 self.assertEqual(len(response.redirect_chain), 1) | |
118 self.assertEqual(response.redirect_chain[0][0], | |
119 'http://testserver' + reverse('email_list-request_subscribe')) | |
120 self.assertEqual(response.redirect_chain[0][1], 302) | |
121 | |
122 # verify subscriber is in pending state | |
123 | |
124 try: | |
125 subscriber = Subscriber.objects.get(email=SUB_PARAMS['email']) | |
126 except Subscriber.DoesNotExist: | |
127 self.fail("No pending subscriber") | |
128 | |
129 self.assertTrue(subscriber.is_pending()) | |
130 | |
131 # test email sent | |
132 self.do_test_email(subscriber) | |
133 | |
134 # simulate a confirm click | |
135 | |
136 response = self.client.get( | |
137 reverse('email_list-confirm', kwargs={'key': subscriber.key}), | |
138 follow=True) | |
139 | |
140 self.assertTrue(response.status_code, 200) | |
141 self.assertEqual(len(response.redirect_chain), 1) | |
142 self.assertEqual(response.redirect_chain[0][0], | |
143 'http://testserver' + reverse('email_list-subscribed')) | |
144 self.assertEqual(response.redirect_chain[0][1], 302) | |
145 | |
146 # verify active user | |
147 try: | |
148 subscriber = Subscriber.objects.get(email=SUB_PARAMS['email']) | |
149 except Subscriber.DoesNotExist: | |
150 self.fail("No active subscriber") | |
151 | |
152 self.assertTrue(subscriber.is_active()) | |
153 | |
154 def do_test_unsubscribe(self): | |
155 # Get the form view | |
156 response = self.client.get(reverse('email_list-main')) | |
157 self.assertEqual(response.status_code, 200) | |
158 self.assertTemplateUsed(response, 'email_list/subscribe_form.html') | |
159 | |
160 # Post a unsubscribe request | |
161 | |
162 response = self.client.post( | |
163 reverse('email_list-main'), | |
164 UNSUB_PARAMS, | |
165 follow=True) | |
166 | |
167 self.assertTrue(response.status_code, 200) | |
168 self.assertEqual(len(response.redirect_chain), 1) | |
169 self.assertEqual(response.redirect_chain[0][0], | |
170 'http://testserver' + reverse('email_list-request_unsubscribe')) | |
171 self.assertEqual(response.redirect_chain[0][1], 302) | |
172 | |
173 # verify subscriber is in leaving state | |
174 | |
175 try: | |
176 subscriber = Subscriber.objects.get(email=UNSUB_PARAMS['email']) | |
177 except Subscriber.DoesNotExist: | |
178 self.fail("No pending subscriber") | |
179 | |
180 self.assertTrue(subscriber.is_leaving()) | |
181 | |
182 # test email sent | |
183 self.do_test_email(subscriber) | |
184 | |
185 # simulate a click to unsubscribe | |
186 | |
187 response = self.client.get( | |
188 reverse('email_list-confirm', kwargs={'key': subscriber.key}), | |
189 follow=True) | |
190 | |
191 self.assertTrue(response.status_code, 200) | |
192 self.assertEqual(len(response.redirect_chain), 1) | |
193 self.assertEqual(response.redirect_chain[0][0], | |
194 'http://testserver' + reverse('email_list-unsubscribed')) | |
195 self.assertEqual(response.redirect_chain[0][1], 302) | |
196 | |
197 # verify subscription has been removed | |
198 | |
199 self.assertRaises(Subscriber.DoesNotExist, Subscriber.objects.get, | |
200 email=UNSUB_PARAMS['email']) | |
201 | |
202 def do_test_email(self, subscriber): | |
203 """ | |
204 Tests to see if the confirmation email was sent. | |
205 | |
206 """ | |
207 self.assertEqual(len(mail.outbox), 1) | |
208 self.assertEqual(len(mail.outbox[0].to), 1) | |
209 self.assertEqual(mail.outbox[0].to[0], subscriber.email) | |
210 self.assertTrue(str(mail.outbox[0].message()).count(subscriber.key) > 0) | |
211 | |
212 # clear outbox | |
213 mail.outbox = [] |