Use urljoin to create proper feed media URLs
[larjonas-mediagoblin.git] / mediagoblin / tests / test_ldap.py
blob6ac0fc46c26059ba8b3641f4f5483f71f54fc28b
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/>.
17 import pkg_resources
18 import pytest
19 import six
20 try:
21 import mock
22 except ImportError:
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")
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'))
45 def return_value():
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(
76 '/auth/ldap/login/',
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'})
92 res.follow()
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.']
114 # Log out
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'
120 ).first()
121 Session.expunge(test_user)
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()
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()