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/>.
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
import auth
23 from mediagoblin
.tools
import template
, mail
26 class TestUserEdit(object):
29 self
.user_password
= u
'toast'
30 self
.user
= fixture_add_user(password
= self
.user_password
)
32 def login(self
, test_app
):
35 'username': self
.user
.username
,
36 'password': self
.user_password
})
39 def test_user_deletion(self
, test_app
):
40 """Delete user via web interface"""
43 # Make sure user exists
44 assert User
.query
.filter_by(username
=u
'chris').first()
46 res
= test_app
.post('/edit/account/delete/', {'confirmed': 'y'})
48 # Make sure user has been deleted
49 assert User
.query
.filter_by(username
=u
'chris').first() == None
51 #TODO: make sure all corresponding items comments etc have been
52 # deleted too. Perhaps in submission test?
54 #Restore user at end of test
55 self
.user
= fixture_add_user(password
= self
.user_password
)
59 def test_change_password(self
, test_app
):
60 """Test changing password correctly and incorrectly"""
63 # test that the password can be changed
64 template
.clear_test_template_context()
67 'old_password': 'toast',
68 'new_password': '123456',
72 # Did we redirect to the correct page?
73 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
75 # test_user has to be fetched again in order to have the current values
76 test_user
= User
.query
.filter_by(username
=u
'chris').first()
77 assert auth
.check_password('123456', test_user
.pw_hash
)
78 # Update current user passwd
79 self
.user_password
= '123456'
81 # test that the password cannot be changed if the given
82 # old_password is wrong
83 template
.clear_test_template_context()
86 'old_password': 'toast',
87 'new_password': '098765',
90 test_user
= User
.query
.filter_by(username
=u
'chris').first()
91 assert not auth
.check_password('098765', test_user
.pw_hash
)
94 def test_change_bio_url(self
, test_app
):
95 """Test changing bio and URL"""
98 # Test if legacy profile editing URL redirects correctly
101 'bio': u
'I love toast!',
102 'url': u
'http://dustycloud.org/'}, expect_errors
=True)
104 # Should redirect to /u/chris/edit/
105 assert res
.status_int
== 302
106 assert res
.headers
['Location'].endswith("/u/chris/edit/")
110 'bio': u
'I love toast!',
111 'url': u
'http://dustycloud.org/'})
113 test_user
= User
.query
.filter_by(username
=u
'chris').first()
114 assert test_user
.bio
== u
'I love toast!'
115 assert test_user
.url
== u
'http://dustycloud.org/'
117 # change a different user than the logged in (should fail with 403)
118 fixture_add_user(username
=u
"foo")
121 'bio': u
'I love toast!',
122 'url': u
'http://dustycloud.org/'}, expect_errors
=True)
123 assert res
.status_int
== 403
125 # test changing the bio and the URL inproperly
126 too_long_bio
= 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
130 # more than 500 characters
132 'url': 'this-is-no-url'})
135 context
= template
.TEMPLATE_TEST_CONTEXT
[
136 'mediagoblin/edit/edit_profile.html']
137 form
= context
['form']
139 assert form
.bio
.errors
== [
140 u
'Field must be between 0 and 500 characters long.']
141 assert form
.url
.errors
== [
142 u
'This address contains errors']
144 def test_email_change(self
, test_app
):
147 # Test email already in db
148 template
.clear_test_template_context()
151 'new_email': 'chris@example.com',
152 'password': 'toast'})
155 context
= template
.TEMPLATE_TEST_CONTEXT
[
156 'mediagoblin/edit/edit_account.html']
157 assert context
['form'].new_email
.errors
== [
158 u
'Sorry, a user with that email address already exists.']
160 # Test successful email change
161 template
.clear_test_template_context()
164 'new_email': 'new@example.com',
165 'password': 'toast'})
169 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
171 # Make sure we get email verification and try verifying
172 assert len(mail
.EMAIL_TEST_INBOX
) == 1
173 message
= mail
.EMAIL_TEST_INBOX
.pop()
174 assert message
['To'] == 'new@example.com'
175 email_context
= template
.TEMPLATE_TEST_CONTEXT
[
176 'mediagoblin/edit/verification.txt']
177 assert email_context
['verification_url'] in \
178 message
.get_payload(decode
=True)
180 path
= urlparse
.urlsplit(email_context
['verification_url'])[2]
181 assert path
== u
'/edit/verify_email/'
183 ## Try verifying with bs verification key, shouldn't work
184 template
.clear_test_template_context()
186 "/edit/verify_email/?token=total_bs")
190 assert urlparse
.urlsplit(res
.location
)[2] == '/'
192 # Email shouldn't be saved
193 email_in_db
= mg_globals
.database
.User
.query
.filter_by(
194 email
='new@example.com').first()
195 email
= User
.query
.filter_by(username
='chris').first().email
196 assert email_in_db
is None
197 assert email
== 'chris@example.com'
199 # Verify email activation works
200 template
.clear_test_template_context()
201 get_params
= urlparse
.urlsplit(email_context
['verification_url'])[3]
202 res
= test_app
.get('%s?%s' % (path
, get_params
))
206 email
= User
.query
.filter_by(username
='chris').first().email
207 assert email
== 'new@example.com'
208 # test changing the url inproperly