1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
23 import unittest
.mock
as mock
25 import six
.moves
.urllib
.parse
as urlparse
27 from mediagoblin
import mg_globals
28 from mediagoblin
.db
.base
import Session
29 from mediagoblin
.db
.models
import LocalUser
30 from mediagoblin
.tests
.tools
import get_app
31 from mediagoblin
.tools
import template
33 pytest
.importorskip("ldap")
37 def ldap_plugin_app(request
):
40 mgoblin_config
=pkg_resources
.resource_filename(
41 'mediagoblin.tests.auth_configs',
42 'ldap_appconfig.ini'))
46 return u
'chris', u
'chris@example.com'
49 def test_ldap_plugin(ldap_plugin_app
):
50 res
= ldap_plugin_app
.get('/auth/login/')
52 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/ldap/login/'
54 res
= ldap_plugin_app
.get('/auth/register/')
56 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/ldap/register/'
58 res
= ldap_plugin_app
.get('/auth/ldap/register/')
60 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/ldap/login/'
62 template
.clear_test_template_context()
63 res
= ldap_plugin_app
.post(
64 '/auth/ldap/login/', {})
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.']
71 @mock.patch('mediagoblin.plugins.ldap.tools.LDAP.login',
72 mock
.Mock(return_value
=return_value()))
73 def _test_authentication():
74 template
.clear_test_template_context()
75 res
= ldap_plugin_app
.post(
77 {'username': u
'chris',
78 'password': u
'toast'})
80 context
= template
.TEMPLATE_TEST_CONTEXT
[
81 'mediagoblin/auth/register.html']
82 register_form
= context
['register_form']
84 assert register_form
.username
.data
== u
'chris'
85 assert register_form
.email
.data
== u
'chris@example.com'
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'})
94 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
95 assert 'mediagoblin/user_pages/user_nonactive.html' in \
96 template
.TEMPLATE_TEST_CONTEXT
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'})
105 context
= template
.TEMPLATE_TEST_CONTEXT
[
106 'mediagoblin/auth/register.html']
107 register_form
= context
['register_form']
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.']
115 ldap_plugin_app
.get('/auth/logout/')
117 # Get user and detach from session
118 test_user
= mg_globals
.database
.LocalUser
.query
.filter(
119 LocalUser
.username
==u
'chris'
121 Session
.expunge(test_user
)
124 template
.clear_test_template_context()
125 res
= ldap_plugin_app
.post(
127 {'username': u
'chris',
128 'password': u
'toast'})
131 assert urlparse
.urlsplit(res
.location
)[2] == '/'
132 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
134 # Make sure user is in the session
135 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/root.html']
136 session
= context
['request'].session
137 assert session
['user_id'] == six
.text_type(test_user
.id)
139 _test_authentication()