Mercurial > public > sg101
comparison gpp/accounts/tests/view_tests.py @ 565:6a265b5768ca
For bitbucket issue #5, rework the duplicate email checking in the registration
form logic. Added tests for registration.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 04 Mar 2012 13:20:40 -0600 |
parents | |
children | 6301dd73683f |
comparison
equal
deleted
inserted
replaced
564:e5d3552d4ad0 | 565:6a265b5768ca |
---|---|
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.contrib.auth.models import User, check_password | |
10 | |
11 from antispam.rate_limit import unblock_ip | |
12 from accounts.models import PendingUser | |
13 from accounts.models import IllegalUsername | |
14 from accounts.models import IllegalEmail | |
15 | |
16 | |
17 class RegistrationTest(TestCase): | |
18 | |
19 def setUp(self): | |
20 u = User.objects.create_user('existing_user', 'existing_user@example.com', 'pw') | |
21 u.save() | |
22 | |
23 # a 2nd user has the same email as another | |
24 u = User.objects.create_user('existing_user2', 'existing_user@example.com', 'pw') | |
25 u.save() | |
26 | |
27 PendingUser.objects.create(username='pending_user', | |
28 email='pending_user@example.com', | |
29 password='pw', | |
30 date_joined=datetime.datetime.now(), | |
31 key='key') | |
32 | |
33 IllegalUsername.objects.create(username='illegalusername') | |
34 IllegalEmail.objects.create(email='illegal@example.com') | |
35 | |
36 def tearDown(self): | |
37 unblock_ip('127.0.0.1') | |
38 | |
39 def test_get_view(self): | |
40 """ | |
41 Test a simple get of the registration view | |
42 | |
43 """ | |
44 response = self.client.get(reverse('accounts-register')) | |
45 self.assertEqual(response.status_code, 200) | |
46 | |
47 def test_existing_user(self): | |
48 """ | |
49 Ensure we can't register with an existing username. | |
50 | |
51 """ | |
52 response = self.client.post(reverse('accounts-register'), { | |
53 'username': 'existing_user', | |
54 'email': 'test@example.com', | |
55 'password1': 'my_password', | |
56 'password2': 'my_password', | |
57 'agree_age': 'on', | |
58 'agree_tos': 'on', | |
59 'agree_privacy': 'on', | |
60 'question1': '101', | |
61 'question2': '', | |
62 }) | |
63 | |
64 self.assertEqual(response.status_code, 200) | |
65 self.assertContains(response, 'A user with that username already exists') | |
66 | |
67 def test_pending_user(self): | |
68 """ | |
69 Ensure we can't register with a pending username. | |
70 | |
71 """ | |
72 response = self.client.post(reverse('accounts-register'), { | |
73 'username': 'pending_user', | |
74 'email': 'test@example.com', | |
75 'password1': 'my_password', | |
76 'password2': 'my_password', | |
77 'agree_age': 'on', | |
78 'agree_tos': 'on', | |
79 'agree_privacy': 'on', | |
80 'question1': '101', | |
81 'question2': '', | |
82 }) | |
83 | |
84 self.assertEqual(response.status_code, 200) | |
85 self.assertContains(response, 'A pending user with that username already exists') | |
86 | |
87 def test_illegal_username(self): | |
88 """ | |
89 Ensure we can't register with a banned username. | |
90 | |
91 """ | |
92 response = self.client.post(reverse('accounts-register'), { | |
93 'username': 'illegalusername', | |
94 'email': 'test@example.com', | |
95 'password1': 'my_password', | |
96 'password2': 'my_password', | |
97 'agree_age': 'on', | |
98 'agree_tos': 'on', | |
99 'agree_privacy': 'on', | |
100 'question1': '101', | |
101 'question2': '', | |
102 }) | |
103 | |
104 self.assertEqual(response.status_code, 200) | |
105 self.assertContains(response, 'That username is not allowed') | |
106 | |
107 def test_duplicate_existing_email(self): | |
108 """ | |
109 Ensure we can't register with a duplicate email address. | |
110 | |
111 """ | |
112 response = self.client.post(reverse('accounts-register'), { | |
113 'username': 'a_new_user', | |
114 'email': 'existing_user@example.com', | |
115 'password1': 'my_password', | |
116 'password2': 'my_password', | |
117 'agree_age': 'on', | |
118 'agree_tos': 'on', | |
119 'agree_privacy': 'on', | |
120 'question1': '101', | |
121 'question2': '', | |
122 }) | |
123 | |
124 self.assertEqual(response.status_code, 200) | |
125 self.assertContains(response, 'A user with that email address already exists') | |
126 | |
127 def test_duplicate_pending_email(self): | |
128 """ | |
129 Ensure we can't register with a duplicate email address. | |
130 | |
131 """ | |
132 response = self.client.post(reverse('accounts-register'), { | |
133 'username': 'a_new_user', | |
134 'email': 'pending_user@example.com', | |
135 'password1': 'my_password', | |
136 'password2': 'my_password', | |
137 'agree_age': 'on', | |
138 'agree_tos': 'on', | |
139 'agree_privacy': 'on', | |
140 'question1': '101', | |
141 'question2': '', | |
142 }) | |
143 | |
144 self.assertEqual(response.status_code, 200) | |
145 self.assertContains(response, 'A pending user with that email address already exists') | |
146 | |
147 def test_illegal_email(self): | |
148 """ | |
149 Ensure we can't register with a banned email address. | |
150 | |
151 """ | |
152 response = self.client.post(reverse('accounts-register'), { | |
153 'username': 'a_new_user', | |
154 'email': 'illegal@example.com', | |
155 'password1': 'my_password', | |
156 'password2': 'my_password', | |
157 'agree_age': 'on', | |
158 'agree_tos': 'on', | |
159 'agree_privacy': 'on', | |
160 'question1': '101', | |
161 'question2': '', | |
162 }) | |
163 | |
164 self.assertEqual(response.status_code, 200) | |
165 self.assertContains(response, 'That email address is not allowed') | |
166 | |
167 def test_password_match(self): | |
168 """ | |
169 Ensure the passwords match. | |
170 | |
171 """ | |
172 response = self.client.post(reverse('accounts-register'), { | |
173 'username': 'a_new_user', | |
174 'email': 'test@example.com', | |
175 'password1': 'my_password', | |
176 'password2': 'my_password_doesnt match', | |
177 'agree_age': 'on', | |
178 'agree_tos': 'on', | |
179 'agree_privacy': 'on', | |
180 'question1': '101', | |
181 'question2': '', | |
182 }) | |
183 | |
184 self.assertEqual(response.status_code, 200) | |
185 self.assertContains(response, "The two password fields didn't match") | |
186 | |
187 def test_question1(self): | |
188 """ | |
189 Ensure our anti-spam question is answered. | |
190 | |
191 """ | |
192 response = self.client.post(reverse('accounts-register'), { | |
193 'username': 'a_new_user', | |
194 'email': 'test@example.com', | |
195 'password1': 'my_password', | |
196 'password2': 'my_password_doesnt match', | |
197 'agree_age': 'on', | |
198 'agree_tos': 'on', | |
199 'agree_privacy': 'on', | |
200 'question1': 'huh', | |
201 'question2': '', | |
202 }) | |
203 | |
204 self.assertEqual(response.status_code, 200) | |
205 self.assertContains(response, "Incorrect answer to our anti-spam question") | |
206 | |
207 def test_question2(self): | |
208 """ | |
209 Ensure our honeypot question check works. | |
210 | |
211 """ | |
212 response = self.client.post(reverse('accounts-register'), { | |
213 'username': 'a_new_user', | |
214 'email': 'test@example.com', | |
215 'password1': 'my_password', | |
216 'password2': 'my_password_doesnt match', | |
217 'agree_age': 'on', | |
218 'agree_tos': 'on', | |
219 'agree_privacy': 'on', | |
220 'question1': '101', | |
221 'question2': 'non blank', | |
222 }) | |
223 | |
224 self.assertEqual(response.status_code, 403) | |
225 | |
226 def test_success(self): | |
227 """ | |
228 Ensure we can successfully register. | |
229 | |
230 """ | |
231 response = self.client.post(reverse('accounts-register'), { | |
232 'username': 'a_new_user', | |
233 'email': 'test@example.com', | |
234 'password1': 'my_password', | |
235 'password2': 'my_password', | |
236 'agree_age': 'on', | |
237 'agree_tos': 'on', | |
238 'agree_privacy': 'on', | |
239 'question1': '101', | |
240 'question2': '', | |
241 }) | |
242 | |
243 self.assertEqual(response.status_code, 302) | |
244 | |
245 try: | |
246 pending = PendingUser.objects.get(username='a_new_user') | |
247 except PendingUser.DoesNotExist: | |
248 self.fail("PendingUser was not created") | |
249 | |
250 self.assertEqual(pending.email, 'test@example.com') | |
251 self.assertTrue(datetime.datetime.now() - pending.date_joined < | |
252 datetime.timedelta(minutes=1)) | |
253 self.assertTrue(check_password('my_password', pending.password)) |