##// END OF EJS Templates
tests: use post for testing webhook at httpbin.org. With recent changes...
marcink -
r2832:d2e46860 default
parent child Browse files
Show More
@@ -1,269 +1,269 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2018 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
37 37 from rhodecode.model.integration import IntegrationModel
38 38 from rhodecode.model.db import Repository
39 39 from rhodecode.model.meta import Session
40 40 from rhodecode.model.settings import SettingsModel
41 41 from rhodecode.integrations.types.webhook import WebhookIntegrationType
42 42
43 43 from rhodecode.tests import GIT_REPO, HG_REPO
44 44 from rhodecode.tests.fixture import Fixture
45 45 from rhodecode.tests.server_utils import RcWebServer
46 46
47 47 REPO_GROUP = 'a_repo_group'
48 48 HG_REPO_WITH_GROUP = '%s/%s' % (REPO_GROUP, HG_REPO)
49 49 GIT_REPO_WITH_GROUP = '%s/%s' % (REPO_GROUP, GIT_REPO)
50 50
51 51
52 52 @pytest.fixture(scope="module")
53 53 def rcextensions(request, db_connection, tmpdir_factory):
54 54 """
55 55 Installs a testing rcextensions pack to ensure they work as expected.
56 56 """
57 57 init_content = textwrap.dedent("""
58 58 # Forward import the example rcextensions to make it
59 59 # active for our tests.
60 60 from rhodecode.tests.other.example_rcextensions import *
61 61 """)
62 62
63 63 # Note: rcextensions are looked up based on the path of the ini file
64 64 root_path = tmpdir_factory.getbasetemp()
65 65 rcextensions_path = root_path.join('rcextensions')
66 66 init_path = rcextensions_path.join('__init__.py')
67 67
68 68 if rcextensions_path.check():
69 69 pytest.fail(
70 70 "Path for rcextensions already exists, please clean up before "
71 71 "test run this path: %s" % (rcextensions_path, ))
72 72 return
73 73
74 74 request.addfinalizer(rcextensions_path.remove)
75 75 init_path.write_binary(init_content, ensure=True)
76 76
77 77
78 78 @pytest.fixture(scope="module")
79 79 def repos(request, db_connection):
80 80 """Create a copy of each test repo in a repo group."""
81 81 fixture = Fixture()
82 82 repo_group = fixture.create_repo_group(REPO_GROUP)
83 83 repo_group_id = repo_group.group_id
84 84 fixture.create_fork(HG_REPO, HG_REPO,
85 85 repo_name_full=HG_REPO_WITH_GROUP,
86 86 repo_group=repo_group_id)
87 87 fixture.create_fork(GIT_REPO, GIT_REPO,
88 88 repo_name_full=GIT_REPO_WITH_GROUP,
89 89 repo_group=repo_group_id)
90 90
91 91 @request.addfinalizer
92 92 def cleanup():
93 93 fixture.destroy_repo(HG_REPO_WITH_GROUP)
94 94 fixture.destroy_repo(GIT_REPO_WITH_GROUP)
95 95 fixture.destroy_repo_group(repo_group_id)
96 96
97 97
98 98 @pytest.fixture(scope="module")
99 99 def rc_web_server_config_modification():
100 100 return []
101 101
102 102
103 103 @pytest.fixture(scope="module")
104 104 def rc_web_server_config_factory(testini_factory, rc_web_server_config_modification):
105 105 """
106 106 Configuration file used for the fixture `rc_web_server`.
107 107 """
108 108
109 109 def factory(rcweb_port, vcsserver_port):
110 110 custom_params = [
111 111 {'handler_console': {'level': 'DEBUG'}},
112 112 {'server:main': {'port': rcweb_port}},
113 113 {'app:main': {'vcs.server': 'localhost:%s' % vcsserver_port}}
114 114 ]
115 115 custom_params.extend(rc_web_server_config_modification)
116 116 return testini_factory(custom_params)
117 117 return factory
118 118
119 119
120 120 @pytest.fixture(scope="module")
121 121 def rc_web_server(
122 122 request, vcsserver_factory, available_port_factory,
123 123 rc_web_server_config_factory, repos, rcextensions):
124 124 """
125 125 Run the web server as a subprocess. with it's own instance of vcsserver
126 126 """
127 127 rcweb_port = available_port_factory()
128 128 print('Using rcweb ops test port {}'.format(rcweb_port))
129 129
130 130 vcsserver_port = available_port_factory()
131 131 print('Using vcsserver ops test port {}'.format(vcsserver_port))
132 132
133 133 vcs_log = os.path.join(tempfile.gettempdir(), 'rc_op_vcs.log')
134 134 vcsserver_factory(
135 135 request, vcsserver_port=vcsserver_port,
136 136 log_file=vcs_log,
137 137 overrides=(
138 138 {'server:main': {'workers': 2}},
139 139 {'server:main': {'graceful_timeout': 10}},
140 140 ))
141 141
142 142 rc_log = os.path.join(tempfile.gettempdir(), 'rc_op_web.log')
143 143 rc_web_server_config = rc_web_server_config_factory(
144 144 rcweb_port=rcweb_port,
145 145 vcsserver_port=vcsserver_port)
146 146 server = RcWebServer(rc_web_server_config, log_file=rc_log)
147 147 server.start()
148 148
149 149 @request.addfinalizer
150 150 def cleanup():
151 151 server.shutdown()
152 152
153 153 server.wait_until_ready()
154 154 return server
155 155
156 156
157 157 @pytest.fixture
158 158 def disable_locking(baseapp):
159 159 r = Repository.get_by_repo_name(GIT_REPO)
160 160 Repository.unlock(r)
161 161 r.enable_locking = False
162 162 Session().add(r)
163 163 Session().commit()
164 164
165 165 r = Repository.get_by_repo_name(HG_REPO)
166 166 Repository.unlock(r)
167 167 r.enable_locking = False
168 168 Session().add(r)
169 169 Session().commit()
170 170
171 171
172 172 @pytest.fixture
173 173 def enable_auth_plugins(request, baseapp, csrf_token):
174 174 """
175 175 Return a factory object that when called, allows to control which
176 176 authentication plugins are enabled.
177 177 """
178 178 def _enable_plugins(plugins_list, override=None):
179 179 override = override or {}
180 180 params = {
181 181 'auth_plugins': ','.join(plugins_list),
182 182 }
183 183
184 184 # helper translate some names to others
185 185 name_map = {
186 186 'token': 'authtoken'
187 187 }
188 188
189 189 for module in plugins_list:
190 190 plugin_name = module.partition('#')[-1]
191 191 if plugin_name in name_map:
192 192 plugin_name = name_map[plugin_name]
193 193 enabled_plugin = 'auth_%s_enabled' % plugin_name
194 194 cache_ttl = 'auth_%s_cache_ttl' % plugin_name
195 195
196 196 # default params that are needed for each plugin,
197 197 # `enabled` and `cache_ttl`
198 198 params.update({
199 199 enabled_plugin: True,
200 200 cache_ttl: 0
201 201 })
202 202 if override.get:
203 203 params.update(override.get(module, {}))
204 204
205 205 validated_params = params
206 206 for k, v in validated_params.items():
207 207 setting = SettingsModel().create_or_update_setting(k, v)
208 208 Session().add(setting)
209 209 Session().commit()
210 210
211 211 def cleanup():
212 212 _enable_plugins(['egg:rhodecode-enterprise-ce#rhodecode'])
213 213
214 214 request.addfinalizer(cleanup)
215 215
216 216 return _enable_plugins
217 217
218 218
219 219 @pytest.fixture
220 220 def fs_repo_only(request, rhodecode_fixtures):
221 221 def fs_repo_fabric(repo_name, repo_type):
222 222 rhodecode_fixtures.create_repo(repo_name, repo_type=repo_type)
223 223 rhodecode_fixtures.destroy_repo(repo_name, fs_remove=False)
224 224
225 225 def cleanup():
226 226 rhodecode_fixtures.destroy_repo(repo_name, fs_remove=True)
227 227 rhodecode_fixtures.destroy_repo_on_filesystem(repo_name)
228 228
229 229 request.addfinalizer(cleanup)
230 230
231 231 return fs_repo_fabric
232 232
233 233
234 234 @pytest.fixture
235 235 def enable_webhook_push_integration(request):
236 236 integration = Integration()
237 237 integration.integration_type = WebhookIntegrationType.key
238 238 Session().add(integration)
239 239
240 240 settings = dict(
241 url='http://httpbin.org',
241 url='http://httpbin.org/post',
242 242 secret_token='secret',
243 243 username=None,
244 244 password=None,
245 245 custom_header_key=None,
246 246 custom_header_val=None,
247 method_type='get',
247 method_type='post',
248 248 events=[events.RepoPushEvent.name],
249 249 log_data=True
250 250 )
251 251
252 252 IntegrationModel().update_integration(
253 253 integration,
254 254 name='IntegrationWebhookTest',
255 255 enabled=True,
256 256 settings=settings,
257 257 repo=None,
258 258 repo_group=None,
259 259 child_repos_only=False,
260 260 )
261 261 Session().commit()
262 262 integration_id = integration.integration_id
263 263
264 264 @request.addfinalizer
265 265 def cleanup():
266 266 integration = Integration.get(integration_id)
267 267 Session().delete(integration)
268 268 Session().commit()
269 269
General Comments 0
You need to be logged in to leave comments. Login now