1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.
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.
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/>.
19 from nose
.tools
import assert_equal
21 from mediagoblin
.auth
import lib
as auth_lib
22 from mediagoblin
.tests
.tools
import setup_fresh_app
23 from mediagoblin
import mg_globals
24 from mediagoblin
import util
27 ########################
28 # Test bcrypt auth funcs
29 ########################
31 def test_bcrypt_check_password():
32 # Check known 'lollerskates' password against check function
33 assert auth_lib
.bcrypt_check_password(
35 '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
37 assert not auth_lib
.bcrypt_check_password(
39 '$2a$12$PXU03zfrVCujBhVeICTwtOaHTUs5FFwsscvSSTJkqx/2RQ0Lhy/nO')
42 # Same thing, but with extra fake salt.
43 assert not auth_lib
.bcrypt_check_password(
45 '$2a$12$ELVlnw3z1FMu6CEGs/L8XO8vl0BuWSlUHgh0rUrry9DUXGMUNWwl6',
49 def test_bcrypt_gen_password_hash():
50 pw
= 'youwillneverguessthis'
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(
56 assert not auth_lib
.bcrypt_check_password(
57 'notthepassword', hashed_pw
)
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')
64 assert not auth_lib
.bcrypt_check_password(
65 'notthepassword', hashed_pw
, '3><7R45417')
69 def test_register_views(test_app
):
71 Massive test function that all our registration-related views all work.
73 # Test doing a simple GET on the page
74 # -----------------------------------
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')
81 # Try to register without providing anything, should error
82 # --------------------------------------------------------
84 util
.clear_test_template_context()
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.']
94 # Try to register with fields that are known to be invalid
95 # --------------------------------------------------------
98 util
.clear_test_template_context()
103 'confirm_password': 'o',
105 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
106 form
= context
['register_form']
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.']
114 util
.clear_test_template_context()
118 'email': 'lollerskates'})
119 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
120 form
= context
['register_form']
122 assert form
.username
.errors
== [
124 assert form
.email
.errors
== [
125 u
'Invalid email address.']
127 ## mismatching passwords
128 util
.clear_test_template_context()
131 'password': 'herpderp',
132 'confirm_password': 'derpherp'})
133 context
= util
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
134 form
= context
['register_form']
136 assert form
.password
.errors
== [
137 u
'Passwords must match.']
139 ## At this point there should be no users in the database ;)
140 assert not mg_globals
.database
.User
.find().count()
142 # Successful register
143 # -------------------
144 util
.clear_test_template_context()
145 response
= test_app
.post(
147 'username': 'happygirl',
148 'password': 'iamsohappy',
149 'confirm_password': 'iamsohappy',
150 'email': 'happygrrl@example.org'})
153 ## Did we redirect to the proper page? Use the right template?
155 urlparse
.urlsplit(response
.location
)[2],
156 '/auth/register/success/')
157 assert util
.TEMPLATE_TEST_CONTEXT
.has_key(
158 'mediagoblin/auth/register_success.html')
160 ## Make sure user is in place
161 new_user
= mg_globals
.database
.User
.find_one(
162 {'username': 'happygirl'})
164 assert new_user
['status'] == u
'needs_email_verification'
165 assert new_user
['email_verified'] == False
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)
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
)
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']]
186 ## Try verifying with bs verification key, shouldn't work
187 util
.clear_test_template_context()
189 "/auth/verify_email/?userid=%s&token=total_bs" % unicode(
191 context
= util
.TEMPLATE_TEST_CONTEXT
[
192 'mediagoblin/auth/verify_email.html']
193 assert context
['verification_successful'] == False
194 new_user
= mg_globals
.database
.User
.find_one(
195 {'username': 'happygirl'})
197 assert new_user
['status'] == u
'needs_email_verification'
198 assert new_user
['email_verified'] == False
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
206 new_user
= mg_globals
.database
.User
.find_one(
207 {'username': 'happygirl'})
209 assert new_user
['status'] == u
'active'
210 assert new_user
['email_verified'] == True
212 ## TODO: Try logging in
216 ## We shouldn't be able to register with that user twice
217 util
.clear_test_template_context()
218 response
= test_app
.post(
220 'username': 'happygirl',
221 'password': 'iamsohappy2',
222 'confirm_password': 'iamsohappy2',
223 'email': 'happygrrl2@example.org'})
225 context
= util
.TEMPLATE_TEST_CONTEXT
[
226 'mediagoblin/auth/register.html']
227 form
= context
['register_form']
228 assert form
.username
.errors
== [
229 u
'Sorry, a user with that name already exists.']
231 ## TODO: Also check for double instances of an email address?