Skip to content

Commit

Permalink
Merge pull request #2175 from BrickXu/fix_2171
Browse files Browse the repository at this point in the history
Not allow to pass an empty actions to viewset.as_view()
  • Loading branch information
tomchristie committed Dec 3, 2014
2 parents 71a8cb2 + 53f5276 commit c5a2d50
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rest_framework/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def as_view(cls, actions=None, **initkwargs):
# eg. 'List' or 'Instance'.
cls.suffix = None

# actions must not be empty
if not actions:
raise TypeError("The `actions` argument must be provided when "
"calling `.as_view()` on a ViewSet. For example "
"`.as_view({'get': 'list'})`")

# sanitize keyword arguments
for key in initkwargs:
if key in cls.http_method_names:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_viewsets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.test import TestCase
from rest_framework import status
from rest_framework.response import Response
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'})


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)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'ACTION': 'LIST'})

def test_initialize_view_set_with_empty_actions(self):
try:
BasicViewSet.as_view()
except TypeError as e:
self.assertEqual(str(e), "The `actions` argument must be provided "
"when calling `.as_view()` on a ViewSet. "
"For example `.as_view({'get': 'list'})`")
else:
self.fail("actions must not be empty.")

0 comments on commit c5a2d50

Please sign in to comment.