2019-05-13 17:07:03 +03:00
|
|
|
import pytest
|
|
|
|
from django.conf.urls import url
|
|
|
|
from django.test import RequestFactory, TestCase, override_settings
|
2019-07-25 16:30:20 +03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-05-13 17:07:03 +03:00
|
|
|
|
|
|
|
from rest_framework import filters, generics, pagination, routers, serializers
|
|
|
|
from rest_framework.compat import uritemplate
|
2019-11-06 23:44:51 +03:00
|
|
|
from rest_framework.parsers import JSONParser, MultiPartParser
|
|
|
|
from rest_framework.renderers import JSONRenderer
|
2019-05-13 17:07:03 +03:00
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.schemas.openapi import AutoSchema, SchemaGenerator
|
|
|
|
|
|
|
|
from . import views
|
|
|
|
|
|
|
|
|
|
|
|
def create_request(path):
|
|
|
|
factory = RequestFactory()
|
|
|
|
request = Request(factory.get(path))
|
|
|
|
return request
|
|
|
|
|
|
|
|
|
|
|
|
def create_view(view_cls, method, request):
|
|
|
|
generator = SchemaGenerator()
|
|
|
|
view = generator.create_view(view_cls.as_view(), method, request)
|
|
|
|
return view
|
|
|
|
|
|
|
|
|
|
|
|
class TestBasics(TestCase):
|
|
|
|
def dummy_view(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_filters(self):
|
|
|
|
classes = [filters.SearchFilter, filters.OrderingFilter]
|
|
|
|
for c in classes:
|
|
|
|
f = c()
|
|
|
|
assert f.get_schema_operation_parameters(self.dummy_view)
|
|
|
|
|
|
|
|
def test_pagination(self):
|
|
|
|
classes = [pagination.PageNumberPagination, pagination.LimitOffsetPagination, pagination.CursorPagination]
|
|
|
|
for c in classes:
|
|
|
|
f = c()
|
|
|
|
assert f.get_schema_operation_parameters(self.dummy_view)
|
|
|
|
|
|
|
|
|
2019-07-20 23:32:15 +03:00
|
|
|
class TestFieldMapping(TestCase):
|
|
|
|
def test_list_field_mapping(self):
|
|
|
|
inspector = AutoSchema()
|
|
|
|
cases = [
|
|
|
|
(serializers.ListField(), {'items': {}, 'type': 'array'}),
|
|
|
|
(serializers.ListField(child=serializers.BooleanField()), {'items': {'type': 'boolean'}, 'type': 'array'}),
|
|
|
|
(serializers.ListField(child=serializers.FloatField()), {'items': {'type': 'number'}, 'type': 'array'}),
|
|
|
|
(serializers.ListField(child=serializers.CharField()), {'items': {'type': 'string'}, 'type': 'array'}),
|
|
|
|
]
|
|
|
|
for field, mapping in cases:
|
|
|
|
with self.subTest(field=field):
|
|
|
|
assert inspector._map_field(field) == mapping
|
|
|
|
|
2019-07-25 16:30:20 +03:00
|
|
|
def test_lazy_string_field(self):
|
|
|
|
class Serializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField(help_text=_('lazy string'))
|
|
|
|
|
|
|
|
inspector = AutoSchema()
|
|
|
|
|
|
|
|
data = inspector._map_serializer(Serializer())
|
|
|
|
assert isinstance(data['properties']['text']['description'], str), "description must be str"
|
|
|
|
|
2019-07-20 23:32:15 +03:00
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
|
|
|
|
class TestOperationIntrospection(TestCase):
|
|
|
|
|
|
|
|
def test_path_without_parameters(self):
|
|
|
|
path = '/example/'
|
|
|
|
method = 'GET'
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
views.ExampleListView,
|
|
|
|
method,
|
|
|
|
create_request(path)
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
operation = inspector.get_operation(path, method)
|
|
|
|
assert operation == {
|
2019-09-03 17:05:43 +03:00
|
|
|
'operationId': 'listExamples',
|
2019-05-13 17:07:03 +03:00
|
|
|
'parameters': [],
|
2019-07-30 23:19:43 +03:00
|
|
|
'responses': {
|
|
|
|
'200': {
|
2019-08-07 23:09:27 +03:00
|
|
|
'description': '',
|
2019-07-30 23:19:43 +03:00
|
|
|
'content': {
|
|
|
|
'application/json': {
|
|
|
|
'schema': {
|
|
|
|
'type': 'array',
|
|
|
|
'items': {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-13 17:07:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_path_with_id_parameter(self):
|
|
|
|
path = '/example/{id}/'
|
|
|
|
method = 'GET'
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
views.ExampleDetailView,
|
|
|
|
method,
|
|
|
|
create_request(path)
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
parameters = inspector._get_path_parameters(path, method)
|
|
|
|
assert parameters == [{
|
|
|
|
'description': '',
|
|
|
|
'in': 'path',
|
|
|
|
'name': 'id',
|
|
|
|
'required': True,
|
|
|
|
'schema': {
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
|
|
|
|
def test_request_body(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'POST'
|
|
|
|
|
|
|
|
class Serializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
read_only = serializers.CharField(read_only=True)
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = Serializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path)
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
request_body = inspector._get_request_body(path, method)
|
|
|
|
assert request_body['content']['application/json']['schema']['required'] == ['text']
|
|
|
|
assert list(request_body['content']['application/json']['schema']['properties'].keys()) == ['text']
|
|
|
|
|
2019-08-07 22:40:21 +03:00
|
|
|
def test_empty_required(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'POST'
|
|
|
|
|
|
|
|
class Serializer(serializers.Serializer):
|
|
|
|
read_only = serializers.CharField(read_only=True)
|
|
|
|
write_only = serializers.CharField(write_only=True, required=False)
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = Serializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path)
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
request_body = inspector._get_request_body(path, method)
|
|
|
|
# there should be no empty 'required' property, see #6834
|
|
|
|
assert 'required' not in request_body['content']['application/json']['schema']
|
|
|
|
|
|
|
|
for response in inspector._get_responses(path, method).values():
|
|
|
|
assert 'required' not in response['content']['application/json']['schema']
|
|
|
|
|
2019-10-10 09:50:20 +03:00
|
|
|
def test_empty_required_with_patch_method(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'PATCH'
|
|
|
|
|
|
|
|
class Serializer(serializers.Serializer):
|
|
|
|
read_only = serializers.CharField(read_only=True)
|
|
|
|
write_only = serializers.CharField(write_only=True, required=False)
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = Serializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path)
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
request_body = inspector._get_request_body(path, method)
|
|
|
|
# there should be no empty 'required' property, see #6834
|
|
|
|
assert 'required' not in request_body['content']['application/json']['schema']
|
|
|
|
for response in inspector._get_responses(path, method).values():
|
|
|
|
assert 'required' not in response['content']['application/json']['schema']
|
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
def test_response_body_generation(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'POST'
|
|
|
|
|
|
|
|
class Serializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
write_only = serializers.CharField(write_only=True)
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = Serializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path)
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
assert responses['200']['content']['application/json']['schema']['required'] == ['text']
|
|
|
|
assert list(responses['200']['content']['application/json']['schema']['properties'].keys()) == ['text']
|
2019-08-07 23:09:27 +03:00
|
|
|
assert 'description' in responses['200']
|
2019-05-13 17:07:03 +03:00
|
|
|
|
|
|
|
def test_response_body_nested_serializer(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'POST'
|
|
|
|
|
|
|
|
class NestedSerializer(serializers.Serializer):
|
|
|
|
number = serializers.IntegerField()
|
|
|
|
|
|
|
|
class Serializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
nested = NestedSerializer()
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = Serializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
schema = responses['200']['content']['application/json']['schema']
|
|
|
|
assert sorted(schema['required']) == ['nested', 'text']
|
|
|
|
assert sorted(list(schema['properties'].keys())) == ['nested', 'text']
|
|
|
|
assert schema['properties']['nested']['type'] == 'object'
|
|
|
|
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
|
|
|
|
assert schema['properties']['nested']['required'] == ['number']
|
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
def test_list_response_body_generation(self):
|
|
|
|
"""Test that an array schema is returned for list views."""
|
|
|
|
path = '/'
|
|
|
|
method = 'GET'
|
|
|
|
|
|
|
|
class ItemSerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = ItemSerializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
assert responses == {
|
|
|
|
'200': {
|
2019-08-07 23:09:27 +03:00
|
|
|
'description': '',
|
2019-07-30 23:19:43 +03:00
|
|
|
'content': {
|
|
|
|
'application/json': {
|
|
|
|
'schema': {
|
|
|
|
'type': 'array',
|
|
|
|
'items': {
|
|
|
|
'properties': {
|
|
|
|
'text': {
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'required': ['text'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:25:44 +03:00
|
|
|
def test_paginated_list_response_body_generation(self):
|
|
|
|
"""Test that pagination properties are added for a paginated list view."""
|
|
|
|
path = '/'
|
|
|
|
method = 'GET'
|
|
|
|
|
|
|
|
class Pagination(pagination.BasePagination):
|
|
|
|
def get_paginated_response_schema(self, schema):
|
|
|
|
return {
|
|
|
|
'type': 'object',
|
|
|
|
'item': schema,
|
|
|
|
}
|
|
|
|
|
|
|
|
class ItemSerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = ItemSerializer
|
|
|
|
pagination_class = Pagination
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
assert responses == {
|
|
|
|
'200': {
|
|
|
|
'description': '',
|
|
|
|
'content': {
|
|
|
|
'application/json': {
|
|
|
|
'schema': {
|
|
|
|
'type': 'object',
|
|
|
|
'item': {
|
|
|
|
'type': 'array',
|
|
|
|
'items': {
|
|
|
|
'properties': {
|
|
|
|
'text': {
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'required': ['text'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-09 16:02:41 +03:00
|
|
|
def test_delete_response_body_generation(self):
|
|
|
|
"""Test that a view's delete method generates a proper response body schema."""
|
|
|
|
path = '/{id}/'
|
|
|
|
method = 'DELETE'
|
|
|
|
|
|
|
|
class View(generics.DestroyAPIView):
|
|
|
|
serializer_class = views.ExampleSerializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
assert responses == {
|
|
|
|
'204': {
|
|
|
|
'description': '',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-06 23:44:51 +03:00
|
|
|
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'
|
|
|
|
|
|
|
|
class ItemSerializer(serializers.Serializer):
|
|
|
|
attachment = serializers.FileField()
|
|
|
|
|
|
|
|
class View(generics.CreateAPIView):
|
|
|
|
serializer_class = ItemSerializer
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
request_body = inspector._get_request_body(path, method)
|
|
|
|
mp_media = request_body['content']['multipart/form-data']
|
|
|
|
attachment = mp_media['schema']['properties']['attachment']
|
|
|
|
assert attachment['format'] == 'binary'
|
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
def test_retrieve_response_body_generation(self):
|
2019-09-03 16:25:44 +03:00
|
|
|
"""
|
|
|
|
Test that a list of properties is returned for retrieve item views.
|
|
|
|
|
|
|
|
Pagination properties should not be added as the view represents a single item.
|
|
|
|
"""
|
2019-07-30 23:19:43 +03:00
|
|
|
path = '/{id}/'
|
|
|
|
method = 'GET'
|
|
|
|
|
2019-09-03 16:25:44 +03:00
|
|
|
class Pagination(pagination.BasePagination):
|
|
|
|
def get_paginated_response_schema(self, schema):
|
|
|
|
return {
|
|
|
|
'type': 'object',
|
|
|
|
'item': schema,
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
class ItemSerializer(serializers.Serializer):
|
|
|
|
text = serializers.CharField()
|
|
|
|
|
|
|
|
class View(generics.GenericAPIView):
|
|
|
|
serializer_class = ItemSerializer
|
2019-09-03 16:25:44 +03:00
|
|
|
pagination_class = Pagination
|
2019-07-30 23:19:43 +03:00
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
View,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
assert responses == {
|
|
|
|
'200': {
|
2019-08-07 23:09:27 +03:00
|
|
|
'description': '',
|
2019-07-30 23:19:43 +03:00
|
|
|
'content': {
|
|
|
|
'application/json': {
|
|
|
|
'schema': {
|
|
|
|
'properties': {
|
|
|
|
'text': {
|
|
|
|
'type': 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'required': ['text'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
def test_operation_id_generation(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'GET'
|
|
|
|
|
|
|
|
view = create_view(
|
|
|
|
views.ExampleGenericAPIView,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
operationId = inspector._get_operation_id(path, method)
|
2019-09-03 17:05:43 +03:00
|
|
|
assert operationId == 'listExamples'
|
2019-05-13 17:07:03 +03:00
|
|
|
|
|
|
|
def test_repeat_operation_ids(self):
|
|
|
|
router = routers.SimpleRouter()
|
|
|
|
router.register('account', views.ExampleGenericViewSet, basename="account")
|
|
|
|
urlpatterns = router.urls
|
|
|
|
|
|
|
|
generator = SchemaGenerator(patterns=urlpatterns)
|
|
|
|
|
|
|
|
request = create_request('/')
|
|
|
|
schema = generator.get_schema(request=request)
|
|
|
|
schema_str = str(schema)
|
|
|
|
print(schema_str)
|
|
|
|
assert schema_str.count("operationId") == 2
|
|
|
|
assert schema_str.count("newExample") == 1
|
|
|
|
assert schema_str.count("oldExample") == 1
|
|
|
|
|
2019-07-20 22:36:13 +03:00
|
|
|
def test_serializer_datefield(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'GET'
|
|
|
|
view = create_view(
|
|
|
|
views.ExampleGenericAPIView,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
2019-07-30 23:19:43 +03:00
|
|
|
response_schema = responses['200']['content']['application/json']['schema']
|
|
|
|
properties = response_schema['items']['properties']
|
|
|
|
assert properties['date']['type'] == properties['datetime']['type'] == 'string'
|
|
|
|
assert properties['date']['format'] == 'date'
|
|
|
|
assert properties['datetime']['format'] == 'date-time'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-10-28 04:13:01 +03:00
|
|
|
def test_serializer_hstorefield(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'GET'
|
|
|
|
view = create_view(
|
|
|
|
views.ExampleGenericAPIView,
|
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
|
|
|
response_schema = responses['200']['content']['application/json']['schema']
|
|
|
|
properties = response_schema['items']['properties']
|
|
|
|
assert properties['hstore']['type'] == 'object'
|
|
|
|
|
2019-07-20 22:36:13 +03:00
|
|
|
def test_serializer_validators(self):
|
|
|
|
path = '/'
|
|
|
|
method = 'GET'
|
|
|
|
view = create_view(
|
2019-07-20 22:54:36 +03:00
|
|
|
views.ExampleValidatedAPIView,
|
2019-07-20 22:36:13 +03:00
|
|
|
method,
|
|
|
|
create_request(path),
|
|
|
|
)
|
|
|
|
inspector = AutoSchema()
|
|
|
|
inspector.view = view
|
|
|
|
|
|
|
|
responses = inspector._get_responses(path, method)
|
2019-07-30 23:19:43 +03:00
|
|
|
response_schema = responses['200']['content']['application/json']['schema']
|
|
|
|
properties = response_schema['items']['properties']
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['integer']['type'] == 'integer'
|
|
|
|
assert properties['integer']['maximum'] == 99
|
|
|
|
assert properties['integer']['minimum'] == -11
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['string']['minLength'] == 2
|
|
|
|
assert properties['string']['maxLength'] == 10
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-09-03 16:43:54 +03:00
|
|
|
assert properties['lst']['minItems'] == 2
|
|
|
|
assert properties['lst']['maxItems'] == 10
|
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['regex']['pattern'] == r'[ABC]12{3}'
|
|
|
|
assert properties['regex']['description'] == 'must have an A, B, or C followed by 1222'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['decimal1']['type'] == 'number'
|
|
|
|
assert properties['decimal1']['multipleOf'] == .01
|
|
|
|
assert properties['decimal1']['maximum'] == 10000
|
|
|
|
assert properties['decimal1']['minimum'] == -10000
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['decimal2']['type'] == 'number'
|
|
|
|
assert properties['decimal2']['multipleOf'] == .0001
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['email']['type'] == 'string'
|
|
|
|
assert properties['email']['format'] == 'email'
|
|
|
|
assert properties['email']['default'] == 'foo@bar.com'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['url']['type'] == 'string'
|
|
|
|
assert properties['url']['nullable'] is True
|
|
|
|
assert properties['url']['default'] == 'http://www.example.com'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['uuid']['type'] == 'string'
|
|
|
|
assert properties['uuid']['format'] == 'uuid'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['ip4']['type'] == 'string'
|
|
|
|
assert properties['ip4']['format'] == 'ipv4'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['ip6']['type'] == 'string'
|
|
|
|
assert properties['ip6']['format'] == 'ipv6'
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-07-30 23:19:43 +03:00
|
|
|
assert properties['ip']['type'] == 'string'
|
|
|
|
assert 'format' not in properties['ip']
|
2019-07-20 22:36:13 +03:00
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
|
|
|
|
@pytest.mark.skipif(uritemplate is None, reason='uritemplate not installed.')
|
|
|
|
@override_settings(REST_FRAMEWORK={'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.openapi.AutoSchema'})
|
|
|
|
class TestGenerator(TestCase):
|
|
|
|
|
|
|
|
def test_override_settings(self):
|
|
|
|
assert isinstance(views.ExampleListView.schema, AutoSchema)
|
|
|
|
|
|
|
|
def test_paths_construction(self):
|
|
|
|
"""Construction of the `paths` key."""
|
|
|
|
patterns = [
|
|
|
|
url(r'^example/?$', views.ExampleListView.as_view()),
|
|
|
|
]
|
|
|
|
generator = SchemaGenerator(patterns=patterns)
|
|
|
|
generator._initialise_endpoints()
|
|
|
|
|
|
|
|
paths = generator.get_paths()
|
|
|
|
|
|
|
|
assert '/example/' in paths
|
|
|
|
example_operations = paths['/example/']
|
|
|
|
assert len(example_operations) == 2
|
|
|
|
assert 'get' in example_operations
|
|
|
|
assert 'post' in example_operations
|
|
|
|
|
2019-06-09 15:29:55 +03:00
|
|
|
def test_prefixed_paths_construction(self):
|
2019-07-20 21:58:20 +03:00
|
|
|
"""Construction of the `paths` key maintains a common prefix."""
|
2019-06-09 15:29:55 +03:00
|
|
|
patterns = [
|
2019-07-20 21:58:20 +03:00
|
|
|
url(r'^v1/example/?$', views.ExampleListView.as_view()),
|
|
|
|
url(r'^v1/example/{pk}/?$', views.ExampleDetailView.as_view()),
|
2019-06-09 15:29:55 +03:00
|
|
|
]
|
|
|
|
generator = SchemaGenerator(patterns=patterns)
|
|
|
|
generator._initialise_endpoints()
|
|
|
|
|
|
|
|
paths = generator.get_paths()
|
|
|
|
|
2019-07-20 21:58:20 +03:00
|
|
|
assert '/v1/example/' in paths
|
|
|
|
assert '/v1/example/{id}/' in paths
|
2019-06-09 15:29:55 +03:00
|
|
|
|
2019-07-20 21:59:03 +03:00
|
|
|
def test_mount_url_prefixed_to_paths(self):
|
|
|
|
patterns = [
|
|
|
|
url(r'^example/?$', views.ExampleListView.as_view()),
|
|
|
|
url(r'^example/{pk}/?$', views.ExampleDetailView.as_view()),
|
|
|
|
]
|
2019-07-20 22:01:00 +03:00
|
|
|
generator = SchemaGenerator(patterns=patterns, url='/api')
|
2019-07-20 21:59:03 +03:00
|
|
|
generator._initialise_endpoints()
|
|
|
|
|
|
|
|
paths = generator.get_paths()
|
|
|
|
|
|
|
|
assert '/api/example/' in paths
|
|
|
|
assert '/api/example/{id}/' in paths
|
|
|
|
|
2019-05-13 17:07:03 +03:00
|
|
|
def test_schema_construction(self):
|
|
|
|
"""Construction of the top level dictionary."""
|
|
|
|
patterns = [
|
|
|
|
url(r'^example/?$', views.ExampleListView.as_view()),
|
|
|
|
]
|
|
|
|
generator = SchemaGenerator(patterns=patterns)
|
|
|
|
|
|
|
|
request = create_request('/')
|
|
|
|
schema = generator.get_schema(request=request)
|
|
|
|
|
|
|
|
assert 'openapi' in schema
|
|
|
|
assert 'paths' in schema
|
2019-09-03 17:07:30 +03:00
|
|
|
|
|
|
|
def test_schema_information(self):
|
|
|
|
"""Construction of the top level dictionary."""
|
|
|
|
patterns = [
|
|
|
|
url(r'^example/?$', views.ExampleListView.as_view()),
|
|
|
|
]
|
|
|
|
generator = SchemaGenerator(patterns=patterns, title='My title', version='1.2.3', description='My description')
|
|
|
|
|
|
|
|
request = create_request('/')
|
|
|
|
schema = generator.get_schema(request=request)
|
|
|
|
|
|
|
|
assert schema['info']['title'] == 'My title'
|
|
|
|
assert schema['info']['version'] == '1.2.3'
|
|
|
|
assert schema['info']['description'] == 'My description'
|