1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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 nose
.tools
import assert_equal
19 from mediagoblin
import mg_globals
20 from mediagoblin
.db
.models
import User
21 from mediagoblin
.tests
.tools
import fixture_add_user
22 from mediagoblin
.tools
import template
23 from mediagoblin
.auth
.lib
import bcrypt_check_password
25 class TestUserEdit(object):
28 self
.user_password
= u
'toast'
29 self
.user
= fixture_add_user(password
= self
.user_password
)
31 def login(self
, test_app
):
34 'username': self
.user
.username
,
35 'password': self
.user_password
})
38 def test_user_deletion(self
, test_app
):
39 """Delete user via web interface"""
42 # Make sure user exists
43 assert User
.query
.filter_by(username
=u
'chris').first()
45 res
= test_app
.post('/edit/account/delete/', {'confirmed': 'y'})
47 # Make sure user has been deleted
48 assert User
.query
.filter_by(username
=u
'chris').first() == None
50 #TODO: make sure all corresponding items comments etc have been
51 # deleted too. Perhaps in submission test?
53 #Restore user at end of test
54 self
.user
= fixture_add_user(password
= self
.user_password
)
58 def test_change_password(self
, test_app
):
59 """Test changing password correctly and incorrectly"""
62 # test that the password can be changed
63 # template.clear_test_template_context()
66 'old_password': 'toast',
67 'new_password': '123456',
68 'wants_comment_notification': 'y'
71 # Check for redirect on success
72 assert_equal(res
.status_int
, 302)
73 # test_user has to be fetched again in order to have the current values
74 test_user
= User
.query
.filter_by(username
=u
'chris').first()
75 assert bcrypt_check_password('123456', test_user
.pw_hash
)
76 # Update current user passwd
77 self
.user_password
= '123456'
79 # test that the password cannot be changed if the given
80 # old_password is wrong template.clear_test_template_context()
83 'old_password': 'toast',
84 'new_password': '098765',
87 test_user
= User
.query
.filter_by(username
=u
'chris').first()
88 assert not bcrypt_check_password('098765', test_user
.pw_hash
)
91 def test_change_bio_url(self
, test_app
):
92 """Test changing bio and URL"""
95 # Test if legacy profile editing URL redirects correctly
98 'bio': u
'I love toast!',
99 'url': u
'http://dustycloud.org/'}, expect_errors
=True)
101 # Should redirect to /u/chris/edit/
102 assert_equal (res
.status_int
, 302)
103 assert res
.headers
['Location'].endswith("/u/chris/edit/")
107 'bio': u
'I love toast!',
108 'url': u
'http://dustycloud.org/'})
110 test_user
= User
.query
.filter_by(username
=u
'chris').first()
111 assert_equal(test_user
.bio
, u
'I love toast!')
112 assert_equal(test_user
.url
, u
'http://dustycloud.org/')
114 # change a different user than the logged in (should fail with 403)
115 fixture_add_user(username
=u
"foo")
118 'bio': u
'I love toast!',
119 'url': u
'http://dustycloud.org/'}, expect_errors
=True)
120 assert_equal(res
.status_int
, 403)
122 # test changing the bio and the URL inproperly
123 too_long_bio
= 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
127 # more than 500 characters
129 'url': 'this-is-no-url'})
132 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/edit/edit_profile.html']
133 form
= context
['form']
135 assert_equal(form
.bio
.errors
, [u
'Field must be between 0 and 500 characters long.'])
136 assert_equal(form
.url
.errors
, [u
'This address contains errors'])
138 # test changing the url inproperly