|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +import json |
| 7 | + |
| 8 | +from django.conf import settings |
| 9 | +from django.test import TestCase |
| 10 | + |
| 11 | + |
| 12 | +class DockerflowEndpointsTestCase(TestCase): |
| 13 | + def setUp(self): |
| 14 | + self.old_setting = settings.DEBUG |
| 15 | + |
| 16 | + def tearDown(self): |
| 17 | + settings.DEBUG = self.old_setting |
| 18 | + |
| 19 | + def test_get_version(self): |
| 20 | + response = self.client.get("/__version__") |
| 21 | + self.assertEqual(response.status_code, 200) |
| 22 | + |
| 23 | + with open(f"{settings.BASE_DIR}/version.json", "r") as version_file: |
| 24 | + self.assertEqual(response.json(), json.loads(version_file.read())) |
| 25 | + |
| 26 | + def test_get_heartbeat_debug(self): |
| 27 | + settings.DEBUG = True |
| 28 | + |
| 29 | + response = self.client.get("/__heartbeat__") |
| 30 | + self.assertEqual(response.status_code, 200) |
| 31 | + |
| 32 | + # In DEBUG mode, we can retrieve checks details |
| 33 | + heartbeat = response.json() |
| 34 | + self.assertEqual(heartbeat["status"], "ok") |
| 35 | + self.assertTrue("checks" in heartbeat) |
| 36 | + self.assertTrue("details" in heartbeat) |
| 37 | + |
| 38 | + def test_get_heartbeat(self): |
| 39 | + settings.DEBUG = False |
| 40 | + |
| 41 | + response = self.client.get("/__heartbeat__") |
| 42 | + self.assertEqual(response.status_code, 200) |
| 43 | + |
| 44 | + # When DEBUG is False, we can't retrieve checks details and the status is certainly |
| 45 | + # equal to "warning" because of the deployment checks that are added: |
| 46 | + # https://github.com/mozilla-services/python-dockerflow/blob/e316f0c5f0aa6d176a6d08d1f568f83658b51339/src/dockerflow/django/views.py#L45 |
| 47 | + self.assertEqual(response.json(), {"status": "warning"}) |
| 48 | + |
| 49 | + def test_get_lbheartbeat(self): |
| 50 | + response = self.client.get("/__lbheartbeat__") |
| 51 | + self.assertEqual(response.status_code, 200) |
0 commit comments