Skip to content

Commit 2662d52

Browse files
committed
fixed route auth parameter override
1 parent 3293fdc commit 2662d52

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

ninja_extra/controllers/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ def add_api_operation(
237237
url_name: Optional[str] = None,
238238
include_in_schema: bool = True,
239239
) -> Operation:
240+
auth = self.auth if auth == NOT_SET else auth
241+
240242
if self._prefix_has_route_param:
241243
path = normalize_path("/".join([i for i in (self.prefix, path) if i]))
242244
if path not in self._path_operations:
@@ -248,7 +250,7 @@ def add_api_operation(
248250
path=path,
249251
methods=methods,
250252
view_func=view_func,
251-
auth=auth or self.auth,
253+
auth=auth,
252254
response=response,
253255
operation_id=operation_id,
254256
summary=summary,

tests/test_route.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,20 @@
3434
anonymous_request.user = AnonymousUser()
3535

3636

37+
class FakeAuth:
38+
def authenticate(self, **kwargs):
39+
return True
40+
41+
3742
@api_controller(
38-
"permission", permissions=[permissions.IsAuthenticated & permissions.IsAdminUser]
43+
"permission", auth=FakeAuth(), permissions=[permissions.IsAuthenticated & permissions.IsAdminUser]
3944
)
4045
class PermissionController:
41-
@http_post("/example_post")
46+
@http_post("/example_post", auth=None)
4247
def example(self):
4348
return {"message": "OK"}
4449

45-
@http_get("/example_get", permissions=[permissions.AllowAny])
50+
@http_get("/example_get", auth=None, permissions=[permissions.AllowAny])
4651
def example_allow_any(self):
4752
return {"message": "OK"}
4853

@@ -309,6 +314,13 @@ def get_real_user_request(cls):
309314
_request.user = user
310315
return _request
311316

317+
def test_permission_controller_example_allow_any_auth_is_none(self):
318+
route_params = PermissionController.example_allow_any.route.route_params
319+
assert route_params.auth is None
320+
321+
response = PermissionController.example_allow_any(anonymous_request)
322+
assert response == {"message": "OK"}
323+
312324
def test_route_is_protected_by_global_controller_permission(self):
313325
with pytest.raises(PermissionDenied) as pex:
314326
PermissionController.example(anonymous_request)
@@ -321,7 +333,7 @@ def test_route_protected_by_global_controller_permission_works(self):
321333
response = PermissionController.example(request)
322334
assert response == {"message": "OK"}
323335

324-
def test_route_is_protected_by_its_permissions(self):
336+
def test_route_is_protected_by_its_permissions_paramater(self):
325337
response = PermissionController.example_allow_any(anonymous_request)
326338
assert response == {"message": "OK"}
327339

0 commit comments

Comments
 (0)