37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
from fastapi.testclient import TestClient
|
|
|
|
from backend.bootstrap.application import create_application
|
|
from backend.bootstrap.settings import Settings
|
|
|
|
|
|
def test_health_reports_runtime_environment(tmp_path) -> None:
|
|
application = create_application(
|
|
Settings(environment="test", debug=False, data_directory=tmp_path)
|
|
)
|
|
|
|
with TestClient(application) as client:
|
|
response = client.get("/api/health")
|
|
|
|
assert response.status_code == 200
|
|
assert response.json() == {"status": "ok", "environment": "test"}
|
|
|
|
|
|
def test_unknown_failure_uses_safe_error_contract(tmp_path) -> None:
|
|
application = create_application(
|
|
Settings(environment="test", debug=False, data_directory=tmp_path)
|
|
)
|
|
|
|
@application.get("/api/test/failure")
|
|
def fail() -> None:
|
|
raise RuntimeError("secret provider details")
|
|
|
|
with TestClient(application, raise_server_exceptions=False) as client:
|
|
response = client.get("/api/test/failure")
|
|
|
|
payload = response.json()["error"]
|
|
assert response.status_code == 500
|
|
assert payload["code"] == "internal_error"
|
|
assert payload["message"] == "服务暂时不可用,请稍后重试。"
|
|
assert "secret provider details" not in response.text
|
|
assert len(payload["correlation_id"]) == 32
|