♻️(backend) rework permission to better align with DRF responsibilities
If a viewset action is not implemented, the permission layer no longer returns a 403. Instead, it lets DRF handle the request and return the appropriate 405 Method Not Allowed response, ensuring cleaner and more standard API error handling.
This commit is contained in:
committed by
aleb_the_flash
parent
5d6ad3f3f6
commit
3887255e9c
@@ -33,12 +33,11 @@ class BaseScopePermission(permissions.BasePermission):
|
|||||||
Raises:
|
Raises:
|
||||||
PermissionDenied: If required scope is missing from token
|
PermissionDenied: If required scope is missing from token
|
||||||
"""
|
"""
|
||||||
# Get the current action (e.g., 'list', 'create')
|
# Get the current action (e.g., 'list', 'create'), if None let DRF handle it
|
||||||
action = getattr(view, "action", None)
|
action = getattr(view, "action", None)
|
||||||
if not action:
|
if not action:
|
||||||
raise exceptions.PermissionDenied(
|
# DRF routers return a 405 for unsupported methods
|
||||||
"Insufficient permissions. Unknown action."
|
return True
|
||||||
)
|
|
||||||
|
|
||||||
required_scope = self.scope_map.get(action)
|
required_scope = self.scope_map.get(action)
|
||||||
if not required_scope:
|
if not required_scope:
|
||||||
|
|||||||
@@ -611,15 +611,15 @@ def test_api_rooms_unknown_actions(settings):
|
|||||||
client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
|
client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
|
||||||
response = client.delete(f"/external-api/v1.0/rooms/{room.id}/")
|
response = client.delete(f"/external-api/v1.0/rooms/{room.id}/")
|
||||||
|
|
||||||
assert response.status_code == 403
|
assert response.status_code == 405
|
||||||
assert "insufficient permissions. unknown action." in str(response.data).lower()
|
assert 'method "delete" not allowed.' in str(response.data).lower()
|
||||||
|
|
||||||
client = APIClient()
|
client = APIClient()
|
||||||
client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
|
client.credentials(HTTP_AUTHORIZATION=f"Bearer {token}")
|
||||||
response = client.patch(f"/external-api/v1.0/rooms/{room.id}/")
|
response = client.patch(f"/external-api/v1.0/rooms/{room.id}/")
|
||||||
|
|
||||||
assert response.status_code == 403
|
assert response.status_code == 405
|
||||||
assert "insufficient permissions. unknown action." in str(response.data).lower()
|
assert 'method "patch" not allowed.' in str(response.data).lower()
|
||||||
|
|
||||||
|
|
||||||
def test_api_rooms_response_no_url(settings):
|
def test_api_rooms_response_no_url(settings):
|
||||||
|
|||||||
Reference in New Issue
Block a user