From 0a37a8ea6d2f60cf6151673138b69bb1dc953e0d Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Fri, 15 Nov 2024 09:29:07 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(backend)=20add=20public=20endpoint=20?= =?UTF-8?q?/api/v1.0/config/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add public endpoint /api/v1.0/config/ to share some public configuration values. --- CHANGELOG.md | 1 + src/backend/core/api/viewsets.py | 26 +++++++++++++ src/backend/core/tests/test_api_config.py | 47 +++++++++++++++++++++++ src/backend/core/urls.py | 1 + src/backend/impress/settings.py | 4 +- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/backend/core/tests/test_api_config.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d2e2a22..8f7776f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to - 🌐(frontend) Add German translation #255 - ✨(frontend) Add a broadcast store #387 +- ✨(backend) config endpoint #425 ## Changed diff --git a/src/backend/core/api/viewsets.py b/src/backend/core/api/viewsets.py index 25d56c62..d6bee63e 100644 --- a/src/backend/core/api/viewsets.py +++ b/src/backend/core/api/viewsets.py @@ -26,11 +26,13 @@ from rest_framework import ( mixins, pagination, status, + views, viewsets, ) from rest_framework import ( response as drf_response, ) +from rest_framework.permissions import AllowAny from core import enums, models from core.services.ai_services import AIService @@ -886,3 +888,27 @@ class InvitationViewset( invitation.document.email_invitation( language, invitation.email, invitation.role, self.request.user ) + + +class ConfigView(views.APIView): + """API ViewSet for sharing some public settings.""" + + permission_classes = [AllowAny] + + def get(self, request): + """ + GET /api/v1.0/config/ + Return a dictionary of public settings. + """ + array_settings = [ + "ENVIRONMENT", + "LANGUAGES", + "LANGUAGE_CODE", + "SENTRY_DSN", + ] + dict_settings = {} + for setting in array_settings: + if hasattr(settings, setting): + dict_settings[setting] = getattr(settings, setting) + + return drf_response.Response(dict_settings) diff --git a/src/backend/core/tests/test_api_config.py b/src/backend/core/tests/test_api_config.py new file mode 100644 index 00000000..ac5d32db --- /dev/null +++ b/src/backend/core/tests/test_api_config.py @@ -0,0 +1,47 @@ +""" +Test config API endpoints in the Impress core app. +""" + +from django.test import override_settings + +import pytest +from rest_framework.status import ( + HTTP_200_OK, +) +from rest_framework.test import APIClient + +from core import factories + +pytestmark = pytest.mark.django_db + + +@override_settings(SENTRY_DSN="https://sentry.test/123") +def test_api_config_anonymous(): + """Anonymous users should be allowed to get the configuration.""" + client = APIClient() + response = client.get("/api/v1.0/config/") + assert response.status_code == HTTP_200_OK + assert response.json() == { + "LANGUAGES": [["en-us", "English"], ["fr-fr", "French"], ["de-de", "German"]], + "LANGUAGE_CODE": "en-us", + "SENTRY_DSN": "https://sentry.test/123", + "ENVIRONMENT": "test", + } + + +@override_settings(SENTRY_DSN="https://sentry.test/123") +def test_api_config_authenticated(): + """Authenticated users should be allowed to get the configuration.""" + user = factories.UserFactory() + + client = APIClient() + client.force_login(user) + + response = client.get("/api/v1.0/config/") + assert response.status_code == HTTP_200_OK + assert response.json() == { + "LANGUAGES": [["en-us", "English"], ["fr-fr", "French"], ["de-de", "German"]], + "LANGUAGE_CODE": "en-us", + "SENTRY_DSN": "https://sentry.test/123", + "ENVIRONMENT": "test", + } diff --git a/src/backend/core/urls.py b/src/backend/core/urls.py index d16e3ee0..ce89f483 100644 --- a/src/backend/core/urls.py +++ b/src/backend/core/urls.py @@ -55,4 +55,5 @@ urlpatterns = [ ] ), ), + path(f"api/{settings.API_VERSION}/config/", viewsets.ConfigView.as_view()), ] diff --git a/src/backend/impress/settings.py b/src/backend/impress/settings.py index 93c70ac9..e1994644 100755 --- a/src/backend/impress/settings.py +++ b/src/backend/impress/settings.py @@ -56,7 +56,7 @@ class Base(Configuration): You may also want to override default configuration by setting the following environment variables: - * DJANGO_SENTRY_DSN + * SENTRY_DSN * DB_NAME * DB_HOST * DB_PASSWORD @@ -372,7 +372,7 @@ class Base(Configuration): CORS_ALLOWED_ORIGIN_REGEXES = values.ListValue([]) # Sentry - SENTRY_DSN = values.Value(None, environ_name="SENTRY_DSN") + SENTRY_DSN = values.Value(None, environ_name="SENTRY_DSN", environ_prefix=None) # Easy thumbnails THUMBNAIL_EXTENSION = "webp"