-
Notifications
You must be signed in to change notification settings - Fork 0
Open API (Swagger, Redoc) 을 지원 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
75f7a8d
chore: add `drf_yasg` to requirements.txt (Yet another Swagger genera…
hepheir 947f36f
feat(doc.views): add permission class `IsDebug` to restrict OpenAPI s…
hepheir 3f2735b
test(doc.urls): add tests for document endpoint accessibility in debu…
hepheir 5b986a3
feat(doc.views): `SchemaView` 의 접근 권한을 `IsDebug` 로 제한
hepheir 7c919fb
chore(doc.urls): register URL configuration for Swagger and Redoc views
hepheir 9a16a9b
chore(app.urls): register URL configurations of `doc` app
hepheir ec5122b
chore(app.settings): add `drf-yasg` configuration for Swagger, Redoc
hepheir d320345
chore(doc): initialize and install app `doc`
hepheir daa182d
feat(doc.views): implement OpenAPI schema views with Swagger and Redo…
hepheir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class DocConfig(AppConfig): | ||
| default_auto_field = 'django.db.models.BigAutoField' | ||
| name = 'doc' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from django.test import TestCase, override_settings | ||
| from rest_framework import status | ||
|
|
||
|
|
||
| class DocEndpointsDebugModeTestCase(TestCase): | ||
| """DEBUG=True일 때 문서 엔드포인트 접근 가능 여부 테스트""" | ||
|
|
||
| @override_settings(DEBUG=True) | ||
| def test_swagger_ui_accessible_in_debug_mode(self): | ||
| """DEBUG 모드에서 Swagger UI 접근 가능""" | ||
| response = self.client.get('/doc/swagger/') | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
|
||
| @override_settings(DEBUG=True) | ||
| def test_redoc_ui_accessible_in_debug_mode(self): | ||
| """DEBUG 모드에서 ReDoc UI 접근 가능""" | ||
| response = self.client.get('/doc/redoc/') | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
|
||
| @override_settings(DEBUG=True) | ||
| def test_schema_json_accessible_in_debug_mode(self): | ||
| """DEBUG 모드에서 스키마 JSON 접근 가능""" | ||
| response = self.client.get('/doc/swagger.json/') | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
|
||
| @override_settings(DEBUG=True) | ||
| def test_schema_yaml_accessible_in_debug_mode(self): | ||
| """DEBUG 모드에서 스키마 YAML 접근 가능""" | ||
| response = self.client.get('/doc/swagger.yaml/') | ||
| self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
hepheir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class DocEndpointsProductionModeTestCase(TestCase): | ||
| """DEBUG=False일 때 문서 엔드포인트 접근 제한 테스트""" | ||
|
|
||
| @override_settings(DEBUG=False, ALLOWED_HOSTS=['testserver']) | ||
| def test_swagger_ui_not_accessible_in_production(self): | ||
| """프로덕션 모드에서 Swagger UI 접근 불가""" | ||
| response = self.client.get('/doc/swagger/') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
|
||
| @override_settings(DEBUG=False, ALLOWED_HOSTS=['testserver']) | ||
| def test_redoc_ui_not_accessible_in_production(self): | ||
| """프로덕션 모드에서 ReDoc UI 접근 불가""" | ||
| response = self.client.get('/doc/redoc/') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
|
||
| @override_settings(DEBUG=False, ALLOWED_HOSTS=['testserver']) | ||
| def test_schema_json_not_accessible_in_production(self): | ||
| """프로덕션 모드에서 스키마 JSON 접근 불가""" | ||
| response = self.client.get('/doc/swagger.json/') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
|
|
||
| @override_settings(DEBUG=False, ALLOWED_HOSTS=['testserver']) | ||
| def test_schema_yaml_not_accessible_in_production(self): | ||
| """프로덕션 모드에서 스키마 YAML 접근 불가""" | ||
| response = self.client.get('/doc/swagger.yaml/') | ||
| self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """ | ||
| URL configuration for doc app. | ||
|
|
||
| The `urlpatterns` list routes URLs to views. For more information please see: | ||
| https://docs.djangoproject.com/en/5.2/topics/http/urls/ | ||
| Examples: | ||
| Function views | ||
| 1. Add an import: from my_app import views | ||
| 2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
| Class-based views | ||
| 1. Add an import: from other_app.views import Home | ||
| 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
| Including another URLconf | ||
| 1. Import the include() function: from django.urls import include, path | ||
| 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
| """ | ||
| from django.urls import path | ||
|
|
||
| from . import views | ||
|
|
||
|
|
||
| urlpatterns = [ | ||
| path('swagger<str:format>/', views.schema_format_view), | ||
| path('swagger/', views.swagger_view), | ||
| path('redoc/', views.redoc_view), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from django.conf import settings | ||
| from drf_yasg.views import get_schema_view | ||
| from rest_framework.permissions import BasePermission | ||
|
|
||
|
|
||
| class IsDebug(BasePermission): | ||
| """ | ||
| Permission class that allows access only when Django's DEBUG mode is enabled. | ||
| Useful for restricting certain endpoints to development environments. | ||
| """ | ||
| def has_permission(self, request, view): | ||
| return settings.DEBUG | ||
|
|
||
|
|
||
| SchemaView = get_schema_view( | ||
| info=settings.OPEN_API_INFO, | ||
hepheir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| permission_classes=[IsDebug], | ||
| public=True, | ||
hepheir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
|
|
||
| redoc_view = SchemaView.with_ui('redoc') | ||
| swagger_view = SchemaView.with_ui('swagger') | ||
| schema_format_view = SchemaView.without_ui() | ||
hepheir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| django | ||
| djangorestframework | ||
| drf-yasg | ||
| pytest-django | ||
| python-dotenv |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.