Skip to content

Commit 78e4ea0

Browse files
johnraztomchristie
authored andcommitted
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.
1 parent 6a29196 commit 78e4ea0

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

rest_framework/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def permission_denied(self, request, message=None):
162162
"""
163163
If request is not permitted, determine what kind of exception to raise.
164164
"""
165-
if not request.successful_authenticator:
165+
if request.authenticators and not request.successful_authenticator:
166166
raise exceptions.NotAuthenticated()
167167
raise exceptions.PermissionDenied(detail=message)
168168

tests/test_authentication.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,28 @@ def test_failing_auth_accessed_in_renderer(self):
321321
response = self.view(request)
322322
content = response.render().content
323323
self.assertEqual(content, b'not authenticated')
324+
325+
326+
class NoAuthenticationClassesTests(TestCase):
327+
def test_permission_message_with_no_authentication_classes(self):
328+
"""
329+
An unauthenticated request made against a view that containes no
330+
`authentication_classes` but do contain `permissions_classes` the error
331+
code returned should be 403 with the exception's message.
332+
"""
333+
334+
class DummyPermission(permissions.BasePermission):
335+
message = 'Dummy permission message'
336+
337+
def has_permission(self, request, view):
338+
return False
339+
340+
request = factory.get('/')
341+
view = MockView.as_view(
342+
authentication_classes=(),
343+
permission_classes=(DummyPermission,),
344+
)
345+
response = view(request)
346+
self.assertEqual(response.status_code,
347+
status.HTTP_403_FORBIDDEN)
348+
self.assertEqual(response.data, {'detail': 'Dummy permission message'})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy