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