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