Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Disable OPTIONS for generated schema endpoints

Roland Geiderbe184dff7c67
diff --git a/wger/core/tests/test_api_schema.py b/wger/core/tests/test_api_schema.py
new file mode 100644
index 000000000..ba87c0a60
--- /dev/null
+++ b/wger/core/tests/test_api_schema.py
@@ -0,0 +1,55 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+# Django
+from django.urls import reverse
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+
+
+class ApiSchemaTestCase(WgerTestCase):
+ """
+ Test the API schema and documentation endpoints
+ """
+
+ def test_schema(self):
+ """The schema endpoint generates the schema"""
+
+ response = self.client.get(reverse('schema'))
+ self.assertEqual(response.status_code, 200)
+
+ def test_swagger_ui(self):
+ """The swagger UI page renders"""
+
+ response = self.client.get(reverse('api-swagger-ui'))
+ self.assertEqual(response.status_code, 200)
+
+ def test_redoc(self):
+ """The redoc page renders"""
+
+ response = self.client.get(reverse('api-redoc'))
+ self.assertEqual(response.status_code, 200)
+
+ def test_swagger_ui_options(self):
+ """An OPTIONS request to the swagger UI does not crash the template rendering"""
+
+ response = self.client.options(reverse('api-swagger-ui'))
+ self.assertEqual(response.status_code, 405)
+
+ def test_redoc_options(self):
+ """An OPTIONS request to the redoc page does not crash the template rendering"""
+
+ response = self.client.options(reverse('api-redoc'))
+ self.assertEqual(response.status_code, 405)
diff --git a/wger/urls.py b/wger/urls.py
index 81aabd688..cb4cc4487 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -354,6 +354,10 @@ urlpatterns += [
name='powersync-data',
),
# Api documentation
+ #
+ # metadata_class=None disables the OPTIONS handler on the HTML views: its
+ # metadata response would otherwise be rendered through the UI template,
+ # which requires context only the GET handler provides, and crash with 500.
path(
'api/v2/schema',
SpectacularAPIView.as_view(),
@@ -361,12 +365,12 @@ urlpatterns += [
),
path(
'api/v2/schema/ui',
- SpectacularSwaggerView.as_view(url_name='schema'),
+ SpectacularSwaggerView.as_view(url_name='schema', metadata_class=None),
name='api-swagger-ui',
),
path(
'api/v2/schema/redoc',
- SpectacularRedocView.as_view(url_name='schema'),
+ SpectacularRedocView.as_view(url_name='schema', metadata_class=None),
name='api-redoc',
),
]