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