Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
Override JWTBearer Auth dependency in FastAPI while testing

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.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda