Commit | Line | Data |
---|---|---|
8e1e744d | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
4b5f4e87 CAW |
2 | # Copyright (C) 2011 Free Software Foundation, Inc |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
1972a888 | 17 | import urlparse |
4b5f4e87 | 18 | |
1972a888 | 19 | from nose.tools import assert_equal |
4b5f4e87 | 20 | |
1972a888 | 21 | from mediagoblin.auth import lib as auth_lib |
3aa4c668 | 22 | from mediagoblin.tests.tools import setup_fresh_app |
6e7ce8d1 | 23 | from mediagoblin import mg_globals |
460ce564 CAW |
24 | from mediagoblin import util |
25 | ||
4b5f4e87 CAW |
26 | |
27 | ######################## | |
28 | # Test bcrypt auth funcs | |
29 | ######################## | |
30 | ||
31 | def test_bcrypt_check_password(): | |
32 | # Check known 'lollerskates' password against check function | |
33 | assert auth_lib.bcrypt_check_password( | |
34 | 'lollerskates', | |
35 | '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO') | |
36 | ||
db780024 CAW |
37 | assert not auth_lib.bcrypt_check_password( |
38 | 'notthepassword', | |
39 | '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO') | |
40 | ||
41 | ||
4b5f4e87 | 42 | # Same thing, but with extra fake salt. |
db780024 CAW |
43 | assert not auth_lib.bcrypt_check_password( |
44 | 'notthepassword', | |
4b5f4e87 CAW |
45 | '$2a$12$ELVlnw3z1FMu6CEGs/L8XO8vl0BuWSlUHgh0rUrry9DUXGMUNWwl6', |
46 | '3><7R45417') | |
47 | ||
48 | ||
49 | def test_bcrypt_gen_password_hash(): | |
50 | pw = 'youwillneverguessthis' | |
51 | ||
52 | # Normal password hash generation, and check on that hash | |
53 | hashed_pw = auth_lib.bcrypt_gen_password_hash(pw) | |
54 | assert auth_lib.bcrypt_check_password( | |
55 | pw, hashed_pw) | |
db780024 CAW |
56 | assert not auth_lib.bcrypt_check_password( |
57 | 'notthepassword', hashed_pw) | |
58 | ||
4b5f4e87 CAW |
59 | |
60 | # Same thing, extra salt. | |
61 | hashed_pw = auth_lib.bcrypt_gen_password_hash(pw, '3><7R45417') | |
62 | assert auth_lib.bcrypt_check_password( | |
63 | pw, hashed_pw, '3><7R45417') | |
db780024 CAW |
64 | assert not auth_lib.bcrypt_check_password( |
65 | 'notthepassword', hashed_pw, '3><7R45417') | |
460ce564 CAW |
66 | |
67 | ||
3aa4c668 CAW |
68 | @setup_fresh_app |
69 | def test_register_views(test_app): | |
2fecc29d CAW |
70 | """ |
71 | Massive test function that all our registration-related views all work. | |
72 | """ | |
460ce564 | 73 | # Test doing a simple GET on the page |
651403f0 CAW |
74 | # ----------------------------------- |
75 | ||
460ce564 CAW |
76 | test_app.get('/auth/register/') |
77 | # Make sure it rendered with the appropriate template | |
78 | assert util.TEMPLATE_TEST_CONTEXT.has_key( | |
79 | 'mediagoblin/auth/register.html') | |
757690cc | 80 | |
460ce564 | 81 | # Try to register without providing anything, should error |
651403f0 CAW |
82 | # -------------------------------------------------------- |
83 | ||
460ce564 CAW |
84 | util.clear_test_template_context() |
85 | test_app.post( | |
86 | '/auth/register/', {}) | |
87 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
88 | form = context['register_form'] | |
89 | assert form.username.errors == [u'This field is required.'] | |
90 | assert form.password.errors == [u'This field is required.'] | |
91 | assert form.confirm_password.errors == [u'This field is required.'] | |
92 | assert form.email.errors == [u'This field is required.'] | |
651403f0 CAW |
93 | |
94 | # Try to register with fields that are known to be invalid | |
95 | # -------------------------------------------------------- | |
96 | ||
97 | ## too short | |
98 | util.clear_test_template_context() | |
99 | test_app.post( | |
100 | '/auth/register/', { | |
101 | 'username': 'l', | |
102 | 'password': 'o', | |
103 | 'confirm_password': 'o', | |
104 | 'email': 'l'}) | |
105 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
106 | form = context['register_form'] | |
107 | ||
108 | assert form.username.errors == [ | |
109 | u'Field must be between 3 and 30 characters long.'] | |
110 | assert form.password.errors == [ | |
111 | u'Field must be between 6 and 30 characters long.'] | |
112 | ||
113 | ## bad form | |
114 | util.clear_test_template_context() | |
115 | test_app.post( | |
116 | '/auth/register/', { | |
117 | 'username': '@_@', | |
118 | 'email': 'lollerskates'}) | |
119 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
120 | form = context['register_form'] | |
121 | ||
122 | assert form.username.errors == [ | |
123 | u'Invalid input.'] | |
124 | assert form.email.errors == [ | |
125 | u'Invalid email address.'] | |
126 | ||
127 | ## mismatching passwords | |
128 | util.clear_test_template_context() | |
129 | test_app.post( | |
130 | '/auth/register/', { | |
131 | 'password': 'herpderp', | |
132 | 'confirm_password': 'derpherp'}) | |
133 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
134 | form = context['register_form'] | |
135 | ||
136 | assert form.password.errors == [ | |
137 | u'Passwords must match.'] | |
138 | ||
139 | ## At this point there should be no users in the database ;) | |
6e7ce8d1 | 140 | assert not mg_globals.database.User.find().count() |
651403f0 CAW |
141 | |
142 | # Successful register | |
143 | # ------------------- | |
1972a888 CAW |
144 | util.clear_test_template_context() |
145 | response = test_app.post( | |
146 | '/auth/register/', { | |
147 | 'username': 'happygirl', | |
148 | 'password': 'iamsohappy', | |
149 | 'confirm_password': 'iamsohappy', | |
150 | 'email': 'happygrrl@example.org'}) | |
151 | response.follow() | |
152 | ||
651403f0 | 153 | ## Did we redirect to the proper page? Use the right template? |
1972a888 CAW |
154 | assert_equal( |
155 | urlparse.urlsplit(response.location)[2], | |
156 | '/auth/register/success/') | |
157 | assert util.TEMPLATE_TEST_CONTEXT.has_key( | |
158 | 'mediagoblin/auth/register_success.html') | |
159 | ||
651403f0 | 160 | ## Make sure user is in place |
6e7ce8d1 | 161 | new_user = mg_globals.database.User.find_one( |
1972a888 CAW |
162 | {'username': 'happygirl'}) |
163 | assert new_user | |
164 | assert new_user['status'] == u'needs_email_verification' | |
165 | assert new_user['email_verified'] == False | |
166 | ||
167 | ## Make sure we get email confirmation, and try verifying | |
168 | assert len(util.EMAIL_TEST_INBOX) == 1 | |
169 | message = util.EMAIL_TEST_INBOX.pop() | |
170 | assert message['To'] == 'happygrrl@example.org' | |
171 | email_context = util.TEMPLATE_TEST_CONTEXT[ | |
172 | 'mediagoblin/auth/verification_email.txt'] | |
173 | assert email_context['verification_url'] in message.get_payload(decode=True) | |
174 | ||
175 | path = urlparse.urlsplit(email_context['verification_url'])[2] | |
176 | get_params = urlparse.urlsplit(email_context['verification_url'])[3] | |
177 | assert path == u'/auth/verify_email/' | |
178 | parsed_get_params = urlparse.parse_qs(get_params) | |
179 | ||
180 | ### user should have these same parameters | |
181 | assert parsed_get_params['userid'] == [ | |
182 | unicode(new_user['_id'])] | |
183 | assert parsed_get_params['token'] == [ | |
184 | new_user['verification_key']] | |
757690cc | 185 | |
7b1e17ed CAW |
186 | ## Try verifying with bs verification key, shouldn't work |
187 | util.clear_test_template_context() | |
188 | test_app.get( | |
189 | "/auth/verify_email/?userid=%s&token=total_bs" % unicode( | |
190 | new_user['_id'])) | |
191 | context = util.TEMPLATE_TEST_CONTEXT[ | |
192 | 'mediagoblin/auth/verify_email.html'] | |
193 | assert context['verification_successful'] == False | |
6e7ce8d1 | 194 | new_user = mg_globals.database.User.find_one( |
7b1e17ed CAW |
195 | {'username': 'happygirl'}) |
196 | assert new_user | |
197 | assert new_user['status'] == u'needs_email_verification' | |
198 | assert new_user['email_verified'] == False | |
199 | ||
200 | ## Verify the email activation works | |
201 | util.clear_test_template_context() | |
202 | test_app.get("%s?%s" % (path, get_params)) | |
203 | context = util.TEMPLATE_TEST_CONTEXT[ | |
204 | 'mediagoblin/auth/verify_email.html'] | |
205 | assert context['verification_successful'] == True | |
6e7ce8d1 | 206 | new_user = mg_globals.database.User.find_one( |
7b1e17ed CAW |
207 | {'username': 'happygirl'}) |
208 | assert new_user | |
209 | assert new_user['status'] == u'active' | |
210 | assert new_user['email_verified'] == True | |
1972a888 | 211 | |
cb9bac0c CAW |
212 | # Uniqueness checks |
213 | # ----------------- | |
214 | ## We shouldn't be able to register with that user twice | |
8a869db8 CAW |
215 | util.clear_test_template_context() |
216 | response = test_app.post( | |
217 | '/auth/register/', { | |
218 | 'username': 'happygirl', | |
219 | 'password': 'iamsohappy2', | |
220 | 'confirm_password': 'iamsohappy2', | |
221 | 'email': 'happygrrl2@example.org'}) | |
757690cc | 222 | |
8a869db8 CAW |
223 | context = util.TEMPLATE_TEST_CONTEXT[ |
224 | 'mediagoblin/auth/register.html'] | |
225 | form = context['register_form'] | |
226 | assert form.username.errors == [ | |
227 | u'Sorry, a user with that name already exists.'] | |
651403f0 | 228 | |
8a869db8 | 229 | ## TODO: Also check for double instances of an email address? |
757690cc CM |
230 | |
231 | ||
232 | @setup_fresh_app | |
233 | def test_authentication_views(test_app): | |
234 | """ | |
235 | Test logging in and logging out | |
236 | """ | |
237 | # Make a new user | |
238 | test_user = mg_globals.database.User() | |
239 | test_user['username'] = u'chris' | |
240 | test_user['email'] = u'chris@example.com' | |
241 | test_user['pw_hash'] = auth_lib.bcrypt_gen_password_hash('toast') | |
242 | test_user.save() | |
243 | ||
244 | # Get login | |
0a4cecdc | 245 | # --------- |
757690cc | 246 | test_app.get('/auth/login/') |
757690cc CM |
247 | assert util.TEMPLATE_TEST_CONTEXT.has_key( |
248 | 'mediagoblin/auth/login.html') | |
249 | ||
0a4cecdc CM |
250 | # Failed login - blank form |
251 | # ------------------------- | |
252 | util.clear_test_template_context() | |
253 | response = test_app.post('/auth/login/') | |
254 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] | |
255 | form = context['login_form'] | |
256 | assert form.username.errors == [u'This field is required.'] | |
257 | assert form.password.errors == [u'This field is required.'] | |
258 | ||
259 | # Failed login - blank user | |
260 | # ------------------------- | |
261 | util.clear_test_template_context() | |
262 | response = test_app.post( | |
263 | '/auth/login/', { | |
264 | 'password': u'toast'}) | |
265 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] | |
266 | form = context['login_form'] | |
267 | assert form.username.errors == [u'This field is required.'] | |
268 | ||
269 | # Failed login - blank password | |
270 | # ----------------------------- | |
271 | util.clear_test_template_context() | |
272 | response = test_app.post( | |
273 | '/auth/login/', { | |
274 | 'username': u'chris'}) | |
275 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] | |
276 | form = context['login_form'] | |
277 | assert form.password.errors == [u'This field is required.'] | |
278 | ||
279 | # Failed login - bad user | |
280 | # ----------------------- | |
281 | util.clear_test_template_context() | |
282 | response = test_app.post( | |
283 | '/auth/login/', { | |
284 | 'username': u'steve', | |
285 | 'password': 'toast'}) | |
286 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] | |
287 | assert context['login_failed'] | |
288 | ||
289 | # Failed login - bad password | |
290 | # --------------------------- | |
291 | util.clear_test_template_context() | |
292 | response = test_app.post( | |
293 | '/auth/login/', { | |
294 | 'username': u'chris', | |
295 | 'password': 'jam'}) | |
296 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] | |
297 | assert context['login_failed'] | |
298 | ||
299 | # Successful login | |
300 | # ---------------- | |
757690cc CM |
301 | util.clear_test_template_context() |
302 | response = test_app.post( | |
303 | '/auth/login/', { | |
304 | 'username': u'chris', | |
305 | 'password': 'toast'}) | |
0a4cecdc CM |
306 | |
307 | # User should be redirected | |
757690cc CM |
308 | response.follow() |
309 | assert_equal( | |
310 | urlparse.urlsplit(response.location)[2], | |
311 | '/') | |
312 | assert util.TEMPLATE_TEST_CONTEXT.has_key( | |
313 | 'mediagoblin/root.html') | |
314 | ||
0a4cecdc CM |
315 | # Make sure user is in the session |
316 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] | |
317 | session = context['request'].session | |
757690cc CM |
318 | assert session['user_id'] == unicode(test_user['_id']) |
319 | ||
0a4cecdc CM |
320 | # Successful logout |
321 | # ----------------- | |
322 | util.clear_test_template_context() | |
323 | response = test_app.get('/auth/logout/') | |
324 | ||
325 | # Should be redirected to index page | |
326 | response.follow() | |
327 | assert_equal( | |
328 | urlparse.urlsplit(response.location)[2], | |
329 | '/') | |
330 | assert util.TEMPLATE_TEST_CONTEXT.has_key( | |
331 | 'mediagoblin/root.html') | |
332 | ||
333 | # Make sure the user is not in the session | |
334 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] | |
335 | session = context['request'].session | |
336 | assert session.has_key('user_id') == False | |
757690cc | 337 | |
12c231c8 CM |
338 | # User is redirected to custom URL if POST['next'] is set |
339 | # ------------------------------------------------------- | |
340 | util.clear_test_template_context() | |
341 | response = test_app.post( | |
342 | '/auth/login/', { | |
343 | 'username': u'chris', | |
344 | 'password': 'toast', | |
345 | 'next' : '/u/chris/'}) | |
346 | assert_equal( | |
347 | urlparse.urlsplit(response.location)[2], | |
348 | '/u/chris/') | |
349 |