2016-07-04 18:38:17 +03:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from django.conf.urls import include, url
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
|
|
|
|
from rest_framework import filters, pagination, permissions, serializers
|
|
|
|
from rest_framework.compat import coreapi
|
2016-08-11 16:07:40 +03:00
|
|
|
from rest_framework.decorators import detail_route, list_route
|
2016-07-27 17:36:36 +03:00
|
|
|
from rest_framework.response import Response
|
2016-07-04 18:38:17 +03:00
|
|
|
from rest_framework.routers import DefaultRouter
|
2016-07-27 17:36:36 +03:00
|
|
|
from rest_framework.schemas import SchemaGenerator
|
2016-07-04 18:38:17 +03:00
|
|
|
from rest_framework.test import APIClient
|
2016-07-27 17:36:36 +03:00
|
|
|
from rest_framework.views import APIView
|
2016-07-04 18:38:17 +03:00
|
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
|
|
|
|
|
|
|
|
class MockUser(object):
|
|
|
|
def is_authenticated(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class ExamplePagination(pagination.PageNumberPagination):
|
|
|
|
page_size = 100
|
|
|
|
|
|
|
|
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
2016-08-11 18:18:33 +03:00
|
|
|
a = serializers.CharField(required=True, help_text='A field description')
|
2016-07-04 18:38:17 +03:00
|
|
|
b = serializers.CharField(required=False)
|
2016-08-22 18:53:53 +03:00
|
|
|
read_only = serializers.CharField(read_only=True)
|
|
|
|
hidden = serializers.HiddenField(default='hello')
|
2016-07-04 18:38:17 +03:00
|
|
|
|
|
|
|
|
2016-08-05 13:19:39 +03:00
|
|
|
class AnotherSerializer(serializers.Serializer):
|
|
|
|
c = serializers.CharField(required=True)
|
|
|
|
d = serializers.CharField(required=False)
|
|
|
|
|
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
class ExampleViewSet(ModelViewSet):
|
|
|
|
pagination_class = ExamplePagination
|
|
|
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
|
|
|
filter_backends = [filters.OrderingFilter]
|
|
|
|
serializer_class = ExampleSerializer
|
|
|
|
|
2016-08-05 13:19:39 +03:00
|
|
|
@detail_route(methods=['post'], serializer_class=AnotherSerializer)
|
|
|
|
def custom_action(self, request, pk):
|
|
|
|
return super(ExampleSerializer, self).retrieve(self, request)
|
|
|
|
|
2016-08-11 16:07:40 +03:00
|
|
|
@list_route()
|
|
|
|
def custom_list_action(self, request):
|
|
|
|
return super(ExampleViewSet, self).list(self, request)
|
|
|
|
|
2016-08-11 13:27:28 +03:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
|
|
|
assert self.request
|
2016-08-15 19:10:55 +03:00
|
|
|
assert self.action
|
2016-08-11 13:27:28 +03:00
|
|
|
return super(ExampleViewSet, self).get_serializer(*args, **kwargs)
|
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
|
2016-07-27 17:36:36 +03:00
|
|
|
class ExampleView(APIView):
|
|
|
|
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return Response()
|
|
|
|
|
|
|
|
|
2016-07-04 18:38:17 +03:00
|
|
|
router = DefaultRouter(schema_title='Example API' if coreapi else None)
|
|
|
|
router.register('example', ExampleViewSet, base_name='example')
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^', include(router.urls))
|
|
|
|
]
|
2016-07-27 17:36:36 +03:00
|
|
|
urlpatterns2 = [
|
|
|
|
url(r'^example-view/$', ExampleView.as_view(), name='example-view')
|
|
|
|
]
|
2016-07-04 18:38:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
|
|
|
@override_settings(ROOT_URLCONF='tests.test_schemas')
|
|
|
|
class TestRouterGeneratedSchema(TestCase):
|
|
|
|
def test_anonymous_request(self):
|
|
|
|
client = APIClient()
|
|
|
|
response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
expected = coreapi.Document(
|
|
|
|
url='',
|
|
|
|
title='Example API',
|
|
|
|
content={
|
|
|
|
'example': {
|
|
|
|
'list': coreapi.Link(
|
|
|
|
url='/example/',
|
|
|
|
action='get',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('page', required=False, location='query'),
|
|
|
|
coreapi.Field('ordering', required=False, location='query')
|
|
|
|
]
|
|
|
|
),
|
2016-08-11 16:07:40 +03:00
|
|
|
'custom_list_action': coreapi.Link(
|
|
|
|
url='/example/custom_list_action/',
|
|
|
|
action='get'
|
|
|
|
),
|
2016-07-04 18:38:17 +03:00
|
|
|
'retrieve': coreapi.Link(
|
|
|
|
url='/example/{pk}/',
|
|
|
|
action='get',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('pk', required=True, location='path')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.assertEqual(response.data, expected)
|
|
|
|
|
|
|
|
def test_authenticated_request(self):
|
|
|
|
client = APIClient()
|
|
|
|
client.force_authenticate(MockUser())
|
|
|
|
response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
expected = coreapi.Document(
|
|
|
|
url='',
|
|
|
|
title='Example API',
|
|
|
|
content={
|
|
|
|
'example': {
|
|
|
|
'list': coreapi.Link(
|
|
|
|
url='/example/',
|
|
|
|
action='get',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('page', required=False, location='query'),
|
|
|
|
coreapi.Field('ordering', required=False, location='query')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
'create': coreapi.Link(
|
|
|
|
url='/example/',
|
|
|
|
action='post',
|
|
|
|
encoding='application/json',
|
|
|
|
fields=[
|
2016-08-11 18:18:33 +03:00
|
|
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
2016-07-04 18:38:17 +03:00
|
|
|
coreapi.Field('b', required=False, location='form')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
'retrieve': coreapi.Link(
|
|
|
|
url='/example/{pk}/',
|
|
|
|
action='get',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('pk', required=True, location='path')
|
|
|
|
]
|
|
|
|
),
|
2016-08-05 13:19:39 +03:00
|
|
|
'custom_action': coreapi.Link(
|
|
|
|
url='/example/{pk}/custom_action/',
|
|
|
|
action='post',
|
|
|
|
encoding='application/json',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('pk', required=True, location='path'),
|
|
|
|
coreapi.Field('c', required=True, location='form'),
|
|
|
|
coreapi.Field('d', required=False, location='form'),
|
|
|
|
]
|
|
|
|
),
|
2016-08-11 16:07:40 +03:00
|
|
|
'custom_list_action': coreapi.Link(
|
|
|
|
url='/example/custom_list_action/',
|
|
|
|
action='get'
|
|
|
|
),
|
2016-07-04 18:38:17 +03:00
|
|
|
'update': coreapi.Link(
|
|
|
|
url='/example/{pk}/',
|
|
|
|
action='put',
|
|
|
|
encoding='application/json',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('pk', required=True, location='path'),
|
2016-08-11 18:18:33 +03:00
|
|
|
coreapi.Field('a', required=True, location='form', description='A field description'),
|
2016-07-04 18:38:17 +03:00
|
|
|
coreapi.Field('b', required=False, location='form')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
'partial_update': coreapi.Link(
|
|
|
|
url='/example/{pk}/',
|
|
|
|
action='patch',
|
|
|
|
encoding='application/json',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('pk', required=True, location='path'),
|
2016-08-11 18:18:33 +03:00
|
|
|
coreapi.Field('a', required=False, location='form', description='A field description'),
|
2016-07-04 18:38:17 +03:00
|
|
|
coreapi.Field('b', required=False, location='form')
|
|
|
|
]
|
|
|
|
),
|
|
|
|
'destroy': coreapi.Link(
|
|
|
|
url='/example/{pk}/',
|
|
|
|
action='delete',
|
|
|
|
fields=[
|
|
|
|
coreapi.Field('pk', required=True, location='path')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.assertEqual(response.data, expected)
|
2016-07-27 17:36:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(coreapi, 'coreapi is not installed')
|
|
|
|
class TestSchemaGenerator(TestCase):
|
|
|
|
def test_view(self):
|
|
|
|
schema_generator = SchemaGenerator(title='Test View', patterns=urlpatterns2)
|
|
|
|
schema = schema_generator.get_schema()
|
|
|
|
expected = coreapi.Document(
|
|
|
|
url='',
|
|
|
|
title='Test View',
|
|
|
|
content={
|
|
|
|
'example-view': {
|
|
|
|
'create': coreapi.Link(
|
|
|
|
url='/example-view/',
|
|
|
|
action='post',
|
|
|
|
fields=[]
|
|
|
|
),
|
|
|
|
'read': coreapi.Link(
|
|
|
|
url='/example-view/',
|
|
|
|
action='get',
|
|
|
|
fields=[]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.assertEquals(schema, expected)
|