from fastapi.testclient import TestClient from app.main import create_app from tests.helpers import assert_request_id def test_liveness(client: TestClient): response = client.get("/health/live") assert response.status_code == 200 assert response.json() == {"status": "ok"} assert_request_id(response) def test_readiness(client: TestClient): response = client.get("/health/ready") assert response.status_code == 200 assert response.json() == {"status": "ok"} assert_request_id(response) def test_readiness_is_503_when_database_is_down(make_settings): """БД недоступна: live по-прежнему 200, ready — ограниченный 503.""" app = create_app( make_settings( database_url="postgresql+psycopg://nobody:nopass@127.0.0.1:1/none", db_connect_timeout_seconds=1, ) ) with TestClient(app) as client: assert client.get("/health/live").status_code == 200 ready = client.get("/health/ready") assert ready.status_code == 503 assert ready.json()["error"]["code"] == "unavailable" assert_request_id(ready) def test_business_request_is_503_when_database_is_down(make_settings): """Недоступность БД не превращается в 500 и не раскрывает детали подключения.""" app = create_app( make_settings( database_url="postgresql+psycopg://nobody:secretpass@127.0.0.1:1/none", db_connect_timeout_seconds=1, ) ) with TestClient(app, raise_server_exceptions=False) as client: response = client.post( "/api/v1/users", json={ "username": "alice", "display_name": "Alice", "password": "Correct-horse-battery-1", }, ) assert response.status_code == 503 assert response.json()["error"]["code"] == "unavailable" assert "secretpass" not in response.text assert_request_id(response)