From e21c83fab6fb7903f0dba2cebda3881783c6eee1 Mon Sep 17 00:00:00 2001 From: Sritan Motati Date: Wed, 5 Feb 2025 16:47:08 -0500 Subject: [PATCH 1/6] health route for mobile backend --- backend/health/__init__.py | 1 + backend/health/apps.py | 5 +++++ backend/health/urls.py | 8 ++++++++ backend/health/views.py | 23 +++++++++++++++++++++++ backend/pennmobile/urls.py | 1 + backend/tests/health/__init__.py | 0 backend/tests/health/test_route.py | 10 ++++++++++ 7 files changed, 48 insertions(+) create mode 100644 backend/health/__init__.py create mode 100644 backend/health/apps.py create mode 100644 backend/health/urls.py create mode 100644 backend/health/views.py create mode 100644 backend/tests/health/__init__.py create mode 100644 backend/tests/health/test_route.py diff --git a/backend/health/__init__.py b/backend/health/__init__.py new file mode 100644 index 00000000..0519ecba --- /dev/null +++ b/backend/health/__init__.py @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/backend/health/apps.py b/backend/health/apps.py new file mode 100644 index 00000000..e860540e --- /dev/null +++ b/backend/health/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class HealthConfig(AppConfig): + name = "health" diff --git a/backend/health/urls.py b/backend/health/urls.py new file mode 100644 index 00000000..d11c0285 --- /dev/null +++ b/backend/health/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from health.views import HealthView + +app_name = "health" + +urlpatterns = [ + path("health/", HealthView.as_view(), name="health"), +] \ No newline at end of file diff --git a/backend/health/views.py b/backend/health/views.py new file mode 100644 index 00000000..6f788bac --- /dev/null +++ b/backend/health/views.py @@ -0,0 +1,23 @@ +from http import HTTPStatus +from django.http import JsonResponse +from django.views.generic import View + +class HealthView(View): + def get(self, request): + """ + Health check endpoint to confirm the backend is running. + --- + summary: Health Check + responses: + "200": + content: + application/json: + schema: + type: object + properties: + message: + type: string + enum: ["OK"] + --- + """ + return JsonResponse({"message": "OK"}, status=HTTPStatus.OK) \ No newline at end of file diff --git a/backend/pennmobile/urls.py b/backend/pennmobile/urls.py index f517a8af..a67e6ddb 100644 --- a/backend/pennmobile/urls.py +++ b/backend/pennmobile/urls.py @@ -28,6 +28,7 @@ path("penndata/", include("penndata.urls")), path("sublet/", include("sublet.urls")), path("wrapped/", include("wrapped.urls")), + path("", include("health.urls")), ] urlpatterns = [ diff --git a/backend/tests/health/__init__.py b/backend/tests/health/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/backend/tests/health/test_route.py b/backend/tests/health/test_route.py new file mode 100644 index 00000000..3eb8b80a --- /dev/null +++ b/backend/tests/health/test_route.py @@ -0,0 +1,10 @@ +from django.test import TestCase +from django.urls import reverse +from http import HTTPStatus + +class HealthTestCase(TestCase): + def test_health(self): + url = reverse("health") + resp = self.client.get(url) + self.assertEqual(resp.status_code, 200) + self.assertEqual(resp.data, {"message": "OK"}) From d4cb1b1455b1f9387c02756ed42eb62196a40025 Mon Sep 17 00:00:00 2001 From: Sritan Motati Date: Fri, 28 Feb 2025 17:07:39 -0500 Subject: [PATCH 2/6] backend lint fix --- backend/health/__init__.py | 1 - backend/health/urls.py | 4 +++- backend/health/views.py | 4 +++- backend/tests/health/test_route.py | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/backend/health/__init__.py b/backend/health/__init__.py index 0519ecba..e69de29b 100644 --- a/backend/health/__init__.py +++ b/backend/health/__init__.py @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/backend/health/urls.py b/backend/health/urls.py index d11c0285..68352e21 100644 --- a/backend/health/urls.py +++ b/backend/health/urls.py @@ -1,8 +1,10 @@ from django.urls import path + from health.views import HealthView + app_name = "health" urlpatterns = [ path("health/", HealthView.as_view(), name="health"), -] \ No newline at end of file +] diff --git a/backend/health/views.py b/backend/health/views.py index 6f788bac..ba5bb8f2 100644 --- a/backend/health/views.py +++ b/backend/health/views.py @@ -1,7 +1,9 @@ from http import HTTPStatus + from django.http import JsonResponse from django.views.generic import View + class HealthView(View): def get(self, request): """ @@ -20,4 +22,4 @@ def get(self, request): enum: ["OK"] --- """ - return JsonResponse({"message": "OK"}, status=HTTPStatus.OK) \ No newline at end of file + return JsonResponse({"message": "OK"}, status=HTTPStatus.OK) diff --git a/backend/tests/health/test_route.py b/backend/tests/health/test_route.py index 3eb8b80a..9445d8e3 100644 --- a/backend/tests/health/test_route.py +++ b/backend/tests/health/test_route.py @@ -1,6 +1,6 @@ from django.test import TestCase from django.urls import reverse -from http import HTTPStatus + class HealthTestCase(TestCase): def test_health(self): From c79c12ab01a485e1d8a34535e595abe5d41094ab Mon Sep 17 00:00:00 2001 From: Sritan Motati Date: Fri, 28 Feb 2025 17:38:43 -0500 Subject: [PATCH 3/6] health route fix --- backend/pennmobile/settings/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/pennmobile/settings/base.py b/backend/pennmobile/settings/base.py index e2bc3154..34906c16 100644 --- a/backend/pennmobile/settings/base.py +++ b/backend/pennmobile/settings/base.py @@ -54,6 +54,7 @@ "options.apps.OptionsConfig", "sublet", "phonenumber_field", + "health", ] MIDDLEWARE = [ From 86a6449785a1670eb072785a0dd08b30a213b209 Mon Sep 17 00:00:00 2001 From: Sritan Motati Date: Fri, 28 Feb 2025 17:49:51 -0500 Subject: [PATCH 4/6] health test fix? --- backend/tests/health/test_route.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/tests/health/test_route.py b/backend/tests/health/test_route.py index 9445d8e3..afa04e53 100644 --- a/backend/tests/health/test_route.py +++ b/backend/tests/health/test_route.py @@ -1,10 +1,10 @@ from django.test import TestCase from django.urls import reverse +from rest_framework.test import APIClient class HealthTestCase(TestCase): def test_health(self): - url = reverse("health") - resp = self.client.get(url) + resp = self.client.get("/health/") self.assertEqual(resp.status_code, 200) self.assertEqual(resp.data, {"message": "OK"}) From cd8de3774b9f06de1a7bcb926abd76d90b706803 Mon Sep 17 00:00:00 2001 From: Sritan Motati Date: Fri, 28 Feb 2025 17:53:35 -0500 Subject: [PATCH 5/6] remove unused imports --- backend/tests/health/test_route.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/tests/health/test_route.py b/backend/tests/health/test_route.py index afa04e53..c08a9158 100644 --- a/backend/tests/health/test_route.py +++ b/backend/tests/health/test_route.py @@ -1,6 +1,4 @@ from django.test import TestCase -from django.urls import reverse -from rest_framework.test import APIClient class HealthTestCase(TestCase): From 790e6bf3196d0a0e4bb43c29423ce91b30d91bf2 Mon Sep 17 00:00:00 2001 From: Sritan Motati Date: Fri, 28 Feb 2025 18:00:33 -0500 Subject: [PATCH 6/6] fixed test case for json --- backend/tests/health/test_route.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/tests/health/test_route.py b/backend/tests/health/test_route.py index c08a9158..445bb86c 100644 --- a/backend/tests/health/test_route.py +++ b/backend/tests/health/test_route.py @@ -5,4 +5,4 @@ class HealthTestCase(TestCase): def test_health(self): resp = self.client.get("/health/") self.assertEqual(resp.status_code, 200) - self.assertEqual(resp.data, {"message": "OK"}) + self.assertEqual(resp.json(), {"message": "OK"})