Merge remote branch 'refs/remotes/dneelyeps/master'
[mediagoblin.git] / mediagoblin / tests / test_auth.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
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
17
18 from mediagoblin.auth import lib as auth_lib
19
20
21 ########################
22 # Test bcrypt auth funcs
23 ########################
24
25 def test_bcrypt_check_password():
26 # Check known 'lollerskates' password against check function
27 assert auth_lib.bcrypt_check_password(
28 'lollerskates',
29 '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
30
31 assert not auth_lib.bcrypt_check_password(
32 'notthepassword',
33 '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
34
35
36 # Same thing, but with extra fake salt.
37 assert not auth_lib.bcrypt_check_password(
38 'notthepassword',
39 '$2a$12$ELVlnw3z1FMu6CEGs/L8XO8vl0BuWSlUHgh0rUrry9DUXGMUNWwl6',
40 '3><7R45417')
41
42
43 def test_bcrypt_gen_password_hash():
44 pw = 'youwillneverguessthis'
45
46 # Normal password hash generation, and check on that hash
47 hashed_pw = auth_lib.bcrypt_gen_password_hash(pw)
48 assert auth_lib.bcrypt_check_password(
49 pw, hashed_pw)
50 assert not auth_lib.bcrypt_check_password(
51 'notthepassword', hashed_pw)
52
53
54 # Same thing, extra salt.
55 hashed_pw = auth_lib.bcrypt_gen_password_hash(pw, '3><7R45417')
56 assert auth_lib.bcrypt_check_password(
57 pw, hashed_pw, '3><7R45417')
58 assert not auth_lib.bcrypt_check_password(
59 'notthepassword', hashed_pw, '3><7R45417')