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