Mercurial > public > sg101
comparison accounts/tests/test_views.py @ 744:8789299c75b1
Django 1.6: test discovery as per unittest.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 29 Dec 2013 14:45:26 -0600 |
parents | accounts/tests/view_tests.py@988782c6ce6c |
children | 840f2579ef1c |
comparison
equal
deleted
inserted
replaced
743:66d46d31d543 | 744:8789299c75b1 |
---|---|
1 """ | |
2 View tests for the accounts application. | |
3 | |
4 """ | |
5 import datetime | |
6 | |
7 from django.test import TestCase | |
8 from django.core.urlresolvers import reverse | |
9 from django.core import mail | |
10 from django.contrib.auth.models import User | |
11 from django.contrib.auth.hashers import check_password | |
12 | |
13 from accounts.models import PendingUser | |
14 from accounts.models import IllegalUsername | |
15 from accounts.models import IllegalEmail | |
16 | |
17 | |
18 class RegistrationTest(TestCase): | |
19 | |
20 def setUp(self): | |
21 u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') | |
22 u.save() | |
23 | |
24 # a 2nd user has the same email as another | |
25 u = User.objects.create_user('existing_user2', 'existing_user@example.com', 'pw') | |
26 u.save() | |
27 | |
28 PendingUser.objects.create(username='pending_user', | |
29 email='pending_user@example.com', | |
30 password='pw', | |
31 date_joined=datetime.datetime.now(), | |
32 key='key') | |
33 | |
34 IllegalUsername.objects.create(username='illegalusername') | |
35 IllegalEmail.objects.create(email='illegal@example.com') | |
36 | |
37 def test_get_view(self): | |
38 """ | |
39 Test a simple get of the registration view | |
40 | |
41 """ | |
42 response = self.client.get(reverse('accounts-register')) | |
43 self.assertEqual(response.status_code, 200) | |
44 | |
45 def test_existing_user(self): | |
46 """ | |
47 Ensure we can't register with an existing username. | |
48 | |
49 """ | |
50 response = self.client.post(reverse('accounts-register'), { | |
51 'username': 'existing_user', | |
52 'email': 'test@example.com', | |
53 'password1': 'my_password', | |
54 'password2': 'my_password', | |
55 'agree_age': 'on', | |
56 'agree_tos': 'on', | |
57 'agree_privacy': 'on', | |
58 'question1': '101', | |
59 'question2': '', | |
60 }) | |
61 | |
62 self.assertEqual(response.status_code, 200) | |
63 self.assertContains(response, 'A user with that username already exists') | |
64 | |
65 def test_pending_user(self): | |
66 """ | |
67 Ensure we can't register with a pending username. | |
68 | |
69 """ | |
70 response = self.client.post(reverse('accounts-register'), { | |
71 'username': 'pending_user', | |
72 'email': 'test@example.com', | |
73 'password1': 'my_password', | |
74 'password2': 'my_password', | |
75 'agree_age': 'on', | |
76 'agree_tos': 'on', | |
77 'agree_privacy': 'on', | |
78 'question1': '101', | |
79 'question2': '', | |
80 }) | |
81 | |
82 self.assertEqual(response.status_code, 200) | |
83 self.assertContains(response, 'A pending user with that username already exists') | |
84 | |
85 def test_illegal_username(self): | |
86 """ | |
87 Ensure we can't register with a banned username. | |
88 | |
89 """ | |
90 response = self.client.post(reverse('accounts-register'), { | |
91 'username': 'illegalusername', | |
92 'email': 'test@example.com', | |
93 'password1': 'my_password', | |
94 'password2': 'my_password', | |
95 'agree_age': 'on', | |
96 'agree_tos': 'on', | |
97 'agree_privacy': 'on', | |
98 'question1': '101', | |
99 'question2': '', | |
100 }) | |
101 | |
102 self.assertEqual(response.status_code, 200) | |
103 self.assertContains(response, 'That username is not allowed') | |
104 | |
105 def test_duplicate_existing_email(self): | |
106 """ | |
107 Ensure we can't register with a duplicate email address. | |
108 | |
109 """ | |
110 response = self.client.post(reverse('accounts-register'), { | |
111 'username': 'a_new_user', | |
112 'email': 'existing_user@example.com', | |
113 'password1': 'my_password', | |
114 'password2': 'my_password', | |
115 'agree_age': 'on', | |
116 'agree_tos': 'on', | |
117 'agree_privacy': 'on', | |
118 'question1': '101', | |
119 'question2': '', | |
120 }) | |
121 | |
122 self.assertEqual(response.status_code, 200) | |
123 self.assertContains(response, 'A user with that email address already exists') | |
124 | |
125 def test_duplicate_pending_email(self): | |
126 """ | |
127 Ensure we can't register with a duplicate email address. | |
128 | |
129 """ | |
130 response = self.client.post(reverse('accounts-register'), { | |
131 'username': 'a_new_user', | |
132 'email': 'pending_user@example.com', | |
133 'password1': 'my_password', | |
134 'password2': 'my_password', | |
135 'agree_age': 'on', | |
136 'agree_tos': 'on', | |
137 'agree_privacy': 'on', | |
138 'question1': '101', | |
139 'question2': '', | |
140 }) | |
141 | |
142 self.assertEqual(response.status_code, 200) | |
143 self.assertContains(response, 'A pending user with that email address already exists') | |
144 | |
145 def test_illegal_email(self): | |
146 """ | |
147 Ensure we can't register with a banned email address. | |
148 | |
149 """ | |
150 response = self.client.post(reverse('accounts-register'), { | |
151 'username': 'a_new_user', | |
152 'email': 'illegal@example.com', | |
153 'password1': 'my_password', | |
154 'password2': 'my_password', | |
155 'agree_age': 'on', | |
156 'agree_tos': 'on', | |
157 'agree_privacy': 'on', | |
158 'question1': '101', | |
159 'question2': '', | |
160 }) | |
161 | |
162 self.assertEqual(response.status_code, 200) | |
163 self.assertContains(response, 'That email address is not allowed') | |
164 | |
165 def test_password_match(self): | |
166 """ | |
167 Ensure the passwords match. | |
168 | |
169 """ | |
170 response = self.client.post(reverse('accounts-register'), { | |
171 'username': 'a_new_user', | |
172 'email': 'test@example.com', | |
173 'password1': 'my_password', | |
174 'password2': 'my_password_doesnt match', | |
175 'agree_age': 'on', | |
176 'agree_tos': 'on', | |
177 'agree_privacy': 'on', | |
178 'question1': '101', | |
179 'question2': '', | |
180 }) | |
181 | |
182 self.assertEqual(response.status_code, 200) | |
183 self.assertContains(response, "The two password fields didn't match") | |
184 | |
185 def test_question1(self): | |
186 """ | |
187 Ensure our anti-spam question is answered. | |
188 | |
189 """ | |
190 response = self.client.post(reverse('accounts-register'), { | |
191 'username': 'a_new_user', | |
192 'email': 'test@example.com', | |
193 'password1': 'my_password', | |
194 'password2': 'my_password_doesnt match', | |
195 'agree_age': 'on', | |
196 'agree_tos': 'on', | |
197 'agree_privacy': 'on', | |
198 'question1': 'huh', | |
199 'question2': '', | |
200 }) | |
201 | |
202 self.assertEqual(response.status_code, 200) | |
203 self.assertContains(response, "Incorrect answer to our anti-spam question") | |
204 | |
205 def test_question2(self): | |
206 """ | |
207 Ensure our honeypot question check works. | |
208 | |
209 """ | |
210 response = self.client.post(reverse('accounts-register'), { | |
211 'username': 'a_new_user', | |
212 'email': 'test@example.com', | |
213 'password1': 'my_password', | |
214 'password2': 'my_password_doesnt match', | |
215 'agree_age': 'on', | |
216 'agree_tos': 'on', | |
217 'agree_privacy': 'on', | |
218 'question1': '101', | |
219 'question2': 'non blank', | |
220 }) | |
221 | |
222 self.assertEqual(response.status_code, 200) | |
223 | |
224 def test_success(self): | |
225 """ | |
226 Ensure we can successfully register. | |
227 | |
228 """ | |
229 response = self.client.post(reverse('accounts-register'), { | |
230 'username': 'a_new_user', | |
231 'email': 'test@example.com', | |
232 'password1': 'my_password', | |
233 'password2': 'my_password', | |
234 'agree_age': 'on', | |
235 'agree_tos': 'on', | |
236 'agree_privacy': 'on', | |
237 'question1': '101', | |
238 'question2': '', | |
239 }) | |
240 | |
241 self.assertEqual(response.status_code, 302) | |
242 | |
243 try: | |
244 pending = PendingUser.objects.get(username='a_new_user') | |
245 except PendingUser.DoesNotExist: | |
246 self.fail("PendingUser was not created") | |
247 | |
248 self.assertEqual(pending.email, 'test@example.com') | |
249 self.assertTrue(datetime.datetime.now() - pending.date_joined < | |
250 datetime.timedelta(minutes=1)) | |
251 self.assertTrue(check_password('my_password', pending.password)) | |
252 | |
253 | |
254 class ForgotUsernameTest(TestCase): | |
255 | |
256 def setUp(self): | |
257 u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') | |
258 u.save() | |
259 | |
260 def test_get_query_view(self): | |
261 """Test a simple get of the username query view""" | |
262 response = self.client.get(reverse('accounts-username_query')) | |
263 self.assertEqual(response.status_code, 200) | |
264 | |
265 def test_get_username_sent_view(self): | |
266 """Test a simple get of the username sent view""" | |
267 response = self.client.get(reverse('accounts-username_sent')) | |
268 self.assertEqual(response.status_code, 200) | |
269 | |
270 def test_invalid_email(self): | |
271 """Test form submittal of unknown email address.""" | |
272 response = self.client.post(reverse('accounts-username_query'), { | |
273 'email': 'bad_address@example.com', | |
274 }, | |
275 follow=True) | |
276 | |
277 self.assertRedirects(response, reverse('accounts-username_sent')) | |
278 | |
279 self.assertEqual(len(mail.outbox), 0) | |
280 | |
281 def test_valid_email(self): | |
282 """Test form submittal of valid email address.""" | |
283 response = self.client.post(reverse('accounts-username_query'), { | |
284 'email': 'existing_user@example.com', | |
285 }, | |
286 follow=True) | |
287 | |
288 self.assertRedirects(response, reverse('accounts-username_sent')) | |
289 | |
290 self.assertEqual(len(mail.outbox), 1) | |
291 if len(mail.outbox): | |
292 self.assertTrue(mail.outbox[0].subject.startswith('Forgotten username')) |