##// END OF EJS Templates
webhook: handle ${commit_id} variable independent from ${branch}....
marcink -
r3041:d813300a stable
parent child
Show More
@@ -32,6 +32,65 def route_path(name, **kwargs):
32 }[name].format(**kwargs)
32 }[name].format(**kwargs)
33
33
34
34
35 def _post_integration_test_helper(app, url, csrf_token, repo, repo_group,
36 admin_view):
37 """
38 Posts form data to create integration at the url given then deletes it and
39 checks if the redirect url is correct.
40 """
41 repo_name = repo.repo_name
42 repo_group_name = repo_group.group_name
43 app.post(url, params={}, status=403) # missing csrf check
44 response = app.post(url, params={'csrf_token': csrf_token})
45 assert response.status_code == 200
46 response.mustcontain('Errors exist')
47
48 scopes_destinations = [
49 ('global',
50 ADMIN_PREFIX + '/integrations'),
51 ('root-repos',
52 ADMIN_PREFIX + '/integrations'),
53 ('repo:%s' % repo_name,
54 '/%s/settings/integrations' % repo_name),
55 ('repogroup:%s' % repo_group_name,
56 '/%s/_settings/integrations' % repo_group_name),
57 ('repogroup-recursive:%s' % repo_group_name,
58 '/%s/_settings/integrations' % repo_group_name),
59 ]
60
61 for scope, destination in scopes_destinations:
62 if admin_view:
63 destination = ADMIN_PREFIX + '/integrations'
64
65 form_data = [
66 ('csrf_token', csrf_token),
67 ('__start__', 'options:mapping'),
68 ('name', 'test integration'),
69 ('scope', scope),
70 ('enabled', 'true'),
71 ('__end__', 'options:mapping'),
72 ('__start__', 'settings:mapping'),
73 ('test_int_field', '34'),
74 ('test_string_field', ''), # empty value on purpose as it's required
75 ('__end__', 'settings:mapping'),
76 ]
77 errors_response = app.post(url, form_data)
78 assert 'Errors exist' in errors_response.body
79
80 form_data[-2] = ('test_string_field', 'data!')
81 assert Session().query(Integration).count() == 0
82 created_response = app.post(url, form_data)
83 assert Session().query(Integration).count() == 1
84
85 delete_response = app.post(
86 created_response.location,
87 params={'csrf_token': csrf_token, 'delete': 'delete'})
88
89 assert Session().query(Integration).count() == 0
90 assert delete_response.location.endswith(destination)
91
92
93
35 @pytest.mark.usefixtures('app', 'autologin_user')
94 @pytest.mark.usefixtures('app', 'autologin_user')
36 class TestIntegrationsView(object):
95 class TestIntegrationsView(object):
37 pass
96 pass
@@ -209,61 +268,3 class TestRepoGroupIntegrationsView(Test
209 _post_integration_test_helper(
268 _post_integration_test_helper(
210 self.app, url, csrf_token, admin_view=False,
269 self.app, url, csrf_token, admin_view=False,
211 repo=backend_random.repo, repo_group=test_repo_group)
270 repo=backend_random.repo, repo_group=test_repo_group)
212
213
214 def _post_integration_test_helper(app, url, csrf_token, repo, repo_group,
215 admin_view):
216 """
217 Posts form data to create integration at the url given then deletes it and
218 checks if the redirect url is correct.
219 """
220 repo_name = repo.repo_name
221 repo_group_name = repo_group.group_name
222 app.post(url, params={}, status=403) # missing csrf check
223 response = app.post(url, params={'csrf_token': csrf_token})
224 assert response.status_code == 200
225 response.mustcontain('Errors exist')
226
227 scopes_destinations = [
228 ('global',
229 ADMIN_PREFIX + '/integrations'),
230 ('root-repos',
231 ADMIN_PREFIX + '/integrations'),
232 ('repo:%s' % repo_name,
233 '/%s/settings/integrations' % repo_name),
234 ('repogroup:%s' % repo_group_name,
235 '/%s/_settings/integrations' % repo_group_name),
236 ('repogroup-recursive:%s' % repo_group_name,
237 '/%s/_settings/integrations' % repo_group_name),
238 ]
239
240 for scope, destination in scopes_destinations:
241 if admin_view:
242 destination = ADMIN_PREFIX + '/integrations'
243
244 form_data = [
245 ('csrf_token', csrf_token),
246 ('__start__', 'options:mapping'),
247 ('name', 'test integration'),
248 ('scope', scope),
249 ('enabled', 'true'),
250 ('__end__', 'options:mapping'),
251 ('__start__', 'settings:mapping'),
252 ('test_int_field', '34'),
253 ('test_string_field', ''), # empty value on purpose as it's required
254 ('__end__', 'settings:mapping'),
255 ]
256 errors_response = app.post(url, form_data)
257 assert 'Errors exist' in errors_response.body
258
259 form_data[-2] = ('test_string_field', 'data!')
260 assert Session().query(Integration).count() == 0
261 created_response = app.post(url, form_data)
262 assert Session().query(Integration).count() == 1
263
264 delete_response = app.post(
265 created_response.location,
266 params={'csrf_token': csrf_token, 'delete': 'delete'})
267
268 assert Session().query(Integration).count() == 0
269 assert delete_response.location.endswith(destination)
@@ -218,11 +218,11 class WebhookDataHandler(CommitParsingDa
218
218
219 def repo_push_event_handler(self, event, data):
219 def repo_push_event_handler(self, event, data):
220 url = self.get_base_parsed_template(data)
220 url = self.get_base_parsed_template(data)
221 url_cals = []
221 url_calls = []
222
222
223 branches_commits = self.aggregate_branch_data(
223 branches_commits = self.aggregate_branch_data(
224 data['push']['branches'], data['push']['commits'])
224 data['push']['branches'], data['push']['commits'])
225 if '${branch}' in url or '${branch_head}' in url:
225 if '${branch}' in url or '${branch_head}' in url or '${commit_id}' in url:
226 # call it multiple times, for each branch if used in variables
226 # call it multiple times, for each branch if used in variables
227 for branch, commit_ids in branches_commits.items():
227 for branch, commit_ids in branches_commits.items():
228 branch_url = string.Template(url).safe_substitute(branch=branch)
228 branch_url = string.Template(url).safe_substitute(branch=branch)
@@ -243,7 +243,7 class WebhookDataHandler(CommitParsingDa
243 log.debug(
243 log.debug(
244 'register %s call(%s) to url %s',
244 'register %s call(%s) to url %s',
245 self.name, event, commit_url)
245 self.name, event, commit_url)
246 url_cals.append(
246 url_calls.append(
247 (commit_url, self.headers, data))
247 (commit_url, self.headers, data))
248
248
249 else:
249 else:
@@ -251,15 +251,15 class WebhookDataHandler(CommitParsingDa
251 log.debug(
251 log.debug(
252 'register %s call(%s) to url %s',
252 'register %s call(%s) to url %s',
253 self.name, event, branch_url)
253 self.name, event, branch_url)
254 url_cals.append(
254 url_calls.append(
255 (branch_url, self.headers, data))
255 (branch_url, self.headers, data))
256
256
257 else:
257 else:
258 log.debug(
258 log.debug(
259 'register %s call(%s) to url %s', self.name, event, url)
259 'register %s call(%s) to url %s', self.name, event, url)
260 url_cals.append((url, self.headers, data))
260 url_calls.append((url, self.headers, data))
261
261
262 return url_cals
262 return url_calls
263
263
264 def repo_create_event_handler(self, event, data):
264 def repo_create_event_handler(self, event, data):
265 url = self.get_base_parsed_template(data)
265 url = self.get_base_parsed_template(data)
@@ -107,6 +107,14 def test_webook_parse_url_for_pull_reque
107 'http://server.com/stable/stable-yyy',
107 'http://server.com/stable/stable-yyy',
108 'http://server.com/dev/dev-xxx',
108 'http://server.com/dev/dev-xxx',
109 'http://server.com/dev/dev-yyy']),
109 'http://server.com/dev/dev-yyy']),
110 ('http://server.com/${branch_head}',
111 ['http://server.com/stable-yyy',
112 'http://server.com/dev-yyy']),
113 ('http://server.com/${commit_id}',
114 ['http://server.com/stable-xxx',
115 'http://server.com/stable-yyy',
116 'http://server.com/dev-xxx',
117 'http://server.com/dev-yyy']),
110 ])
118 ])
111 def test_webook_parse_url_for_push_event(
119 def test_webook_parse_url_for_push_event(
112 baseapp, repo_push_event, base_data, template, expected_urls):
120 baseapp, repo_push_event, base_data, template, expected_urls):
General Comments 0
You need to be logged in to leave comments. Login now