Commit | Line | Data |
---|---|---|
515e3bd9 | 1 | |
8e1e744d | 2 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 3 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
4b5f4e87 CAW |
4 | # |
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU Affero General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU Affero General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
fd19da34 | 17 | |
dfd3f561 RE |
18 | import pkg_resources |
19 | import pytest | |
4b5f4e87 | 20 | |
e49b7e02 BP |
21 | import six |
22 | ||
fd19da34 BP |
23 | import six.moves.urllib.parse as urlparse |
24 | ||
7e55bcb8 | 25 | from mediagoblin import mg_globals |
b0c8328e | 26 | from mediagoblin.db.models import User |
dfd3f561 | 27 | from mediagoblin.tests.tools import get_app, fixture_add_user |
152a3bfa | 28 | from mediagoblin.tools import template, mail |
3b8c733b | 29 | from mediagoblin.auth import tools as auth_tools |
460ce564 | 30 | |
4b5f4e87 | 31 | |
1be247b3 | 32 | def test_register_views(test_app): |
2fecc29d CAW |
33 | """ |
34 | Massive test function that all our registration-related views all work. | |
35 | """ | |
460ce564 | 36 | # Test doing a simple GET on the page |
651403f0 CAW |
37 | # ----------------------------------- |
38 | ||
460ce564 CAW |
39 | test_app.get('/auth/register/') |
40 | # Make sure it rendered with the appropriate template | |
04453ccf | 41 | assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT |
757690cc | 42 | |
460ce564 | 43 | # Try to register without providing anything, should error |
651403f0 CAW |
44 | # -------------------------------------------------------- |
45 | ||
ae3bc7fa | 46 | template.clear_test_template_context() |
460ce564 CAW |
47 | test_app.post( |
48 | '/auth/register/', {}) | |
ae3bc7fa | 49 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] |
460ce564 CAW |
50 | form = context['register_form'] |
51 | assert form.username.errors == [u'This field is required.'] | |
52 | assert form.password.errors == [u'This field is required.'] | |
460ce564 | 53 | assert form.email.errors == [u'This field is required.'] |
651403f0 CAW |
54 | |
55 | # Try to register with fields that are known to be invalid | |
56 | # -------------------------------------------------------- | |
57 | ||
58 | ## too short | |
ae3bc7fa | 59 | template.clear_test_template_context() |
651403f0 CAW |
60 | test_app.post( |
61 | '/auth/register/', { | |
62 | 'username': 'l', | |
63 | 'password': 'o', | |
651403f0 | 64 | 'email': 'l'}) |
ae3bc7fa | 65 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] |
651403f0 CAW |
66 | form = context['register_form'] |
67 | ||
7d503a89 CAW |
68 | assert form.username.errors == [u'Field must be between 3 and 30 characters long.'] |
69 | assert form.password.errors == [u'Field must be between 5 and 1024 characters long.'] | |
651403f0 CAW |
70 | |
71 | ## bad form | |
ae3bc7fa | 72 | template.clear_test_template_context() |
651403f0 CAW |
73 | test_app.post( |
74 | '/auth/register/', { | |
75 | 'username': '@_@', | |
76 | 'email': 'lollerskates'}) | |
ae3bc7fa | 77 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html'] |
651403f0 CAW |
78 | form = context['register_form'] |
79 | ||
7d503a89 CAW |
80 | assert form.username.errors == [u'This field does not take email addresses.'] |
81 | assert form.email.errors == [u'This field requires an email address.'] | |
651403f0 | 82 | |
651403f0 | 83 | ## At this point there should be no users in the database ;) |
7d503a89 | 84 | assert User.query.count() == 0 |
651403f0 CAW |
85 | |
86 | # Successful register | |
87 | # ------------------- | |
ae3bc7fa | 88 | template.clear_test_template_context() |
1972a888 CAW |
89 | response = test_app.post( |
90 | '/auth/register/', { | |
e1561d04 | 91 | 'username': u'angrygirl', |
92 | 'password': 'iamsoangry', | |
93 | 'email': 'angrygrrl@example.org'}) | |
1972a888 CAW |
94 | response.follow() |
95 | ||
651403f0 | 96 | ## Did we redirect to the proper page? Use the right template? |
e1561d04 | 97 | assert urlparse.urlsplit(response.location)[2] == '/u/angrygirl/' |
515e3bd9 | 98 | assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT |
1972a888 | 99 | |
651403f0 | 100 | ## Make sure user is in place |
44082b12 | 101 | new_user = mg_globals.database.User.query.filter_by( |
e1561d04 | 102 | username=u'angrygirl').first() |
1972a888 | 103 | assert new_user |
1972a888 | 104 | |
8e91df87 | 105 | ## Make sure that the proper privileges are granted on registration |
106 | ||
107 | assert new_user.has_privilege(u'commenter') | |
108 | assert new_user.has_privilege(u'uploader') | |
109 | assert new_user.has_privilege(u'reporter') | |
110 | assert not new_user.has_privilege(u'active') | |
f73f4c4b | 111 | ## Make sure user is logged in |
ae3bc7fa | 112 | request = template.TEMPLATE_TEST_CONTEXT[ |
515e3bd9 | 113 | 'mediagoblin/user_pages/user_nonactive.html']['request'] |
e49b7e02 | 114 | assert request.session['user_id'] == six.text_type(new_user.id) |
f73f4c4b | 115 | |
1972a888 | 116 | ## Make sure we get email confirmation, and try verifying |
152a3bfa AW |
117 | assert len(mail.EMAIL_TEST_INBOX) == 1 |
118 | message = mail.EMAIL_TEST_INBOX.pop() | |
e1561d04 | 119 | assert message['To'] == 'angrygrrl@example.org' |
ae3bc7fa | 120 | email_context = template.TEMPLATE_TEST_CONTEXT[ |
1972a888 | 121 | 'mediagoblin/auth/verification_email.txt'] |
cda3055b | 122 | assert email_context['verification_url'].encode('ascii') in message.get_payload(decode=True) |
1972a888 CAW |
123 | |
124 | path = urlparse.urlsplit(email_context['verification_url'])[2] | |
125 | get_params = urlparse.urlsplit(email_context['verification_url'])[3] | |
126 | assert path == u'/auth/verify_email/' | |
127 | parsed_get_params = urlparse.parse_qs(get_params) | |
128 | ||
7b1e17ed | 129 | ## Try verifying with bs verification key, shouldn't work |
ae3bc7fa | 130 | template.clear_test_template_context() |
a656ccd5 | 131 | response = test_app.get( |
342f06f7 | 132 | "/auth/verify_email/?token=total_bs") |
a656ccd5 | 133 | response.follow() |
342f06f7 RE |
134 | |
135 | # Correct redirect? | |
136 | assert urlparse.urlsplit(response.location)[2] == '/' | |
137 | ||
a656ccd5 CAW |
138 | # assert context['verification_successful'] == True |
139 | # TODO: Would be good to test messages here when we can do so... | |
44082b12 | 140 | new_user = mg_globals.database.User.query.filter_by( |
e1561d04 | 141 | username=u'angrygirl').first() |
7b1e17ed | 142 | assert new_user |
7b1e17ed CAW |
143 | |
144 | ## Verify the email activation works | |
ae3bc7fa | 145 | template.clear_test_template_context() |
a656ccd5 CAW |
146 | response = test_app.get("%s?%s" % (path, get_params)) |
147 | response.follow() | |
ae3bc7fa | 148 | context = template.TEMPLATE_TEST_CONTEXT[ |
e054ae9b | 149 | 'mediagoblin/user_pages/user.html'] |
a656ccd5 CAW |
150 | # assert context['verification_successful'] == True |
151 | # TODO: Would be good to test messages here when we can do so... | |
44082b12 | 152 | new_user = mg_globals.database.User.query.filter_by( |
e1561d04 | 153 | username=u'angrygirl').first() |
7b1e17ed | 154 | assert new_user |
1972a888 | 155 | |
cb9bac0c CAW |
156 | # Uniqueness checks |
157 | # ----------------- | |
158 | ## We shouldn't be able to register with that user twice | |
ae3bc7fa | 159 | template.clear_test_template_context() |
8a869db8 CAW |
160 | response = test_app.post( |
161 | '/auth/register/', { | |
e1561d04 | 162 | 'username': u'angrygirl', |
163 | 'password': 'iamsoangry2', | |
164 | 'email': 'angrygrrl2@example.org'}) | |
757690cc | 165 | |
ae3bc7fa | 166 | context = template.TEMPLATE_TEST_CONTEXT[ |
8a869db8 CAW |
167 | 'mediagoblin/auth/register.html'] |
168 | form = context['register_form'] | |
169 | assert form.username.errors == [ | |
170 | u'Sorry, a user with that name already exists.'] | |
651403f0 | 171 | |
8a869db8 | 172 | ## TODO: Also check for double instances of an email address? |
757690cc | 173 | |
f339b76a RE |
174 | ### Oops, forgot the password |
175 | # ------------------- | |
176 | template.clear_test_template_context() | |
177 | response = test_app.post( | |
178 | '/auth/forgot_password/', | |
e1561d04 | 179 | {'username': u'angrygirl'}) |
f339b76a RE |
180 | response.follow() |
181 | ||
182 | ## Did we redirect to the proper page? Use the right template? | |
183 | assert urlparse.urlsplit(response.location)[2] == '/auth/login/' | |
184 | assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT | |
185 | ||
186 | ## Make sure link to change password is sent by email | |
187 | assert len(mail.EMAIL_TEST_INBOX) == 1 | |
188 | message = mail.EMAIL_TEST_INBOX.pop() | |
e1561d04 | 189 | assert message['To'] == 'angrygrrl@example.org' |
f339b76a | 190 | email_context = template.TEMPLATE_TEST_CONTEXT[ |
af665c4e | 191 | 'mediagoblin/plugins/basic_auth/fp_verification_email.txt'] |
f339b76a | 192 | #TODO - change the name of verification_url to something forgot-password-ish |
cda3055b | 193 | assert email_context['verification_url'].encode('ascii') in message.get_payload(decode=True) |
f339b76a RE |
194 | |
195 | path = urlparse.urlsplit(email_context['verification_url'])[2] | |
196 | get_params = urlparse.urlsplit(email_context['verification_url'])[3] | |
f339b76a | 197 | parsed_get_params = urlparse.parse_qs(get_params) |
342f06f7 | 198 | assert path == u'/auth/forgot_password/verify/' |
f339b76a RE |
199 | |
200 | ## Try using a bs password-changing verification key, shouldn't work | |
201 | template.clear_test_template_context() | |
202 | response = test_app.get( | |
342f06f7 RE |
203 | "/auth/forgot_password/verify/?token=total_bs") |
204 | response.follow() | |
f339b76a | 205 | |
342f06f7 RE |
206 | # Correct redirect? |
207 | assert urlparse.urlsplit(response.location)[2] == '/' | |
f339b76a RE |
208 | |
209 | ## Verify step 1 of password-change works -- can see form to change password | |
210 | template.clear_test_template_context() | |
211 | response = test_app.get("%s?%s" % (path, get_params)) | |
af665c4e RE |
212 | assert 'mediagoblin/plugins/basic_auth/change_fp.html' in \ |
213 | template.TEMPLATE_TEST_CONTEXT | |
f339b76a RE |
214 | |
215 | ## Verify step 2.1 of password-change works -- report success to user | |
216 | template.clear_test_template_context() | |
217 | response = test_app.post( | |
218 | '/auth/forgot_password/verify/', { | |
e1561d04 | 219 | 'password': 'iamveryveryangry', |
f339b76a RE |
220 | 'token': parsed_get_params['token']}) |
221 | response.follow() | |
222 | assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT | |
223 | ||
224 | ## Verify step 2.2 of password-change works -- login w/ new password success | |
225 | template.clear_test_template_context() | |
226 | response = test_app.post( | |
227 | '/auth/login/', { | |
e1561d04 | 228 | 'username': u'angrygirl', |
229 | 'password': 'iamveryveryangry'}) | |
f339b76a RE |
230 | |
231 | # User should be redirected | |
232 | response.follow() | |
233 | assert urlparse.urlsplit(response.location)[2] == '/' | |
234 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT | |
235 | ||
757690cc | 236 | |
5c2ece74 | 237 | def test_authentication_views(test_app): |
757690cc CM |
238 | """ |
239 | Test logging in and logging out | |
240 | """ | |
241 | # Make a new user | |
e1561d04 | 242 | test_user = fixture_add_user() |
757690cc | 243 | |
5adb906a | 244 | |
757690cc | 245 | # Get login |
0a4cecdc | 246 | # --------- |
757690cc | 247 | test_app.get('/auth/login/') |
04453ccf | 248 | assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT |
757690cc | 249 | |
0a4cecdc CM |
250 | # Failed login - blank form |
251 | # ------------------------- | |
ae3bc7fa | 252 | template.clear_test_template_context() |
0a4cecdc | 253 | response = test_app.post('/auth/login/') |
ae3bc7fa | 254 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] |
0a4cecdc CM |
255 | form = context['login_form'] |
256 | assert form.username.errors == [u'This field is required.'] | |
0a4cecdc CM |
257 | |
258 | # Failed login - blank user | |
259 | # ------------------------- | |
ae3bc7fa | 260 | template.clear_test_template_context() |
0a4cecdc CM |
261 | response = test_app.post( |
262 | '/auth/login/', { | |
263 | 'password': u'toast'}) | |
ae3bc7fa | 264 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] |
0a4cecdc CM |
265 | form = context['login_form'] |
266 | assert form.username.errors == [u'This field is required.'] | |
267 | ||
268 | # Failed login - blank password | |
269 | # ----------------------------- | |
ae3bc7fa | 270 | template.clear_test_template_context() |
0a4cecdc CM |
271 | response = test_app.post( |
272 | '/auth/login/', { | |
273 | 'username': u'chris'}) | |
e4deacd9 | 274 | assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT |
0a4cecdc CM |
275 | |
276 | # Failed login - bad user | |
277 | # ----------------------- | |
ae3bc7fa | 278 | template.clear_test_template_context() |
0a4cecdc CM |
279 | response = test_app.post( |
280 | '/auth/login/', { | |
281 | 'username': u'steve', | |
282 | 'password': 'toast'}) | |
ae3bc7fa | 283 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] |
0a4cecdc CM |
284 | assert context['login_failed'] |
285 | ||
286 | # Failed login - bad password | |
287 | # --------------------------- | |
ae3bc7fa | 288 | template.clear_test_template_context() |
0a4cecdc CM |
289 | response = test_app.post( |
290 | '/auth/login/', { | |
291 | 'username': u'chris', | |
a89df961 | 292 | 'password': 'jam_and_ham'}) |
ae3bc7fa | 293 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] |
0a4cecdc CM |
294 | assert context['login_failed'] |
295 | ||
296 | # Successful login | |
297 | # ---------------- | |
ae3bc7fa | 298 | template.clear_test_template_context() |
757690cc CM |
299 | response = test_app.post( |
300 | '/auth/login/', { | |
301 | 'username': u'chris', | |
302 | 'password': 'toast'}) | |
0a4cecdc CM |
303 | |
304 | # User should be redirected | |
757690cc | 305 | response.follow() |
7d503a89 | 306 | assert urlparse.urlsplit(response.location)[2] == '/' |
04453ccf | 307 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT |
757690cc | 308 | |
0a4cecdc | 309 | # Make sure user is in the session |
ae3bc7fa | 310 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] |
0a4cecdc | 311 | session = context['request'].session |
e49b7e02 | 312 | assert session['user_id'] == six.text_type(test_user.id) |
757690cc | 313 | |
0a4cecdc CM |
314 | # Successful logout |
315 | # ----------------- | |
ae3bc7fa | 316 | template.clear_test_template_context() |
0a4cecdc CM |
317 | response = test_app.get('/auth/logout/') |
318 | ||
319 | # Should be redirected to index page | |
320 | response.follow() | |
7d503a89 | 321 | assert urlparse.urlsplit(response.location)[2] == '/' |
04453ccf | 322 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT |
0a4cecdc CM |
323 | |
324 | # Make sure the user is not in the session | |
ae3bc7fa | 325 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] |
0a4cecdc | 326 | session = context['request'].session |
04453ccf | 327 | assert 'user_id' not in session |
757690cc | 328 | |
12c231c8 CM |
329 | # User is redirected to custom URL if POST['next'] is set |
330 | # ------------------------------------------------------- | |
ae3bc7fa | 331 | template.clear_test_template_context() |
12c231c8 CM |
332 | response = test_app.post( |
333 | '/auth/login/', { | |
334 | 'username': u'chris', | |
335 | 'password': 'toast', | |
336 | 'next' : '/u/chris/'}) | |
7d503a89 | 337 | assert urlparse.urlsplit(response.location)[2] == '/u/chris/' |
dfd3f561 | 338 | |
dfd3f561 | 339 | @pytest.fixture() |
5101c469 | 340 | def authentication_disabled_app(request): |
dfd3f561 RE |
341 | return get_app( |
342 | request, | |
343 | mgoblin_config=pkg_resources.resource_filename( | |
344 | 'mediagoblin.tests.auth_configs', | |
5101c469 | 345 | 'authentication_disabled_appconfig.ini')) |
dfd3f561 RE |
346 | |
347 | ||
5101c469 | 348 | def test_authentication_disabled_app(authentication_disabled_app): |
dfd3f561 | 349 | # app.auth should = false |
2c901db0 | 350 | assert mg_globals |
dfd3f561 RE |
351 | assert mg_globals.app.auth is False |
352 | ||
353 | # Try to visit register page | |
354 | template.clear_test_template_context() | |
5101c469 | 355 | response = authentication_disabled_app.get('/auth/register/') |
dfd3f561 RE |
356 | response.follow() |
357 | ||
358 | # Correct redirect? | |
359 | assert urlparse.urlsplit(response.location)[2] == '/' | |
360 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT | |
361 | ||
362 | # Try to vist login page | |
363 | template.clear_test_template_context() | |
5101c469 | 364 | response = authentication_disabled_app.get('/auth/login/') |
dfd3f561 RE |
365 | response.follow() |
366 | ||
367 | # Correct redirect? | |
368 | assert urlparse.urlsplit(response.location)[2] == '/' | |
369 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT | |
370 | ||
3b8c733b RE |
371 | ## Test check_login_simple should return None |
372 | assert auth_tools.check_login_simple('test', 'simple') is None | |
f339b76a RE |
373 | |
374 | # Try to visit the forgot password page | |
375 | template.clear_test_template_context() | |
5101c469 | 376 | response = authentication_disabled_app.get('/auth/register/') |
f339b76a RE |
377 | response.follow() |
378 | ||
379 | # Correct redirect? | |
380 | assert urlparse.urlsplit(response.location)[2] == '/' | |
381 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT |