test_01_02_module_live.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """
  2. Swagger 模块 1(健康检查)+ 模块 2(企微)— 全部真实联调
  3. 覆盖 http://localhost:3101/api-docs 中:
  4. 1-健康检查: GET /api/health
  5. 2-企微: 全部 9 个接口
  6. """
  7. from __future__ import annotations
  8. import json
  9. import time
  10. import uuid
  11. from pathlib import Path
  12. import pytest
  13. import requests
  14. pytestmark = [pytest.mark.integration, pytest.mark.qiwei_live]
  15. FIXTURE = Path(__file__).parent / "fixtures" / "qiwei_webhook_sample.json"
  16. # ---------- 模块 1 ----------
  17. def test_m01_health_live(require_pc_server):
  18. r = requests.get(f"{require_pc_server}/health", timeout=5)
  19. assert r.status_code == 200
  20. body = r.json()
  21. assert body["status"] == "ok"
  22. assert "qiwei" in body["modules"]
  23. # ---------- 模块 2 工具 ----------
  24. def _webhook_payload(live_guid: str, room_id: str, doc_suffix: str) -> tuple[dict, str]:
  25. payload = json.loads(FIXTURE.read_text(encoding="utf-8"))
  26. base = int(time.time() * 1000) + (hash(doc_suffix) % 100_000)
  27. docid = f"DOC_M12_{doc_suffix}"
  28. for i, item in enumerate(payload.get("data", [])):
  29. item["guid"] = live_guid
  30. item["fromRoomId"] = room_id
  31. item["timestamp"] = base + i
  32. if item.get("msgType") == 13:
  33. item["msgData"]["linkUrl"] = (
  34. f"https://doc.weixin.qq.com/txdoc/excel?docid={docid}"
  35. )
  36. return payload, docid
  37. # ---------- 模块 2:按 Swagger 顺序 ----------
  38. def test_m02_proxy_get_room_list(api, live_guid):
  39. data = api.assert_success(
  40. api.post(
  41. "/qiwei/proxy",
  42. json={
  43. "method": "/room/getRoomList",
  44. "params": {"guid": live_guid, "nextStartIndex": 0},
  45. },
  46. )
  47. )
  48. assert isinstance(data, dict)
  49. def test_m02_proxy_get_session_list(api, live_guid, live_room_ids):
  50. data = api.assert_success(
  51. api.post(
  52. "/qiwei/proxy",
  53. json={"method": "/session/getSessionList", "params": {"guid": live_guid}},
  54. )
  55. )
  56. ids = set()
  57. for key in ("collectList", "shieldList", "topList", "markList"):
  58. for item in data.get(key) or []:
  59. if item.get("sessionType") == 1:
  60. ids.add(str(item["sessionId"]))
  61. for rid in live_room_ids:
  62. assert rid in ids
  63. def test_m02_staff_status(api, live_guid):
  64. data = api.assert_success(api.get(f"/qiwei/staff/{live_guid}/status"))
  65. assert data.get("guid") == live_guid or data.get("userId")
  66. assert data["userOnlineStatus"] in (1, 2)
  67. def test_m02_staff_batch_status(api, live_guid):
  68. data = api.assert_success(
  69. api.post("/qiwei/staff/batch-status", json={"guids": [live_guid]})
  70. )
  71. assert len(data) == 1
  72. def test_m02_sync_messages(api, live_guid):
  73. data = api.assert_success(
  74. api.post("/qiwei/sync", json={"guid": live_guid, "msgSeq": 0, "limit": 20})
  75. )
  76. assert "messages" in data
  77. def test_m02_webhook_register_doc(api, live_guid, live_room_id):
  78. suffix = uuid.uuid4().hex[:8]
  79. payload, docid = _webhook_payload(live_guid, live_room_id, suffix)
  80. data = api.assert_success(api.post("/qiwei/webhook", json=payload))
  81. assert data["docLinksFound"] >= 1
  82. doc = api.assert_success(api.get("/qiwei/room-docs", params={"roomId": live_room_id}))
  83. assert docid in doc["docId"]
  84. def test_m02_messages_list(api, live_guid, live_room_id):
  85. msgs = api.assert_success(
  86. api.get("/qiwei/messages", params={"roomId": live_room_id, "guid": live_guid, "limit": 20})
  87. )
  88. assert isinstance(msgs, list)
  89. def test_m02_room_docs_list(api, live_room_id):
  90. docs = api.assert_success(api.get("/qiwei/room-docs"))
  91. assert isinstance(docs, list)
  92. assert any(d.get("roomId") == live_room_id for d in docs)
  93. def test_m02_room_doc_anomalies_and_resolve(api, live_guid, live_room_id):
  94. suffix = uuid.uuid4().hex[:8]
  95. p1, doc1 = _webhook_payload(live_guid, live_room_id, f"x{suffix}")
  96. api.assert_success(api.post("/qiwei/webhook", json=p1))
  97. p2, doc2 = _webhook_payload(live_guid, live_room_id, f"y{suffix}")
  98. wh2 = api.assert_success(api.post("/qiwei/webhook", json=p2))
  99. assert wh2["processedCount"] >= 1
  100. anomalies = api.assert_success(api.get("/qiwei/room-doc-anomalies"))
  101. hit = [a for a in anomalies if a.get("roomId") == live_room_id and a.get("newDocId") == doc2]
  102. assert hit, anomalies
  103. resolved = api.assert_success(
  104. api.put(
  105. f"/qiwei/room-doc-anomalies/{live_room_id}/resolve",
  106. json={"newDocId": doc2, "resolution": "keep_new"},
  107. )
  108. )
  109. assert resolved["status"] == "resolved"