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 | ||
17 | ||
18 | from mediagoblin.auth import lib as auth_lib | |
19 | ||
460ce564 CAW |
20 | from mediagoblin.tests.tools import get_test_app |
21 | ||
651403f0 | 22 | from mediagoblin import globals as mgoblin_globals |
460ce564 CAW |
23 | from mediagoblin import util |
24 | ||
4b5f4e87 CAW |
25 | |
26 | ######################## | |
27 | # Test bcrypt auth funcs | |
28 | ######################## | |
29 | ||
30 | def test_bcrypt_check_password(): | |
31 | # Check known 'lollerskates' password against check function | |
32 | assert auth_lib.bcrypt_check_password( | |
33 | 'lollerskates', | |
34 | '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO') | |
35 | ||
db780024 CAW |
36 | assert not auth_lib.bcrypt_check_password( |
37 | 'notthepassword', | |
38 | '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO') | |
39 | ||
40 | ||
4b5f4e87 | 41 | # Same thing, but with extra fake salt. |
db780024 CAW |
42 | assert not auth_lib.bcrypt_check_password( |
43 | 'notthepassword', | |
4b5f4e87 CAW |
44 | '$2a$12$ELVlnw3z1FMu6CEGs/L8XO8vl0BuWSlUHgh0rUrry9DUXGMUNWwl6', |
45 | '3><7R45417') | |
46 | ||
47 | ||
48 | def test_bcrypt_gen_password_hash(): | |
49 | pw = 'youwillneverguessthis' | |
50 | ||
51 | # Normal password hash generation, and check on that hash | |
52 | hashed_pw = auth_lib.bcrypt_gen_password_hash(pw) | |
53 | assert auth_lib.bcrypt_check_password( | |
54 | pw, hashed_pw) | |
db780024 CAW |
55 | assert not auth_lib.bcrypt_check_password( |
56 | 'notthepassword', hashed_pw) | |
57 | ||
4b5f4e87 CAW |
58 | |
59 | # Same thing, extra salt. | |
60 | hashed_pw = auth_lib.bcrypt_gen_password_hash(pw, '3><7R45417') | |
61 | assert auth_lib.bcrypt_check_password( | |
62 | pw, hashed_pw, '3><7R45417') | |
db780024 CAW |
63 | assert not auth_lib.bcrypt_check_password( |
64 | 'notthepassword', hashed_pw, '3><7R45417') | |
460ce564 CAW |
65 | |
66 | ||
67 | def test_register_views(): | |
68 | util.clear_test_template_context() | |
69 | test_app = get_test_app() | |
70 | ||
71 | # Test doing a simple GET on the page | |
651403f0 CAW |
72 | # ----------------------------------- |
73 | ||
460ce564 CAW |
74 | test_app.get('/auth/register/') |
75 | # Make sure it rendered with the appropriate template | |
76 | assert util.TEMPLATE_TEST_CONTEXT.has_key( | |
77 | 'mediagoblin/auth/register.html') | |
78 | ||
79 | # Try to register without providing anything, should error | |
651403f0 CAW |
80 | # -------------------------------------------------------- |
81 | ||
460ce564 CAW |
82 | util.clear_test_template_context() |
83 | test_app.post( | |
84 | '/auth/register/', {}) | |
85 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
86 | form = context['register_form'] | |
87 | assert form.username.errors == [u'This field is required.'] | |
88 | assert form.password.errors == [u'This field is required.'] | |
89 | assert form.confirm_password.errors == [u'This field is required.'] | |
90 | assert form.email.errors == [u'This field is required.'] | |
651403f0 CAW |
91 | |
92 | # Try to register with fields that are known to be invalid | |
93 | # -------------------------------------------------------- | |
94 | ||
95 | ## too short | |
96 | util.clear_test_template_context() | |
97 | test_app.post( | |
98 | '/auth/register/', { | |
99 | 'username': 'l', | |
100 | 'password': 'o', | |
101 | 'confirm_password': 'o', | |
102 | 'email': 'l'}) | |
103 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
104 | form = context['register_form'] | |
105 | ||
106 | assert form.username.errors == [ | |
107 | u'Field must be between 3 and 30 characters long.'] | |
108 | assert form.password.errors == [ | |
109 | u'Field must be between 6 and 30 characters long.'] | |
110 | ||
111 | ## bad form | |
112 | util.clear_test_template_context() | |
113 | test_app.post( | |
114 | '/auth/register/', { | |
115 | 'username': '@_@', | |
116 | 'email': 'lollerskates'}) | |
117 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
118 | form = context['register_form'] | |
119 | ||
120 | assert form.username.errors == [ | |
121 | u'Invalid input.'] | |
122 | assert form.email.errors == [ | |
123 | u'Invalid email address.'] | |
124 | ||
125 | ## mismatching passwords | |
126 | util.clear_test_template_context() | |
127 | test_app.post( | |
128 | '/auth/register/', { | |
129 | 'password': 'herpderp', | |
130 | 'confirm_password': 'derpherp'}) | |
131 | context = util.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] | |
132 | form = context['register_form'] | |
133 | ||
134 | assert form.password.errors == [ | |
135 | u'Passwords must match.'] | |
136 | ||
137 | ## At this point there should be no users in the database ;) | |
138 | assert not mgoblin_globals.database.User.find().count() | |
139 | ||
140 | # Successful register | |
141 | # ------------------- | |
142 | ## Did we redirect to the proper page? Use the right template? | |
143 | ## Make sure user is in place | |
144 | ## Make sure we get email confirmation | |
145 | ## Try logging in | |
146 | ||
cb9bac0c CAW |
147 | # Uniqueness checks |
148 | # ----------------- | |
149 | ## We shouldn't be able to register with that user twice | |
651403f0 | 150 | |
cb9bac0c | 151 | ## Also check for double instances of an email address |