api: return health data as a dictionary with service slugs as keys (#26835)

This commit is contained in:
Christophe Siraut 2018-11-15 12:59:07 +01:00 committed by Frédéric Péters
parent 9dc099c6a3
commit 2f6af6bd39
2 changed files with 27 additions and 21 deletions

View File

@ -16,6 +16,7 @@
from rest_framework import generics
from rest_framework import permissions
from rest_framework.response import Response
from hobo.environment.utils import get_installed_services
from hobo.rest.serializers import HealthBaseSerializer
@ -27,3 +28,19 @@ class HealthList(generics.ListAPIView):
def get_queryset(self):
return [x for x in get_installed_services() if not x.secondary]
def list(self, request, *args, **kwargs):
"""
Custom dictionary object
"""
self.object_list = self.filter_queryset(self.get_queryset())
# Switch between paginated or standard style responses
page = self.paginate_queryset(self.object_list)
if page is not None:
serializer = self.get_pagination_serializer(page)
else:
serializer = self.get_serializer(self.object_list, many=True)
response = {d['slug']: d for d in serializer.data}
return Response({'data': response})

View File

@ -11,16 +11,6 @@ from hobo.environment.models import Authentic, Combo
pytestmark = pytest.mark.django_db
def get_entry(ls, title):
"""
Search a list of dictionnaries
"""
entries = [i for i in ls if i['title'] == title]
if len(entries) > 1:
raise(Exception('There is more than 1 %s' % title))
return entries[0]
@pytest.fixture
def services(request):
now = timezone.now()
@ -38,9 +28,8 @@ def test_response(app, services, monkeypatch):
response = app.get('/api/health/')
assert response.status_code == 200
content = json.loads(response.content)
assert len(content) == 2
assert content[0]['title'] == 'blues'
assert content[1]['title'] == 'jazz'
assert 'blues' in content['data'].keys()
assert 'jazz' in content['data'].keys()
def test_is_resolvable(app, services, monkeypatch):
@ -53,8 +42,8 @@ def test_is_resolvable(app, services, monkeypatch):
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200))
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
blues = content['data']['blues']
jazz = content['data']['jazz']
assert not blues['is_resolvable']
assert jazz['is_resolvable']
@ -69,8 +58,8 @@ def test_is_running(app, services, monkeypatch):
monkeypatch.setattr(requests, 'get', get)
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
blues = content['data']['blues']
jazz = content['data']['jazz']
assert not blues['is_running']
assert jazz['is_running']
@ -80,8 +69,8 @@ def test_is_operational(app, services, monkeypatch):
monkeypatch.setattr(requests, 'get', lambda x, verify: MagicMock(status_code=200))
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
blues = content['data']['blues']
jazz = content['data']['jazz']
assert blues['is_operational']
assert not jazz['is_operational']
@ -96,7 +85,7 @@ def test_has_valid_certificate(app, services, monkeypatch):
monkeypatch.setattr(requests, 'get', get)
response = app.get('/api/health/')
content = json.loads(response.content)
blues = get_entry(content, 'blues')
jazz = get_entry(content, 'jazz')
blues = content['data']['blues']
jazz = content['data']['jazz']
assert blues['has_valid_certificate']
assert not jazz['has_valid_certificate']