Test: Add test for empty realm to fix coverage

This commit is contained in:
kumarvishwajeettrivedi 2025-11-22 17:45:05 +05:30
parent a053ad6bea
commit 7596d0cbfd
1 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,26 @@
from fastapi import Depends, FastAPI, HTTPException
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from fastapi.testclient import TestClient
app = FastAPI()
security = HTTPBasic(realm="")
@app.get("/users/me")
def read_current_user(credentials: HTTPBasicCredentials = Depends(security)):
return {"username": credentials.username, "password": credentials.password}
client = TestClient(app)
def test_security_http_basic_empty_realm():
response = client.get("/users/me", auth=("john", "secret"))
assert response.status_code == 200, response.text
assert response.json() == {"username": "john", "password": "secret"}
def test_security_http_basic_invalid_credentials_empty_realm():
response = client.get(
"/users/me", headers={"Authorization": "Basic notabase64token"}
)
assert response.status_code == 401, response.text
assert response.headers["WWW-Authenticate"] == "Basic"
assert response.json() == {"detail": "Invalid authentication credentials"}