mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-24 02:24:03 +03:00
0148a9f8da
* View suffix already set by initializer * Add 'name' and 'description' attributes to ViewSet ViewSets may now provide their `name` and `description` attributes directly, instead of relying on view introspection to derive them. These attributes may also be provided with the view's initkwargs. The ViewSet `name` and `suffix` initkwargs are mutually exclusive. The `action` decorator now provides the `name` and `description` to the view's initkwargs. By default, these values are derived from the method name and its docstring. The `name` may be overridden by providing it as an argument to the decorator. The `get_view_name` and `get_view_description` hooks now provide the view instance to the handler, instead of the view class. The default implementations of these handlers now respect the `name`/`description`. * Add 'extra actions' to ViewSet & browsable APIs * Update simple router tests Removed old test logic around link/action decorators from `v2.3`. Also simplified the test by making the results explicit instead of computed. * Add method mapping to ViewSet actions * Document extra action method mapping
237 lines
7.0 KiB
Python
237 lines
7.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
from __future__ import unicode_literals
|
||
|
||
from django.conf.urls import url
|
||
from django.test import TestCase, override_settings
|
||
|
||
from rest_framework.decorators import action
|
||
from rest_framework.routers import SimpleRouter
|
||
from rest_framework.serializers import ModelSerializer
|
||
from rest_framework.utils import json
|
||
from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
||
from rest_framework.utils.urls import remove_query_param, replace_query_param
|
||
from rest_framework.views import APIView
|
||
from rest_framework.viewsets import ModelViewSet
|
||
from tests.models import BasicModel
|
||
|
||
|
||
class Root(APIView):
|
||
pass
|
||
|
||
|
||
class ResourceRoot(APIView):
|
||
pass
|
||
|
||
|
||
class ResourceInstance(APIView):
|
||
pass
|
||
|
||
|
||
class NestedResourceRoot(APIView):
|
||
pass
|
||
|
||
|
||
class NestedResourceInstance(APIView):
|
||
pass
|
||
|
||
|
||
class CustomNameResourceInstance(APIView):
|
||
def get_view_name(self):
|
||
return "Foo"
|
||
|
||
|
||
class ResourceViewSet(ModelViewSet):
|
||
serializer_class = ModelSerializer
|
||
queryset = BasicModel.objects.all()
|
||
|
||
@action(detail=False)
|
||
def list_action(self, request, *args, **kwargs):
|
||
raise NotImplementedError
|
||
|
||
@action(detail=True)
|
||
def detail_action(self, request, *args, **kwargs):
|
||
raise NotImplementedError
|
||
|
||
|
||
router = SimpleRouter()
|
||
router.register(r'resources', ResourceViewSet)
|
||
urlpatterns = [
|
||
url(r'^$', Root.as_view()),
|
||
url(r'^resource/$', ResourceRoot.as_view()),
|
||
url(r'^resource/customname$', CustomNameResourceInstance.as_view()),
|
||
url(r'^resource/(?P<key>[0-9]+)$', ResourceInstance.as_view()),
|
||
url(r'^resource/(?P<key>[0-9]+)/$', NestedResourceRoot.as_view()),
|
||
url(r'^resource/(?P<key>[0-9]+)/(?P<other>[A-Za-z]+)$', NestedResourceInstance.as_view()),
|
||
]
|
||
urlpatterns += router.urls
|
||
|
||
|
||
@override_settings(ROOT_URLCONF='tests.test_utils')
|
||
class BreadcrumbTests(TestCase):
|
||
"""
|
||
Tests the breadcrumb functionality used by the HTML renderer.
|
||
"""
|
||
def test_root_breadcrumbs(self):
|
||
url = '/'
|
||
assert get_breadcrumbs(url) == [('Root', '/')]
|
||
|
||
def test_resource_root_breadcrumbs(self):
|
||
url = '/resource/'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'), ('Resource Root', '/resource/')
|
||
]
|
||
|
||
def test_resource_instance_breadcrumbs(self):
|
||
url = '/resource/123'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource Root', '/resource/'),
|
||
('Resource Instance', '/resource/123')
|
||
]
|
||
|
||
def test_resource_instance_customname_breadcrumbs(self):
|
||
url = '/resource/customname'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource Root', '/resource/'),
|
||
('Foo', '/resource/customname')
|
||
]
|
||
|
||
def test_nested_resource_breadcrumbs(self):
|
||
url = '/resource/123/'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource Root', '/resource/'),
|
||
('Resource Instance', '/resource/123'),
|
||
('Nested Resource Root', '/resource/123/')
|
||
]
|
||
|
||
def test_nested_resource_instance_breadcrumbs(self):
|
||
url = '/resource/123/abc'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource Root', '/resource/'),
|
||
('Resource Instance', '/resource/123'),
|
||
('Nested Resource Root', '/resource/123/'),
|
||
('Nested Resource Instance', '/resource/123/abc')
|
||
]
|
||
|
||
def test_broken_url_breadcrumbs_handled_gracefully(self):
|
||
url = '/foobar'
|
||
assert get_breadcrumbs(url) == [('Root', '/')]
|
||
|
||
def test_modelviewset_resource_instance_breadcrumbs(self):
|
||
url = '/resources/1/'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource List', '/resources/'),
|
||
('Resource Instance', '/resources/1/')
|
||
]
|
||
|
||
def test_modelviewset_list_action_breadcrumbs(self):
|
||
url = '/resources/list_action/'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource List', '/resources/'),
|
||
('List action', '/resources/list_action/'),
|
||
]
|
||
|
||
def test_modelviewset_detail_action_breadcrumbs(self):
|
||
url = '/resources/1/detail_action/'
|
||
assert get_breadcrumbs(url) == [
|
||
('Root', '/'),
|
||
('Resource List', '/resources/'),
|
||
('Resource Instance', '/resources/1/'),
|
||
('Detail action', '/resources/1/detail_action/'),
|
||
]
|
||
|
||
|
||
class JsonFloatTests(TestCase):
|
||
"""
|
||
Internaly, wrapped json functions should adhere to strict float handling
|
||
"""
|
||
|
||
def test_dumps(self):
|
||
with self.assertRaises(ValueError):
|
||
json.dumps(float('inf'))
|
||
|
||
with self.assertRaises(ValueError):
|
||
json.dumps(float('nan'))
|
||
|
||
def test_loads(self):
|
||
with self.assertRaises(ValueError):
|
||
json.loads("Infinity")
|
||
|
||
with self.assertRaises(ValueError):
|
||
json.loads("NaN")
|
||
|
||
|
||
@override_settings(STRICT_JSON=False)
|
||
class NonStrictJsonFloatTests(JsonFloatTests):
|
||
"""
|
||
'STRICT_JSON = False' should not somehow affect internal json behavior
|
||
"""
|
||
|
||
|
||
class UrlsReplaceQueryParamTests(TestCase):
|
||
"""
|
||
Tests the replace_query_param functionality.
|
||
"""
|
||
def test_valid_unicode_preserved(self):
|
||
# Encoded string: '查询'
|
||
q = '/?q=%E6%9F%A5%E8%AF%A2'
|
||
new_key = 'page'
|
||
new_value = 2
|
||
value = '%E6%9F%A5%E8%AF%A2'
|
||
|
||
assert new_key in replace_query_param(q, new_key, new_value)
|
||
assert value in replace_query_param(q, new_key, new_value)
|
||
|
||
def test_valid_unicode_replaced(self):
|
||
q = '/?page=1'
|
||
value = '1'
|
||
new_key = 'q'
|
||
new_value = '%E6%9F%A5%E8%AF%A2'
|
||
|
||
assert new_key in replace_query_param(q, new_key, new_value)
|
||
assert value in replace_query_param(q, new_key, new_value)
|
||
|
||
def test_invalid_unicode(self):
|
||
# Encoded string: '<27><><script>alert(313)</script>=1'
|
||
q = '/e/?%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1'
|
||
key = 'from'
|
||
value = 'login'
|
||
|
||
assert key in replace_query_param(q, key, value)
|
||
|
||
|
||
class UrlsRemoveQueryParamTests(TestCase):
|
||
"""
|
||
Tests the remove_query_param functionality.
|
||
"""
|
||
def test_valid_unicode_preserved(self):
|
||
q = '/?q=%E6%9F%A5%E8%AF%A2'
|
||
new_key = 'page'
|
||
new_value = 2
|
||
value = '%E6%9F%A5%E8%AF%A2'
|
||
|
||
assert new_key in replace_query_param(q, new_key, new_value)
|
||
assert value in replace_query_param(q, new_key, new_value)
|
||
|
||
def test_valid_unicode_removed(self):
|
||
q = '/?page=2345&q=%E6%9F%A5%E8%AF%A2'
|
||
key = 'page'
|
||
value = '2345'
|
||
removed_key = 'q'
|
||
|
||
assert key in remove_query_param(q, removed_key)
|
||
assert value in remove_query_param(q, removed_key)
|
||
assert '%' not in remove_query_param(q, removed_key)
|
||
|
||
def test_invalid_unicode(self):
|
||
q = '/?from=login&page=2&%FF%FE%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%33%31%33%29%3C%2F%73%63%72%69%70%74%3E=1'
|
||
key = 'from'
|
||
removed_key = 'page'
|
||
|
||
assert key in remove_query_param(q, removed_key)
|