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): ...@@ -297,9 +297,7 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_commit_hook_project(self): def test_view_commit_hook_project(self):
response = self.client.get( response = self.client.get(
reverse('hook-commit-project', kwargs={ reverse('hook-commit-project', kwargs=self.kw_project)
'project': self.subproject.project.slug
})
) )
self.assertContains(response, 'Commit performed') self.assertContains(response, 'Commit performed')
...@@ -307,10 +305,7 @@ class HooksViewTest(ViewTestCase): ...@@ -307,10 +305,7 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_commit_hook_subproject(self): def test_view_commit_hook_subproject(self):
response = self.client.get( response = self.client.get(
reverse('hook-commit-subproject', kwargs={ reverse('hook-commit-subproject', kwargs=self.kw_subproject)
'project': self.subproject.project.slug,
'subproject': self.subproject.slug,
})
) )
self.assertContains(response, 'Commit performed') self.assertContains(response, 'Commit performed')
...@@ -318,9 +313,7 @@ class HooksViewTest(ViewTestCase): ...@@ -318,9 +313,7 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_project(self): def test_view_hook_project(self):
response = self.client.get( response = self.client.get(
reverse('hook-project', kwargs={ reverse('hook-project', kwargs=self.kw_project)
'project': self.subproject.project.slug
})
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'Update triggered')
...@@ -328,22 +321,31 @@ class HooksViewTest(ViewTestCase): ...@@ -328,22 +321,31 @@ class HooksViewTest(ViewTestCase):
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_subproject(self): def test_view_hook_subproject(self):
response = self.client.get( response = self.client.get(
reverse('hook-subproject', kwargs={ reverse('hook-subproject', kwargs=self.kw_subproject)
'project': self.subproject.project.slug,
'subproject': self.subproject.slug,
})
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'Update triggered')
@OverrideSettings(ENABLE_HOOKS=True) @OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False) @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( response = self.client.post(
reverse('hook-github'), reverse('hook-github'),
{'payload': GITHUB_PAYLOAD} {'payload': GITHUB_PAYLOAD}
) )
self.assertContains(response, 'Update triggered') 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(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
def test_view_hook_gitlab(self): def test_view_hook_gitlab(self):
...@@ -351,7 +353,7 @@ class HooksViewTest(ViewTestCase): ...@@ -351,7 +353,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-gitlab'), GITLAB_PAYLOAD, reverse('hook-gitlab'), GITLAB_PAYLOAD,
content_type="application/json" content_type="application/json"
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True) @OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
...@@ -360,7 +362,7 @@ class HooksViewTest(ViewTestCase): ...@@ -360,7 +362,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'), reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_GIT} {'payload': BITBUCKET_PAYLOAD_GIT}
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True) @OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
...@@ -369,7 +371,7 @@ class HooksViewTest(ViewTestCase): ...@@ -369,7 +371,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'), reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_HG} {'payload': BITBUCKET_PAYLOAD_HG}
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True) @OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
...@@ -378,7 +380,7 @@ class HooksViewTest(ViewTestCase): ...@@ -378,7 +380,7 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'), reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_HG_NO_COMMIT} {'payload': BITBUCKET_PAYLOAD_HG_NO_COMMIT}
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=True) @OverrideSettings(ENABLE_HOOKS=True)
@OverrideSettings(BACKGROUND_HOOKS=False) @OverrideSettings(BACKGROUND_HOOKS=False)
...@@ -387,39 +389,15 @@ class HooksViewTest(ViewTestCase): ...@@ -387,39 +389,15 @@ class HooksViewTest(ViewTestCase):
reverse('hook-bitbucket'), reverse('hook-bitbucket'),
{'payload': BITBUCKET_PAYLOAD_WEBHOOK} {'payload': BITBUCKET_PAYLOAD_WEBHOOK}
) )
self.assertContains(response, 'Update triggered') self.assertContains(response, 'No matching repositories found!')
@OverrideSettings(ENABLE_HOOKS=False) @OverrideSettings(ENABLE_HOOKS=False)
def test_disabled(self): def test_disabled(self):
''' '''
Test for hooks disabling. Test for hooks disabling.
''' '''
response = self.client.get( self.assert_disabled()
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)
response = self.client.post( response = self.client.post(
reverse('hook-github'), reverse('hook-github'),
{'payload': GITHUB_PAYLOAD} {'payload': GITHUB_PAYLOAD}
...@@ -436,6 +414,29 @@ class HooksViewTest(ViewTestCase): ...@@ -436,6 +414,29 @@ class HooksViewTest(ViewTestCase):
) )
self.assertEqual(response.status_code, 405) 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): def test_wrong_payload_github(self):
''' '''
Tests for invalid payloads with github. Tests for invalid payloads with github.
......
...@@ -70,10 +70,10 @@ GITHUB_REPOS = ( ...@@ -70,10 +70,10 @@ GITHUB_REPOS = (
HOOK_HANDLERS = {} HOOK_HANDLERS = {}
def hook_response(response='Update triggered'): def hook_response(response='Update triggered', status='success'):
"""Generic okay hook response""" """Generic okay hook response"""
return JsonResponse( return JsonResponse(
data={'status': 'success', 'message': response}, data={'status': status, 'message': response},
) )
...@@ -208,9 +208,11 @@ def vcs_service_hook(request, service): ...@@ -208,9 +208,11 @@ def vcs_service_hook(request, service):
subprojects = subprojects.filter(branch=branch) subprojects = subprojects.filter(branch=branch)
# Trigger updates # Trigger updates
updates = 0
for obj in subprojects: for obj in subprojects:
if not obj.project.enable_hooks: if not obj.project.enable_hooks:
continue continue
updates += 1
LOGGER.info( LOGGER.info(
'%s notification will update %s', '%s notification will update %s',
service_long_name, service_long_name,
...@@ -218,6 +220,9 @@ def vcs_service_hook(request, service): ...@@ -218,6 +220,9 @@ def vcs_service_hook(request, service):
) )
perform_update(obj) perform_update(obj)
if updates == 0:
return hook_response('No matching repositories found!', 'failure')
return hook_response() 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