2013-05-12 06:26:34 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2018-07-06 12:03:12 +03:00
|
|
|
import warnings
|
2015-06-25 23:55:51 +03:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2017-01-19 19:00:07 +03:00
|
|
|
import pytest
|
2017-10-05 21:41:38 +03:00
|
|
|
from django.conf.urls import include, url
|
2015-06-25 23:55:51 +03:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2013-05-31 14:50:37 +04:00
|
|
|
from django.db import models
|
2016-06-01 17:31:00 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2018-07-06 11:33:10 +03:00
|
|
|
from django.urls import resolve, reverse
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
|
|
from rest_framework import permissions, serializers, viewsets
|
2017-10-16 12:31:13 +03:00
|
|
|
from rest_framework.compat import get_regex_pattern
|
2018-01-25 11:40:49 +03:00
|
|
|
from rest_framework.decorators import action
|
2013-05-31 14:50:37 +04:00
|
|
|
from rest_framework.response import Response
|
2015-06-25 23:55:51 +03:00
|
|
|
from rest_framework.routers import DefaultRouter, SimpleRouter
|
2018-01-02 13:14:25 +03:00
|
|
|
from rest_framework.test import APIRequestFactory, URLPatternsTestCase
|
2017-07-07 19:47:08 +03:00
|
|
|
from rest_framework.utils import json
|
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'
|
|
|
|
|
|
|
|
|
2015-03-13 03:07:20 +03:00
|
|
|
class KWargedNoteViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = RouterTestModel.objects.all()
|
|
|
|
serializer_class = NoteSerializer
|
|
|
|
lookup_field = 'text__contains'
|
|
|
|
lookup_url_kwarg = 'text'
|
|
|
|
|
|
|
|
|
2014-12-28 15:02:52 +03:00
|
|
|
class MockViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = None
|
|
|
|
serializer_class = None
|
|
|
|
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
class EmptyPrefixSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = RouterTestModel
|
|
|
|
fields = ('uuid', 'text')
|
|
|
|
|
|
|
|
|
|
|
|
class EmptyPrefixViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = [RouterTestModel(id=1, uuid='111', text='First'), RouterTestModel(id=2, uuid='222', text='Second')]
|
|
|
|
serializer_class = EmptyPrefixSerializer
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
index = int(self.kwargs['pk']) - 1
|
|
|
|
return self.queryset[index]
|
|
|
|
|
|
|
|
|
2017-05-28 12:38:09 +03:00
|
|
|
class RegexUrlPathViewSet(viewsets.ViewSet):
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=False, url_path='list/(?P<kwarg>[0-9]{4})')
|
2017-05-28 12:38:09 +03:00
|
|
|
def regex_url_path_list(self, request, *args, **kwargs):
|
|
|
|
kwarg = self.kwargs.get('kwarg', '')
|
|
|
|
return Response({'kwarg': kwarg})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=True, url_path='detail/(?P<kwarg>[0-9]{4})')
|
2017-05-28 12:38:09 +03:00
|
|
|
def regex_url_path_detail(self, request, *args, **kwargs):
|
|
|
|
pk = self.kwargs.get('pk', '')
|
|
|
|
kwarg = self.kwargs.get('kwarg', '')
|
|
|
|
return Response({'pk': pk, 'kwarg': kwarg})
|
|
|
|
|
|
|
|
|
2014-12-28 15:02:52 +03:00
|
|
|
notes_router = SimpleRouter()
|
|
|
|
notes_router.register(r'notes', NoteViewSet)
|
|
|
|
|
2015-03-13 03:07:20 +03:00
|
|
|
kwarged_notes_router = SimpleRouter()
|
|
|
|
kwarged_notes_router.register(r'notes', KWargedNoteViewSet)
|
|
|
|
|
2014-12-28 15:02:52 +03:00
|
|
|
namespaced_router = DefaultRouter()
|
2018-07-06 12:03:12 +03:00
|
|
|
namespaced_router.register(r'example', MockViewSet, basename='example')
|
2014-12-28 15:02:52 +03:00
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
empty_prefix_router = SimpleRouter()
|
2018-07-06 12:03:12 +03:00
|
|
|
empty_prefix_router.register(r'', EmptyPrefixViewSet, basename='empty_prefix')
|
2016-10-10 15:03:46 +03:00
|
|
|
|
2017-05-28 12:38:09 +03:00
|
|
|
regex_url_path_router = SimpleRouter()
|
2018-07-06 12:03:12 +03:00
|
|
|
regex_url_path_router.register(r'', RegexUrlPathViewSet, basename='regex')
|
2017-05-28 12:38:09 +03:00
|
|
|
|
2013-05-12 06:26:34 +04:00
|
|
|
|
|
|
|
class BasicViewSet(viewsets.ViewSet):
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'list'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(methods=['post'], detail=True)
|
2013-05-12 06:26:34 +04:00
|
|
|
def action1(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action1'})
|
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
@action(methods=['post', 'delete'], detail=True)
|
2013-05-12 06:26:34 +04:00
|
|
|
def action2(self, request, *args, **kwargs):
|
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
@action(methods=['post'], detail=True)
|
|
|
|
def action3(self, request, pk, *args, **kwargs):
|
|
|
|
return Response({'post': pk})
|
|
|
|
|
|
|
|
@action3.mapping.delete
|
|
|
|
def action3_delete(self, request, pk, *args, **kwargs):
|
|
|
|
return Response({'delete': pk})
|
2013-06-02 23:40:56 +04:00
|
|
|
|
2013-05-12 06:26:34 +04:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
class TestSimpleRouter(URLPatternsTestCase, TestCase):
|
|
|
|
router = SimpleRouter()
|
|
|
|
router.register('basics', BasicViewSet, base_name='basic')
|
2013-05-12 06:26:34 +04:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
urlpatterns = [
|
|
|
|
url(r'^api/', include(router.urls)),
|
|
|
|
]
|
2013-05-12 06:26:34 +04:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.router = SimpleRouter()
|
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
def test_action_routes(self):
|
|
|
|
# Get action routes (first two are list/detail)
|
|
|
|
routes = self.router.get_routes(BasicViewSet)[2:]
|
|
|
|
|
|
|
|
assert routes[0].url == '^{prefix}/{lookup}/action1{trailing_slash}$'
|
|
|
|
assert routes[0].mapping == {
|
|
|
|
'post': 'action1',
|
|
|
|
}
|
|
|
|
|
|
|
|
assert routes[1].url == '^{prefix}/{lookup}/action2{trailing_slash}$'
|
|
|
|
assert routes[1].mapping == {
|
|
|
|
'post': 'action2',
|
|
|
|
'delete': 'action2',
|
|
|
|
}
|
|
|
|
|
|
|
|
assert routes[2].url == '^{prefix}/{lookup}/action3{trailing_slash}$'
|
|
|
|
assert routes[2].mapping == {
|
|
|
|
'post': 'action3',
|
|
|
|
'delete': 'action3_delete',
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_multiple_action_handlers(self):
|
|
|
|
# Standard action
|
|
|
|
response = self.client.post(reverse('basic-action3', args=[1]))
|
|
|
|
assert response.data == {'post': '1'}
|
|
|
|
|
|
|
|
# Additional handler registered with MethodMapper
|
|
|
|
response = self.client.delete(reverse('basic-action3', args=[1]))
|
|
|
|
assert response.data == {'delete': '1'}
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
|
2018-01-02 13:14:25 +03:00
|
|
|
class TestRootView(URLPatternsTestCase, TestCase):
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^non-namespaced/', include(namespaced_router.urls)),
|
|
|
|
url(r'^namespaced/', include((namespaced_router.urls, 'namespaced'), namespace='namespaced')),
|
|
|
|
]
|
|
|
|
|
2014-12-28 15:02:52 +03:00
|
|
|
def test_retrieve_namespaced_root(self):
|
|
|
|
response = self.client.get('/namespaced/')
|
2017-01-19 19:00:07 +03:00
|
|
|
assert response.data == {"example": "http://testserver/namespaced/example/"}
|
2014-12-28 15:02:52 +03:00
|
|
|
|
|
|
|
def test_retrieve_non_namespaced_root(self):
|
|
|
|
response = self.client.get('/non-namespaced/')
|
2017-01-19 19:00:07 +03:00
|
|
|
assert response.data == {"example": "http://testserver/non-namespaced/example/"}
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
|
2018-01-02 13:14:25 +03:00
|
|
|
class TestCustomLookupFields(URLPatternsTestCase, TestCase):
|
2013-05-31 14:50:37 +04:00
|
|
|
"""
|
|
|
|
Ensure that custom lookup fields are correctly routed.
|
|
|
|
"""
|
2018-01-02 13:14:25 +03:00
|
|
|
urlpatterns = [
|
|
|
|
url(r'^example/', include(notes_router.urls)),
|
|
|
|
url(r'^example2/', include(kwarged_notes_router.urls)),
|
|
|
|
]
|
|
|
|
|
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')
|
2017-04-14 02:56:44 +03:00
|
|
|
RouterTestModel.objects.create(uuid='a b', text='baz qux')
|
2014-09-18 14:20:56 +04:00
|
|
|
|
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]
|
2017-10-16 12:31:13 +03:00
|
|
|
detail_url_pattern = get_regex_pattern(detail_route)
|
2017-01-19 19:00:07 +03:00
|
|
|
assert '<uuid>' in detail_url_pattern
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
def test_retrieve_lookup_field_list_view(self):
|
2014-12-28 15:02:52 +03:00
|
|
|
response = self.client.get('/example/notes/')
|
2017-04-14 02:56:44 +03:00
|
|
|
assert response.data == [
|
|
|
|
{"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"},
|
|
|
|
{"url": "http://testserver/example/notes/a%20b/", "uuid": "a b", "text": "baz qux"},
|
|
|
|
]
|
2013-05-31 14:50:37 +04:00
|
|
|
|
|
|
|
def test_retrieve_lookup_field_detail_view(self):
|
2014-12-28 15:02:52 +03:00
|
|
|
response = self.client.get('/example/notes/123/')
|
2017-01-19 19:00:07 +03:00
|
|
|
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"}
|
2013-05-31 14:50:37 +04:00
|
|
|
|
2017-04-14 02:56:44 +03:00
|
|
|
def test_retrieve_lookup_field_url_encoded_detail_view_(self):
|
|
|
|
response = self.client.get('/example/notes/a%20b/')
|
|
|
|
assert response.data == {"url": "http://testserver/example/notes/a%20b/", "uuid": "a b", "text": "baz qux"}
|
|
|
|
|
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)):
|
2017-10-16 12:31:13 +03:00
|
|
|
assert expected[idx] == get_regex_pattern(self.urls[idx])
|
2014-01-03 02:44:47 +04:00
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_routers')
|
2018-01-02 13:14:25 +03:00
|
|
|
class TestLookupUrlKwargs(URLPatternsTestCase, TestCase):
|
2015-03-13 03:07:20 +03:00
|
|
|
"""
|
|
|
|
Ensure the router honors lookup_url_kwarg.
|
|
|
|
|
|
|
|
Setup a deep lookup_field, but map it to a simple URL kwarg.
|
|
|
|
"""
|
2018-01-02 13:14:25 +03:00
|
|
|
urlpatterns = [
|
|
|
|
url(r'^example/', include(notes_router.urls)),
|
|
|
|
url(r'^example2/', include(kwarged_notes_router.urls)),
|
|
|
|
]
|
|
|
|
|
2015-03-13 03:07:20 +03:00
|
|
|
def setUp(self):
|
|
|
|
RouterTestModel.objects.create(uuid='123', text='foo bar')
|
|
|
|
|
|
|
|
def test_custom_lookup_url_kwarg_route(self):
|
|
|
|
detail_route = kwarged_notes_router.urls[-1]
|
2017-10-16 12:31:13 +03:00
|
|
|
detail_url_pattern = get_regex_pattern(detail_route)
|
2017-01-19 19:00:07 +03:00
|
|
|
assert '^notes/(?P<text>' in detail_url_pattern
|
2015-03-13 03:07:20 +03:00
|
|
|
|
|
|
|
def test_retrieve_lookup_url_kwarg_detail_view(self):
|
|
|
|
response = self.client.get('/example2/notes/fo/')
|
2017-01-19 19:00:07 +03:00
|
|
|
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"}
|
2015-03-13 03:07:20 +03:00
|
|
|
|
2017-04-14 02:56:44 +03:00
|
|
|
def test_retrieve_lookup_url_encoded_kwarg_detail_view(self):
|
|
|
|
response = self.client.get('/example2/notes/foo%20bar/')
|
|
|
|
assert response.data == {"url": "http://testserver/example/notes/123/", "uuid": "123", "text": "foo bar"}
|
|
|
|
|
2015-03-13 03:07:20 +03:00
|
|
|
|
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):
|
2015-01-29 19:28:03 +03:00
|
|
|
queryset = RouterTestModel.objects.all()
|
2013-06-04 23:59:12 +04:00
|
|
|
|
|
|
|
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)):
|
2017-10-16 12:31:13 +03:00
|
|
|
assert expected[idx] == get_regex_pattern(self.urls[idx])
|
2013-06-04 23:59:12 +04:00
|
|
|
|
|
|
|
|
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):
|
2015-01-29 19:28:03 +03:00
|
|
|
queryset = RouterTestModel.objects.all()
|
2013-06-04 23:59:12 +04:00
|
|
|
|
|
|
|
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)):
|
2017-10-16 12:31:13 +03:00
|
|
|
assert expected[idx] == get_regex_pattern(self.urls[idx])
|
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):
|
2015-01-29 19:28:03 +03:00
|
|
|
queryset = RouterTestModel.objects.all()
|
|
|
|
|
2013-06-08 06:49:18 +04:00
|
|
|
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'
|
2017-01-19 19:00:07 +03:00
|
|
|
assert expected == self.urls[-1].name
|
2013-06-08 06:49:18 +04:00
|
|
|
|
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 = []
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(methods=['post'], detail=True, 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()
|
2018-07-06 12:03:12 +03:00
|
|
|
self.router.register(r'test', TestViewSet, basename='test')
|
2013-06-22 01:03:07 +04:00
|
|
|
self.view = self.router.urls[-1].callback
|
|
|
|
|
|
|
|
def test_action_kwargs(self):
|
|
|
|
request = factory.post('/test/0/custom/')
|
|
|
|
response = self.view(request)
|
2017-01-19 19:00:07 +03:00
|
|
|
assert 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):
|
|
|
|
"""
|
2018-01-25 11:40:49 +03:00
|
|
|
Ensure `@action` 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):
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(methods=['post'], detail=True)
|
2013-06-27 02:00:42 +04:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
return Response({
|
|
|
|
'hello': 'world'
|
|
|
|
})
|
|
|
|
|
|
|
|
self.router = SimpleRouter()
|
2018-07-06 12:03:12 +03:00
|
|
|
self.router.register(r'test', TestViewSet, basename='test')
|
2013-06-27 02:00:42 +04:00
|
|
|
|
2017-01-19 19:00:07 +03:00
|
|
|
with pytest.raises(ImproperlyConfigured):
|
2013-06-27 02:00:42 +04:00
|
|
|
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'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(methods=['post'], detail=False)
|
2013-07-16 02:35:13 +04:00
|
|
|
def list_route_post(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'action1'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(methods=['post'], detail=True)
|
2013-07-16 02:35:13 +04:00
|
|
|
def detail_route_post(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'action2'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=False)
|
2013-07-16 02:35:13 +04:00
|
|
|
def list_route_get(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'link1'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=True)
|
2013-07-16 02:35:13 +04:00
|
|
|
def detail_route_get(self, request, *args, **kwargs):
|
2013-06-06 01:39:14 +04:00
|
|
|
return Response({'method': 'link2'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=False, 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'})
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=True, 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
|
|
|
|
2015-02-24 19:14:53 +03:00
|
|
|
class SubDynamicListAndDetailViewSet(DynamicListAndDetailViewSet):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2015-02-24 19:14:53 +03:00
|
|
|
def _test_list_and_detail_route_decorators(self, viewset):
|
|
|
|
routes = self.router.get_routes(viewset)
|
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_'):
|
2017-01-19 19:00:07 +03:00
|
|
|
assert route.url == '^{{prefix}}/{0}{{trailing_slash}}$'.format(url_path)
|
2013-06-06 01:39:14 +04:00
|
|
|
else:
|
2017-01-19 19:00:07 +03:00
|
|
|
assert route.url == '^{{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'
|
2017-01-19 19:00:07 +03:00
|
|
|
assert route.mapping[method_map] == method_name
|
2015-02-24 19:14:53 +03:00
|
|
|
|
|
|
|
def test_list_and_detail_route_decorators(self):
|
|
|
|
self._test_list_and_detail_route_decorators(DynamicListAndDetailViewSet)
|
|
|
|
|
|
|
|
def test_inherited_list_and_detail_route_decorators(self):
|
|
|
|
self._test_list_and_detail_route_decorators(SubDynamicListAndDetailViewSet)
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
|
2018-01-02 13:14:25 +03:00
|
|
|
class TestEmptyPrefix(URLPatternsTestCase, TestCase):
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^empty-prefix/', include(empty_prefix_router.urls)),
|
|
|
|
]
|
|
|
|
|
2016-10-10 15:03:46 +03:00
|
|
|
def test_empty_prefix_list(self):
|
|
|
|
response = self.client.get('/empty-prefix/')
|
2017-01-19 19:00:07 +03:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert json.loads(response.content.decode('utf-8')) == [{'uuid': '111', 'text': 'First'},
|
|
|
|
{'uuid': '222', 'text': 'Second'}]
|
2016-10-10 15:03:46 +03:00
|
|
|
|
|
|
|
def test_empty_prefix_detail(self):
|
|
|
|
response = self.client.get('/empty-prefix/1/')
|
2017-01-19 19:00:07 +03:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert json.loads(response.content.decode('utf-8')) == {'uuid': '111', 'text': 'First'}
|
2017-05-28 12:38:09 +03:00
|
|
|
|
|
|
|
|
2018-01-02 13:14:25 +03:00
|
|
|
class TestRegexUrlPath(URLPatternsTestCase, TestCase):
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^regex/', include(regex_url_path_router.urls)),
|
|
|
|
]
|
|
|
|
|
2017-05-28 12:38:09 +03:00
|
|
|
def test_regex_url_path_list(self):
|
|
|
|
kwarg = '1234'
|
|
|
|
response = self.client.get('/regex/list/{}/'.format(kwarg))
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert json.loads(response.content.decode('utf-8')) == {'kwarg': kwarg}
|
|
|
|
|
|
|
|
def test_regex_url_path_detail(self):
|
|
|
|
pk = '1'
|
|
|
|
kwarg = '1234'
|
|
|
|
response = self.client.get('/regex/{}/detail/{}/'.format(pk, kwarg))
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert json.loads(response.content.decode('utf-8')) == {'pk': pk, 'kwarg': kwarg}
|
2017-12-04 13:55:49 +03:00
|
|
|
|
|
|
|
|
2018-01-02 13:14:25 +03:00
|
|
|
class TestViewInitkwargs(URLPatternsTestCase, TestCase):
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^example/', include(notes_router.urls)),
|
|
|
|
]
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
def test_suffix(self):
|
|
|
|
match = resolve('/example/notes/')
|
|
|
|
initkwargs = match.func.initkwargs
|
|
|
|
|
|
|
|
assert initkwargs['suffix'] == 'List'
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
def test_detail(self):
|
|
|
|
match = resolve('/example/notes/')
|
|
|
|
initkwargs = match.func.initkwargs
|
|
|
|
|
|
|
|
assert not initkwargs['detail']
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
def test_basename(self):
|
|
|
|
match = resolve('/example/notes/')
|
|
|
|
initkwargs = match.func.initkwargs
|
|
|
|
|
|
|
|
assert initkwargs['basename'] == 'routertestmodel'
|
2018-07-06 12:03:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestBaseNameRename(TestCase):
|
|
|
|
|
|
|
|
def test_base_name_and_basename_assertion(self):
|
|
|
|
router = SimpleRouter()
|
|
|
|
|
|
|
|
msg = "Do not provide both the `basename` and `base_name` arguments."
|
|
|
|
with warnings.catch_warnings(record=True) as w, \
|
|
|
|
self.assertRaisesMessage(AssertionError, msg):
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
router.register('mock', MockViewSet, 'mock', base_name='mock')
|
|
|
|
|
|
|
|
msg = "The `base_name` argument has been deprecated in favor of `basename`."
|
|
|
|
assert len(w) == 1
|
|
|
|
assert str(w[0].message) == msg
|
|
|
|
|
|
|
|
def test_base_name_argument_deprecation(self):
|
|
|
|
router = SimpleRouter()
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
router.register('mock', MockViewSet, base_name='mock')
|
|
|
|
|
|
|
|
msg = "The `base_name` argument has been deprecated in favor of `basename`."
|
|
|
|
assert len(w) == 1
|
|
|
|
assert str(w[0].message) == msg
|
|
|
|
assert router.registry == [
|
|
|
|
('mock', MockViewSet, 'mock'),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_basename_argument_no_warnings(self):
|
|
|
|
router = SimpleRouter()
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
router.register('mock', MockViewSet, basename='mock')
|
|
|
|
|
|
|
|
assert len(w) == 0
|
|
|
|
assert router.registry == [
|
|
|
|
('mock', MockViewSet, 'mock'),
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_get_default_base_name_deprecation(self):
|
|
|
|
msg = "`CustomRouter.get_default_base_name` method should be renamed `get_default_basename`."
|
|
|
|
|
|
|
|
# Class definition should raise a warning
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
|
|
|
|
class CustomRouter(SimpleRouter):
|
|
|
|
def get_default_base_name(self, viewset):
|
|
|
|
return 'foo'
|
|
|
|
|
|
|
|
assert len(w) == 1
|
|
|
|
assert str(w[0].message) == msg
|
|
|
|
|
|
|
|
# Deprecated method implementation should still be called
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
|
|
|
|
router = CustomRouter()
|
|
|
|
router.register('mock', MockViewSet)
|
|
|
|
|
|
|
|
assert len(w) == 0
|
|
|
|
assert router.registry == [
|
|
|
|
('mock', MockViewSet, 'foo'),
|
|
|
|
]
|