Commit 6a45b40d authored by Michal Čihař's avatar Michal Čihař

Improve hooks processing and testing

- let github/gitlab/bitbucket hooks report status if something matched
- test this functionality
- test forbidden commit hooks
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent c30e3550
......@@ -297,9 +297,7 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_commit_hook_project(self):
response = self.client.get(
reverse('hook-commit-project', kwargs={
'project': self.subproject.project.slug
})
reverse('hook-commit-project', kwargs=self.kw_project)
)
self.assertContains(response, 'Commit performed')
......@@ -307,10 +305,7 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_commit_hook_subproject(self):
response = self.client.get(
reverse('hook-commit-subproject', kwargs={
'project': self.subproject.project.slug,
'subproject': self.subproject.slug,
})
reverse('hook-commit-subproject', kwargs=self.kw_subproject)
)
self.assertContains(response, 'Commit performed')
......@@ -318,9 +313,7 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_project(self):
response = self.client.get(
reverse('hook-project', kwargs={
'project': self.subproject.project.slug
})
reverse('hook-project', kwargs=self.kw_project)
)
self.assertContains(response, 'Update triggered')
......@@ -328,22 +321,31 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_subproject(self):
response = self.client.get(
reverse('hook-subproject', kwargs={
'project': self.subproject.project.slug,
'subproject': self.subproject.slug,
})
reverse('hook-subproject', kwargs=self.kw_subproject)
)
self.assertContains(response, 'Update triggered')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_github(self):
def test_view_hook_github_exists(self):
# Adjust matching repo
self.subproject.repo = 'git://github.com/defunkt/github.git'
self.subproject.save()
response = self.client.post(
reverse('hook-github'),
{'payload': GITHUB_PAYLOAD}
)
self.assertContains(response, 'Update triggered')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_github(self):
response = self.client.post(
reverse('hook-github'),
{'payload': GITHUB_PAYLOAD}
)
self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_gitlab(self):
......@@ -351,7 +353,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-gitlab'), GITLAB_PAYLOAD,
content_type="application/json"
)
self.assertContains(response, 'Update triggered')
self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
......@@ -360,7 +362,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_GIT}
)
self.assertContains(response, 'Update triggered')
self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
......@@ -369,7 +371,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_HG}
)
self.assertContains(response, 'Update triggered')
self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
......@@ -378,7 +380,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_HG_NO_COMMIT}
)
self.assertContains(response, 'Update triggered')
self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False)
......@@ -387,39 +389,15 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_WEBHOOK}
)
self.assertContains(response, 'Update triggered')
self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=False)
def test_disabled(self):
'''
Test for hooks disabling.
'''
response = self.client.get(
reverse('hook-commit-project', kwargs={
'project': self.subproject.project.slug
})
)
self.assertEqual(response.status_code, 405)
response = self.client.get(
reverse('hook-commit-subproject', kwargs={
'project': self.subproject.project.slug,
'subproject': self.subproject.slug,
})
)
self.assertEqual(response.status_code, 405)
response = self.client.get(
reverse('hook-project', kwargs={
'project': self.subproject.project.slug
})
)
self.assertEqual(response.status_code, 405)
response = self.client.get(
reverse('hook-subproject', kwargs={
'project': self.subproject.project.slug,
'subproject': self.subproject.slug,
})
)
self.assertEqual(response.status_code, 405)
self.assert_disabled()
response = self.client.post(
reverse('hook-github'),
{'payload': GITHUB_PAYLOAD}
......@@ -436,6 +414,29 @@ class HooksViewTest(ViewTestCase):
)
self.assertEqual(response.status_code, 405)
def test_project_disabled(self):
self.project.enable_hooks = False
self.project.save()
self.assert_disabled()
def assert_disabled(self):
response = self.client.get(
reverse('hook-commit-project', kwargs=self.kw_project)
)
self.assertEqual(response.status_code, 405)
response = self.client.get(
reverse('hook-commit-subproject', kwargs=self.kw_subproject)
)
self.assertEqual(response.status_code, 405)
response = self.client.get(
reverse('hook-project', kwargs=self.kw_project)
)
self.assertEqual(response.status_code, 405)
response = self.client.get(
reverse('hook-subproject', kwargs=self.kw_subproject)
)
self.assertEqual(response.status_code, 405)
def test_wrong_payload_github(self):
'''
Tests for invalid payloads with github.
......
......@@ -70,10 +70,10 @@ GITHUB_REPOS = (
HOOK_HANDLERS = {}
def hook_response(response='Update triggered'):
def hook_response(response='Update triggered', status='success'):
"""Generic okay hook response"""
return JsonResponse(
data={'status': 'success', 'message': response},
data={'status': status, 'message': response},
)
......@@ -208,9 +208,11 @@ def vcs_service_hook(request, service):
subprojects = subprojects.filter(branch=branch)
# Trigger updates
updates = 0
for obj in subprojects:
if not obj.project.enable_hooks:
continue
updates += 1
LOGGER.info(
'%s notification will update %s',
service_long_name,
......@@ -218,6 +220,9 @@ def vcs_service_hook(request, service):
)
perform_update(obj)
if updates == 0:
return hook_response('No matching repositories found!', 'failure')
return hook_response()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment