Merge remote-tracking branch 'tilly-q/OPW-Moderation-Update' into merge-tillyq-moderation
[mediagoblin.git] / mediagoblin / tests / test_ldap.py
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/>.
16 import urlparse
17 import pkg_resources
18 import pytest
19 import mock
20
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
25
26 pytest.importorskip("ldap")
27
28
29 @pytest.fixture()
30 def ldap_plugin_app(request):
31 return get_app(
32 request,
33 mgoblin_config=pkg_resources.resource_filename(
34 'mediagoblin.tests.auth_configs',
35 'ldap_appconfig.ini'))
36
37
38 def return_value():
39 return u'chris', u'chris@example.com'
40
41
42 def test_ldap_plugin(ldap_plugin_app):
43 res = ldap_plugin_app.get('/auth/login/')
44
45 assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/login/'
46
47 res = ldap_plugin_app.get('/auth/register/')
48
49 assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/register/'
50
51 res = ldap_plugin_app.get('/auth/ldap/register/')
52
53 assert urlparse.urlsplit(res.location)[2] == '/auth/ldap/login/'
54
55 template.clear_test_template_context()
56 res = ldap_plugin_app.post(
57 '/auth/ldap/login/', {})
58
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.']
63
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(
68 '/auth/ldap/login/',
69 {'username': u'chris',
70 'password': u'toast'})
71
72 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
73 register_form = context['register_form']
74
75 assert register_form.username.data == u'chris'
76 assert register_form.email.data == u'chris@example.com'
77
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'})
83 res.follow()
84
85 assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
86 assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT
87
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'})
94
95 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
96 register_form = context['register_form']
97
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.']
100
101 # Log out
102 ldap_plugin_app.get('/auth/logout/')
103
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)
108
109 # Log back in
110 template.clear_test_template_context()
111 res = ldap_plugin_app.post(
112 '/auth/ldap/login/',
113 {'username': u'chris',
114 'password': u'toast'})
115 res.follow()
116
117 assert urlparse.urlsplit(res.location)[2] == '/'
118 assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
119
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)
124
125 _test_authentication()