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 import six.moves.urllib.parse as urlparse
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,
31 privileges=[u'active'])
33 def login(self, test_app):
36 'username': self.user.username,
37 'password': self.user_password})
40 def test_user_deletion(self, test_app):
41 """Delete user via web interface"""
44 # Make sure user exists
45 assert User.query.filter_by(username=u'chris').first()
47 res = test_app.post('/edit/account/delete/', {'confirmed': 'y'})
49 # Make sure user has been deleted
50 assert User.query.filter_by(username=u'chris').first() == None
52 #TODO: make sure all corresponding items comments etc have been
53 # deleted too. Perhaps in submission test?
55 #Restore user at end of test
56 self.user = fixture_add_user(password = self.user_password,
57 privileges=[u'active'])
61 def test_change_bio_url(self, test_app):
62 """Test changing bio and URL"""
65 # Test if legacy profile editing URL redirects correctly
68 'bio': u'I love toast!',
69 'url': u'http://dustycloud.org/'}, expect_errors=True)
71 # Should redirect to /u/chris/edit/
72 assert res.status_int == 302
73 assert res.headers['Location'].endswith("/u/chris/edit/")
77 'bio': u'I love toast!',
78 'url': u'http://dustycloud.org/'})
80 test_user = User.query.filter_by(username=u'chris').first()
81 assert test_user.bio == u'I love toast!'
82 assert test_user.url == u'http://dustycloud.org/'
84 # change a different user than the logged in (should fail with 403)
85 fixture_add_user(username=u"foo",
86 privileges=[u'active'])
89 'bio': u'I love toast!',
90 'url': u'http://dustycloud.org/'}, expect_errors=True)
91 assert res.status_int == 403
93 # test changing the bio and the URL inproperly
94 too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
98 # more than 500 characters
100 'url': 'this-is-no-url'})
103 context = template.TEMPLATE_TEST_CONTEXT[
104 'mediagoblin/edit/edit_profile.html']
105 form = context['form']
107 assert form.bio.errors == [
108 u'Field must be between 0 and 500 characters long.']
109 assert form.url.errors == [
110 u'This address contains errors']
112 def test_email_change(self, test_app):
115 # Test email already in db
116 template.clear_test_template_context()
119 'new_email': 'chris@example.com',
120 'password': 'toast'})
123 context = template.TEMPLATE_TEST_CONTEXT[
124 'mediagoblin/edit/change_email.html']
125 assert context['form'].new_email.errors == [
126 u'Sorry, a user with that email address already exists.']
128 # Test successful email change
129 template.clear_test_template_context()
132 'new_email': 'new@example.com',
133 'password': 'toast'})
137 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
139 # Make sure we get email verification and try verifying
140 assert len(mail.EMAIL_TEST_INBOX) == 1
141 message = mail.EMAIL_TEST_INBOX.pop()
142 assert message['To'] == 'new@example.com'
143 email_context = template.TEMPLATE_TEST_CONTEXT[
144 'mediagoblin/edit/verification.txt']
145 assert email_context['verification_url'].encode('ascii') in message.get_payload(decode=True)
147 path = urlparse.urlsplit(email_context['verification_url'])[2]
148 assert path == u'/edit/verify_email/'
150 ## Try verifying with bs verification key, shouldn't work
151 template.clear_test_template_context()
153 "/edit/verify_email/?token=total_bs")
157 assert urlparse.urlsplit(res.location)[2] == '/'
159 # Email shouldn't be saved
160 email_in_db = mg_globals.database.User.query.filter_by(
161 email='new@example.com').first()
162 email = User.query.filter_by(username='chris').first().email
163 assert email_in_db is None
164 assert email == 'chris@example.com'
166 # Verify email activation works
167 template.clear_test_template_context()
168 get_params = urlparse.urlsplit(email_context['verification_url'])[3]
169 res = test_app.get('%s?%s' % (path, get_params))
173 email = User.query.filter_by(username='chris').first().email
174 assert email == 'new@example.com'
175 # test changing the url inproperly