Use swagger only in debug mode

This commit is contained in:
Igor 2025-03-24 10:46:08 +01:00
parent ad4fbefce7
commit cdee044612
4 changed files with 18 additions and 9 deletions

View File

@ -43,12 +43,6 @@ urlpatterns += [
path("api/", include("config.api_router")),
# DRF auth token
path("api/auth-token/", obtain_auth_token),
path("api/schema/", SpectacularAPIView.as_view(), name="api-schema"),
path(
"api/docs/",
SpectacularSwaggerView.as_view(url_name="api-schema"),
name="api-docs",
),
]
{%- endif %}
@ -56,6 +50,8 @@ if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
path("api/schema/", SpectacularAPIView.as_view(), name="api-schema"),
path("api/docs/", SpectacularSwaggerView.as_view(url_name="api-schema"), name="api-docs"),
path(
"400/",
default_views.bad_request,

View File

@ -24,7 +24,7 @@ services:
- ./.envs/.local/.django
- ./.envs/.local/.postgres
- path: ./.env # Use this .env file to override the values .envs/.local/.django
- required: false
required: false
ports:
- '8000:8000'
command: /start

View File

@ -1,23 +1,36 @@
from http import HTTPStatus
import pytest
from django.conf import settings
from django.urls import NoReverseMatch
from django.urls import reverse
def test_api_docs_accessible_by_admin(admin_client):
@pytest.mark.skipif(not settings.DEBUG, reason="Swagger is implemented only in debug mode")
def test_swagger_accessible_by_admin(admin_client):
url = reverse("api-docs")
response = admin_client.get(url)
assert response.status_code == HTTPStatus.OK
@pytest.mark.skipif(not settings.DEBUG, reason="Swagger is implemented only in debug mode")
@pytest.mark.django_db
def test_api_docs_not_accessible_by_anonymous_users(client):
def test_swagger_ui_not_accessible(client):
url = reverse("api-docs")
response = client.get(url)
assert response.status_code == HTTPStatus.FORBIDDEN
@pytest.mark.skipif(not settings.DEBUG, reason="Swagger is implemented only in debug mode")
def test_api_schema_generated_successfully(admin_client):
url = reverse("api-schema")
response = admin_client.get(url)
assert response.status_code == HTTPStatus.OK
@pytest.mark.skipif(settings.DEBUG, reason="Swagger is implemented only in debug mode")
def test_swagger_not_accessible():
assert not settings.DEBUG, "Swagger is implemented only in debug mode"
with pytest.raises(NoReverseMatch):
reverse("api-docs")