Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda