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