From 1a9a3d32502b8f9f5eef9994c7b32c496a115927 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Wed, 2 Jun 2021 14:55:33 +0300 Subject: [PATCH] Add tests for the `service_health` view function --- .../apps/main/tests/test_service_health.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 onadata/apps/main/tests/test_service_health.py diff --git a/onadata/apps/main/tests/test_service_health.py b/onadata/apps/main/tests/test_service_health.py new file mode 100644 index 0000000000..ecf75bea89 --- /dev/null +++ b/onadata/apps/main/tests/test_service_health.py @@ -0,0 +1,42 @@ +import json +from django.http import HttpRequest +from django.core.cache.backends.base import InvalidCacheBackendError +from mock import patch + +from onadata.apps.main.tests.test_base import TestBase +from onadata.apps.main.views import service_health + + +class TestServiceHealthView(TestBase): + def test_service_health(self): + """ + Test that the `service_health` view function + works as expected: + 1. Returns a 200 when secondary services are healthy + 2. Returns a 500 when a secondary service is not available + """ + req = HttpRequest() + resp = service_health(req) + + self.assertEqual(resp.status_code, 200) + self.assertEqual( + json.loads(resp.content.decode('utf-8')), + { + 'default-Database': 'OK', + 'Cache-Service': 'OK' + }) + + with patch('onadata.apps.main.views.cache') as cache_mock: + cache_mock.set.side_effect = InvalidCacheBackendError( + 'Invalid cache configuration') + resp = service_health(req) + + self.assertEqual(resp.status_code, 500) + self.assertEqual( + json.loads(resp.content.decode('utf-8')), + { + 'default-Database': 'OK', + 'Cache-Service': ( + 'Degraded state; ' + 'Invalid cache configuration') + })