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