comparison email_list/tests/test_views.py @ 130:3062c547bb90

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