Skip to content

Commit

Permalink
feat: Report 404 when running an unknown health check
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed Nov 14, 2019
1 parent c6d424f commit 6e7ea94
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 16 additions & 2 deletions healthpoint/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.utils.http import urlencode


class HealthTestCase(TestCase):
Expand All @@ -16,5 +17,18 @@ def test_health_success(self):
resp = self._test_func('healthpoint.tests.health.success')
self.assertEqual(resp.status_code, 200)

def _test_func(self, func):
return self.client.get(reverse('healthpoint_health') + '?test=' + func)
def test_unknown_health(self):
resp = self._test_func('does.not.exist')
self.assertEqual(resp.status_code, 404)

def test_unknown_health_and_success(self):
resp = self._test_func('does.not.exist', 'healthpoint.tests.health.success')
self.assertEqual(resp.status_code, 404)

def test_unknown_health_and_failure(self):
resp = self._test_func('does.not.exist', 'healthpoint.tests.health.failure')
self.assertEqual(resp.status_code, 503)

def _test_func(self, *func):
params = urlencode({'test': func}, doseq=True)
return self.client.get(reverse('healthpoint_health') + '?' + params)
8 changes: 7 additions & 1 deletion healthpoint/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _show_health_details(request):


def health(request):
tests = request.GET.getlist('test')
tests = set(request.GET.getlist('test'))
data = {'success': {}, 'error': {}}
status = 200
for health_check in get_health_checks():
Expand All @@ -33,10 +33,16 @@ def health(request):
health_check.__qualname__])
if tests and func not in tests:
continue
tests.discard(func)
success, detail = health_check()
data['success' if success else 'error'][func] = detail
if not success:
status = 503
if tests:
if status == 200:
status = 404
for test in tests:
data['error'][test] = 'Unknown health check'
# Only staff members are allowed to see details...
if not _show_health_details(request):
data = {}
Expand Down

0 comments on commit 6e7ea94

Please sign in to comment.