Show More
@@ -1,188 +1,189 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2010-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2010-2017 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 mock |
|
21 | import mock | |
22 | import pytest |
|
22 | import pytest | |
23 |
|
23 | |||
24 | from rhodecode.lib.auth import _RhodeCodeCryptoBCrypt |
|
24 | from rhodecode.lib.auth import _RhodeCodeCryptoBCrypt | |
25 | from rhodecode.authentication.base import RhodeCodeAuthPluginBase |
|
25 | from rhodecode.authentication.base import RhodeCodeAuthPluginBase | |
26 | from rhodecode.authentication.plugins.auth_ldap import RhodeCodeAuthPlugin |
|
26 | from rhodecode.authentication.plugins.auth_ldap import RhodeCodeAuthPlugin | |
27 | from rhodecode.model import db |
|
27 | from rhodecode.model import db | |
28 |
|
28 | |||
29 |
|
29 | |||
30 | class TestAuthPlugin(RhodeCodeAuthPluginBase): |
|
30 | class RcTestAuthPlugin(RhodeCodeAuthPluginBase): | |
31 |
|
31 | |||
32 | def name(self): |
|
32 | def name(self): | |
33 | return 'stub_auth' |
|
33 | return 'stub_auth' | |
34 |
|
34 | |||
|
35 | ||||
35 | def test_authenticate_returns_from_auth(stub_auth_data): |
|
36 | def test_authenticate_returns_from_auth(stub_auth_data): | |
36 | plugin = TestAuthPlugin('stub_id') |
|
37 | plugin = RcTestAuthPlugin('stub_id') | |
37 | with mock.patch.object(plugin, 'auth') as auth_mock: |
|
38 | with mock.patch.object(plugin, 'auth') as auth_mock: | |
38 | auth_mock.return_value = stub_auth_data |
|
39 | auth_mock.return_value = stub_auth_data | |
39 | result = plugin._authenticate(mock.Mock(), 'test', 'password', {}) |
|
40 | result = plugin._authenticate(mock.Mock(), 'test', 'password', {}) | |
40 | assert stub_auth_data == result |
|
41 | assert stub_auth_data == result | |
41 |
|
42 | |||
42 |
|
43 | |||
43 | def test_authenticate_returns_empty_auth_data(): |
|
44 | def test_authenticate_returns_empty_auth_data(): | |
44 | auth_data = {} |
|
45 | auth_data = {} | |
45 | plugin = TestAuthPlugin('stub_id') |
|
46 | plugin = RcTestAuthPlugin('stub_id') | |
46 | with mock.patch.object(plugin, 'auth') as auth_mock: |
|
47 | with mock.patch.object(plugin, 'auth') as auth_mock: | |
47 | auth_mock.return_value = auth_data |
|
48 | auth_mock.return_value = auth_data | |
48 | result = plugin._authenticate(mock.Mock(), 'test', 'password', {}) |
|
49 | result = plugin._authenticate(mock.Mock(), 'test', 'password', {}) | |
49 | assert auth_data == result |
|
50 | assert auth_data == result | |
50 |
|
51 | |||
51 |
|
52 | |||
52 | def test_authenticate_skips_hash_migration_if_mismatch(stub_auth_data): |
|
53 | def test_authenticate_skips_hash_migration_if_mismatch(stub_auth_data): | |
53 | stub_auth_data['_hash_migrate'] = 'new-hash' |
|
54 | stub_auth_data['_hash_migrate'] = 'new-hash' | |
54 | plugin = TestAuthPlugin('stub_id') |
|
55 | plugin = RcTestAuthPlugin('stub_id') | |
55 | with mock.patch.object(plugin, 'auth') as auth_mock: |
|
56 | with mock.patch.object(plugin, 'auth') as auth_mock: | |
56 | auth_mock.return_value = stub_auth_data |
|
57 | auth_mock.return_value = stub_auth_data | |
57 | result = plugin._authenticate(mock.Mock(), 'test', 'password', {}) |
|
58 | result = plugin._authenticate(mock.Mock(), 'test', 'password', {}) | |
58 |
|
59 | |||
59 | user = db.User.get_by_username(stub_auth_data['username']) |
|
60 | user = db.User.get_by_username(stub_auth_data['username']) | |
60 | assert user.password != 'new-hash' |
|
61 | assert user.password != 'new-hash' | |
61 | assert result == stub_auth_data |
|
62 | assert result == stub_auth_data | |
62 |
|
63 | |||
63 |
|
64 | |||
64 | def test_authenticate_migrates_to_new_hash(stub_auth_data): |
|
65 | def test_authenticate_migrates_to_new_hash(stub_auth_data): | |
65 | new_password = b'new-password' |
|
66 | new_password = b'new-password' | |
66 | new_hash = _RhodeCodeCryptoBCrypt().hash_create(new_password) |
|
67 | new_hash = _RhodeCodeCryptoBCrypt().hash_create(new_password) | |
67 | stub_auth_data['_hash_migrate'] = new_hash |
|
68 | stub_auth_data['_hash_migrate'] = new_hash | |
68 | plugin = TestAuthPlugin('stub_id') |
|
69 | plugin = RcTestAuthPlugin('stub_id') | |
69 | with mock.patch.object(plugin, 'auth') as auth_mock: |
|
70 | with mock.patch.object(plugin, 'auth') as auth_mock: | |
70 | auth_mock.return_value = stub_auth_data |
|
71 | auth_mock.return_value = stub_auth_data | |
71 | result = plugin._authenticate( |
|
72 | result = plugin._authenticate( | |
72 | mock.Mock(), stub_auth_data['username'], new_password, {}) |
|
73 | mock.Mock(), stub_auth_data['username'], new_password, {}) | |
73 |
|
74 | |||
74 | user = db.User.get_by_username(stub_auth_data['username']) |
|
75 | user = db.User.get_by_username(stub_auth_data['username']) | |
75 | assert user.password == new_hash |
|
76 | assert user.password == new_hash | |
76 | assert result == stub_auth_data |
|
77 | assert result == stub_auth_data | |
77 |
|
78 | |||
78 |
|
79 | |||
79 | @pytest.fixture |
|
80 | @pytest.fixture | |
80 | def stub_auth_data(user_util): |
|
81 | def stub_auth_data(user_util): | |
81 | user = user_util.create_user() |
|
82 | user = user_util.create_user() | |
82 | data = { |
|
83 | data = { | |
83 | 'username': user.username, |
|
84 | 'username': user.username, | |
84 | 'password': 'password', |
|
85 | 'password': 'password', | |
85 | 'email': 'test@example.org', |
|
86 | 'email': 'test@example.org', | |
86 | 'firstname': 'John', |
|
87 | 'firstname': 'John', | |
87 | 'lastname': 'Smith', |
|
88 | 'lastname': 'Smith', | |
88 | 'groups': [], |
|
89 | 'groups': [], | |
89 | 'active': True, |
|
90 | 'active': True, | |
90 | 'admin': False, |
|
91 | 'admin': False, | |
91 | 'extern_name': 'test', |
|
92 | 'extern_name': 'test', | |
92 | 'extern_type': 'ldap', |
|
93 | 'extern_type': 'ldap', | |
93 | 'active_from_extern': True |
|
94 | 'active_from_extern': True | |
94 | } |
|
95 | } | |
95 | return data |
|
96 | return data | |
96 |
|
97 | |||
97 |
|
98 | |||
98 | class TestRhodeCodeAuthPlugin(object): |
|
99 | class TestRhodeCodeAuthPlugin(object): | |
99 | def setup_method(self, method): |
|
100 | def setup_method(self, method): | |
100 | self.finalizers = [] |
|
101 | self.finalizers = [] | |
101 | self.user = mock.Mock() |
|
102 | self.user = mock.Mock() | |
102 | self.user.username = 'test' |
|
103 | self.user.username = 'test' | |
103 | self.user.password = 'old-password' |
|
104 | self.user.password = 'old-password' | |
104 | self.fake_auth = { |
|
105 | self.fake_auth = { | |
105 | 'username': 'test', |
|
106 | 'username': 'test', | |
106 | 'password': 'test', |
|
107 | 'password': 'test', | |
107 | 'email': 'test@example.org', |
|
108 | 'email': 'test@example.org', | |
108 | 'firstname': 'John', |
|
109 | 'firstname': 'John', | |
109 | 'lastname': 'Smith', |
|
110 | 'lastname': 'Smith', | |
110 | 'groups': [], |
|
111 | 'groups': [], | |
111 | 'active': True, |
|
112 | 'active': True, | |
112 | 'admin': False, |
|
113 | 'admin': False, | |
113 | 'extern_name': 'test', |
|
114 | 'extern_name': 'test', | |
114 | 'extern_type': 'ldap', |
|
115 | 'extern_type': 'ldap', | |
115 | 'active_from_extern': True |
|
116 | 'active_from_extern': True | |
116 | } |
|
117 | } | |
117 |
|
118 | |||
118 | def teardown_method(self, method): |
|
119 | def teardown_method(self, method): | |
119 | if self.finalizers: |
|
120 | if self.finalizers: | |
120 | for finalizer in self.finalizers: |
|
121 | for finalizer in self.finalizers: | |
121 | finalizer() |
|
122 | finalizer() | |
122 | self.finalizers = [] |
|
123 | self.finalizers = [] | |
123 |
|
124 | |||
124 | def test_fake_password_is_created_for_the_new_user(self): |
|
125 | def test_fake_password_is_created_for_the_new_user(self): | |
125 | self._patch() |
|
126 | self._patch() | |
126 | auth_plugin = RhodeCodeAuthPlugin('stub_id') |
|
127 | auth_plugin = RhodeCodeAuthPlugin('stub_id') | |
127 | auth_plugin._authenticate(self.user, 'test', 'test', []) |
|
128 | auth_plugin._authenticate(self.user, 'test', 'test', []) | |
128 | self.password_generator_mock.assert_called_once_with(length=16) |
|
129 | self.password_generator_mock.assert_called_once_with(length=16) | |
129 | create_user_kwargs = self.create_user_mock.call_args[1] |
|
130 | create_user_kwargs = self.create_user_mock.call_args[1] | |
130 | assert create_user_kwargs['password'] == 'new-password' |
|
131 | assert create_user_kwargs['password'] == 'new-password' | |
131 |
|
132 | |||
132 | def test_fake_password_is_not_created_for_the_existing_user(self): |
|
133 | def test_fake_password_is_not_created_for_the_existing_user(self): | |
133 | self._patch() |
|
134 | self._patch() | |
134 | self.get_user_mock.return_value = self.user |
|
135 | self.get_user_mock.return_value = self.user | |
135 | auth_plugin = RhodeCodeAuthPlugin('stub_id') |
|
136 | auth_plugin = RhodeCodeAuthPlugin('stub_id') | |
136 | auth_plugin._authenticate(self.user, 'test', 'test', []) |
|
137 | auth_plugin._authenticate(self.user, 'test', 'test', []) | |
137 | assert self.password_generator_mock.called is False |
|
138 | assert self.password_generator_mock.called is False | |
138 | create_user_kwargs = self.create_user_mock.call_args[1] |
|
139 | create_user_kwargs = self.create_user_mock.call_args[1] | |
139 | assert create_user_kwargs['password'] == self.user.password |
|
140 | assert create_user_kwargs['password'] == self.user.password | |
140 |
|
141 | |||
141 | def _patch(self): |
|
142 | def _patch(self): | |
142 | get_user_patch = mock.patch('rhodecode.model.db.User.get_by_username') |
|
143 | get_user_patch = mock.patch('rhodecode.model.db.User.get_by_username') | |
143 | self.get_user_mock = get_user_patch.start() |
|
144 | self.get_user_mock = get_user_patch.start() | |
144 | self.get_user_mock.return_value = None |
|
145 | self.get_user_mock.return_value = None | |
145 | self.finalizers.append(get_user_patch.stop) |
|
146 | self.finalizers.append(get_user_patch.stop) | |
146 |
|
147 | |||
147 | create_user_patch = mock.patch( |
|
148 | create_user_patch = mock.patch( | |
148 | 'rhodecode.model.user.UserModel.create_or_update') |
|
149 | 'rhodecode.model.user.UserModel.create_or_update') | |
149 | self.create_user_mock = create_user_patch.start() |
|
150 | self.create_user_mock = create_user_patch.start() | |
150 | self.create_user_mock.return_value = None |
|
151 | self.create_user_mock.return_value = None | |
151 | self.finalizers.append(create_user_patch.stop) |
|
152 | self.finalizers.append(create_user_patch.stop) | |
152 |
|
153 | |||
153 | auth_patch = mock.patch.object(RhodeCodeAuthPlugin, 'auth') |
|
154 | auth_patch = mock.patch.object(RhodeCodeAuthPlugin, 'auth') | |
154 | self.auth_mock = auth_patch.start() |
|
155 | self.auth_mock = auth_patch.start() | |
155 | self.auth_mock.return_value = self.fake_auth |
|
156 | self.auth_mock.return_value = self.fake_auth | |
156 | self.finalizers.append(auth_patch.stop) |
|
157 | self.finalizers.append(auth_patch.stop) | |
157 |
|
158 | |||
158 | password_generator_patch = mock.patch( |
|
159 | password_generator_patch = mock.patch( | |
159 | 'rhodecode.lib.auth.PasswordGenerator.gen_password') |
|
160 | 'rhodecode.lib.auth.PasswordGenerator.gen_password') | |
160 | self.password_generator_mock = password_generator_patch.start() |
|
161 | self.password_generator_mock = password_generator_patch.start() | |
161 | self.password_generator_mock.return_value = 'new-password' |
|
162 | self.password_generator_mock.return_value = 'new-password' | |
162 | self.finalizers.append(password_generator_patch.stop) |
|
163 | self.finalizers.append(password_generator_patch.stop) | |
163 |
|
164 | |||
164 |
|
165 | |||
165 | def test_missing_ldap(): |
|
166 | def test_missing_ldap(): | |
166 | from rhodecode.model.validators import Missing |
|
167 | from rhodecode.model.validators import Missing | |
167 |
|
168 | |||
168 | try: |
|
169 | try: | |
169 | import ldap_not_existing |
|
170 | import ldap_not_existing | |
170 | except ImportError: |
|
171 | except ImportError: | |
171 | # means that python-ldap is not installed |
|
172 | # means that python-ldap is not installed | |
172 | ldap_not_existing = Missing |
|
173 | ldap_not_existing = Missing | |
173 |
|
174 | |||
174 | # missing is singleton |
|
175 | # missing is singleton | |
175 | assert ldap_not_existing == Missing |
|
176 | assert ldap_not_existing == Missing | |
176 |
|
177 | |||
177 |
|
178 | |||
178 | def test_import_ldap(): |
|
179 | def test_import_ldap(): | |
179 | from rhodecode.model.validators import Missing |
|
180 | from rhodecode.model.validators import Missing | |
180 |
|
181 | |||
181 | try: |
|
182 | try: | |
182 | import ldap |
|
183 | import ldap | |
183 | except ImportError: |
|
184 | except ImportError: | |
184 | # means that python-ldap is not installed |
|
185 | # means that python-ldap is not installed | |
185 | ldap = Missing |
|
186 | ldap = Missing | |
186 |
|
187 | |||
187 | # missing is singleton |
|
188 | # missing is singleton | |
188 | assert False is (ldap == Missing) |
|
189 | assert False is (ldap == Missing) |
General Comments 0
You need to be logged in to leave comments.
Login now