You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
Here are the involved codes:
conftest.py
def init_test_db(_app):
engine = create_engine(
settings.sql_alchemy_database_url,
connect_args={"check_same_thread": False},
)
def override_get_session():
with Session(engine, autoflush=True) as session:
with session.begin():
yield session
def override_get_session_no_transaction():
with Session(engine) as session:
yield session
_app.dependency_overrides[get_session] = override_get_session
_app.dependency_overrides[get_session_no_transaction] = (
override_get_session_no_transaction
)
SQLBaseModel.metadata.create_all(bind=engine)
with Session(engine) as session:
for user in fake_users.values():
session.add(user)
session.commit()
return engine
@pytest.fixture
def app_with_db():
from app.main import app
test_engine = init_test_db(app)
yield app
app.dependency_overrides = {}
drop_test_db(test_engine)
@pytest.fixture()
def app_with_db_and_jwt(app_with_db):
def override_jwt_dependency(fake_jwt):
print(fake_jwt)
return TokenData(id=1, email="[email protected]")
app_with_db.dependency_overrides[validate_access_token] = override_jwt_dependency
yield app_with_db
The problem might be, that the override override_jwt_dependency has one argument fake_jwt. This should be the same as adding it this argument in the me function, resulting in an expected query parameter fake_jwt. (.../api/me?fake_jwt=<some value>).
Using a function without any arguments as override however should work:
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?
Here are the involved codes:
conftest.py
test.py
schemas.py
router.py
dependencies.py
The text was updated successfully, but these errors were encountered: