-
First Check
Commit to Help
Example Codeclient = TestClient(app)
def test_login():
response = client.post(
"/login",
data={
"username": "testuser1",
"password": "qwerty@123"
}
)
assert response.status_code == 200
assert response.json()["access_token"]
def test_login_b():
response = client.post(
"/login",
data={
"username": "johndoe",
"password": "qwerty@123"
}
)
assert response.status_code == 200
assert response.json()["access_token"]
### LOGIN ###
@app.post("/login", tags=["auth"])
async def login(request: OAuth2PasswordRequestForm = Depends()):
user = await db["users"].find_one({"username": request.username})
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Invalid username or password"
)
if not Hash.verify(user["password"], request.password):
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Invalid username or password"
)
access_token = create_access_token(data={"sub": user["username"]})
return {"access_token": access_token, "token_type": "bearer"} DescriptionI am working on a blog management project using MongoDB Atlas as my database. All the routes are working fine but I am facing issues with Tests. Please help me understand and fix this. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.111.0 Pydantic Version2.7.1 Python Version3.12.3 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I guess you are using pytest-asyncio library. |
Beta Was this translation helpful? Give feedback.
-
This StackOverflow answer did the job |
Beta Was this translation helpful? Give feedback.
This StackOverflow answer did the job