Show More
@@ -1,199 +1,202 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2016 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
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/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import pytest |
|
21 | import pytest | |
22 |
|
22 | |||
23 |
from rhodecode.tests import assert_session_flash |
|
23 | from rhodecode.tests import assert_session_flash | |
|
24 | from rhodecode.tests.utils import AssertResponse | |||
24 | from rhodecode.model.db import Session |
|
25 | from rhodecode.model.db import Session | |
25 | from rhodecode.model.settings import SettingsModel |
|
26 | from rhodecode.model.settings import SettingsModel | |
26 |
|
27 | |||
27 |
|
28 | |||
28 | def assert_auth_settings_updated(response): |
|
29 | def assert_auth_settings_updated(response): | |
29 | assert response.status_int == 302, 'Expected response HTTP Found 302' |
|
30 | assert response.status_int == 302, 'Expected response HTTP Found 302' | |
30 | assert_session_flash(response, 'Auth settings updated successfully') |
|
31 | assert_session_flash(response, 'Auth settings updated successfully') | |
31 |
|
32 | |||
32 |
|
33 | |||
33 | @pytest.mark.usefixtures("autologin_user", "app") |
|
34 | @pytest.mark.usefixtures("autologin_user", "app") | |
34 | class TestAuthSettingsController(object): |
|
35 | class TestAuthSettingsController(object): | |
35 |
|
36 | |||
36 | def _enable_plugins(self, plugins_list, csrf_token, override=None, |
|
37 | def _enable_plugins(self, plugins_list, csrf_token, override=None, | |
37 | verify_response=False): |
|
38 | verify_response=False): | |
38 | test_url = '/_admin/auth' |
|
39 | test_url = '/_admin/auth' | |
39 | params = { |
|
40 | params = { | |
40 | 'auth_plugins': plugins_list, |
|
41 | 'auth_plugins': plugins_list, | |
41 | 'csrf_token': csrf_token, |
|
42 | 'csrf_token': csrf_token, | |
42 | } |
|
43 | } | |
43 | if override: |
|
44 | if override: | |
44 | params.update(override) |
|
45 | params.update(override) | |
45 | _enabled_plugins = [] |
|
46 | _enabled_plugins = [] | |
46 | for plugin in plugins_list.split(','): |
|
47 | for plugin in plugins_list.split(','): | |
47 | plugin_name = plugin.partition('#')[-1] |
|
48 | plugin_name = plugin.partition('#')[-1] | |
48 | enabled_plugin = '%s_enabled' % plugin_name |
|
49 | enabled_plugin = '%s_enabled' % plugin_name | |
49 | cache_ttl = '%s_auth_cache_ttl' % plugin_name |
|
50 | cache_ttl = '%s_auth_cache_ttl' % plugin_name | |
50 |
|
51 | |||
51 | # default params that are needed for each plugin, |
|
52 | # default params that are needed for each plugin, | |
52 | # `enabled` and `auth_cache_ttl` |
|
53 | # `enabled` and `auth_cache_ttl` | |
53 | params.update({ |
|
54 | params.update({ | |
54 | enabled_plugin: True, |
|
55 | enabled_plugin: True, | |
55 | cache_ttl: 0 |
|
56 | cache_ttl: 0 | |
56 | }) |
|
57 | }) | |
57 | _enabled_plugins.append(enabled_plugin) |
|
58 | _enabled_plugins.append(enabled_plugin) | |
58 |
|
59 | |||
59 | # we need to clean any enabled plugin before, since they require |
|
60 | # we need to clean any enabled plugin before, since they require | |
60 | # form params to be present |
|
61 | # form params to be present | |
61 | db_plugin = SettingsModel().get_setting_by_name('auth_plugins') |
|
62 | db_plugin = SettingsModel().get_setting_by_name('auth_plugins') | |
62 | db_plugin.app_settings_value = \ |
|
63 | db_plugin.app_settings_value = \ | |
63 | 'egg:rhodecode-enterprise-ce#rhodecode' |
|
64 | 'egg:rhodecode-enterprise-ce#rhodecode' | |
64 | Session().add(db_plugin) |
|
65 | Session().add(db_plugin) | |
65 | Session().commit() |
|
66 | Session().commit() | |
66 | for _plugin in _enabled_plugins: |
|
67 | for _plugin in _enabled_plugins: | |
67 | db_plugin = SettingsModel().get_setting_by_name(_plugin) |
|
68 | db_plugin = SettingsModel().get_setting_by_name(_plugin) | |
68 | if db_plugin: |
|
69 | if db_plugin: | |
69 | Session.delete(db_plugin) |
|
70 | Session.delete(db_plugin) | |
70 | Session().commit() |
|
71 | Session().commit() | |
71 |
|
72 | |||
72 | response = self.app.post(url=test_url, params=params) |
|
73 | response = self.app.post(url=test_url, params=params) | |
73 |
|
74 | |||
74 | if verify_response: |
|
75 | if verify_response: | |
75 | assert_auth_settings_updated(response) |
|
76 | assert_auth_settings_updated(response) | |
76 | return params |
|
77 | return params | |
77 |
|
78 | |||
78 | def _post_ldap_settings(self, params, override=None, force=False): |
|
79 | def _post_ldap_settings(self, params, override=None, force=False): | |
79 |
|
80 | |||
80 | params.update({ |
|
81 | params.update({ | |
81 | 'filter': 'user', |
|
82 | 'filter': 'user', | |
82 | 'user_member_of': '', |
|
83 | 'user_member_of': '', | |
83 | 'user_search_base': '', |
|
84 | 'user_search_base': '', | |
84 | 'user_search_filter': 'test_filter', |
|
85 | 'user_search_filter': 'test_filter', | |
85 |
|
86 | |||
86 | 'host': 'dc.example.com', |
|
87 | 'host': 'dc.example.com', | |
87 | 'port': '999', |
|
88 | 'port': '999', | |
88 | 'tls_kind': 'PLAIN', |
|
89 | 'tls_kind': 'PLAIN', | |
89 | 'tls_reqcert': 'NEVER', |
|
90 | 'tls_reqcert': 'NEVER', | |
90 |
|
91 | |||
91 | 'dn_user': 'test_user', |
|
92 | 'dn_user': 'test_user', | |
92 | 'dn_pass': 'test_pass', |
|
93 | 'dn_pass': 'test_pass', | |
93 | 'base_dn': 'test_base_dn', |
|
94 | 'base_dn': 'test_base_dn', | |
94 | 'search_scope': 'BASE', |
|
95 | 'search_scope': 'BASE', | |
95 | 'attr_login': 'test_attr_login', |
|
96 | 'attr_login': 'test_attr_login', | |
96 | 'attr_firstname': 'ima', |
|
97 | 'attr_firstname': 'ima', | |
97 | 'attr_lastname': 'tester', |
|
98 | 'attr_lastname': 'tester', | |
98 | 'attr_email': 'test@example.com', |
|
99 | 'attr_email': 'test@example.com', | |
99 | 'auth_cache_ttl': '0', |
|
100 | 'auth_cache_ttl': '0', | |
100 | }) |
|
101 | }) | |
101 | if force: |
|
102 | if force: | |
102 | params = {} |
|
103 | params = {} | |
103 | params.update(override or {}) |
|
104 | params.update(override or {}) | |
104 |
|
105 | |||
105 | test_url = '/_admin/auth/ldap/' |
|
106 | test_url = '/_admin/auth/ldap/' | |
106 |
|
107 | |||
107 | response = self.app.post(url=test_url, params=params) |
|
108 | response = self.app.post(url=test_url, params=params) | |
108 | return response |
|
109 | return response | |
109 |
|
110 | |||
110 | def test_index(self): |
|
111 | def test_index(self): | |
111 | response = self.app.get('/_admin/auth') |
|
112 | response = self.app.get('/_admin/auth') | |
112 | response.mustcontain('Authentication Plugins') |
|
113 | response.mustcontain('Authentication Plugins') | |
113 |
|
114 | |||
114 | @pytest.mark.parametrize("disable_plugin, needs_import", [ |
|
115 | @pytest.mark.parametrize("disable_plugin, needs_import", [ | |
115 | pytest.mark.xfail(('egg:rhodecode-enterprise-ce#container', None), |
|
116 | pytest.mark.xfail(('egg:rhodecode-enterprise-ce#container', None), | |
116 | reason="Migration of container plugin pending."), |
|
117 | reason="Migration of container plugin pending."), | |
117 | ('egg:rhodecode-enterprise-ce#crowd', None), |
|
118 | ('egg:rhodecode-enterprise-ce#crowd', None), | |
118 | ('egg:rhodecode-enterprise-ce#jasig_cas', None), |
|
119 | ('egg:rhodecode-enterprise-ce#jasig_cas', None), | |
119 | ('egg:rhodecode-enterprise-ce#ldap', None), |
|
120 | ('egg:rhodecode-enterprise-ce#ldap', None), | |
120 | ('egg:rhodecode-enterprise-ce#pam', "pam"), |
|
121 | ('egg:rhodecode-enterprise-ce#pam', "pam"), | |
121 | ]) |
|
122 | ]) | |
122 | def test_disable_plugin(self, csrf_token, disable_plugin, needs_import): |
|
123 | def test_disable_plugin(self, csrf_token, disable_plugin, needs_import): | |
123 | # TODO: johbo: "pam" is currently not available on darwin, |
|
124 | # TODO: johbo: "pam" is currently not available on darwin, | |
124 | # although the docs state that it should work on darwin. |
|
125 | # although the docs state that it should work on darwin. | |
125 | if needs_import: |
|
126 | if needs_import: | |
126 | pytest.importorskip(needs_import) |
|
127 | pytest.importorskip(needs_import) | |
127 |
|
128 | |||
128 | self._enable_plugins( |
|
129 | self._enable_plugins( | |
129 | 'egg:rhodecode-enterprise-ce#rhodecode,' + disable_plugin, |
|
130 | 'egg:rhodecode-enterprise-ce#rhodecode,' + disable_plugin, | |
130 | csrf_token, verify_response=True) |
|
131 | csrf_token, verify_response=True) | |
131 |
|
132 | |||
132 | self._enable_plugins( |
|
133 | self._enable_plugins( | |
133 | 'egg:rhodecode-enterprise-ce#rhodecode', csrf_token, |
|
134 | 'egg:rhodecode-enterprise-ce#rhodecode', csrf_token, | |
134 | verify_response=True) |
|
135 | verify_response=True) | |
135 |
|
136 | |||
136 | def test_ldap_save_settings(self, csrf_token): |
|
137 | def test_ldap_save_settings(self, csrf_token): | |
137 | params = self._enable_plugins( |
|
138 | params = self._enable_plugins( | |
138 | 'egg:rhodecode-enterprise-ce#rhodecode,' |
|
139 | 'egg:rhodecode-enterprise-ce#rhodecode,' | |
139 | 'egg:rhodecode-enterprise-ce#ldap', |
|
140 | 'egg:rhodecode-enterprise-ce#ldap', | |
140 | csrf_token) |
|
141 | csrf_token) | |
141 | response = self._post_ldap_settings(params) |
|
142 | response = self._post_ldap_settings(params) | |
142 | assert_auth_settings_updated(response) |
|
143 | assert_auth_settings_updated(response) | |
143 |
|
144 | |||
144 | new_settings = SettingsModel().get_auth_settings() |
|
145 | new_settings = SettingsModel().get_auth_settings() | |
145 | assert new_settings['auth_ldap_host'] == u'dc.example.com', \ |
|
146 | assert new_settings['auth_ldap_host'] == u'dc.example.com', \ | |
146 | 'fail db write compare' |
|
147 | 'fail db write compare' | |
147 |
|
148 | |||
148 | def test_ldap_error_form_wrong_port_number(self, csrf_token): |
|
149 | def test_ldap_error_form_wrong_port_number(self, csrf_token): | |
149 | params = self._enable_plugins( |
|
150 | params = self._enable_plugins( | |
150 | 'egg:rhodecode-enterprise-ce#rhodecode,' |
|
151 | 'egg:rhodecode-enterprise-ce#rhodecode,' | |
151 | 'egg:rhodecode-enterprise-ce#ldap', |
|
152 | 'egg:rhodecode-enterprise-ce#ldap', | |
152 | csrf_token) |
|
153 | csrf_token) | |
|
154 | invalid_port_value = 'invalid-port-number' | |||
153 | response = self._post_ldap_settings(params, override={ |
|
155 | response = self._post_ldap_settings(params, override={ | |
154 |
'port': |
|
156 | 'port': invalid_port_value, | |
155 | }) |
|
157 | }) | |
156 | response.mustcontain( |
|
158 | assertr = AssertResponse(response) | |
157 | '<span class="error-message">"invalid-port-number"' |
|
159 | assertr.element_contains( | |
158 | ' is not a number</span>') |
|
160 | '.form .field #port ~ .error-message', | |
|
161 | invalid_port_value) | |||
159 |
|
162 | |||
160 | def test_ldap_error_form(self, csrf_token): |
|
163 | def test_ldap_error_form(self, csrf_token): | |
161 | params = self._enable_plugins( |
|
164 | params = self._enable_plugins( | |
162 | 'egg:rhodecode-enterprise-ce#rhodecode,' |
|
165 | 'egg:rhodecode-enterprise-ce#rhodecode,' | |
163 | 'egg:rhodecode-enterprise-ce#ldap', |
|
166 | 'egg:rhodecode-enterprise-ce#ldap', | |
164 | csrf_token) |
|
167 | csrf_token) | |
165 | response = self._post_ldap_settings(params, override={ |
|
168 | response = self._post_ldap_settings(params, override={ | |
166 | 'attr_login': '', |
|
169 | 'attr_login': '', | |
167 | }) |
|
170 | }) | |
168 | response.mustcontain("""<span class="error-message">The LDAP Login""" |
|
171 | response.mustcontain("""<span class="error-message">The LDAP Login""" | |
169 | """ attribute of the CN must be specified""") |
|
172 | """ attribute of the CN must be specified""") | |
170 |
|
173 | |||
171 | def test_post_ldap_group_settings(self, csrf_token): |
|
174 | def test_post_ldap_group_settings(self, csrf_token): | |
172 | params = self._enable_plugins( |
|
175 | params = self._enable_plugins( | |
173 | 'egg:rhodecode-enterprise-ce#rhodecode,' |
|
176 | 'egg:rhodecode-enterprise-ce#rhodecode,' | |
174 | 'egg:rhodecode-enterprise-ce#ldap', |
|
177 | 'egg:rhodecode-enterprise-ce#ldap', | |
175 | csrf_token) |
|
178 | csrf_token) | |
176 |
|
179 | |||
177 | response = self._post_ldap_settings(params, override={ |
|
180 | response = self._post_ldap_settings(params, override={ | |
178 | 'host': 'dc-legacy.example.com', |
|
181 | 'host': 'dc-legacy.example.com', | |
179 | 'port': '999', |
|
182 | 'port': '999', | |
180 | 'tls_kind': 'PLAIN', |
|
183 | 'tls_kind': 'PLAIN', | |
181 | 'tls_reqcert': 'NEVER', |
|
184 | 'tls_reqcert': 'NEVER', | |
182 | 'dn_user': 'test_user', |
|
185 | 'dn_user': 'test_user', | |
183 | 'dn_pass': 'test_pass', |
|
186 | 'dn_pass': 'test_pass', | |
184 | 'base_dn': 'test_base_dn', |
|
187 | 'base_dn': 'test_base_dn', | |
185 | 'filter': 'test_filter', |
|
188 | 'filter': 'test_filter', | |
186 | 'search_scope': 'BASE', |
|
189 | 'search_scope': 'BASE', | |
187 | 'attr_login': 'test_attr_login', |
|
190 | 'attr_login': 'test_attr_login', | |
188 | 'attr_firstname': 'ima', |
|
191 | 'attr_firstname': 'ima', | |
189 | 'attr_lastname': 'tester', |
|
192 | 'attr_lastname': 'tester', | |
190 | 'attr_email': 'test@example.com', |
|
193 | 'attr_email': 'test@example.com', | |
191 | 'auth_cache_ttl': '60', |
|
194 | 'auth_cache_ttl': '60', | |
192 | 'csrf_token': csrf_token, |
|
195 | 'csrf_token': csrf_token, | |
193 | } |
|
196 | } | |
194 | ) |
|
197 | ) | |
195 | assert_auth_settings_updated(response) |
|
198 | assert_auth_settings_updated(response) | |
196 |
|
199 | |||
197 | new_settings = SettingsModel().get_auth_settings() |
|
200 | new_settings = SettingsModel().get_auth_settings() | |
198 | assert new_settings['auth_ldap_host'] == u'dc-legacy.example.com', \ |
|
201 | assert new_settings['auth_ldap_host'] == u'dc-legacy.example.com', \ | |
199 | 'fail db write compare' |
|
202 | 'fail db write compare' |
General Comments 0
You need to be logged in to leave comments.
Login now