test_aigc_safety.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from __future__ import annotations
  2. from unittest.mock import MagicMock
  3. from supply_infra.aigc.client import AigcClient
  4. from supply_infra.config import InfraSettings
  5. def test_aigc_client_requires_token_for_live_calls(monkeypatch) -> None:
  6. settings = InfraSettings(_env_file=None)
  7. monkeypatch.setattr(
  8. "supply_infra.aigc.client.get_infra_settings",
  9. lambda: settings,
  10. )
  11. client = AigcClient(token="")
  12. try:
  13. client.create_video_crawler_plan(["123"])
  14. except RuntimeError as exc:
  15. assert "AIGC_API_TOKEN" in str(exc)
  16. else:
  17. raise AssertionError("missing token must raise")
  18. def test_aigc_client_posts_to_platform(monkeypatch) -> None:
  19. settings = InfraSettings(_env_file=None)
  20. monkeypatch.setattr(
  21. "supply_infra.aigc.client.get_infra_settings",
  22. lambda: settings,
  23. )
  24. class _Response:
  25. def raise_for_status(self) -> None:
  26. return None
  27. def json(self) -> dict:
  28. return {"code": 0, "data": {"id": "plan-1"}}
  29. calls: list[dict] = []
  30. def _post(*, url, json, headers, timeout):
  31. calls.append({"url": url, "json": json})
  32. return _Response()
  33. monkeypatch.setattr("supply_infra.aigc.client.requests.post", _post)
  34. client = AigcClient(token="secret")
  35. result = client.create_video_crawler_plan(["123"])
  36. assert result["success"] is True
  37. assert result["crawler_plan_id"] == "plan-1"
  38. assert len(calls) == 1
  39. def test_bind_skips_duplicate_existing_crawler_source(monkeypatch) -> None:
  40. settings = InfraSettings(_env_file=None)
  41. monkeypatch.setattr(
  42. "supply_infra.aigc.client.get_infra_settings",
  43. lambda: settings,
  44. )
  45. client = AigcClient(token="secret")
  46. monkeypatch.setattr(
  47. client,
  48. "_get_produce_plan_detail",
  49. lambda _plan_id: (
  50. {
  51. "name": "produce-plan",
  52. "inputSourceGroups": [
  53. {
  54. "inputSources": [
  55. {
  56. "contentType": 1,
  57. "inputSourceType": 2,
  58. "inputSourceValue": "crawler-1",
  59. "inputSourceModal": 4,
  60. "inputSourceChannel": 2,
  61. }
  62. ]
  63. }
  64. ],
  65. },
  66. None,
  67. ),
  68. )
  69. post = MagicMock()
  70. monkeypatch.setattr(client, "_post", post)
  71. result = client.bind_crawler_to_produce_plan(
  72. "crawler-1",
  73. "produce-1",
  74. crawler_plan_name="crawler-name",
  75. )
  76. assert result["success"] is True
  77. assert result["already_bound"] is True
  78. post.assert_not_called()