From b3a851ab6a215bd175017df70b01ceee8445e869 Mon Sep 17 00:00:00 2001 From: Christophe Siraut Date: Thu, 15 Nov 2018 12:59:07 +0100 Subject: [PATCH] health api: return dictionnary (#26835) --- hobo/rest/views.py | 17 +++++++++++++++++ tests/test_health_api.py | 31 ++++++++++--------------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/hobo/rest/views.py b/hobo/rest/views.py index 050a93f..4cf716f 100644 --- a/hobo/rest/views.py +++ b/hobo/rest/views.py @@ -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 dictionnary 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(response) diff --git a/tests/test_health_api.py b/tests/test_health_api.py index d923bf9..5ff129c 100644 --- a/tests/test_health_api.py +++ b/tests/test_health_api.py @@ -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.keys() + assert 'jazz' in content.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.get('blues') + jazz = content.get('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.get('blues') + jazz = content.get('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.get('blues') + jazz = content.get('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.get('blues') + jazz = content.get('jazz') assert blues['has_valid_certificate'] assert not jazz['has_valid_certificate'] -- 2.19.1