Commit | Line | Data |
---|---|---|
d68ada28 RE |
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/>. | |
fd19da34 | 16 | |
d68ada28 RE |
17 | import pkg_resources |
18 | import pytest | |
e49b7e02 | 19 | import six |
9459fa3c BP |
20 | try: |
21 | import mock | |
22 | except ImportError: | |
23 | import unittest.mock as mock | |
d68ada28 | 24 | |
fd19da34 BP |
25 | import six.moves.urllib.parse as urlparse |
26 | ||
d68ada28 RE |
27 | from mediagoblin import mg_globals |
28 | from mediagoblin.db.base import Session | |
d88fcb03 | 29 | from mediagoblin.db.models import LocalUser |
d68ada28 RE |
30 | from mediagoblin.tests.tools import get_app |
31 | from mediagoblin.tools import template | |
32 | ||
b1ac6795 RE |
33 | pytest.importorskip("ldap") |
34 | ||
d68ada28 RE |
35 | |
36 | @pytest.fixture() | |
37 | def ldap_plugin_app(request): | |
38 | return get_app( | |
39 | request, | |
40 | mgoblin_config=pkg_resources.resource_filename( | |
41 | 'mediagoblin.tests.auth_configs', | |
42 | 'ldap_appconfig.ini')) | |
43 | ||
44 | ||
45 | def return_value(): | |
46 | return u'chris', u'chris@example.com' | |
47 | ||
48 | ||
49 | def test_ldap_plugin(ldap_plugin_app): | |
50 | res = ldap_plugin_app.get('/auth/login/') | |
51 | ||
52 | assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/login/' | |
53 | ||
54 | res = ldap_plugin_app.get('/auth/register/') | |
55 | ||
56 | assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/register/' | |
57 | ||
58 | res = ldap_plugin_app.get('/auth/ldap/register/') | |
59 | ||
60 | assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/login/' | |
61 | ||
62 | template.clear_test_template_context() | |
63 | res = ldap_plugin_app.post( | |
64 | '/auth/ldap/login/', {}) | |
65 | ||
66 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/login.html'] | |
67 | form = context['login_form'] | |
68 | assert form.username.errors == [u'This field is required.'] | |
69 | assert form.password.errors == [u'This field is required.'] | |
70 | ||
a4337755 CAW |
71 | @mock.patch('mediagoblin.plugins.ldap.tools.LDAP.login', |
72 | mock.Mock(return_value=return_value())) | |
d68ada28 RE |
73 | def _test_authentication(): |
74 | template.clear_test_template_context() | |
75 | res = ldap_plugin_app.post( | |
76 | '/auth/ldap/login/', | |
77 | {'username': u'chris', | |
78 | 'password': u'toast'}) | |
79 | ||
a4337755 CAW |
80 | context = template.TEMPLATE_TEST_CONTEXT[ |
81 | 'mediagoblin/auth/register.html'] | |
d68ada28 RE |
82 | register_form = context['register_form'] |
83 | ||
84 | assert register_form.username.data == u'chris' | |
85 | assert register_form.email.data == u'chris@example.com' | |
86 | ||
87 | template.clear_test_template_context() | |
88 | res = ldap_plugin_app.post( | |
89 | '/auth/ldap/register/', | |
90 | {'username': u'chris', | |
91 | 'email': u'chris@example.com'}) | |
92 | res.follow() | |
93 | ||
94 | assert urlparse.urlsplit(res.location)[2] == '/u/chris/' | |
a4337755 CAW |
95 | assert 'mediagoblin/user_pages/user_nonactive.html' in \ |
96 | template.TEMPLATE_TEST_CONTEXT | |
d68ada28 RE |
97 | |
98 | # Try to register with same email and username | |
99 | template.clear_test_template_context() | |
100 | res = ldap_plugin_app.post( | |
101 | '/auth/ldap/register/', | |
102 | {'username': u'chris', | |
103 | 'email': u'chris@example.com'}) | |
104 | ||
a4337755 CAW |
105 | context = template.TEMPLATE_TEST_CONTEXT[ |
106 | 'mediagoblin/auth/register.html'] | |
d68ada28 RE |
107 | register_form = context['register_form'] |
108 | ||
a4337755 CAW |
109 | assert register_form.email.errors == [ |
110 | u'Sorry, a user with that email address already exists.'] | |
111 | assert register_form.username.errors == [ | |
112 | u'Sorry, a user with that name already exists.'] | |
d68ada28 RE |
113 | |
114 | # Log out | |
115 | ldap_plugin_app.get('/auth/logout/') | |
116 | ||
117 | # Get user and detach from session | |
b4997540 | 118 | test_user = mg_globals.database.LocalUser.query.filter( |
d88fcb03 JT |
119 | LocalUser.username==u'chris' |
120 | ).first() | |
d68ada28 RE |
121 | Session.expunge(test_user) |
122 | ||
123 | # Log back in | |
124 | template.clear_test_template_context() | |
125 | res = ldap_plugin_app.post( | |
126 | '/auth/ldap/login/', | |
127 | {'username': u'chris', | |
128 | 'password': u'toast'}) | |
129 | res.follow() | |
130 | ||
131 | assert urlparse.urlsplit(res.location)[2] == '/' | |
132 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT | |
133 | ||
134 | # Make sure user is in the session | |
135 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] | |
136 | session = context['request'].session | |
e49b7e02 | 137 | assert session['user_id'] == six.text_type(test_user.id) |
d68ada28 RE |
138 | |
139 | _test_authentication() |