forked from mtusi2026/mtusi2026lab5
63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
from httpx import AsyncClient
|
|
|
|
from app.main import create_app
|
|
from conftest import close_app, make_client
|
|
from helpers import assert_request_id
|
|
|
|
|
|
async def test_liveness(client: AsyncClient):
|
|
response = await client.get("/health/live")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok"}
|
|
assert_request_id(response)
|
|
|
|
|
|
async def test_readiness(client: AsyncClient):
|
|
response = await client.get("/health/ready")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok"}
|
|
assert_request_id(response)
|
|
|
|
|
|
async def test_readiness_is_503_when_database_is_down(make_settings):
|
|
app = create_app(
|
|
make_settings(
|
|
database_url="postgresql+asyncpg://nobody:nopass@127.0.0.1:1/none",
|
|
db_connect_timeout_seconds=1,
|
|
)
|
|
)
|
|
async with make_client(app) as client:
|
|
assert (await client.get("/health/live")).status_code == 200
|
|
|
|
ready = await client.get("/health/ready")
|
|
assert ready.status_code == 503
|
|
assert ready.json()["error"]["code"] == "unavailable"
|
|
assert_request_id(ready)
|
|
await close_app(app)
|
|
|
|
|
|
async def test_business_request_is_503_when_database_is_down(make_settings):
|
|
app = create_app(
|
|
make_settings(
|
|
database_url="postgresql+asyncpg://nobody:secretpass@127.0.0.1:1/none",
|
|
db_connect_timeout_seconds=1,
|
|
)
|
|
)
|
|
async with make_client(app, raise_app_exceptions=False) as client:
|
|
response = await client.post(
|
|
"/api/v1/users",
|
|
json={
|
|
"username": "alice",
|
|
"display_name": "Alice",
|
|
"password": "Correct-horse-battery-1",
|
|
},
|
|
)
|
|
await close_app(app)
|
|
|
|
assert response.status_code == 503
|
|
assert response.json()["error"]["code"] == "unavailable"
|
|
assert "secretpass" not in response.text
|
|
assert_request_id(response)
|