Merge remote branch 'remotes/aaronw/bug614_verification_crash'
[mediagoblin.git] / mediagoblin / tests / test_edit.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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/>.
16
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
22
23
24 @setup_fresh_app
25 def test_change_password(test_app):
26 """Test changing password correctly and incorrectly"""
27 # set up new user
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')
34 test_user.save()
35
36 test_app.post(
37 '/auth/login/', {
38 'username': u'chris',
39 'password': 'toast'})
40
41 # test that the password can be changed
42 # template.clear_test_template_context()
43 test_app.post(
44 '/edit/profile/', {
45 'bio': u'',
46 'url': u'',
47 'old_password': 'toast',
48 'new_password': '123456',
49 'confirm_password': '123456'})
50
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'})
53
54 assert bcrypt_check_password('123456', test_user['pw_hash'])
55
56 # test that the password cannot be changed if the given old_password
57 # is wrong
58 # template.clear_test_template_context()
59 test_app.post(
60 '/edit/profile/', {
61 'bio': u'',
62 'url': u'',
63 'old_password': 'toast',
64 'new_password': '098765',
65 'confirm_password': '098765'})
66
67 test_user = mg_globals.database.User.one({'username': 'chris'})
68
69 assert not bcrypt_check_password('098765', test_user['pw_hash'])
70
71
72 @setup_fresh_app
73 def change_bio_url(test_app):
74 """Test changing bio and URL"""
75 # set up new user
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')
82 test_user.save()
83
84 # test changing the bio and the URL properly
85 test_app.post(
86 '/edit/profile/', {
87 'bio': u'I love toast!',
88 'url': u'http://dustycloud.org/'})
89
90 test_user = mg_globals.database.User.one({'username': 'chris'})
91
92 assert test_user['bio'] == u'I love toast!'
93 assert test_user['url'] == u'http://dustycloud.org/'
94
95 # test changing the bio and the URL inproperly
96 too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
97
98 test_app.post(
99 '/edit/profile/', {
100 # more than 500 characters
101 'bio': too_long_bio,
102 'url': 'this-is-no-url'})
103
104 test_user = mg_globals.database.User.one({'username': 'chris'})
105
106 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/edit/edit_profile.html']
107 form = context['edit_profile_form']
108
109 assert form.bio.errors == [u'Field must be between 0 and 500 characters long.']
110 assert form.url.errors == [u'Improperly formed URL']
111
112 # test changing the url inproperly