2014-09-24 17:09:49 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2017-01-27 12:25:51 +03:00
|
|
|
import pytest
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
2015-05-28 22:41:10 +03:00
|
|
|
from django.db import models
|
|
|
|
from django.test import TestCase
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
|
|
from rest_framework import (
|
|
|
|
exceptions, metadata, serializers, status, versioning, views
|
|
|
|
)
|
2015-01-24 22:00:36 +03:00
|
|
|
from rest_framework.renderers import BrowsableAPIRenderer
|
2014-09-24 17:09:49 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
|
|
|
|
2016-06-01 18:33:03 +03:00
|
|
|
from .models import BasicModel
|
|
|
|
|
2017-11-23 10:57:31 +03:00
|
|
|
request = APIRequestFactory().options('/')
|
2014-09-24 17:09:49 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestMetadata:
|
2017-01-27 12:25:51 +03:00
|
|
|
|
|
|
|
def test_determine_metadata_abstract_method_raises_proper_error(self):
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
metadata.BaseMetadata().determine_metadata(None, None)
|
|
|
|
|
2014-09-24 17:09:49 +04:00
|
|
|
def test_metadata(self):
|
|
|
|
"""
|
|
|
|
OPTIONS requests to views should return a valid 200 response.
|
|
|
|
"""
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
"""Example view."""
|
|
|
|
pass
|
|
|
|
|
2015-01-16 19:55:28 +03:00
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
2014-09-24 17:09:49 +04:00
|
|
|
expected = {
|
|
|
|
'name': 'Example',
|
|
|
|
'description': 'Example view.',
|
|
|
|
'renders': [
|
|
|
|
'application/json',
|
|
|
|
'text/html'
|
|
|
|
],
|
|
|
|
'parses': [
|
|
|
|
'application/json',
|
|
|
|
'application/x-www-form-urlencoded',
|
|
|
|
'multipart/form-data'
|
|
|
|
]
|
|
|
|
}
|
2015-01-16 19:55:28 +03:00
|
|
|
assert response.status_code == status.HTTP_200_OK
|
2014-09-24 17:09:49 +04:00
|
|
|
assert response.data == expected
|
|
|
|
|
|
|
|
def test_none_metadata(self):
|
|
|
|
"""
|
|
|
|
OPTIONS requests to views where `metadata_class = None` should raise
|
|
|
|
a MethodNotAllowed exception, which will result in an HTTP 405 response.
|
|
|
|
"""
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
metadata_class = None
|
|
|
|
|
2015-01-16 19:55:28 +03:00
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
|
|
|
assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
|
|
|
|
assert response.data == {'detail': 'Method "OPTIONS" not allowed.'}
|
2014-09-24 17:09:49 +04:00
|
|
|
|
|
|
|
def test_actions(self):
|
|
|
|
"""
|
|
|
|
On generic views OPTIONS should return an 'actions' key with metadata
|
|
|
|
on the fields that may be supplied to PUT and POST requests.
|
|
|
|
"""
|
2015-07-16 17:17:18 +03:00
|
|
|
class NestedField(serializers.Serializer):
|
|
|
|
a = serializers.IntegerField()
|
|
|
|
b = serializers.IntegerField()
|
|
|
|
|
2014-09-24 17:09:49 +04:00
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
choice_field = serializers.ChoiceField(['red', 'green', 'blue'])
|
2015-02-20 19:34:49 +03:00
|
|
|
integer_field = serializers.IntegerField(
|
|
|
|
min_value=1, max_value=1000
|
|
|
|
)
|
|
|
|
char_field = serializers.CharField(
|
|
|
|
required=False, min_length=3, max_length=40
|
|
|
|
)
|
2015-07-02 15:15:43 +03:00
|
|
|
list_field = serializers.ListField(
|
|
|
|
child=serializers.ListField(
|
|
|
|
child=serializers.IntegerField()
|
|
|
|
)
|
|
|
|
)
|
2015-07-16 17:17:18 +03:00
|
|
|
nested_field = NestedField()
|
2018-03-13 17:49:15 +03:00
|
|
|
uuid_field = serializers.UUIDField(label="UUID field")
|
2014-09-24 17:09:49 +04:00
|
|
|
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
"""Example view."""
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
return ExampleSerializer()
|
|
|
|
|
2015-01-16 19:55:28 +03:00
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
2014-09-24 17:09:49 +04:00
|
|
|
expected = {
|
|
|
|
'name': 'Example',
|
|
|
|
'description': 'Example view.',
|
|
|
|
'renders': [
|
|
|
|
'application/json',
|
|
|
|
'text/html'
|
|
|
|
],
|
|
|
|
'parses': [
|
|
|
|
'application/json',
|
|
|
|
'application/x-www-form-urlencoded',
|
|
|
|
'multipart/form-data'
|
|
|
|
],
|
|
|
|
'actions': {
|
|
|
|
'POST': {
|
|
|
|
'choice_field': {
|
|
|
|
'type': 'choice',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'Choice field',
|
|
|
|
'choices': [
|
2014-09-25 13:49:25 +04:00
|
|
|
{'display_name': 'red', 'value': 'red'},
|
2014-09-24 17:09:49 +04:00
|
|
|
{'display_name': 'green', 'value': 'green'},
|
2014-09-25 13:49:25 +04:00
|
|
|
{'display_name': 'blue', 'value': 'blue'}
|
2014-09-24 17:09:49 +04:00
|
|
|
]
|
|
|
|
},
|
|
|
|
'integer_field': {
|
|
|
|
'type': 'integer',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
2015-02-20 19:34:49 +03:00
|
|
|
'label': 'Integer field',
|
|
|
|
'min_value': 1,
|
|
|
|
'max_value': 1000,
|
|
|
|
|
2014-09-24 17:09:49 +04:00
|
|
|
},
|
|
|
|
'char_field': {
|
|
|
|
'type': 'string',
|
|
|
|
'required': False,
|
|
|
|
'read_only': False,
|
2015-02-20 19:34:49 +03:00
|
|
|
'label': 'Char field',
|
|
|
|
'min_length': 3,
|
|
|
|
'max_length': 40
|
2015-07-02 15:15:43 +03:00
|
|
|
},
|
|
|
|
'list_field': {
|
|
|
|
'type': 'list',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'List field',
|
|
|
|
'child': {
|
|
|
|
'type': 'list',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'child': {
|
|
|
|
'type': 'integer',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 17:17:18 +03:00
|
|
|
},
|
|
|
|
'nested_field': {
|
|
|
|
'type': 'nested object',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'Nested field',
|
|
|
|
'children': {
|
|
|
|
'a': {
|
|
|
|
'type': 'integer',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'A'
|
|
|
|
},
|
|
|
|
'b': {
|
|
|
|
'type': 'integer',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'B'
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 17:49:15 +03:00
|
|
|
},
|
|
|
|
'uuid_field': {
|
|
|
|
"type": "string",
|
|
|
|
"required": True,
|
|
|
|
"read_only": False,
|
|
|
|
"label": "UUID field",
|
|
|
|
},
|
2014-09-24 17:09:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-16 19:55:28 +03:00
|
|
|
assert response.status_code == status.HTTP_200_OK
|
2014-09-24 17:09:49 +04:00
|
|
|
assert response.data == expected
|
|
|
|
|
|
|
|
def test_global_permissions(self):
|
|
|
|
"""
|
|
|
|
If a user does not have global permissions on an action, then any
|
|
|
|
metadata associated with it should not be included in OPTION responses.
|
|
|
|
"""
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
choice_field = serializers.ChoiceField(['red', 'green', 'blue'])
|
|
|
|
integer_field = serializers.IntegerField(max_value=10)
|
|
|
|
char_field = serializers.CharField(required=False)
|
|
|
|
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
"""Example view."""
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def put(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
return ExampleSerializer()
|
|
|
|
|
|
|
|
def check_permissions(self, request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
raise exceptions.PermissionDenied()
|
|
|
|
|
2015-01-16 19:55:28 +03:00
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
2018-01-08 12:49:46 +03:00
|
|
|
assert list(response.data['actions']) == ['PUT']
|
2014-09-24 17:09:49 +04:00
|
|
|
|
|
|
|
def test_object_permissions(self):
|
|
|
|
"""
|
|
|
|
If a user does not have object permissions on an action, then any
|
|
|
|
metadata associated with it should not be included in OPTION responses.
|
|
|
|
"""
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
choice_field = serializers.ChoiceField(['red', 'green', 'blue'])
|
|
|
|
integer_field = serializers.IntegerField(max_value=10)
|
|
|
|
char_field = serializers.CharField(required=False)
|
|
|
|
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
"""Example view."""
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def put(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
return ExampleSerializer()
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
if self.request.method == 'PUT':
|
|
|
|
raise exceptions.PermissionDenied()
|
|
|
|
|
2015-01-16 19:55:28 +03:00
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
2014-09-24 17:09:49 +04:00
|
|
|
assert list(response.data['actions'].keys()) == ['POST']
|
2015-01-24 22:00:36 +03:00
|
|
|
|
|
|
|
def test_bug_2455_clone_request(self):
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
renderer_classes = (BrowsableAPIRenderer,)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
assert hasattr(self.request, 'version')
|
|
|
|
return serializers.Serializer()
|
|
|
|
|
|
|
|
view = ExampleView.as_view()
|
|
|
|
view(request=request)
|
2015-01-28 04:10:17 +03:00
|
|
|
|
|
|
|
def test_bug_2477_clone_request(self):
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
renderer_classes = (BrowsableAPIRenderer,)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
assert hasattr(self.request, 'versioning_scheme')
|
|
|
|
return serializers.Serializer()
|
|
|
|
|
|
|
|
scheme = versioning.QueryParameterVersioning
|
|
|
|
view = ExampleView.as_view(versioning_class=scheme)
|
|
|
|
view(request=request)
|
2015-03-25 04:31:35 +03:00
|
|
|
|
2018-03-24 01:47:10 +03:00
|
|
|
def test_dont_show_hidden_fields(self):
|
|
|
|
"""
|
|
|
|
HiddenField shouldn't show up in SimpleMetadata at all.
|
|
|
|
"""
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
integer_field = serializers.IntegerField(max_value=10)
|
|
|
|
hidden_field = serializers.HiddenField(default=1)
|
|
|
|
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
"""Example view."""
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
return ExampleSerializer()
|
|
|
|
|
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert set(response.data['actions']['POST'].keys()) == {'integer_field'}
|
|
|
|
|
2017-01-27 12:25:51 +03:00
|
|
|
def test_list_serializer_metadata_returns_info_about_fields_of_child_serializer(self):
|
|
|
|
class ExampleSerializer(serializers.Serializer):
|
|
|
|
integer_field = serializers.IntegerField(max_value=10)
|
|
|
|
char_field = serializers.CharField(required=False)
|
|
|
|
|
|
|
|
class ExampleListSerializer(serializers.ListSerializer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
options = metadata.SimpleMetadata()
|
|
|
|
child_serializer = ExampleSerializer()
|
|
|
|
list_serializer = ExampleListSerializer(child=child_serializer)
|
|
|
|
assert options.get_serializer_info(list_serializer) == options.get_serializer_info(child_serializer)
|
|
|
|
|
2016-06-01 18:33:03 +03:00
|
|
|
|
|
|
|
class TestSimpleMetadataFieldInfo(TestCase):
|
2015-03-25 04:31:35 +03:00
|
|
|
def test_null_boolean_field_info_type(self):
|
|
|
|
options = metadata.SimpleMetadata()
|
|
|
|
field_info = options.get_field_info(serializers.NullBooleanField())
|
2017-01-12 11:21:15 +03:00
|
|
|
assert field_info['type'] == 'boolean'
|
2016-06-01 18:33:03 +03:00
|
|
|
|
|
|
|
def test_related_field_choices(self):
|
|
|
|
options = metadata.SimpleMetadata()
|
|
|
|
BasicModel.objects.create()
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
field_info = options.get_field_info(
|
|
|
|
serializers.RelatedField(queryset=BasicModel.objects.all())
|
|
|
|
)
|
2017-01-12 11:21:15 +03:00
|
|
|
assert 'choices' not in field_info
|
2015-05-28 22:41:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestModelSerializerMetadata(TestCase):
|
|
|
|
def test_read_only_primary_key_related_field(self):
|
|
|
|
"""
|
|
|
|
On generic views OPTIONS should return an 'actions' key with metadata
|
|
|
|
on the fields that may be supplied to PUT and POST requests. It should
|
|
|
|
not fail when a read_only PrimaryKeyRelatedField is present
|
|
|
|
"""
|
|
|
|
class Parent(models.Model):
|
|
|
|
integer_field = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(1000)])
|
|
|
|
children = models.ManyToManyField('Child')
|
|
|
|
name = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
|
|
|
|
class Child(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
class ExampleSerializer(serializers.ModelSerializer):
|
|
|
|
children = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Parent
|
2016-06-02 16:39:10 +03:00
|
|
|
fields = '__all__'
|
2015-05-28 22:41:10 +03:00
|
|
|
|
|
|
|
class ExampleView(views.APIView):
|
|
|
|
"""Example view."""
|
|
|
|
def post(self, request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_serializer(self):
|
|
|
|
return ExampleSerializer()
|
|
|
|
|
|
|
|
view = ExampleView.as_view()
|
|
|
|
response = view(request=request)
|
|
|
|
expected = {
|
|
|
|
'name': 'Example',
|
|
|
|
'description': 'Example view.',
|
|
|
|
'renders': [
|
|
|
|
'application/json',
|
|
|
|
'text/html'
|
|
|
|
],
|
|
|
|
'parses': [
|
|
|
|
'application/json',
|
|
|
|
'application/x-www-form-urlencoded',
|
|
|
|
'multipart/form-data'
|
|
|
|
],
|
|
|
|
'actions': {
|
|
|
|
'POST': {
|
|
|
|
'id': {
|
|
|
|
'type': 'integer',
|
|
|
|
'required': False,
|
|
|
|
'read_only': True,
|
|
|
|
'label': 'ID'
|
|
|
|
},
|
|
|
|
'children': {
|
|
|
|
'type': 'field',
|
|
|
|
'required': False,
|
|
|
|
'read_only': True,
|
|
|
|
'label': 'Children'
|
|
|
|
},
|
|
|
|
'integer_field': {
|
|
|
|
'type': 'integer',
|
|
|
|
'required': True,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'Integer field',
|
|
|
|
'min_value': 1,
|
|
|
|
'max_value': 1000
|
|
|
|
},
|
|
|
|
'name': {
|
|
|
|
'type': 'string',
|
|
|
|
'required': False,
|
|
|
|
'read_only': False,
|
|
|
|
'label': 'Name',
|
|
|
|
'max_length': 100
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert response.data == expected
|