2017-11-06 13:46:37 +03:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-05 00:55:35 +04:00
|
|
|
|
from __future__ import unicode_literals
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
2015-06-11 01:51:33 +03:00
|
|
|
|
from django.conf.urls import url
|
2016-06-01 17:31:00 +03:00
|
|
|
|
from django.test import TestCase, override_settings
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
2016-12-21 01:19:00 +03:00
|
|
|
|
from rest_framework.routers import SimpleRouter
|
|
|
|
|
from rest_framework.serializers import ModelSerializer
|
2017-07-07 19:46:17 +03:00
|
|
|
|
from rest_framework.utils import json
|
2012-09-20 16:06:27 +04:00
|
|
|
|
from rest_framework.utils.breadcrumbs import get_breadcrumbs
|
2017-11-06 13:46:37 +03:00
|
|
|
|
from rest_framework.utils.urls import remove_query_param, replace_query_param
|
2012-09-20 16:06:27 +04:00
|
|
|
|
from rest_framework.views import APIView
|
2016-12-21 01:19:00 +03:00
|
|
|
|
from rest_framework.viewsets import ModelViewSet
|
2014-09-19 18:46:32 +04:00
|
|
|
|
from tests.models import BasicModel
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Root(APIView):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceRoot(APIView):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResourceInstance(APIView):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NestedResourceRoot(APIView):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NestedResourceInstance(APIView):
|
|
|
|
|
pass
|
|
|
|
|
|
2014-09-19 18:46:32 +04:00
|
|
|
|
|
2015-08-14 14:16:57 +03:00
|
|
|
|
class CustomNameResourceInstance(APIView):
|
|
|
|
|
def get_view_name(self):
|
|
|
|
|
return "Foo"
|
|
|
|
|
|
2014-09-19 18:46:32 +04:00
|
|
|
|
|
2016-12-21 01:19:00 +03:00
|
|
|
|
class ResourceViewSet(ModelViewSet):
|
|
|
|
|
serializer_class = ModelSerializer
|
|
|
|
|
queryset = BasicModel.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
router = SimpleRouter()
|
|
|
|
|
router.register(r'resources', ResourceViewSet)
|
2015-06-11 01:45:23 +03:00
|
|
|
|
urlpatterns = [
|
2012-09-20 16:06:27 +04:00
|
|
|
|
url(r'^$', Root.as_view()),
|
|
|
|
|
url(r'^resource/$', ResourceRoot.as_view()),
|
2015-08-14 14:16:57 +03:00
|
|
|
|
url(r'^resource/customname$', CustomNameResourceInstance.as_view()),
|
2012-09-20 16:06:27 +04:00
|
|
|
|
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()),
|
2015-06-11 01:45:23 +03:00
|
|
|
|
]
|
2016-12-21 01:19:00 +03:00
|
|
|
|
urlpatterns += router.urls
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
|
@override_settings(ROOT_URLCONF='tests.test_utils')
|
2012-09-20 16:06:27 +04:00
|
|
|
|
class BreadcrumbTests(TestCase):
|
2014-09-19 18:46:32 +04:00
|
|
|
|
"""
|
|
|
|
|
Tests the breadcrumb functionality used by the HTML renderer.
|
|
|
|
|
"""
|
2012-09-20 16:06:27 +04:00
|
|
|
|
def test_root_breadcrumbs(self):
|
|
|
|
|
url = '/'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [('Root', '/')]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
def test_resource_root_breadcrumbs(self):
|
|
|
|
|
url = '/resource/'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [
|
|
|
|
|
('Root', '/'), ('Resource Root', '/resource/')
|
|
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
def test_resource_instance_breadcrumbs(self):
|
|
|
|
|
url = '/resource/123'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [
|
|
|
|
|
('Root', '/'),
|
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
|
('Resource Instance', '/resource/123')
|
|
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
2015-08-14 14:16:57 +03:00
|
|
|
|
def test_resource_instance_customname_breadcrumbs(self):
|
|
|
|
|
url = '/resource/customname'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [
|
|
|
|
|
('Root', '/'),
|
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
|
('Foo', '/resource/customname')
|
|
|
|
|
]
|
2015-08-14 14:16:57 +03:00
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
|
def test_nested_resource_breadcrumbs(self):
|
|
|
|
|
url = '/resource/123/'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [
|
|
|
|
|
('Root', '/'),
|
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
|
('Resource Instance', '/resource/123'),
|
|
|
|
|
('Nested Resource Root', '/resource/123/')
|
|
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
def test_nested_resource_instance_breadcrumbs(self):
|
|
|
|
|
url = '/resource/123/abc'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [
|
|
|
|
|
('Root', '/'),
|
|
|
|
|
('Resource Root', '/resource/'),
|
|
|
|
|
('Resource Instance', '/resource/123'),
|
|
|
|
|
('Nested Resource Root', '/resource/123/'),
|
|
|
|
|
('Nested Resource Instance', '/resource/123/abc')
|
|
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
|
def test_broken_url_breadcrumbs_handled_gracefully(self):
|
|
|
|
|
url = '/foobar'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [('Root', '/')]
|
2014-09-19 18:46:32 +04:00
|
|
|
|
|
2016-12-21 01:19:00 +03:00
|
|
|
|
def test_modelviewset_resource_instance_breadcrumbs(self):
|
|
|
|
|
url = '/resources/1/'
|
2017-01-04 12:13:32 +03:00
|
|
|
|
assert get_breadcrumbs(url) == [
|
|
|
|
|
('Root', '/'),
|
|
|
|
|
('Resource List', '/resources/'),
|
|
|
|
|
('Resource Instance', '/resources/1/')
|
|
|
|
|
]
|
2016-12-21 01:19:00 +03:00
|
|
|
|
|
2014-09-19 18:46:32 +04:00
|
|
|
|
|
2017-07-07 19:46:17 +03:00
|
|
|
|
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")
|
2017-07-10 22:23:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@override_settings(STRICT_JSON=False)
|
|
|
|
|
class NonStrictJsonFloatTests(JsonFloatTests):
|
|
|
|
|
"""
|
|
|
|
|
'STRICT_JSON = False' should not somehow affect internal json behavior
|
|
|
|
|
"""
|
2017-11-06 13:46:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|