1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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/>.
17 from mediagoblin
import mg_globals
18 from mediagoblin
.tests
.tools
import setup_fresh_app
19 from mediagoblin
.tools
import template
20 from mediagoblin
.auth
.lib
import bcrypt_check_password
, \
21 bcrypt_gen_password_hash
25 def test_change_password(test_app
):
26 """Test changing password correctly and incorrectly"""
28 test_user
= mg_globals
.database
.User()
29 test_user
['username'] = u
'chris'
30 test_user
['email'] = u
'chris@example.com'
31 test_user
['email_verified'] = True
32 test_user
['status'] = u
'active'
33 test_user
['pw_hash'] = bcrypt_gen_password_hash('toast')
41 # test that the password can be changed
42 # template.clear_test_template_context()
47 'old_password': 'toast',
48 'new_password': '123456',
49 'confirm_password': '123456'})
51 # test_user has to be fetched again in order to have the current values
52 test_user
= mg_globals
.database
.User
.one({'username': 'chris'})
54 assert bcrypt_check_password('123456', test_user
['pw_hash'])
56 # test that the password cannot be changed if the given old_password
58 # template.clear_test_template_context()
63 'old_password': 'toast',
64 'new_password': '098765',
65 'confirm_password': '098765'})
67 test_user
= mg_globals
.database
.User
.one({'username': 'chris'})
69 assert not bcrypt_check_password('098765', test_user
['pw_hash'])
73 def change_bio_url(test_app
):
74 """Test changing bio and URL"""
76 test_user
= mg_globals
.database
.User()
77 test_user
['username'] = u
'chris'
78 test_user
['email'] = u
'chris@example.com'
79 test_user
['email_verified'] = True
80 test_user
['status'] = u
'active'
81 test_user
['pw_hash'] = bcrypt_gen_password_hash('toast')
84 # test changing the bio and the URL properly
87 'bio': u
'I love toast!',
88 'url': u
'http://dustycloud.org/'})
90 test_user
= mg_globals
.database
.User
.one({'username': 'chris'})
92 assert test_user
['bio'] == u
'I love toast!'
93 assert test_user
['url'] == u
'http://dustycloud.org/'
95 # test changing the bio and the URL inproperly
96 too_long_bio
= 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
100 # more than 500 characters
102 'url': 'this-is-no-url'})
104 test_user
= mg_globals
.database
.User
.one({'username': 'chris'})
106 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/edit/edit_profile.html']
107 form
= context
['edit_profile_form']
109 assert form
.bio
.errors
== [u
'Field must be between 0 and 500 characters long.']
110 assert form
.url
.errors
== [u
'Improperly formed URL']
112 # test changing the url inproperly