Show More
@@ -1,345 +1,346 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2020 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 | """ |
|
22 | 22 | py.test config for test suite for making push/pull operations. |
|
23 | 23 | |
|
24 | 24 | .. important:: |
|
25 | 25 | |
|
26 | 26 | You must have git >= 1.8.5 for tests to work fine. With 68b939b git started |
|
27 | 27 | to redirect things to stderr instead of stdout. |
|
28 | 28 | """ |
|
29 | 29 | |
|
30 | 30 | import os |
|
31 | 31 | import tempfile |
|
32 | 32 | import textwrap |
|
33 | 33 | import pytest |
|
34 | 34 | import logging |
|
35 | 35 | |
|
36 | 36 | from rhodecode import events |
|
37 | from rhodecode.lib.str_utils import safe_bytes | |
|
37 | 38 | from rhodecode.model.db import Integration, UserRepoToPerm, Permission, \ |
|
38 | 39 | UserToRepoBranchPermission, User |
|
39 | 40 | from rhodecode.model.integration import IntegrationModel |
|
40 | 41 | from rhodecode.model.db import Repository |
|
41 | 42 | from rhodecode.model.meta import Session |
|
42 | 43 | from rhodecode.model.settings import SettingsModel |
|
43 | 44 | from rhodecode.integrations.types.webhook import WebhookIntegrationType |
|
44 | 45 | |
|
45 | 46 | from rhodecode.tests import GIT_REPO, HG_REPO |
|
46 | 47 | from rhodecode.tests.fixture import Fixture |
|
47 | 48 | from rhodecode.tests.server_utils import RcWebServer |
|
48 | 49 | |
|
49 | 50 | REPO_GROUP = 'a_repo_group' |
|
50 | 51 | HG_REPO_WITH_GROUP = '%s/%s' % (REPO_GROUP, HG_REPO) |
|
51 | 52 | GIT_REPO_WITH_GROUP = '%s/%s' % (REPO_GROUP, GIT_REPO) |
|
52 | 53 | |
|
53 | 54 | log = logging.getLogger(__name__) |
|
54 | 55 | |
|
55 | 56 | |
|
56 | 57 | @pytest.fixture(scope="module") |
|
57 | 58 | def rcextensions(request, db_connection, tmpdir_factory): |
|
58 | 59 | """ |
|
59 | 60 | Installs a testing rcextensions pack to ensure they work as expected. |
|
60 | 61 | """ |
|
61 | 62 | init_content = textwrap.dedent(""" |
|
62 | 63 | # Forward import the example rcextensions to make it |
|
63 | 64 | # active for our tests. |
|
64 | 65 | from rhodecode.tests.other.example_rcextensions import * |
|
65 | 66 | """) |
|
66 | 67 | |
|
67 | 68 | # Note: rcextensions are looked up based on the path of the ini file |
|
68 | 69 | root_path = tmpdir_factory.getbasetemp() |
|
69 | 70 | rcextensions_path = root_path.join('rcextensions') |
|
70 | 71 | init_path = rcextensions_path.join('__init__.py') |
|
71 | 72 | |
|
72 | 73 | if rcextensions_path.check(): |
|
73 | 74 | pytest.fail( |
|
74 | 75 | "Path for rcextensions already exists, please clean up before " |
|
75 | 76 | "test run this path: %s" % (rcextensions_path, )) |
|
76 | 77 | return |
|
77 | 78 | |
|
78 | 79 | request.addfinalizer(rcextensions_path.remove) |
|
79 | init_path.write_binary(init_content, ensure=True) | |
|
80 | init_path.write_binary(safe_bytes(init_content), ensure=True) | |
|
80 | 81 | |
|
81 | 82 | |
|
82 | 83 | @pytest.fixture(scope="module") |
|
83 | 84 | def repos(request, db_connection): |
|
84 | 85 | """Create a copy of each test repo in a repo group.""" |
|
85 | 86 | fixture = Fixture() |
|
86 | 87 | repo_group = fixture.create_repo_group(REPO_GROUP) |
|
87 | 88 | repo_group_id = repo_group.group_id |
|
88 | 89 | fixture.create_fork(HG_REPO, HG_REPO, |
|
89 | 90 | repo_name_full=HG_REPO_WITH_GROUP, |
|
90 | 91 | repo_group=repo_group_id) |
|
91 | 92 | fixture.create_fork(GIT_REPO, GIT_REPO, |
|
92 | 93 | repo_name_full=GIT_REPO_WITH_GROUP, |
|
93 | 94 | repo_group=repo_group_id) |
|
94 | 95 | |
|
95 | 96 | @request.addfinalizer |
|
96 | 97 | def cleanup(): |
|
97 | 98 | fixture.destroy_repo(HG_REPO_WITH_GROUP) |
|
98 | 99 | fixture.destroy_repo(GIT_REPO_WITH_GROUP) |
|
99 | 100 | fixture.destroy_repo_group(repo_group_id) |
|
100 | 101 | |
|
101 | 102 | |
|
102 | 103 | @pytest.fixture(scope="module") |
|
103 | 104 | def rc_web_server_config_modification(): |
|
104 | 105 | return [] |
|
105 | 106 | |
|
106 | 107 | |
|
107 | 108 | @pytest.fixture(scope="module") |
|
108 | 109 | def rc_web_server_config_factory(testini_factory, rc_web_server_config_modification): |
|
109 | 110 | """ |
|
110 | 111 | Configuration file used for the fixture `rc_web_server`. |
|
111 | 112 | """ |
|
112 | 113 | |
|
113 | 114 | def factory(rcweb_port, vcsserver_port): |
|
114 | 115 | custom_params = [ |
|
115 | 116 | {'handler_console': {'level': 'DEBUG'}}, |
|
116 | 117 | {'server:main': {'port': rcweb_port}}, |
|
117 | 118 | {'app:main': {'vcs.server': 'localhost:%s' % vcsserver_port}} |
|
118 | 119 | ] |
|
119 | 120 | custom_params.extend(rc_web_server_config_modification) |
|
120 | 121 | return testini_factory(custom_params) |
|
121 | 122 | return factory |
|
122 | 123 | |
|
123 | 124 | |
|
124 | 125 | @pytest.fixture(scope="module") |
|
125 | 126 | def rc_web_server( |
|
126 | 127 | request, vcsserver_factory, available_port_factory, |
|
127 | 128 | rc_web_server_config_factory, repos, rcextensions): |
|
128 | 129 | """ |
|
129 | 130 | Run the web server as a subprocess. with it's own instance of vcsserver |
|
130 | 131 | """ |
|
131 | 132 | rcweb_port = available_port_factory() |
|
132 | 133 | log.info('Using rcweb ops test port {}'.format(rcweb_port)) |
|
133 | 134 | |
|
134 | 135 | vcsserver_port = available_port_factory() |
|
135 | 136 | log.info('Using vcsserver ops test port {}'.format(vcsserver_port)) |
|
136 | 137 | |
|
137 | 138 | vcs_log = os.path.join(tempfile.gettempdir(), 'rc_op_vcs.log') |
|
138 | 139 | vcsserver_factory( |
|
139 | 140 | request, vcsserver_port=vcsserver_port, |
|
140 | 141 | log_file=vcs_log, |
|
141 | 142 | overrides=( |
|
142 | 143 | {'server:main': {'workers': 2}}, |
|
143 | 144 | {'server:main': {'graceful_timeout': 10}}, |
|
144 | 145 | )) |
|
145 | 146 | |
|
146 | 147 | rc_log = os.path.join(tempfile.gettempdir(), 'rc_op_web.log') |
|
147 | 148 | rc_web_server_config = rc_web_server_config_factory( |
|
148 | 149 | rcweb_port=rcweb_port, |
|
149 | 150 | vcsserver_port=vcsserver_port) |
|
150 | 151 | server = RcWebServer(rc_web_server_config, log_file=rc_log) |
|
151 | 152 | server.start() |
|
152 | 153 | |
|
153 | 154 | @request.addfinalizer |
|
154 | 155 | def cleanup(): |
|
155 | 156 | server.shutdown() |
|
156 | 157 | |
|
157 | 158 | server.wait_until_ready() |
|
158 | 159 | return server |
|
159 | 160 | |
|
160 | 161 | |
|
161 | 162 | @pytest.fixture() |
|
162 | 163 | def disable_locking(baseapp): |
|
163 | 164 | r = Repository.get_by_repo_name(GIT_REPO) |
|
164 | 165 | Repository.unlock(r) |
|
165 | 166 | r.enable_locking = False |
|
166 | 167 | Session().add(r) |
|
167 | 168 | Session().commit() |
|
168 | 169 | |
|
169 | 170 | r = Repository.get_by_repo_name(HG_REPO) |
|
170 | 171 | Repository.unlock(r) |
|
171 | 172 | r.enable_locking = False |
|
172 | 173 | Session().add(r) |
|
173 | 174 | Session().commit() |
|
174 | 175 | |
|
175 | 176 | |
|
176 | 177 | @pytest.fixture() |
|
177 | 178 | def enable_auth_plugins(request, baseapp, csrf_token): |
|
178 | 179 | """ |
|
179 | 180 | Return a factory object that when called, allows to control which |
|
180 | 181 | authentication plugins are enabled. |
|
181 | 182 | """ |
|
182 | 183 | def _enable_plugins(plugins_list, override=None): |
|
183 | 184 | override = override or {} |
|
184 | 185 | params = { |
|
185 | 186 | 'auth_plugins': ','.join(plugins_list), |
|
186 | 187 | } |
|
187 | 188 | |
|
188 | 189 | # helper translate some names to others |
|
189 | 190 | name_map = { |
|
190 | 191 | 'token': 'authtoken' |
|
191 | 192 | } |
|
192 | 193 | |
|
193 | 194 | for module in plugins_list: |
|
194 | 195 | plugin_name = module.partition('#')[-1] |
|
195 | 196 | if plugin_name in name_map: |
|
196 | 197 | plugin_name = name_map[plugin_name] |
|
197 | 198 | enabled_plugin = 'auth_%s_enabled' % plugin_name |
|
198 | 199 | cache_ttl = 'auth_%s_cache_ttl' % plugin_name |
|
199 | 200 | |
|
200 | 201 | # default params that are needed for each plugin, |
|
201 | 202 | # `enabled` and `cache_ttl` |
|
202 | 203 | params.update({ |
|
203 | 204 | enabled_plugin: True, |
|
204 | 205 | cache_ttl: 0 |
|
205 | 206 | }) |
|
206 | 207 | if override.get: |
|
207 | 208 | params.update(override.get(module, {})) |
|
208 | 209 | |
|
209 | 210 | validated_params = params |
|
210 | 211 | for k, v in validated_params.items(): |
|
211 | 212 | setting = SettingsModel().create_or_update_setting(k, v) |
|
212 | 213 | Session().add(setting) |
|
213 | 214 | Session().commit() |
|
214 | 215 | |
|
215 | 216 | SettingsModel().invalidate_settings_cache() |
|
216 | 217 | |
|
217 | 218 | def cleanup(): |
|
218 | 219 | _enable_plugins(['egg:rhodecode-enterprise-ce#rhodecode']) |
|
219 | 220 | |
|
220 | 221 | request.addfinalizer(cleanup) |
|
221 | 222 | |
|
222 | 223 | return _enable_plugins |
|
223 | 224 | |
|
224 | 225 | |
|
225 | 226 | @pytest.fixture() |
|
226 | 227 | def fs_repo_only(request, rhodecode_fixtures): |
|
227 | 228 | def fs_repo_fabric(repo_name, repo_type): |
|
228 | 229 | rhodecode_fixtures.create_repo(repo_name, repo_type=repo_type) |
|
229 | 230 | rhodecode_fixtures.destroy_repo(repo_name, fs_remove=False) |
|
230 | 231 | |
|
231 | 232 | def cleanup(): |
|
232 | 233 | rhodecode_fixtures.destroy_repo(repo_name, fs_remove=True) |
|
233 | 234 | rhodecode_fixtures.destroy_repo_on_filesystem(repo_name) |
|
234 | 235 | |
|
235 | 236 | request.addfinalizer(cleanup) |
|
236 | 237 | |
|
237 | 238 | return fs_repo_fabric |
|
238 | 239 | |
|
239 | 240 | |
|
240 | 241 | @pytest.fixture() |
|
241 | 242 | def enable_webhook_push_integration(request): |
|
242 | 243 | integration = Integration() |
|
243 | 244 | integration.integration_type = WebhookIntegrationType.key |
|
244 | 245 | Session().add(integration) |
|
245 | 246 | |
|
246 | 247 | settings = dict( |
|
247 | 248 | url='http://httpbin.org/post', |
|
248 | 249 | secret_token='secret', |
|
249 | 250 | username=None, |
|
250 | 251 | password=None, |
|
251 | 252 | custom_header_key=None, |
|
252 | 253 | custom_header_val=None, |
|
253 | 254 | method_type='post', |
|
254 | 255 | events=[events.RepoPushEvent.name], |
|
255 | 256 | log_data=True |
|
256 | 257 | ) |
|
257 | 258 | |
|
258 | 259 | IntegrationModel().update_integration( |
|
259 | 260 | integration, |
|
260 | 261 | name='IntegrationWebhookTest', |
|
261 | 262 | enabled=True, |
|
262 | 263 | settings=settings, |
|
263 | 264 | repo=None, |
|
264 | 265 | repo_group=None, |
|
265 | 266 | child_repos_only=False, |
|
266 | 267 | ) |
|
267 | 268 | Session().commit() |
|
268 | 269 | integration_id = integration.integration_id |
|
269 | 270 | |
|
270 | 271 | @request.addfinalizer |
|
271 | 272 | def cleanup(): |
|
272 | 273 | integration = Integration.get(integration_id) |
|
273 | 274 | Session().delete(integration) |
|
274 | 275 | Session().commit() |
|
275 | 276 | |
|
276 | 277 | |
|
277 | 278 | @pytest.fixture() |
|
278 | 279 | def branch_permission_setter(request): |
|
279 | 280 | """ |
|
280 | 281 | |
|
281 | 282 | def my_test(branch_permission_setter) |
|
282 | 283 | branch_permission_setter(repo_name, username, pattern='*', permission='branch.push') |
|
283 | 284 | |
|
284 | 285 | """ |
|
285 | 286 | |
|
286 | 287 | rule_id = None |
|
287 | 288 | write_perm_id = None |
|
288 | 289 | write_perm = None |
|
289 | 290 | rule = None |
|
290 | 291 | |
|
291 | 292 | def _branch_permissions_setter( |
|
292 | 293 | repo_name, username, pattern='*', permission='branch.push_force'): |
|
293 | 294 | global rule_id, write_perm_id |
|
294 | 295 | global rule, write_perm |
|
295 | 296 | |
|
296 | 297 | repo = Repository.get_by_repo_name(repo_name) |
|
297 | 298 | repo_id = repo.repo_id |
|
298 | 299 | |
|
299 | 300 | user = User.get_by_username(username) |
|
300 | 301 | user_id = user.user_id |
|
301 | 302 | |
|
302 | 303 | rule_perm_obj = Permission.get_by_key(permission) |
|
303 | 304 | |
|
304 | 305 | # add new entry, based on existing perm entry |
|
305 | 306 | perm = UserRepoToPerm.query() \ |
|
306 | 307 | .filter(UserRepoToPerm.repository_id == repo_id) \ |
|
307 | 308 | .filter(UserRepoToPerm.user_id == user_id) \ |
|
308 | 309 | .first() |
|
309 | 310 | |
|
310 | 311 | if not perm: |
|
311 | 312 | # such user isn't defined in Permissions for repository |
|
312 | 313 | # we now on-the-fly add new permission |
|
313 | 314 | |
|
314 | 315 | write_perm = UserRepoToPerm() |
|
315 | 316 | write_perm.permission = Permission.get_by_key('repository.write') |
|
316 | 317 | write_perm.repository_id = repo_id |
|
317 | 318 | write_perm.user_id = user_id |
|
318 | 319 | Session().add(write_perm) |
|
319 | 320 | Session().flush() |
|
320 | 321 | |
|
321 | 322 | perm = write_perm |
|
322 | 323 | |
|
323 | 324 | rule = UserToRepoBranchPermission() |
|
324 | 325 | rule.rule_to_perm_id = perm.repo_to_perm_id |
|
325 | 326 | rule.branch_pattern = pattern |
|
326 | 327 | rule.rule_order = 10 |
|
327 | 328 | rule.permission = rule_perm_obj |
|
328 | 329 | rule.repository_id = repo_id |
|
329 | 330 | Session().add(rule) |
|
330 | 331 | Session().commit() |
|
331 | 332 | |
|
332 | 333 | return rule |
|
333 | 334 | |
|
334 | 335 | @request.addfinalizer |
|
335 | 336 | def cleanup(): |
|
336 | 337 | if rule: |
|
337 | 338 | Session().delete(rule) |
|
338 | 339 | Session().commit() |
|
339 | 340 | if write_perm: |
|
340 | 341 | Session().delete(write_perm) |
|
341 | 342 | Session().commit() |
|
342 | 343 | |
|
343 | 344 | return _branch_permissions_setter |
|
344 | 345 | |
|
345 | 346 |
General Comments 0
You need to be logged in to leave comments.
Login now