How can I override my JWT auth dependency in the endponits in FastAPI testing? #1801
-
I managed to override my DB dependency for my testing in FastAPI but when I try to apply the same technique for overriding my JWT auth dependency I still get a 422 response with the following detail:
I followed all documentation and suggested posts on this topic of overriding dependencies for testing and still haven't found my issue. Does anyone know what's the problem here? conftest.py
test.py
schemas.py
router.py
dependencies.py
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
In case you did not find a solution yet. The problem might be, that the override Using a function without any arguments as override however should work: # conftest.py
...
@pytest.fixture()
def app_with_db_and_jwt(app_with_db):
def override_jwt_dependency():
return TokenData(id=1, email="[email protected]")
app_with_db.dependency_overrides[validate_access_token] = override_jwt_dependency
yield app_with_db
app_with_db.dependency_overrides.pop(validate_access_token) # remove override |
Beta Was this translation helpful? Give feedback.
In case you did not find a solution yet.
The problem might be, that the override
override_jwt_dependency
has one argumentfake_jwt
. This should be the same as adding it this argument in theme
function, resulting in an expected query parameterfake_jwt
. (.../api/me?fake_jwt=<some value>
).Using a function without any arguments as override however should work: