Merge commit 'refs/merge-requests/55' of git://gitorious.org/mediagoblin/mediagoblin...
[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_password(self, test_app):
60 """Test changing password correctly and incorrectly"""
61 self.login(test_app)
62
63 # test that the password can be changed
64 template.clear_test_template_context()
65 res = test_app.post(
66 '/edit/password/', {
67 'old_password': 'toast',
68 'new_password': '123456',
69 })
70 res.follow()
71
72 # Did we redirect to the correct page?
73 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
74
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'
80
81 # test that the password cannot be changed if the given
82 # old_password is wrong
83 template.clear_test_template_context()
84 test_app.post(
85 '/edit/password/', {
86 'old_password': 'toast',
87 'new_password': '098765',
88 })
89
90 test_user = User.query.filter_by(username=u'chris').first()
91 assert not auth.check_password('098765', test_user.pw_hash)
92
93
94 def test_change_bio_url(self, test_app):
95 """Test changing bio and URL"""
96 self.login(test_app)
97
98 # Test if legacy profile editing URL redirects correctly
99 res = test_app.post(
100 '/edit/profile/', {
101 'bio': u'I love toast!',
102 'url': u'http://dustycloud.org/'}, expect_errors=True)
103
104 # Should redirect to /u/chris/edit/
105 assert res.status_int == 302
106 assert res.headers['Location'].endswith("/u/chris/edit/")
107
108 res = test_app.post(
109 '/u/chris/edit/', {
110 'bio': u'I love toast!',
111 'url': u'http://dustycloud.org/'})
112
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/'
116
117 # change a different user than the logged in (should fail with 403)
118 fixture_add_user(username=u"foo")
119 res = test_app.post(
120 '/u/foo/edit/', {
121 'bio': u'I love toast!',
122 'url': u'http://dustycloud.org/'}, expect_errors=True)
123 assert res.status_int == 403
124
125 # test changing the bio and the URL inproperly
126 too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
127
128 test_app.post(
129 '/u/chris/edit/', {
130 # more than 500 characters
131 'bio': too_long_bio,
132 'url': 'this-is-no-url'})
133
134 # Check form errors
135 context = template.TEMPLATE_TEST_CONTEXT[
136 'mediagoblin/edit/edit_profile.html']
137 form = context['form']
138
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']
143
144 def test_email_change(self, test_app):
145 self.login(test_app)
146
147 # Test email already in db
148 template.clear_test_template_context()
149 test_app.post(
150 '/edit/account/', {
151 'new_email': 'chris@example.com',
152 'password': 'toast'})
153
154 # Check form errors
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.']
159
160 # Test successful email change
161 template.clear_test_template_context()
162 res = test_app.post(
163 '/edit/account/', {
164 'new_email': 'new@example.com',
165 'password': 'toast'})
166 res.follow()
167
168 # Correct redirect?
169 assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
170
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)
179
180 path = urlparse.urlsplit(email_context['verification_url'])[2]
181 assert path == u'/edit/verify_email/'
182
183 ## Try verifying with bs verification key, shouldn't work
184 template.clear_test_template_context()
185 res = test_app.get(
186 "/edit/verify_email/?token=total_bs")
187 res.follow()
188
189 # Correct redirect?
190 assert urlparse.urlsplit(res.location)[2] == '/'
191
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'
198
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))
203 res.follow()
204
205 # New email saved?
206 email = User.query.filter_by(username='chris').first().email
207 assert email == 'new@example.com'
208 # test changing the url inproperly