Files
watchman/services/watchman/tests/test_envelope.py
T
unknownandCursor 8fb16f4a4a feat(infra): 落地 M01 基础框架与本机开发环境
交付 FastAPI/Celery/Vue 薄壳、FORCE RLS、Compose 回环与 review 修复证据,关闭 M01 DONE_LOCAL。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-17 15:00:39 +08:00

104 lines
3.7 KiB
Python

"""Envelope and health-check acceptance tests."""
from __future__ import annotations
from fastapi.testclient import TestClient
from watchman.api.app import create_app
from watchman.core.config import reload_settings
from watchman.core.db import reset_engine
def test_health_success_envelope() -> None:
client = TestClient(create_app())
response = client.get("/health", headers={"X-Request-Id": "req-health-1"})
assert response.status_code == 200
body = response.json()
assert body["success"] is True
assert body["request_id"] == "req-health-1"
assert response.headers["X-Request-Id"] == "req-health-1"
assert body["data"]["status"] == "ok"
assert "database" not in body["data"]
assert "tenant" not in str(body).lower()
assert "password" not in str(body).lower()
def test_validation_error_envelope() -> None:
client = TestClient(create_app())
response = client.get("/_probe/validation-error")
assert response.status_code == 400
body = response.json()
assert body["success"] is False
assert body["error"]["code"] == 40000
assert "traceback" not in str(body).lower()
assert response.headers["X-Request-Id"]
def test_unhandled_error_envelope_hides_stack_and_keeps_request_id() -> None:
client = TestClient(create_app(), raise_server_exceptions=False)
response = client.get("/_probe/boom", headers={"X-Request-Id": "req-boom-1"})
assert response.status_code == 500
body = response.json()
assert body["success"] is False
assert body["error"]["code"] == 50000
assert body["request_id"] == "req-boom-1"
assert response.headers["X-Request-Id"] == "req-boom-1"
assert body["error"]["message"] == "Internal server error"
assert "traceback" not in str(body).lower()
assert "runtimeerror" not in str(body).lower()
def test_probe_and_docs_hidden_outside_non_production(monkeypatch) -> None: # type: ignore[no-untyped-def]
monkeypatch.setenv("ENV_NAME", "prod")
reload_settings()
reset_engine()
client = TestClient(create_app(validate_runtime=False))
assert client.get("/_probe/boom").status_code == 404
assert client.get("/docs").status_code == 404
assert client.get("/openapi.json").status_code == 404
monkeypatch.setenv("ENV_NAME", "dev")
reload_settings()
reset_engine()
def test_health_envelope_has_no_timestamp() -> None:
client = TestClient(create_app())
body = client.get("/health").json()
assert "timestamp" not in body
def test_app_error_codes_envelope_shape() -> None:
from watchman.core.errors import ForbiddenError, ServiceUnavailableError, UnauthorizedError
app = create_app(validate_runtime=False)
@app.get("/_probe/unauthorized")
async def probe_unauthorized() -> None:
raise UnauthorizedError()
@app.get("/_probe/forbidden")
async def probe_forbidden() -> None:
raise ForbiddenError(permission_node="perm:platform:audit.read")
@app.get("/_probe/unavailable")
async def probe_unavailable() -> None:
raise ServiceUnavailableError()
client = TestClient(app, raise_server_exceptions=False)
unauthorized = client.get("/_probe/unauthorized").json()
assert unauthorized["success"] is False
assert unauthorized["error"]["code"] == 41000
assert "permission_node" not in unauthorized["error"]
assert "timestamp" not in unauthorized
forbidden = client.get("/_probe/forbidden").json()
assert forbidden["error"]["code"] == 42000
assert forbidden["error"]["permission_node"] == "perm:platform:audit.read"
unavailable = client.get("/_probe/unavailable")
assert unavailable.status_code == 503
body = unavailable.json()
assert body["error"]["code"] == 50000
assert body["error"]["retryable"] is True