moved change_pass to basic_auth and fixed some typos with the moving of forgot pass
[mediagoblin.git] / mediagoblin / tests / test_edit.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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 import urlparse
18
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
24
25
26 class TestUserEdit(object):
27 def setup(self):
28 # set up new user
29 self.user_password = u'toast'
30 self.user = fixture_add_user(password = self.user_password)
31
32 def login(self, test_app):
33 test_app.post(
34 '/auth/login/', {
35 'username': self.user.username,
36 'password': self.user_password})
37
38
39 def test_user_deletion(self, test_app):
40 """Delete user via web interface"""
41 self.login(test_app)
42
43 # Make sure user exists
44 assert User.query.filter_by(username=u'chris').first()
45
46 res = test_app.post('/edit/account/delete/', {'confirmed': 'y'})
47
48 # Make sure user has been deleted
49 assert User.query.filter_by(username=u'chris').first() == None
50
51 #TODO: make sure all corresponding items comments etc have been
52 # deleted too. Perhaps in submission test?
53
54 #Restore user at end of test
55 self.user = fixture_add_user(password = self.user_password)
56 self.login(test_app)
57
58
59 def test_change_bio_url(self, test_app):
60 """Test changing bio and URL"""
61 self.login(test_app)
62
63 # Test if legacy profile editing URL redirects correctly
64 res = test_app.post(
65 '/edit/profile/', {
66 'bio': u'I love toast!',
67 'url': u'http://dustycloud.org/'}, expect_errors=True)
68
69 # Should redirect to /u/chris/edit/
70 assert res.status_int == 302
71 assert res.headers['Location'].endswith("/u/chris/edit/")
72
73 res = test_app.post(
74 '/u/chris/edit/', {
75 'bio': u'I love toast!',
76 'url': u'http://dustycloud.org/'})
77
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/'
81
82 # change a different user than the logged in (should fail with 403)
83 fixture_add_user(username=u"foo")
84 res = test_app.post(
85 '/u/foo/edit/', {
86 'bio': u'I love toast!',
87 'url': u'http://dustycloud.org/'}, expect_errors=True)
88 assert res.status_int == 403
89
90 # test changing the bio and the URL inproperly
91 too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
92
93 test_app.post(
94 '/u/chris/edit/', {
95 # more than 500 characters
96 'bio': too_long_bio,
97 'url': 'this-is-no-url'})
98
99 # Check form errors
100 context = template.TEMPLATE_TEST_CONTEXT[
101 'mediagoblin/edit/edit_profile.html']
102 form = context['form']
103
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']
108
109 def test_email_change(self, test_app):
110 self.login(test_app)
111
112 # Test email already in db
113 template.clear_test_template_context()
114 test_app.post(
115 '/edit/account/', {
116 'new_email': 'chris@example.com',
117 'password': 'toast'})
118
119 # Check form errors
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.']
124
125 # Test successful email change
126 template.clear_test_template_context()
127 res = test_app.post(
128 '/edit/account/', {
129 'new_email': 'new@example.com',
130 'password': 'toast'})
131 res.follow()
132
133 # Correct redirect?
134 assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
135
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)
144
145 path = urlparse.urlsplit(email_context['verification_url'])[2]
146 assert path == u'/edit/verify_email/'
147
148 ## Try verifying with bs verification key, shouldn't work
149 template.clear_test_template_context()
150 res = test_app.get(
151 "/edit/verify_email/?token=total_bs")
152 res.follow()
153
154 # Correct redirect?
155 assert urlparse.urlsplit(res.location)[2] == '/'
156
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'
163
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))
168 res.follow()
169
170 # New email saved?
171 email = User.query.filter_by(username='chris').first().email
172 assert email == 'new@example.com'
173 # test changing the url inproperly