From 140f7f104c49cdfe2962dde7210bd8231101b1b3 Mon Sep 17 00:00:00 2001 From: Dima Knivets Date: Sun, 15 Sep 2019 15:19:00 +0300 Subject: [PATCH] added tests for #6863 implementation --- tests/schemas/test_openapi.py | 57 ++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 6c9089e05..9197b4478 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -5,6 +5,8 @@ from django.utils.translation import gettext_lazy as _ from rest_framework import filters, generics, pagination, routers, serializers from rest_framework.compat import uritemplate +from rest_framework.parsers import JSONParser, MultiPartParser +from rest_framework.renderers import JSONRenderer from rest_framework.request import Request from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator @@ -339,8 +341,55 @@ class TestOperationIntrospection(TestCase): }, } - def test_multipart_request_body_generation(self): - """Test that a view's delete method generates a proper response body schema.""" + def test_parser_mapping(self): + """Test that view's parsers are mapped to OA media types""" + path = '/{id}/' + method = 'POST' + + class View(generics.CreateAPIView): + serializer_class = views.ExampleSerializer + parser_classes = [JSONParser, MultiPartParser] + + view = create_view( + View, + method, + create_request(path), + ) + inspector = AutoSchema() + inspector.view = view + + request_body = inspector._get_request_body(path, method) + + assert len(request_body['content'].keys()) == 2 + assert 'multipart/form-data' in request_body['content'] + assert 'application/json' in request_body['content'] + + def test_renderer_mapping(self): + """Test that view's renderers are mapped to OA media types""" + path = '/{id}/' + method = 'GET' + + class View(generics.CreateAPIView): + serializer_class = views.ExampleSerializer + renderer_classes = [JSONRenderer] + + view = create_view( + View, + method, + create_request(path), + ) + inspector = AutoSchema() + inspector.view = view + + responses = inspector._get_responses(path, method) + # TODO this should be changed once the multiple response + # schema support is there + success_response = responses['200'] + + assert len(success_response['content'].keys()) == 1 + assert 'application/json' in success_response['content'] + + def test_serializer_filefield(self): path = '/{id}/' method = 'POST' @@ -359,8 +408,8 @@ class TestOperationIntrospection(TestCase): inspector.view = view request_body = inspector._get_request_body(path, method) - assert 'multipart/form-data' in request_body['content'] - attachment = request_body['content']['multipart/form-data']['schema']['properties']['attachment'] + mp_media = request_body['content']['multipart/form-data'] + attachment = mp_media['schema']['properties']['attachment'] assert attachment['format'] == 'binary' def test_retrieve_response_body_generation(self):