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_bio_url(self
, test_app
):
60 """Test changing bio and URL"""
63 # Test if legacy profile editing URL redirects correctly
66 'bio': u
'I love toast!',
67 'url': u
'http://dustycloud.org/'}, expect_errors
=True)
69 # Should redirect to /u/chris/edit/
70 assert res
.status_int
== 302
71 assert res
.headers
['Location'].endswith("/u/chris/edit/")
75 'bio': u
'I love toast!',
76 'url': u
'http://dustycloud.org/'})
78 test_user
= User
.query
.filter_by(username
=u
'chris').first()
79 assert test_user
.bio
== u
'I love toast!'
80 assert test_user
.url
== u
'http://dustycloud.org/'
82 # change a different user than the logged in (should fail with 403)
83 fixture_add_user(username
=u
"foo")
86 'bio': u
'I love toast!',
87 'url': u
'http://dustycloud.org/'}, expect_errors
=True)
88 assert res
.status_int
== 403
90 # test changing the bio and the URL inproperly
91 too_long_bio
= 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
95 # more than 500 characters
97 'url': 'this-is-no-url'})
100 context
= template
.TEMPLATE_TEST_CONTEXT
[
101 'mediagoblin/edit/edit_profile.html']
102 form
= context
['form']
104 assert form
.bio
.errors
== [
105 u
'Field must be between 0 and 500 characters long.']
106 assert form
.url
.errors
== [
107 u
'This address contains errors']
109 def test_email_change(self
, test_app
):
112 # Test email already in db
113 template
.clear_test_template_context()
116 'new_email': 'chris@example.com',
117 'password': 'toast'})
120 context
= template
.TEMPLATE_TEST_CONTEXT
[
121 'mediagoblin/edit/edit_account.html']
122 assert context
['form'].new_email
.errors
== [
123 u
'Sorry, a user with that email address already exists.']
125 # Test successful email change
126 template
.clear_test_template_context()
129 'new_email': 'new@example.com',
130 'password': 'toast'})
134 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
136 # Make sure we get email verification and try verifying
137 assert len(mail
.EMAIL_TEST_INBOX
) == 1
138 message
= mail
.EMAIL_TEST_INBOX
.pop()
139 assert message
['To'] == 'new@example.com'
140 email_context
= template
.TEMPLATE_TEST_CONTEXT
[
141 'mediagoblin/edit/verification.txt']
142 assert email_context
['verification_url'] in \
143 message
.get_payload(decode
=True)
145 path
= urlparse
.urlsplit(email_context
['verification_url'])[2]
146 assert path
== u
'/edit/verify_email/'
148 ## Try verifying with bs verification key, shouldn't work
149 template
.clear_test_template_context()
151 "/edit/verify_email/?token=total_bs")
155 assert urlparse
.urlsplit(res
.location
)[2] == '/'
157 # Email shouldn't be saved
158 email_in_db
= mg_globals
.database
.User
.query
.filter_by(
159 email
='new@example.com').first()
160 email
= User
.query
.filter_by(username
='chris').first().email
161 assert email_in_db
is None
162 assert email
== 'chris@example.com'
164 # Verify email activation works
165 template
.clear_test_template_context()
166 get_params
= urlparse
.urlsplit(email_context
['verification_url'])[3]
167 res
= test_app
.get('%s?%s' % (path
, get_params
))
171 email
= User
.query
.filter_by(username
='chris').first().email
172 assert email
== 'new@example.com'
173 # test changing the url inproperly