Skip to content

Commit 836e3c7

Browse files
committed
fix action tests
1 parent 8778afe commit 836e3c7

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

nest/core/decorators/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def add_route_to_router(
156156
if guards:
157157
dependencies = route_kwargs.get("dependencies", [])
158158
for guard in guards:
159-
dependencies.append(Depends(guard.as_dependency()))
159+
dependencies.append(guard.as_dependency())
160160
route_kwargs["dependencies"] = dependencies
161161

162162
router.add_api_route(**route_kwargs)

nest/core/decorators/guards.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import Request, HTTPException, status, Security
1+
from fastapi import Request, HTTPException, status, Security, Depends
22
from fastapi.security.base import SecurityBase
33
from typing import Optional
44
import inspect
@@ -266,11 +266,10 @@ def as_dependency(cls):
266266
**Internal Implementation Details:**
267267
268268
- If no security_scheme: Creates a simple dependency that validates the request
269-
- If security_scheme exists: Creates a dependency that uses FastAPI's Security()
270-
to extract credentials and pass them to the guard
269+
- If security_scheme exists: Creates a dependency with Security parameter for OpenAPI
271270
272271
The returned dependency will:
273-
- Appear in OpenAPI schema (if security_scheme is set)
272+
- Appear in OpenAPI schema (if security_scheme is set)
274273
- Extract credentials automatically (if security_scheme is set)
275274
- Execute guard logic and raise 403 on failure
276275
"""
@@ -280,16 +279,20 @@ async def dependency(request: Request):
280279
guard = cls()
281280
await guard(request)
282281

283-
return dependency
282+
return Depends(dependency)
284283

285-
# Security scheme configured - use FastAPI Security dependency
284+
# Security scheme configured - create function with Security parameter
285+
# This allows FastAPI to detect the security requirement for OpenAPI
286286
security_scheme = cls.security_scheme
287287

288-
async def dependency(request: Request, credentials=Security(security_scheme)):
288+
async def security_dependency(
289+
request: Request,
290+
credentials=Security(security_scheme)
291+
):
289292
guard = cls()
290293
await guard(request, credentials)
291294

292-
return dependency
295+
return Depends(security_dependency)
293296

294297

295298
def UseGuards(*guards):

tests/test_core/test_decorators/test_guard.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ def can_activate(self, request: Request, credentials) -> bool:
2323
return True
2424

2525

26+
class JWTGuard(BaseGuard):
27+
security_scheme = HTTPBearer()
28+
29+
def can_activate(self, request: Request, credentials=None) -> bool:
30+
if credentials and credentials.scheme == "Bearer":
31+
return self.validate_jwt(credentials.credentials)
32+
return False
33+
34+
2635
@Controller("/guard")
2736
class GuardController:
2837
@Get("/")
@@ -44,6 +53,18 @@ def test_guard_added_to_route_dependencies():
4453
assert callable(deps[0].dependency)
4554

4655

56+
def _has_security_requirements(dependant):
57+
"""Recursively check if a dependant or its dependencies have security requirements."""
58+
if dependant.security_requirements:
59+
return True
60+
61+
for dep in dependant.dependencies:
62+
if _has_security_requirements(dep):
63+
return True
64+
65+
return False
66+
67+
4768
def test_openapi_security_requirement():
4869
@Controller("/bearer")
4970
class BearerController:
@@ -54,4 +75,7 @@ def root(self):
5475

5576
router = BearerController.get_router()
5677
route = router.routes[0]
57-
assert route.dependant.security_requirements
78+
79+
# Check if security requirements exist anywhere in the dependency tree
80+
assert _has_security_requirements(route.dependant), \
81+
"Security requirements should be present in the dependency tree for OpenAPI integration"

0 commit comments

Comments
 (0)