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