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