test_api.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from fastapi.testclient import TestClient
  2. import app.main as main
  3. class FakeHost:
  4. async def list_runs(self, _headers):
  5. return {
  6. "items": [
  7. {
  8. "id": 42,
  9. "status": "running",
  10. "summary": "正在生成脚本",
  11. "start_time": "2026-07-21T01:00:00Z",
  12. "end_time": None,
  13. }
  14. ]
  15. }
  16. def test_runs_are_normalized_from_host(monkeypatch) -> None:
  17. monkeypatch.setattr(main, "host", FakeHost())
  18. response = TestClient(main.app).get("/api/runs")
  19. assert response.status_code == 200
  20. assert response.json()[0] == {
  21. "script_build_id": 42,
  22. "status": "running",
  23. "summary": "正在生成脚本",
  24. "started_at": "2026-07-21T01:00:00Z",
  25. "completed_at": None,
  26. }
  27. def test_browser_auth_credentials_are_allowed_for_the_frontend() -> None:
  28. response = TestClient(main.app).options(
  29. "/api/runs",
  30. headers={
  31. "Origin": "http://127.0.0.1:3008",
  32. "Access-Control-Request-Method": "GET",
  33. },
  34. )
  35. assert response.headers["access-control-allow-origin"] == "http://127.0.0.1:3008"
  36. assert response.headers["access-control-allow-credentials"] == "true"