Commit | Line | Data |
---|---|---|
c8ccd23e | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
c8ccd23e JK |
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 | ||
eb396abc | 17 | import urlparse |
a3b98853 | 18 | |
c8ccd23e | 19 | from mediagoblin import mg_globals |
a3b98853 | 20 | from mediagoblin.db.models import User |
5c2ece74 | 21 | from mediagoblin.tests.tools import fixture_add_user |
b194f29f | 22 | from mediagoblin import auth |
377db0e7 | 23 | from mediagoblin.tools import template, mail |
fa723291 | 24 | |
c8ccd23e | 25 | |
a3b98853 | 26 | class TestUserEdit(object): |
958080be | 27 | def setup(self): |
a3b98853 SS |
28 | # set up new user |
29 | self.user_password = u'toast' | |
2c901db0 | 30 | self.user = fixture_add_user(password = self.user_password, |
31 | privileges=[u'active']) | |
a3b98853 | 32 | |
5c2ece74 CAW |
33 | def login(self, test_app): |
34 | test_app.post( | |
a3b98853 SS |
35 | '/auth/login/', { |
36 | 'username': self.user.username, | |
37 | 'password': self.user_password}) | |
38 | ||
39 | ||
5c2ece74 | 40 | def test_user_deletion(self, test_app): |
6deb589d | 41 | """Delete user via web interface""" |
5c2ece74 CAW |
42 | self.login(test_app) |
43 | ||
6deb589d SS |
44 | # Make sure user exists |
45 | assert User.query.filter_by(username=u'chris').first() | |
46 | ||
5c2ece74 | 47 | res = test_app.post('/edit/account/delete/', {'confirmed': 'y'}) |
6deb589d SS |
48 | |
49 | # Make sure user has been deleted | |
50 | assert User.query.filter_by(username=u'chris').first() == None | |
51 | ||
52 | #TODO: make sure all corresponding items comments etc have been | |
53 | # deleted too. Perhaps in submission test? | |
54 | ||
55 | #Restore user at end of test | |
2c901db0 | 56 | self.user = fixture_add_user(password = self.user_password, |
57 | privileges=[u'active']) | |
5c2ece74 | 58 | self.login(test_app) |
6deb589d SS |
59 | |
60 | ||
5c2ece74 | 61 | def test_change_bio_url(self, test_app): |
a3b98853 | 62 | """Test changing bio and URL""" |
5c2ece74 CAW |
63 | self.login(test_app) |
64 | ||
a3b98853 | 65 | # Test if legacy profile editing URL redirects correctly |
5c2ece74 | 66 | res = test_app.post( |
a3b98853 SS |
67 | '/edit/profile/', { |
68 | 'bio': u'I love toast!', | |
69 | 'url': u'http://dustycloud.org/'}, expect_errors=True) | |
70 | ||
71 | # Should redirect to /u/chris/edit/ | |
7d503a89 | 72 | assert res.status_int == 302 |
a3b98853 SS |
73 | assert res.headers['Location'].endswith("/u/chris/edit/") |
74 | ||
5c2ece74 | 75 | res = test_app.post( |
a3b98853 SS |
76 | '/u/chris/edit/', { |
77 | 'bio': u'I love toast!', | |
78 | 'url': u'http://dustycloud.org/'}) | |
79 | ||
80 | test_user = User.query.filter_by(username=u'chris').first() | |
7d503a89 CAW |
81 | assert test_user.bio == u'I love toast!' |
82 | assert test_user.url == u'http://dustycloud.org/' | |
a3b98853 SS |
83 | |
84 | # change a different user than the logged in (should fail with 403) | |
2c901db0 | 85 | fixture_add_user(username=u"foo", |
86 | privileges=[u'active']) | |
5c2ece74 | 87 | res = test_app.post( |
a3b98853 SS |
88 | '/u/foo/edit/', { |
89 | 'bio': u'I love toast!', | |
90 | 'url': u'http://dustycloud.org/'}, expect_errors=True) | |
7d503a89 | 91 | assert res.status_int == 403 |
a3b98853 SS |
92 | |
93 | # test changing the bio and the URL inproperly | |
94 | too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't' | |
95 | ||
5c2ece74 | 96 | test_app.post( |
a3b98853 SS |
97 | '/u/chris/edit/', { |
98 | # more than 500 characters | |
99 | 'bio': too_long_bio, | |
100 | 'url': 'this-is-no-url'}) | |
101 | ||
102 | # Check form errors | |
7d503a89 CAW |
103 | context = template.TEMPLATE_TEST_CONTEXT[ |
104 | 'mediagoblin/edit/edit_profile.html'] | |
a3b98853 SS |
105 | form = context['form'] |
106 | ||
7d503a89 CAW |
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'] | |
a3b98853 | 111 | |
377db0e7 RE |
112 | def test_email_change(self, test_app): |
113 | self.login(test_app) | |
114 | ||
377db0e7 RE |
115 | # Test email already in db |
116 | template.clear_test_template_context() | |
117 | test_app.post( | |
4710097b | 118 | '/edit/email/', { |
377db0e7 RE |
119 | 'new_email': 'chris@example.com', |
120 | 'password': 'toast'}) | |
121 | ||
122 | # Check form errors | |
123 | context = template.TEMPLATE_TEST_CONTEXT[ | |
4710097b | 124 | 'mediagoblin/edit/change_email.html'] |
377db0e7 RE |
125 | assert context['form'].new_email.errors == [ |
126 | u'Sorry, a user with that email address already exists.'] | |
127 | ||
377db0e7 RE |
128 | # Test successful email change |
129 | template.clear_test_template_context() | |
130 | res = test_app.post( | |
4710097b | 131 | '/edit/email/', { |
377db0e7 RE |
132 | 'new_email': 'new@example.com', |
133 | 'password': 'toast'}) | |
134 | res.follow() | |
135 | ||
136 | # Correct redirect? | |
4710097b | 137 | assert urlparse.urlsplit(res.location)[2] == '/edit/account/' |
377db0e7 RE |
138 | |
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'] in \ | |
146 | message.get_payload(decode=True) | |
147 | ||
148 | path = urlparse.urlsplit(email_context['verification_url'])[2] | |
149 | assert path == u'/edit/verify_email/' | |
150 | ||
151 | ## Try verifying with bs verification key, shouldn't work | |
152 | template.clear_test_template_context() | |
153 | res = test_app.get( | |
154 | "/edit/verify_email/?token=total_bs") | |
155 | res.follow() | |
156 | ||
157 | # Correct redirect? | |
158 | assert urlparse.urlsplit(res.location)[2] == '/' | |
159 | ||
160 | # Email shouldn't be saved | |
44082b12 RE |
161 | email_in_db = mg_globals.database.User.query.filter_by( |
162 | email='new@example.com').first() | |
377db0e7 RE |
163 | email = User.query.filter_by(username='chris').first().email |
164 | assert email_in_db is None | |
165 | assert email == 'chris@example.com' | |
166 | ||
167 | # Verify email activation works | |
168 | template.clear_test_template_context() | |
169 | get_params = urlparse.urlsplit(email_context['verification_url'])[3] | |
170 | res = test_app.get('%s?%s' % (path, get_params)) | |
171 | res.follow() | |
172 | ||
173 | # New email saved? | |
174 | email = User.query.filter_by(username='chris').first().email | |
175 | assert email == 'new@example.com' | |
a3b98853 | 176 | # test changing the url inproperly |