Can't get dependency_overrides to work #6515
-
First Check
Commit to Help
Example CodeProject structure (with only relevant files):
from typing import List, Optional
from fastapi import APIRouter, Depends
from frontegg.fastapi.secure_access import FronteggSecurity, User
router = APIRouter()
@router.get("/bar", response_model=Optional[List[schemas.Foo]])
def list_bar(user: User = Depends(FronteggSecurity(permissions=["foo"]))):
logger.debug(f"Authenticated user: {user}")
return []
from fastapi import FastAPI
from app.routers import foo_router
app = FastAPI()
app.include_router(foo_router.router, prefix="/foo", tags=["Foo"])
from fastapi.testclient import TestClient
from frontegg.fastapi.secure_access import FronteggSecurity, User
from app.main import app
def override_frontegg():
return User(
sub="user1",
tenantId="tenant1",
type="userToken",
access_token="abc123",
)
app.dependency_overrides[FronteggSecurity] = override_frontegg
def test_list_bar(client: TestClient) -> None:
r = client.get("/foo/bar")
response = r.json()
assert r.status_code == 200
assert response == [] DescriptionHey there! I've attached the relevant project structure and related files in the However, when I run my tests, I get a 401 response, which means that the override isn't working as expected. It actually makes the call to my external auth provider (FrontEgg) although it shouldn't, based on the override that I've set. Am I missing something here? Thanks! Operating SystemmacOS Operating System DetailsmacOS Monterey 12.2 running on Apple MacBook Pro 13" with M1 chip. FastAPI Version0.73.0 Python VersionPython 3.9.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You cannot override dependencies that receive parameters. Well, you cannot override any dependency (object) that will have a different key value in a dictionary if you recreate it. What you can do is: x = FronteggSecurity(permissions=["foo"]) Then you use |
Beta Was this translation helpful? Give feedback.
-
@Kludex that worked for me, thanks! |
Beta Was this translation helpful? Give feedback.
You cannot override dependencies that receive parameters. Well, you cannot override any dependency (object) that will have a different key value in a dictionary if you recreate it.
What you can do is:
Then you use
x
as dependency e.g.Depends(x)
, and you can override it as well withapp.dependency_overrides[x] = y
.