mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 08:14:16 +03:00
No auth view failing permission should raise 403
A view with no `authentication_classes` set and that fails a permission check should raise a 403 with the message from the failing permission.
This commit is contained in:
parent
6a29196712
commit
78e4ea0d6e
|
@ -162,7 +162,7 @@ class APIView(View):
|
|||
"""
|
||||
If request is not permitted, determine what kind of exception to raise.
|
||||
"""
|
||||
if not request.successful_authenticator:
|
||||
if request.authenticators and not request.successful_authenticator:
|
||||
raise exceptions.NotAuthenticated()
|
||||
raise exceptions.PermissionDenied(detail=message)
|
||||
|
||||
|
|
|
@ -321,3 +321,28 @@ class FailingAuthAccessedInRenderer(TestCase):
|
|||
response = self.view(request)
|
||||
content = response.render().content
|
||||
self.assertEqual(content, b'not authenticated')
|
||||
|
||||
|
||||
class NoAuthenticationClassesTests(TestCase):
|
||||
def test_permission_message_with_no_authentication_classes(self):
|
||||
"""
|
||||
An unauthenticated request made against a view that containes no
|
||||
`authentication_classes` but do contain `permissions_classes` the error
|
||||
code returned should be 403 with the exception's message.
|
||||
"""
|
||||
|
||||
class DummyPermission(permissions.BasePermission):
|
||||
message = 'Dummy permission message'
|
||||
|
||||
def has_permission(self, request, view):
|
||||
return False
|
||||
|
||||
request = factory.get('/')
|
||||
view = MockView.as_view(
|
||||
authentication_classes=(),
|
||||
permission_classes=(DummyPermission,),
|
||||
)
|
||||
response = view(request)
|
||||
self.assertEqual(response.status_code,
|
||||
status.HTTP_403_FORBIDDEN)
|
||||
self.assertEqual(response.data, {'detail': 'Dummy permission message'})
|
||||
|
|
Loading…
Reference in New Issue
Block a user