| 1 |
import pytest |
| 2 |
from django.urls import path |
| 3 |
from django.utils.decorators import method_decorator |
| 4 |
from rest_framework.authtoken.views import ObtainAuthToken |
| 5 |
from rest_framework.settings import api_settings |
| 6 |
|
| 7 |
from drf_yasg import openapi |
| 8 |
from drf_yasg.errors import SwaggerGenerationError |
| 9 |
from drf_yasg.generators import OpenAPISchemaGenerator |
| 10 |
from drf_yasg.utils import swagger_auto_schema |
| 11 |
|
| 12 |
|
| 13 |
def test_no_form_parameters_with_non_form_parsers(): |
| 14 |
# see https://github.com/axnsan12/drf-yasg/issues/270 |
| 15 |
# test that manual form parameters for views that haven't set |
| 16 |
# all their parsers classes to form parsers are not allowed |
| 17 |
# even when the request body is empty |
| 18 |
|
| 19 |
@method_decorator( |
| 20 |
name="post", |
| 21 |
decorator=swagger_auto_schema( |
| 22 |
operation_description="Logins a user and returns a token", |
| 23 |
manual_parameters=[ |
| 24 |
openapi.Parameter( |
| 25 |
"username", |
| 26 |
openapi.IN_FORM, |
| 27 |
required=True, |
| 28 |
type=openapi.TYPE_STRING, |
| 29 |
description="Valid username or email for authentication", |
| 30 |
), |
| 31 |
], |
| 32 |
), |
| 33 |
) |
| 34 |
class CustomObtainAuthToken(ObtainAuthToken): |
| 35 |
throttle_classes = api_settings.DEFAULT_THROTTLE_CLASSES |
| 36 |
|
| 37 |
urlpatterns = [ |
| 38 |
path("token/", CustomObtainAuthToken.as_view()), |
| 39 |
] |
| 40 |
|
| 41 |
generator = OpenAPISchemaGenerator( |
| 42 |
info=openapi.Info(title="Test generator", default_version="v1"), |
| 43 |
patterns=urlpatterns, |
| 44 |
) |
| 45 |
|
| 46 |
with pytest.raises(SwaggerGenerationError): |
| 47 |
generator.get_schema(None, True) |