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