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/>.
21 from mediagoblin
import mg_globals
22 from mediagoblin
.db
.base
import Session
23 from mediagoblin
.tests
.tools
import get_app
24 from mediagoblin
.tools
import template
26 pytest
.importorskip("ldap")
30 def ldap_plugin_app(request
):
33 mgoblin_config
=pkg_resources
.resource_filename(
34 'mediagoblin.tests.auth_configs',
35 'ldap_appconfig.ini'))
39 return u
'chris', u
'chris@example.com'
42 def test_ldap_plugin(ldap_plugin_app
):
43 res
= ldap_plugin_app
.get('/auth/login/')
45 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/ldap/login/'
47 res
= ldap_plugin_app
.get('/auth/register/')
49 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/ldap/register/'
51 res
= ldap_plugin_app
.get('/auth/ldap/register/')
53 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/ldap/login/'
55 template
.clear_test_template_context()
56 res
= ldap_plugin_app
.post(
57 '/auth/ldap/login/', {})
59 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/login.html']
60 form
= context
['login_form']
61 assert form
.username
.errors
== [u
'This field is required.']
62 assert form
.password
.errors
== [u
'This field is required.']
64 @mock.patch('mediagoblin.plugins.ldap.tools.LDAP.login', mock
.Mock(return_value
=return_value()))
65 def _test_authentication():
66 template
.clear_test_template_context()
67 res
= ldap_plugin_app
.post(
69 {'username': u
'chris',
70 'password': u
'toast'})
72 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
73 register_form
= context
['register_form']
75 assert register_form
.username
.data
== u
'chris'
76 assert register_form
.email
.data
== u
'chris@example.com'
78 template
.clear_test_template_context()
79 res
= ldap_plugin_app
.post(
80 '/auth/ldap/register/',
81 {'username': u
'chris',
82 'email': u
'chris@example.com'})
85 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
86 assert 'mediagoblin/user_pages/user_nonactive.html' in template
.TEMPLATE_TEST_CONTEXT
88 # Try to register with same email and username
89 template
.clear_test_template_context()
90 res
= ldap_plugin_app
.post(
91 '/auth/ldap/register/',
92 {'username': u
'chris',
93 'email': u
'chris@example.com'})
95 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
96 register_form
= context
['register_form']
98 assert register_form
.email
.errors
== [u
'Sorry, a user with that email address already exists.']
99 assert register_form
.username
.errors
== [u
'Sorry, a user with that name already exists.']
102 ldap_plugin_app
.get('/auth/logout/')
104 # Get user and detach from session
105 test_user
= mg_globals
.database
.User
.query
.filter_by(
106 username
=u
'chris').first()
107 Session
.expunge(test_user
)
110 template
.clear_test_template_context()
111 res
= ldap_plugin_app
.post(
113 {'username': u
'chris',
114 'password': u
'toast'})
117 assert urlparse
.urlsplit(res
.location
)[2] == '/'
118 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
120 # Make sure user is in the session
121 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/root.html']
122 session
= context
['request'].session
123 assert session
['user_id'] == unicode(test_user
.id)
125 _test_authentication()