2024-09-07 14:21:18 +03:00
|
|
|
import unittest
|
2020-08-06 07:29:47 +03:00
|
|
|
from functools import wraps
|
2018-07-06 11:33:10 +03:00
|
|
|
|
2018-05-08 15:28:46 +03:00
|
|
|
import pytest
|
2024-09-07 14:21:18 +03:00
|
|
|
from django import VERSION as DJANGO_VERSION
|
2017-12-04 13:55:49 +03:00
|
|
|
from django.db import models
|
|
|
|
from django.test import TestCase, override_settings
|
2020-09-08 17:32:27 +03:00
|
|
|
from django.urls import include, path
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2014-12-02 07:55:34 +03:00
|
|
|
from rest_framework import status
|
2018-01-25 11:40:49 +03:00
|
|
|
from rest_framework.decorators import action
|
2014-12-02 07:55:34 +03:00
|
|
|
from rest_framework.response import Response
|
2017-12-04 13:55:49 +03:00
|
|
|
from rest_framework.routers import SimpleRouter
|
2014-12-02 07:55:34 +03:00
|
|
|
from rest_framework.test import APIRequestFactory
|
|
|
|
from rest_framework.viewsets import GenericViewSet
|
|
|
|
|
|
|
|
factory = APIRequestFactory()
|
|
|
|
|
|
|
|
|
|
|
|
class BasicViewSet(GenericViewSet):
|
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
return Response({'ACTION': 'LIST'})
|
|
|
|
|
|
|
|
|
2017-06-22 16:22:17 +03:00
|
|
|
class InstanceViewSet(GenericViewSet):
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
return self.dummy(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def dummy(self, request, *args, **kwargs):
|
|
|
|
return Response({'view': self})
|
|
|
|
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
class Action(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-08-06 07:29:47 +03:00
|
|
|
def decorate(fn):
|
|
|
|
@wraps(fn)
|
|
|
|
def wrapper(self, request, *args, **kwargs):
|
|
|
|
return fn(self, request, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
class ActionViewSet(GenericViewSet):
|
|
|
|
queryset = Action.objects.all()
|
|
|
|
|
|
|
|
def list(self, request, *args, **kwargs):
|
2020-03-09 12:43:02 +03:00
|
|
|
response = Response()
|
|
|
|
response.view = self
|
|
|
|
return response
|
2017-12-04 13:55:49 +03:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
2020-04-29 13:21:42 +03:00
|
|
|
response = Response()
|
|
|
|
response.view = self
|
|
|
|
return response
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=False)
|
2017-12-04 13:55:49 +03:00
|
|
|
def list_action(self, request, *args, **kwargs):
|
2020-03-09 12:43:02 +03:00
|
|
|
response = Response()
|
|
|
|
response.view = self
|
|
|
|
return response
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=False, url_name='list-custom')
|
2017-12-04 13:55:49 +03:00
|
|
|
def custom_list_action(self, request, *args, **kwargs):
|
2018-06-25 00:56:31 +03:00
|
|
|
raise NotImplementedError
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=True)
|
2017-12-04 13:55:49 +03:00
|
|
|
def detail_action(self, request, *args, **kwargs):
|
2018-06-25 00:56:31 +03:00
|
|
|
raise NotImplementedError
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
@action(detail=True, url_name='detail-custom')
|
2017-12-04 13:55:49 +03:00
|
|
|
def custom_detail_action(self, request, *args, **kwargs):
|
2018-06-25 00:56:31 +03:00
|
|
|
raise NotImplementedError
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
@action(detail=True, url_path=r'unresolvable/(?P<arg>\w+)', url_name='unresolvable')
|
|
|
|
def unresolvable_detail_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2020-08-06 07:29:47 +03:00
|
|
|
@action(detail=False)
|
|
|
|
@decorate
|
|
|
|
def wrapped_list_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@action(detail=True)
|
|
|
|
@decorate
|
|
|
|
def wrapped_detail_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-10-02 17:22:21 +03:00
|
|
|
class ActionNamesViewSet(GenericViewSet):
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
2020-04-29 13:21:42 +03:00
|
|
|
response = Response()
|
|
|
|
response.view = self
|
|
|
|
return response
|
2018-10-02 17:22:21 +03:00
|
|
|
|
|
|
|
@action(detail=True)
|
|
|
|
def unnamed_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@action(detail=True, name='Custom Name')
|
|
|
|
def named_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@action(detail=True, suffix='Custom Suffix')
|
|
|
|
def suffixed_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2020-03-05 13:18:22 +03:00
|
|
|
class ThingWithMapping:
|
|
|
|
def __init__(self):
|
|
|
|
self.mapping = {}
|
|
|
|
|
|
|
|
|
|
|
|
class ActionViewSetWithMapping(ActionViewSet):
|
|
|
|
mapper = ThingWithMapping()
|
|
|
|
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
router = SimpleRouter()
|
|
|
|
router.register(r'actions', ActionViewSet)
|
2018-07-06 12:03:12 +03:00
|
|
|
router.register(r'actions-alt', ActionViewSet, basename='actions-alt')
|
2018-10-02 17:22:21 +03:00
|
|
|
router.register(r'names', ActionNamesViewSet, basename='names')
|
2020-03-05 13:18:22 +03:00
|
|
|
router.register(r'mapping', ActionViewSetWithMapping, basename='mapping')
|
2017-12-04 13:55:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
urlpatterns = [
|
2020-09-08 17:32:27 +03:00
|
|
|
path('api/', include(router.urls)),
|
2017-12-04 13:55:49 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2014-12-02 07:55:34 +03:00
|
|
|
class InitializeViewSetsTestCase(TestCase):
|
|
|
|
def test_initialize_view_set_with_actions(self):
|
|
|
|
request = factory.get('/', '', content_type='application/json')
|
|
|
|
my_view = BasicViewSet.as_view(actions={
|
|
|
|
'get': 'list',
|
|
|
|
})
|
|
|
|
|
|
|
|
response = my_view(request)
|
2016-11-23 16:17:00 +03:00
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
assert response.data == {'ACTION': 'LIST'}
|
2014-12-02 07:55:34 +03:00
|
|
|
|
2020-03-05 16:18:48 +03:00
|
|
|
def test_head_request_against_viewset(self):
|
2017-03-13 15:51:03 +03:00
|
|
|
request = factory.head('/', '', content_type='application/json')
|
|
|
|
my_view = BasicViewSet.as_view(actions={
|
|
|
|
'get': 'list',
|
|
|
|
})
|
|
|
|
|
|
|
|
response = my_view(request)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
2014-12-02 07:55:34 +03:00
|
|
|
def test_initialize_view_set_with_empty_actions(self):
|
2018-05-08 15:28:46 +03:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2014-12-02 07:55:34 +03:00
|
|
|
BasicViewSet.as_view()
|
2018-05-08 15:28:46 +03:00
|
|
|
|
|
|
|
assert str(excinfo.value) == (
|
|
|
|
"The `actions` argument must be provided "
|
|
|
|
"when calling `.as_view()` on a ViewSet. "
|
|
|
|
"For example `.as_view({'get': 'list'})`")
|
2017-06-22 16:22:17 +03:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
def test_initialize_view_set_with_both_name_and_suffix(self):
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
|
|
BasicViewSet.as_view(name='', suffix='', actions={
|
|
|
|
'get': 'list',
|
|
|
|
})
|
|
|
|
|
|
|
|
assert str(excinfo.value) == (
|
|
|
|
"BasicViewSet() received both `name` and `suffix`, "
|
|
|
|
"which are mutually exclusive arguments.")
|
|
|
|
|
2017-06-22 16:22:17 +03:00
|
|
|
def test_args_kwargs_request_action_map_on_self(self):
|
|
|
|
"""
|
|
|
|
Test a view only has args, kwargs, request, action_map
|
|
|
|
once `as_view` has been called.
|
|
|
|
"""
|
|
|
|
bare_view = InstanceViewSet()
|
|
|
|
view = InstanceViewSet.as_view(actions={
|
|
|
|
'get': 'dummy',
|
|
|
|
})(factory.get('/')).data['view']
|
|
|
|
|
|
|
|
for attribute in ('args', 'kwargs', 'request', 'action_map'):
|
|
|
|
self.assertNotIn(attribute, dir(bare_view))
|
|
|
|
self.assertIn(attribute, dir(view))
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2020-03-09 12:43:02 +03:00
|
|
|
def test_viewset_action_attr(self):
|
|
|
|
view = ActionViewSet.as_view(actions={'get': 'list'})
|
|
|
|
|
|
|
|
get = view(factory.get('/'))
|
|
|
|
head = view(factory.head('/'))
|
|
|
|
assert get.view.action == 'list'
|
|
|
|
assert head.view.action == 'list'
|
|
|
|
|
|
|
|
def test_viewset_action_attr_for_extra_action(self):
|
|
|
|
view = ActionViewSet.as_view(actions=dict(ActionViewSet.list_action.mapping))
|
|
|
|
|
|
|
|
get = view(factory.get('/'))
|
|
|
|
head = view(factory.head('/'))
|
|
|
|
assert get.view.action == 'list_action'
|
|
|
|
assert head.view.action == 'list_action'
|
|
|
|
|
2024-09-07 14:21:18 +03:00
|
|
|
@unittest.skipUnless(DJANGO_VERSION >= (5, 1), 'Only for Django 5.1+')
|
|
|
|
def test_login_required_middleware_compat(self):
|
|
|
|
view = ActionViewSet.as_view(actions={'get': 'list'})
|
|
|
|
assert view.login_required is False
|
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
class GetExtraActionsTests(TestCase):
|
2018-01-25 11:40:49 +03:00
|
|
|
|
|
|
|
def test_extra_actions(self):
|
|
|
|
view = ActionViewSet()
|
|
|
|
actual = [action.__name__ for action in view.get_extra_actions()]
|
2018-07-06 11:33:10 +03:00
|
|
|
expected = [
|
|
|
|
'custom_detail_action',
|
|
|
|
'custom_list_action',
|
|
|
|
'detail_action',
|
|
|
|
'list_action',
|
|
|
|
'unresolvable_detail_action',
|
2020-08-06 07:29:47 +03:00
|
|
|
'wrapped_detail_action',
|
|
|
|
'wrapped_list_action',
|
2018-07-06 11:33:10 +03:00
|
|
|
]
|
2018-01-25 11:40:49 +03:00
|
|
|
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
2020-03-05 13:18:22 +03:00
|
|
|
def test_should_only_return_decorated_methods(self):
|
|
|
|
view = ActionViewSetWithMapping()
|
|
|
|
actual = [action.__name__ for action in view.get_extra_actions()]
|
|
|
|
expected = [
|
|
|
|
'custom_detail_action',
|
|
|
|
'custom_list_action',
|
|
|
|
'detail_action',
|
|
|
|
'list_action',
|
|
|
|
'unresolvable_detail_action',
|
2020-08-06 07:29:47 +03:00
|
|
|
'wrapped_detail_action',
|
|
|
|
'wrapped_list_action',
|
2020-03-05 13:18:22 +03:00
|
|
|
]
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
2020-08-06 07:29:47 +03:00
|
|
|
def test_attr_name_check(self):
|
|
|
|
def decorate(fn):
|
|
|
|
def wrapper(self, request, *args, **kwargs):
|
|
|
|
return fn(self, request, *args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
class ActionViewSet(GenericViewSet):
|
|
|
|
queryset = Action.objects.all()
|
|
|
|
|
|
|
|
@action(detail=False)
|
|
|
|
@decorate
|
|
|
|
def wrapped_list_action(self, request, *args, **kwargs):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
view = ActionViewSet()
|
|
|
|
with pytest.raises(AssertionError) as excinfo:
|
|
|
|
view.get_extra_actions()
|
|
|
|
|
|
|
|
assert str(excinfo.value) == (
|
|
|
|
'Expected function (`wrapper`) to match its attribute name '
|
|
|
|
'(`wrapped_list_action`). If using a decorator, ensure the inner '
|
|
|
|
'function is decorated with `functools.wraps`, or that '
|
|
|
|
'`wrapper.__name__` is otherwise set to `wrapped_list_action`.')
|
|
|
|
|
2018-01-25 11:40:49 +03:00
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_viewsets')
|
|
|
|
class GetExtraActionUrlMapTests(TestCase):
|
|
|
|
|
|
|
|
def test_list_view(self):
|
|
|
|
response = self.client.get('/api/actions/')
|
2020-04-29 13:21:42 +03:00
|
|
|
view = response.view
|
2018-07-06 11:33:10 +03:00
|
|
|
|
2023-04-30 12:20:02 +03:00
|
|
|
expected = {
|
|
|
|
'Custom list action': 'http://testserver/api/actions/custom_list_action/',
|
|
|
|
'List action': 'http://testserver/api/actions/list_action/',
|
|
|
|
'Wrapped list action': 'http://testserver/api/actions/wrapped_list_action/',
|
|
|
|
}
|
2018-07-06 11:33:10 +03:00
|
|
|
|
|
|
|
self.assertEqual(view.get_extra_action_url_map(), expected)
|
|
|
|
|
|
|
|
def test_detail_view(self):
|
|
|
|
response = self.client.get('/api/actions/1/')
|
2020-04-29 13:21:42 +03:00
|
|
|
view = response.view
|
2018-07-06 11:33:10 +03:00
|
|
|
|
2023-04-30 12:20:02 +03:00
|
|
|
expected = {
|
|
|
|
'Custom detail action': 'http://testserver/api/actions/1/custom_detail_action/',
|
|
|
|
'Detail action': 'http://testserver/api/actions/1/detail_action/',
|
|
|
|
'Wrapped detail action': 'http://testserver/api/actions/1/wrapped_detail_action/',
|
2018-07-06 11:33:10 +03:00
|
|
|
# "Unresolvable detail action" excluded, since it's not resolvable
|
2023-04-30 12:20:02 +03:00
|
|
|
}
|
2018-07-06 11:33:10 +03:00
|
|
|
|
|
|
|
self.assertEqual(view.get_extra_action_url_map(), expected)
|
|
|
|
|
|
|
|
def test_uninitialized_view(self):
|
2023-04-30 12:20:02 +03:00
|
|
|
self.assertEqual(ActionViewSet().get_extra_action_url_map(), {})
|
2018-07-06 11:33:10 +03:00
|
|
|
|
2018-10-02 17:22:21 +03:00
|
|
|
def test_action_names(self):
|
|
|
|
# Action 'name' and 'suffix' kwargs should be respected
|
|
|
|
response = self.client.get('/api/names/1/')
|
2020-04-29 13:21:42 +03:00
|
|
|
view = response.view
|
2018-10-02 17:22:21 +03:00
|
|
|
|
2023-04-30 12:20:02 +03:00
|
|
|
expected = {
|
|
|
|
'Custom Name': 'http://testserver/api/names/1/named_action/',
|
|
|
|
'Action Names Custom Suffix': 'http://testserver/api/names/1/suffixed_action/',
|
|
|
|
'Unnamed action': 'http://testserver/api/names/1/unnamed_action/',
|
|
|
|
}
|
2018-10-02 17:22:21 +03:00
|
|
|
|
|
|
|
self.assertEqual(view.get_extra_action_url_map(), expected)
|
|
|
|
|
2018-07-06 11:33:10 +03:00
|
|
|
|
2017-12-04 13:55:49 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_viewsets')
|
|
|
|
class ReverseActionTests(TestCase):
|
|
|
|
def test_default_basename(self):
|
|
|
|
view = ActionViewSet()
|
2018-07-06 12:03:12 +03:00
|
|
|
view.basename = router.get_default_basename(ActionViewSet)
|
2017-12-04 13:55:49 +03:00
|
|
|
view.request = None
|
|
|
|
|
|
|
|
assert view.reverse_action('list') == '/api/actions/'
|
|
|
|
assert view.reverse_action('list-action') == '/api/actions/list_action/'
|
|
|
|
assert view.reverse_action('list-custom') == '/api/actions/custom_list_action/'
|
|
|
|
|
|
|
|
assert view.reverse_action('detail', args=['1']) == '/api/actions/1/'
|
|
|
|
assert view.reverse_action('detail-action', args=['1']) == '/api/actions/1/detail_action/'
|
|
|
|
assert view.reverse_action('detail-custom', args=['1']) == '/api/actions/1/custom_detail_action/'
|
|
|
|
|
|
|
|
def test_custom_basename(self):
|
|
|
|
view = ActionViewSet()
|
|
|
|
view.basename = 'actions-alt'
|
|
|
|
view.request = None
|
|
|
|
|
|
|
|
assert view.reverse_action('list') == '/api/actions-alt/'
|
|
|
|
assert view.reverse_action('list-action') == '/api/actions-alt/list_action/'
|
|
|
|
assert view.reverse_action('list-custom') == '/api/actions-alt/custom_list_action/'
|
|
|
|
|
|
|
|
assert view.reverse_action('detail', args=['1']) == '/api/actions-alt/1/'
|
|
|
|
assert view.reverse_action('detail-action', args=['1']) == '/api/actions-alt/1/detail_action/'
|
|
|
|
assert view.reverse_action('detail-custom', args=['1']) == '/api/actions-alt/1/custom_detail_action/'
|
|
|
|
|
|
|
|
def test_request_passing(self):
|
|
|
|
view = ActionViewSet()
|
2018-07-06 12:03:12 +03:00
|
|
|
view.basename = router.get_default_basename(ActionViewSet)
|
2017-12-04 13:55:49 +03:00
|
|
|
view.request = factory.get('/')
|
|
|
|
|
|
|
|
# Passing the view's request object should result in an absolute URL.
|
|
|
|
assert view.reverse_action('list') == 'http://testserver/api/actions/'
|
|
|
|
|
|
|
|
# Users should be able to explicitly not pass the view's request.
|
|
|
|
assert view.reverse_action('list', request=None) == '/api/actions/'
|