Show More
@@ -1,307 +1,307 b'' | |||
|
1 | 1 | |
|
2 | 2 | # Copyright (C) 2010-2023 RhodeCode GmbH |
|
3 | 3 | # |
|
4 | 4 | # This program is free software: you can redistribute it and/or modify |
|
5 | 5 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | 6 | # (only), as published by the Free Software Foundation. |
|
7 | 7 | # |
|
8 | 8 | # This program is distributed in the hope that it will be useful, |
|
9 | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | 11 | # GNU General Public License for more details. |
|
12 | 12 | # |
|
13 | 13 | # You should have received a copy of the GNU Affero General Public License |
|
14 | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | 15 | # |
|
16 | 16 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | 17 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | 18 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | 19 | |
|
20 | 20 | """ |
|
21 | 21 | py.test config for test suite for making push/pull operations. |
|
22 | 22 | |
|
23 | 23 | .. important:: |
|
24 | 24 | |
|
25 | 25 | You must have git >= 1.8.5 for tests to work fine. With 68b939b git started |
|
26 | 26 | to redirect things to stderr instead of stdout. |
|
27 | 27 | """ |
|
28 | 28 | |
|
29 | 29 | import os |
|
30 | 30 | import tempfile |
|
31 | 31 | import textwrap |
|
32 | 32 | import pytest |
|
33 | 33 | import logging |
|
34 | 34 | import requests |
|
35 | 35 | |
|
36 | 36 | from rhodecode import events |
|
37 | 37 | from rhodecode.lib.str_utils import safe_bytes |
|
38 | 38 | from rhodecode.model.db import Integration, UserRepoToPerm, Permission, \ |
|
39 | 39 | UserToRepoBranchPermission, User |
|
40 | 40 | from rhodecode.model.integration import IntegrationModel |
|
41 | 41 | from rhodecode.model.db import Repository |
|
42 | 42 | from rhodecode.model.meta import Session |
|
43 | 43 | from rhodecode.integrations.types.webhook import WebhookIntegrationType |
|
44 | 44 | |
|
45 | 45 | from rhodecode.tests import GIT_REPO, HG_REPO |
|
46 | from rhodecode.tests.conftest import HTTPBIN_DOMAIN | |
|
46 | from rhodecode.tests.conftest import HTTPBIN_DOMAIN, HTTPBIN_POST | |
|
47 | 47 | from rhodecode.tests.fixture import Fixture |
|
48 | 48 | from rhodecode.tests.server_utils import RcWebServer |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | REPO_GROUP = 'a_repo_group' |
|
52 | 52 | HG_REPO_WITH_GROUP = f'{REPO_GROUP}/{HG_REPO}' |
|
53 | 53 | GIT_REPO_WITH_GROUP = f'{REPO_GROUP}/{GIT_REPO}' |
|
54 | 54 | |
|
55 | 55 | log = logging.getLogger(__name__) |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def check_httpbin_connection(): |
|
59 | 59 | try: |
|
60 | 60 | response = requests.get(HTTPBIN_DOMAIN) |
|
61 | 61 | return response.status_code == 200 |
|
62 | 62 | except Exception as e: |
|
63 | 63 | print(e) |
|
64 | 64 | |
|
65 | 65 | return False |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | @pytest.fixture(scope="module") |
|
69 | 69 | def rcextensions(request, db_connection, tmpdir_factory): |
|
70 | 70 | """ |
|
71 | 71 | Installs a testing rcextensions pack to ensure they work as expected. |
|
72 | 72 | """ |
|
73 | 73 | init_content = textwrap.dedent(""" |
|
74 | 74 | # Forward import the example rcextensions to make it |
|
75 | 75 | # active for our tests. |
|
76 | 76 | from rhodecode.tests.other.example_rcextensions import * |
|
77 | 77 | """) |
|
78 | 78 | |
|
79 | 79 | # Note: rcextensions are looked up based on the path of the ini file |
|
80 | 80 | root_path = tmpdir_factory.getbasetemp() |
|
81 | 81 | rcextensions_path = root_path.join('rcextensions') |
|
82 | 82 | init_path = rcextensions_path.join('__init__.py') |
|
83 | 83 | |
|
84 | 84 | if rcextensions_path.check(): |
|
85 | 85 | pytest.fail( |
|
86 | 86 | "Path for rcextensions already exists, please clean up before " |
|
87 | 87 | "test run this path: %s" % (rcextensions_path, )) |
|
88 | 88 | else: |
|
89 | 89 | request.addfinalizer(rcextensions_path.remove) |
|
90 | 90 | init_path.write_binary(safe_bytes(init_content), ensure=True) |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | @pytest.fixture(scope="module") |
|
94 | 94 | def repos(request, db_connection): |
|
95 | 95 | """Create a copy of each test repo in a repo group.""" |
|
96 | 96 | fixture = Fixture() |
|
97 | 97 | repo_group = fixture.create_repo_group(REPO_GROUP) |
|
98 | 98 | repo_group_id = repo_group.group_id |
|
99 | 99 | fixture.create_fork(HG_REPO, HG_REPO, |
|
100 | 100 | repo_name_full=HG_REPO_WITH_GROUP, |
|
101 | 101 | repo_group=repo_group_id) |
|
102 | 102 | fixture.create_fork(GIT_REPO, GIT_REPO, |
|
103 | 103 | repo_name_full=GIT_REPO_WITH_GROUP, |
|
104 | 104 | repo_group=repo_group_id) |
|
105 | 105 | |
|
106 | 106 | @request.addfinalizer |
|
107 | 107 | def cleanup(): |
|
108 | 108 | fixture.destroy_repo(HG_REPO_WITH_GROUP) |
|
109 | 109 | fixture.destroy_repo(GIT_REPO_WITH_GROUP) |
|
110 | 110 | fixture.destroy_repo_group(repo_group_id) |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | @pytest.fixture(scope="module") |
|
114 | 114 | def rc_web_server_config_modification(): |
|
115 | 115 | return [] |
|
116 | 116 | |
|
117 | 117 | |
|
118 | 118 | @pytest.fixture(scope="module") |
|
119 | 119 | def rc_web_server_config_factory(testini_factory, rc_web_server_config_modification): |
|
120 | 120 | """ |
|
121 | 121 | Configuration file used for the fixture `rc_web_server`. |
|
122 | 122 | """ |
|
123 | 123 | |
|
124 | 124 | def factory(rcweb_port, vcsserver_port): |
|
125 | 125 | custom_params = [ |
|
126 | 126 | {'handler_console': {'level': 'DEBUG'}}, |
|
127 | 127 | {'server:main': {'port': rcweb_port}}, |
|
128 | 128 | {'app:main': {'vcs.server': 'localhost:%s' % vcsserver_port}} |
|
129 | 129 | ] |
|
130 | 130 | custom_params.extend(rc_web_server_config_modification) |
|
131 | 131 | return testini_factory(custom_params) |
|
132 | 132 | return factory |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | @pytest.fixture(scope="module") |
|
136 | 136 | def rc_web_server( |
|
137 | 137 | request, vcsserver_factory, available_port_factory, |
|
138 | 138 | rc_web_server_config_factory, repos, rcextensions): |
|
139 | 139 | """ |
|
140 | 140 | Run the web server as a subprocess. with its own instance of vcsserver |
|
141 | 141 | """ |
|
142 | 142 | rcweb_port = available_port_factory() |
|
143 | 143 | log.info('Using rcweb ops test port {}'.format(rcweb_port)) |
|
144 | 144 | |
|
145 | 145 | vcsserver_port = available_port_factory() |
|
146 | 146 | log.info('Using vcsserver ops test port {}'.format(vcsserver_port)) |
|
147 | 147 | |
|
148 | 148 | vcs_log = os.path.join(tempfile.gettempdir(), 'rc_op_vcs.log') |
|
149 | 149 | vcsserver_factory( |
|
150 | 150 | request, vcsserver_port=vcsserver_port, |
|
151 | 151 | log_file=vcs_log, |
|
152 | 152 | overrides=( |
|
153 | 153 | {'server:main': {'workers': 2}}, |
|
154 | 154 | {'server:main': {'graceful_timeout': 10}}, |
|
155 | 155 | )) |
|
156 | 156 | |
|
157 | 157 | rc_log = os.path.join(tempfile.gettempdir(), 'rc_op_web.log') |
|
158 | 158 | rc_web_server_config = rc_web_server_config_factory( |
|
159 | 159 | rcweb_port=rcweb_port, |
|
160 | 160 | vcsserver_port=vcsserver_port) |
|
161 | 161 | server = RcWebServer(rc_web_server_config, log_file=rc_log) |
|
162 | 162 | server.start() |
|
163 | 163 | |
|
164 | 164 | @request.addfinalizer |
|
165 | 165 | def cleanup(): |
|
166 | 166 | server.shutdown() |
|
167 | 167 | |
|
168 | 168 | server.wait_until_ready() |
|
169 | 169 | return server |
|
170 | 170 | |
|
171 | 171 | |
|
172 | 172 | @pytest.fixture() |
|
173 | 173 | def disable_locking(baseapp): |
|
174 | 174 | r = Repository.get_by_repo_name(GIT_REPO) |
|
175 | 175 | Repository.unlock(r) |
|
176 | 176 | r.enable_locking = False |
|
177 | 177 | Session().add(r) |
|
178 | 178 | Session().commit() |
|
179 | 179 | |
|
180 | 180 | r = Repository.get_by_repo_name(HG_REPO) |
|
181 | 181 | Repository.unlock(r) |
|
182 | 182 | r.enable_locking = False |
|
183 | 183 | Session().add(r) |
|
184 | 184 | Session().commit() |
|
185 | 185 | |
|
186 | 186 | |
|
187 | 187 | @pytest.fixture() |
|
188 | 188 | def fs_repo_only(request, rhodecode_fixtures): |
|
189 | 189 | def fs_repo_fabric(repo_name, repo_type): |
|
190 | 190 | rhodecode_fixtures.create_repo(repo_name, repo_type=repo_type) |
|
191 | 191 | rhodecode_fixtures.destroy_repo(repo_name, fs_remove=False) |
|
192 | 192 | |
|
193 | 193 | def cleanup(): |
|
194 | 194 | rhodecode_fixtures.destroy_repo(repo_name, fs_remove=True) |
|
195 | 195 | rhodecode_fixtures.destroy_repo_on_filesystem(repo_name) |
|
196 | 196 | |
|
197 | 197 | request.addfinalizer(cleanup) |
|
198 | 198 | |
|
199 | 199 | return fs_repo_fabric |
|
200 | 200 | |
|
201 | 201 | |
|
202 | 202 | @pytest.fixture() |
|
203 | 203 | def enable_webhook_push_integration(request): |
|
204 | 204 | integration = Integration() |
|
205 | 205 | integration.integration_type = WebhookIntegrationType.key |
|
206 | 206 | Session().add(integration) |
|
207 | 207 | |
|
208 | 208 | settings = dict( |
|
209 | 209 | url=HTTPBIN_POST, |
|
210 | 210 | secret_token='secret', |
|
211 | 211 | username=None, |
|
212 | 212 | password=None, |
|
213 | 213 | custom_header_key=None, |
|
214 | 214 | custom_header_val=None, |
|
215 | 215 | method_type='post', |
|
216 | 216 | events=[events.RepoPushEvent.name], |
|
217 | 217 | log_data=True |
|
218 | 218 | ) |
|
219 | 219 | |
|
220 | 220 | IntegrationModel().update_integration( |
|
221 | 221 | integration, |
|
222 | 222 | name='IntegrationWebhookTest', |
|
223 | 223 | enabled=True, |
|
224 | 224 | settings=settings, |
|
225 | 225 | repo=None, |
|
226 | 226 | repo_group=None, |
|
227 | 227 | child_repos_only=False, |
|
228 | 228 | ) |
|
229 | 229 | Session().commit() |
|
230 | 230 | integration_id = integration.integration_id |
|
231 | 231 | |
|
232 | 232 | @request.addfinalizer |
|
233 | 233 | def cleanup(): |
|
234 | 234 | integration = Integration.get(integration_id) |
|
235 | 235 | Session().delete(integration) |
|
236 | 236 | Session().commit() |
|
237 | 237 | |
|
238 | 238 | |
|
239 | 239 | @pytest.fixture() |
|
240 | 240 | def branch_permission_setter(request): |
|
241 | 241 | """ |
|
242 | 242 | |
|
243 | 243 | def my_test(branch_permission_setter) |
|
244 | 244 | branch_permission_setter(repo_name, username, pattern='*', permission='branch.push') |
|
245 | 245 | |
|
246 | 246 | """ |
|
247 | 247 | |
|
248 | 248 | rule_id = None |
|
249 | 249 | write_perm_id = None |
|
250 | 250 | write_perm = None |
|
251 | 251 | rule = None |
|
252 | 252 | |
|
253 | 253 | def _branch_permissions_setter( |
|
254 | 254 | repo_name, username, pattern='*', permission='branch.push_force'): |
|
255 | 255 | global rule_id, write_perm_id |
|
256 | 256 | global rule, write_perm |
|
257 | 257 | |
|
258 | 258 | repo = Repository.get_by_repo_name(repo_name) |
|
259 | 259 | repo_id = repo.repo_id |
|
260 | 260 | |
|
261 | 261 | user = User.get_by_username(username) |
|
262 | 262 | user_id = user.user_id |
|
263 | 263 | |
|
264 | 264 | rule_perm_obj = Permission.get_by_key(permission) |
|
265 | 265 | |
|
266 | 266 | # add new entry, based on existing perm entry |
|
267 | 267 | perm = UserRepoToPerm.query() \ |
|
268 | 268 | .filter(UserRepoToPerm.repository_id == repo_id) \ |
|
269 | 269 | .filter(UserRepoToPerm.user_id == user_id) \ |
|
270 | 270 | .first() |
|
271 | 271 | |
|
272 | 272 | if not perm: |
|
273 | 273 | # such user isn't defined in Permissions for repository |
|
274 | 274 | # we now on-the-fly add new permission |
|
275 | 275 | |
|
276 | 276 | write_perm = UserRepoToPerm() |
|
277 | 277 | write_perm.permission = Permission.get_by_key('repository.write') |
|
278 | 278 | write_perm.repository_id = repo_id |
|
279 | 279 | write_perm.user_id = user_id |
|
280 | 280 | Session().add(write_perm) |
|
281 | 281 | Session().flush() |
|
282 | 282 | |
|
283 | 283 | perm = write_perm |
|
284 | 284 | |
|
285 | 285 | rule = UserToRepoBranchPermission() |
|
286 | 286 | rule.rule_to_perm_id = perm.repo_to_perm_id |
|
287 | 287 | rule.branch_pattern = pattern |
|
288 | 288 | rule.rule_order = 10 |
|
289 | 289 | rule.permission = rule_perm_obj |
|
290 | 290 | rule.repository_id = repo_id |
|
291 | 291 | Session().add(rule) |
|
292 | 292 | Session().commit() |
|
293 | 293 | |
|
294 | 294 | return rule |
|
295 | 295 | |
|
296 | 296 | @request.addfinalizer |
|
297 | 297 | def cleanup(): |
|
298 | 298 | if rule: |
|
299 | 299 | Session().delete(rule) |
|
300 | 300 | Session().commit() |
|
301 | 301 | if write_perm: |
|
302 | 302 | Session().delete(write_perm) |
|
303 | 303 | Session().commit() |
|
304 | 304 | |
|
305 | 305 | return _branch_permissions_setter |
|
306 | 306 | |
|
307 | 307 |
General Comments 0
You need to be logged in to leave comments.
Login now