moved change_pass to basic_auth and fixed some typos with the moving of forgot pass
[mediagoblin.git] / mediagoblin / tests / test_basic_auth.py
CommitLineData
7a98eb73
RE
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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/>.
af665c4e
RE
16import urlparse
17
18from mediagoblin.db.models import User
3bcdc490 19from mediagoblin.plugins.basic_auth import tools as auth_tools
af665c4e
RE
20from mediagoblin.tests.tools import fixture_add_user
21from mediagoblin.tools import template
7a98eb73
RE
22from mediagoblin.tools.testing import _activate_testing
23
24_activate_testing()
25
26
27########################
28# Test bcrypt auth funcs
29########################
30
31
32def test_bcrypt_check_password():
33 # Check known 'lollerskates' password against check function
3bcdc490 34 assert auth_tools.bcrypt_check_password(
7a98eb73
RE
35 'lollerskates',
36 '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
37
3bcdc490 38 assert not auth_tools.bcrypt_check_password(
7a98eb73
RE
39 'notthepassword',
40 '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
41
42 # Same thing, but with extra fake salt.
3bcdc490 43 assert not auth_tools.bcrypt_check_password(
7a98eb73
RE
44 'notthepassword',
45 '$2a$12$ELVlnw3z1FMu6CEGs/L8XO8vl0BuWSlUHgh0rUrry9DUXGMUNWwl6',
46 '3><7R45417')
47
48
49def test_bcrypt_gen_password_hash():
50 pw = 'youwillneverguessthis'
51
52 # Normal password hash generation, and check on that hash
3bcdc490
RE
53 hashed_pw = auth_tools.bcrypt_gen_password_hash(pw)
54 assert auth_tools.bcrypt_check_password(
7a98eb73 55 pw, hashed_pw)
3bcdc490 56 assert not auth_tools.bcrypt_check_password(
7a98eb73
RE
57 'notthepassword', hashed_pw)
58
59 # Same thing, extra salt.
3bcdc490
RE
60 hashed_pw = auth_tools.bcrypt_gen_password_hash(pw, '3><7R45417')
61 assert auth_tools.bcrypt_check_password(
7a98eb73 62 pw, hashed_pw, '3><7R45417')
3bcdc490 63 assert not auth_tools.bcrypt_check_password(
7a98eb73 64 'notthepassword', hashed_pw, '3><7R45417')
af665c4e
RE
65
66
67def test_change_password(self, test_app):
68 """Test changing password correctly and incorrectly"""
69 test_user = fixture_add_user(password=u'toast')
70
71 test_app.post(
72 '/auth/login/', {
73 'username': u'chris',
74 'password': u'toast'})
75
76 # test that the password can be changed
77 res = test_app.post(
78 '/edit/password/', {
79 'old_password': 'toast',
80 'new_password': '123456',
81 })
82 res.follow()
83
84 # Did we redirect to the correct page?
85 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
86
87 # test_user has to be fetched again in order to have the current values
88 test_user = User.query.filter_by(username=u'chris').first()
89 assert auth_tools.bcrypt_check_password('123456', test_user.pw_hash)
90
91 # test that the password cannot be changed if the given
92 # old_password is wrong
93 template.clear_test_template_context()
94 test_app.post(
95 '/edit/password/', {
96 'old_password': 'toast',
97 'new_password': '098765',
98 })
99
100 test_user = User.query.filter_by(username=u'chris').first()
101 assert not auth_tools.bcrypt_check_password('098765', test_user.pw_hash)