2013-05-12 06:26:34 +04:00
|
|
|
from __future__ import unicode_literals
|
2014-12-28 15:02:52 +03:00
|
|
|
from django.conf.urls import url, include
|
2013-05-31 14:50:37 +04:00
|
|
|
from django.db import models
|
2013-05-12 06:26:34 +04:00
|
|
|
from django.test import TestCase
|
2013-06-27 02:00:42 +04:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-12-28 14:26:38 +03:00
|
|
|
from rest_framework import serializers, viewsets, permissions
|
2013-07-16 02:35:13 +04:00
|
|
|
from rest_framework.decorators import detail_route, list_route
|
2013-05-31 14:50:37 +04:00
|
|
|
from rest_framework.response import Response
|
2013-06-08 06:49:18 +04:00
|
|
|
from rest_framework.routers import SimpleRouter, DefaultRouter
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2014-11-03 16:44:47 +03:00
|
|
|
from collections import namedtuple
|
2013-05-12 06:26:34 +04:00
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2013-05-12 06:26:34 +04:00
|
|
|
|
2014-12-28 15:02:52 +03:00
|
|
|
|
|
|
|
class RouterTestModel(models.Model):
|
|
|
|
uuid = models.CharField(max_length=20)
|
|
|
|
text = models.CharField(max_length=200)
|
|
|
|
|
|
|
|
|
|
|
|
class NoteSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
url = serializers.HyperlinkedIdentityField(view_name='routertestmodel-detail', lookup_field='uuid')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = RouterTestModel
|
|
|
|
fields = ('url', 'uuid', 'text')
|
|
|
|
|
|
|
|
|
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = RouterTestModel.objects.all()
|
|
|
|
serializer_class = NoteSerializer
|
|
|
|
lookup_field = 'uuid'
|
|
|
|
|
|
|
|
|
|
|
|
class MockViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = None
|
|
|
|
serializer_class = None
|
|
|
|
|
|
|
|
|
|
|
|
notes_router = SimpleRouter()
|
|
|
|
notes_router.register(r'notes', NoteViewSet)
|
|
|
|
|
|
|
|
namespaced_router = DefaultRouter()
|
|
|
|
namespaced_router.register(r'example', MockViewSet, base_name='example')
|
|
|
|
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
|
|
|
url(r'^namespaced/', include(namespaced_router.urls, namespace='example')),
|
|
|
|
url(r'^example/', include(notes_router.urls)),
|
|
|
|
]
|
2013-05-31 14:50:37 +04:00
|
|
|
|
2013-05-12 06:26:34 +04:00
|
|
|
|
|
|
|
class BasicViewSet(viewsets.ViewSet):
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'list'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route(methods=['post'])
|
2013-05-12 06:26:34 +04:00
|
|
|
def action1(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action1'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route(methods=['post'])
|
2013-05-12 06:26:34 +04:00
|
|
|
def action2(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route(methods=['post', 'delete'])
|
2013-06-02 23:40:56 +04:00
|
|
|
def action3(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route()
|
2013-05-12 06:26:34 +04:00
|
|
|
def link1(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'link1'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route()
|
2013-05-12 06:26:34 +04:00
|
|
|
def link2(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'link2'})
|
|
|
|
|
|
|
|
|
|
|
|
class TestSimpleRouter(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
|
|
|
|
def test_link_and_action_decorator(self):
|
|
|
|
routes = self.router.get_routes(BasicViewSet)
|
|
|
|
decorator_routes = routes[2:]
|
2013-05-12 17:22:14 +04:00
|
|
|
# Make sure all these endpoints exist and none have been clobbered
|
2013-06-02 23:40:56 +04:00
|
|
|
for i, endpoint in enumerate(['action1', 'action2', 'action3', 'link1', 'link2']):
|
2013-05-12 17:22:14 +04:00
|
|
|
route = decorator_routes[i]
|
|
|
|
# check url listing
|
|
|
|
self.assertEqual(route.url,
|
2013-06-04 23:59:12 +04:00
|
|
|
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(endpoint))
|
2013-05-12 17:22:14 +04:00
|
|
|
# check method to function mapping
|
2013-06-02 23:40:56 +04:00
|
|
|
if endpoint == 'action3':
|
|
|
|
methods_map = ['post', 'delete']
|
|
|
|
elif endpoint.startswith('action'):
|
|
|
|
methods_map = ['post']
|
2013-05-12 17:22:14 +04:00
|
|
|
else:
|
2013-06-02 23:40:56 +04:00
|
|
|
methods_map = ['get']
|
|
|
|
for method in methods_map:
|
|
|
|
self.assertEqual(route.mapping[method], endpoint)
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
|
2014-12-28 15:02:52 +03:00
|
|
|
class TestRootView(TestCase):
|
|
|
|
urls = 'tests.test_routers'
|
|
|
|
|
|
|
|
def test_retrieve_namespaced_root(self):
|
|
|
|
response = self.client.get('/namespaced/')
|
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
|
|
|
{
|
|
|
|
"example": "http://testserver/namespaced/example/",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_retrieve_non_namespaced_root(self):
|
|
|
|
response = self.client.get('/non-namespaced/')
|
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
|
|
|
{
|
|
|
|
"example": "http://testserver/non-namespaced/example/",
|
|
|
|
}
|
|
|
|
)
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
|
|
|
|
class TestCustomLookupFields(TestCase):
|
|
|
|
"""
|
|
|
|
Ensure that custom lookup fields are correctly routed.
|
|
|
|
"""
|
2014-03-02 15:40:30 +04:00
|
|
|
urls = 'tests.test_routers'
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
def setUp(self):
|
2014-09-18 14:20:56 +04:00
|
|
|
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
|
|
|
|
2013-05-31 14:50:37 +04:00
|
|
|
def test_custom_lookup_field_route(self):
|
2014-12-28 15:02:52 +03:00
|
|
|
detail_route = notes_router.urls[-1]
|
2013-05-31 14:50:37 +04:00
|
|
|
detail_url_pattern = detail_route.regex.pattern
|
|
|
|
self.assertIn('<uuid>', detail_url_pattern)
|
|
|
|
|
|
|
|
def test_retrieve_lookup_field_list_view(self):
|
2014-12-28 15:02:52 +03:00
|
|
|
response = self.client.get('/example/notes/')
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
2013-05-31 14:50:37 +04:00
|
|
|
[{
|
2014-12-28 15:02:52 +03:00
|
|
|
"url": "http://testserver/example/notes/123/",
|
2013-05-31 14:50:37 +04:00
|
|
|
"uuid": "123", "text": "foo bar"
|
|
|
|
}]
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_retrieve_lookup_field_detail_view(self):
|
2014-12-28 15:02:52 +03:00
|
|
|
response = self.client.get('/example/notes/123/')
|
2014-08-19 16:28:07 +04:00
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
2013-05-31 14:50:37 +04:00
|
|
|
{
|
2014-12-28 15:02:52 +03:00
|
|
|
"url": "http://testserver/example/notes/123/",
|
2013-05-31 14:50:37 +04:00
|
|
|
"uuid": "123", "text": "foo bar"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-06-04 23:59:12 +04:00
|
|
|
|
2014-01-03 02:44:47 +04:00
|
|
|
class TestLookupValueRegex(TestCase):
|
|
|
|
"""
|
|
|
|
Ensure the router honors lookup_value_regex when applied
|
|
|
|
to the viewset.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = RouterTestModel.objects.all()
|
|
|
|
lookup_field = 'uuid'
|
|
|
|
lookup_value_regex = '[0-9a-f]{32}'
|
|
|
|
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
self.router.register(r'notes', NoteViewSet)
|
|
|
|
self.urls = self.router.urls
|
|
|
|
|
|
|
|
def test_urls_limited_by_lookup_value_regex(self):
|
|
|
|
expected = ['^notes/$', '^notes/(?P<uuid>[0-9a-f]{32})/$']
|
|
|
|
for idx in range(len(expected)):
|
|
|
|
self.assertEqual(expected[idx], self.urls[idx].regex.pattern)
|
|
|
|
|
|
|
|
|
2013-06-22 01:03:07 +04:00
|
|
|
class TestTrailingSlashIncluded(TestCase):
|
2013-06-04 23:59:12 +04:00
|
|
|
def setUp(self):
|
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
|
|
|
model = RouterTestModel
|
|
|
|
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
self.router.register(r'notes', NoteViewSet)
|
|
|
|
self.urls = self.router.urls
|
|
|
|
|
|
|
|
def test_urls_have_trailing_slash_by_default(self):
|
2014-01-05 01:57:50 +04:00
|
|
|
expected = ['^notes/$', '^notes/(?P<pk>[^/.]+)/$']
|
2013-06-04 23:59:12 +04:00
|
|
|
for idx in range(len(expected)):
|
|
|
|
self.assertEqual(expected[idx], self.urls[idx].regex.pattern)
|
|
|
|
|
|
|
|
|
2013-06-22 01:03:07 +04:00
|
|
|
class TestTrailingSlashRemoved(TestCase):
|
2013-06-04 23:59:12 +04:00
|
|
|
def setUp(self):
|
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
|
|
|
model = RouterTestModel
|
|
|
|
|
|
|
|
self.router = SimpleRouter(trailing_slash=False)
|
|
|
|
self.router.register(r'notes', NoteViewSet)
|
|
|
|
self.urls = self.router.urls
|
|
|
|
|
|
|
|
def test_urls_can_have_trailing_slash_removed(self):
|
2013-08-23 18:18:47 +04:00
|
|
|
expected = ['^notes$', '^notes/(?P<pk>[^/.]+)$']
|
2013-06-04 23:59:12 +04:00
|
|
|
for idx in range(len(expected)):
|
|
|
|
self.assertEqual(expected[idx], self.urls[idx].regex.pattern)
|
2013-06-08 06:49:18 +04:00
|
|
|
|
2013-06-22 01:03:07 +04:00
|
|
|
|
2013-06-08 06:49:18 +04:00
|
|
|
class TestNameableRoot(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
class NoteViewSet(viewsets.ModelViewSet):
|
|
|
|
model = RouterTestModel
|
|
|
|
self.router = DefaultRouter()
|
|
|
|
self.router.root_view_name = 'nameable-root'
|
|
|
|
self.router.register(r'notes', NoteViewSet)
|
|
|
|
self.urls = self.router.urls
|
|
|
|
|
|
|
|
def test_router_has_custom_name(self):
|
|
|
|
expected = 'nameable-root'
|
|
|
|
self.assertEqual(expected, self.urls[0].name)
|
|
|
|
|
2013-06-22 01:03:07 +04:00
|
|
|
|
|
|
|
class TestActionKeywordArgs(TestCase):
|
|
|
|
"""
|
|
|
|
Ensure keyword arguments passed in the `@action` decorator
|
|
|
|
are properly handled. Refs #940.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
class TestViewSet(viewsets.ModelViewSet):
|
|
|
|
permission_classes = []
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route(methods=['post'], permission_classes=[permissions.AllowAny])
|
2013-06-22 01:03:07 +04:00
|
|
|
def custom(self, request, *args, **kwargs):
|
|
|
|
return Response({
|
|
|
|
'permission_classes': self.permission_classes
|
|
|
|
})
|
|
|
|
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
self.router.register(r'test', TestViewSet, base_name='test')
|
|
|
|
self.view = self.router.urls[-1].callback
|
|
|
|
|
|
|
|
def test_action_kwargs(self):
|
|
|
|
request = factory.post('/test/0/custom/')
|
|
|
|
response = self.view(request)
|
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
|
|
|
{'permission_classes': [permissions.AllowAny]}
|
|
|
|
)
|
2013-06-27 02:00:42 +04:00
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
|
2013-06-27 02:00:42 +04:00
|
|
|
class TestActionAppliedToExistingRoute(TestCase):
|
|
|
|
"""
|
2013-07-16 02:35:13 +04:00
|
|
|
Ensure `@detail_route` decorator raises an except when applied
|
2013-06-27 02:00:42 +04:00
|
|
|
to an existing route
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_exception_raised_when_action_applied_to_existing_route(self):
|
|
|
|
class TestViewSet(viewsets.ModelViewSet):
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route(methods=['post'])
|
2013-06-27 02:00:42 +04:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
return Response({
|
|
|
|
'hello': 'world'
|
|
|
|
})
|
|
|
|
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
self.router.register(r'test', TestViewSet, base_name='test')
|
|
|
|
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.router.urls
|
2013-06-06 01:39:14 +04:00
|
|
|
|
|
|
|
|
2013-07-13 19:11:53 +04:00
|
|
|
class DynamicListAndDetailViewSet(viewsets.ViewSet):
|
2013-06-06 01:39:14 +04:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'list'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@list_route(methods=['post'])
|
|
|
|
def list_route_post(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'action1'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route(methods=['post'])
|
|
|
|
def detail_route_post(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@list_route()
|
|
|
|
def list_route_get(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'link1'})
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
@detail_route()
|
|
|
|
def detail_route_get(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'link2'})
|
|
|
|
|
2014-12-19 17:23:48 +03:00
|
|
|
@list_route(url_path="list_custom-route")
|
2014-11-03 16:44:47 +03:00
|
|
|
def list_custom_route_get(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'link1'})
|
|
|
|
|
2014-12-19 17:23:48 +03:00
|
|
|
@detail_route(url_path="detail_custom-route")
|
2014-11-03 16:44:47 +03:00
|
|
|
def detail_custom_route_get(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'link2'})
|
|
|
|
|
2013-06-06 01:39:14 +04:00
|
|
|
|
2013-07-13 19:11:53 +04:00
|
|
|
class TestDynamicListAndDetailRouter(TestCase):
|
2013-06-06 01:39:14 +04:00
|
|
|
def setUp(self):
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
|
2013-07-16 02:35:13 +04:00
|
|
|
def test_list_and_detail_route_decorators(self):
|
2013-07-13 19:11:53 +04:00
|
|
|
routes = self.router.get_routes(DynamicListAndDetailViewSet)
|
2013-06-06 01:39:14 +04:00
|
|
|
decorator_routes = [r for r in routes if not (r.name.endswith('-list') or r.name.endswith('-detail'))]
|
2014-11-03 16:44:47 +03:00
|
|
|
|
2014-12-19 17:23:48 +03:00
|
|
|
MethodNamesMap = namedtuple('MethodNamesMap', 'method_name url_path')
|
2013-06-06 01:39:14 +04:00
|
|
|
# Make sure all these endpoints exist and none have been clobbered
|
2014-11-03 16:44:47 +03:00
|
|
|
for i, endpoint in enumerate([MethodNamesMap('list_custom_route_get', 'list_custom-route'),
|
|
|
|
MethodNamesMap('list_route_get', 'list_route_get'),
|
|
|
|
MethodNamesMap('list_route_post', 'list_route_post'),
|
|
|
|
MethodNamesMap('detail_custom_route_get', 'detail_custom-route'),
|
|
|
|
MethodNamesMap('detail_route_get', 'detail_route_get'),
|
|
|
|
MethodNamesMap('detail_route_post', 'detail_route_post')
|
|
|
|
]):
|
2013-06-06 01:39:14 +04:00
|
|
|
route = decorator_routes[i]
|
|
|
|
# check url listing
|
2014-11-03 16:44:47 +03:00
|
|
|
method_name = endpoint.method_name
|
2014-12-19 17:23:48 +03:00
|
|
|
url_path = endpoint.url_path
|
2014-11-03 16:44:47 +03:00
|
|
|
|
|
|
|
if method_name.startswith('list_'):
|
2013-06-06 01:39:14 +04:00
|
|
|
self.assertEqual(route.url,
|
2014-12-19 17:23:48 +03:00
|
|
|
'^{{prefix}}/{0}{{trailing_slash}}$'.format(url_path))
|
2013-06-06 01:39:14 +04:00
|
|
|
else:
|
|
|
|
self.assertEqual(route.url,
|
2014-12-19 17:23:48 +03:00
|
|
|
'^{{prefix}}/{{lookup}}/{0}{{trailing_slash}}$'.format(url_path))
|
2013-06-06 01:39:14 +04:00
|
|
|
# check method to function mapping
|
2014-11-03 16:44:47 +03:00
|
|
|
if method_name.endswith('_post'):
|
2013-06-06 01:39:14 +04:00
|
|
|
method_map = 'post'
|
|
|
|
else:
|
|
|
|
method_map = 'get'
|
2014-11-03 16:44:47 +03:00
|
|
|
self.assertEqual(route.mapping[method_map], method_name)
|