Replies: 5 comments 4 replies
-
It's now supported in a recent version 🎉 Here are the new docs: Edit: new link: https://fastapi.tiangolo.com/advanced/testing-dependencies/ Make sure to update your FastAPI version before trying it. |
Beta Was this translation helpful? Give feedback.
-
I think this should solve your use case, so I'll close this issue now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
The documentation link has changed to: https://fastapi.tiangolo.com/advanced/testing-dependencies/ |
Beta Was this translation helpful? Give feedback.
-
Hey @tiangolo ! minimal project 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"]) And lastly, 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 == [] 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! |
Beta Was this translation helpful? Give feedback.
-
In my case, the mapping of class override is already set. However, it still call the original class. Overwrite dependencyStill invoke the original class |
Beta Was this translation helpful? Give feedback.
-
Description
I'm not sure here is the best place for this question as it's more related to testing and
unittest
rather than FastAPI. However, maybe one of you had the same problem and can recommend a solution.I want to write tests for my api endpoints which have security dependency at the
include_router
level.Below I highlight the important parts of the important files.
The security function gets the api key from header and checks it against the database; it returns the client or raise a 401 http exception.
How can I patch the
get_client
function for my tests in order to have a certain api key for my requests? My tests consist in calling an endpoint usingstarlette.testclient.TestClient
with the test api key in headers.Of course, in this form, my test fails because 'my-test-api-key' key is not in db. And I don't want to be. What I want is to replace
get_client
withfake_get_client
at runtime to pass the test.I considered patching the
get_client
function likebut this won't work.
Is it possible to patch my
get_client
easily in my test, or should I choose another approach?Beta Was this translation helpful? Give feedback.
All reactions