I'm having trouble trying to override the JWT validation from my testing stage.
The validation is working fine when calling the API endpoints or when I call them using the Swagger UI.
All my endpoints are declared as follows:
@router.get("/some-path/{id}", response_model=SomeModel, dependencies=[Depends(JWTBearer())])
async def read_card_basic(*, db: Session = Depends(get_db), id: int):
# do something
And the JWTBearer class has a __call__ method that perfoms the validation:
async def __call__(self, request: Request):
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
if credentials:
if not credentials.scheme == "Bearer":
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid authentication scheme.")
if not self.verify_jwt(credentials.credentials):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid or expired token.")
return credentials.credentials
else:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid authorization code.")
I'm using Pytest and I'm already doing dependencies overriding in the conftest.py file like this:
def pytest_configure() -> None:
"""
Pytest will identify this function automatically and will run it before running any test
"""
initialize_testing_db()
initialize_other_testing_db()
create_data()
create_other_data()
app.dependency_overrides[get_db] = get_test_db
app.dependency_overrides[get_other_db] = get_other_test_db
I tried following the examples in the documentation but I can't find a way to remove it since we get the token from an external service and we don't need it during testing.
Any help would be appreciated and I can add more information if needed.