test_18_workbench_live.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """工作台与通知 — 真实联调(模板 + API-14 sendText)"""
  2. import pytest
  3. from load_env import load_backend_env
  4. pytestmark = [pytest.mark.integration, pytest.mark.qiwei_live]
  5. @pytest.fixture(scope="session")
  6. def live_staff_id(api_session, live_guid: str) -> int:
  7. """创建绑定真实 guid 的人员,供通知 receiverId 使用"""
  8. digits = "".join(c for c in live_guid if c.isdigit())[:8] or "00000001"
  9. phone = f"139{digits}"
  10. created = api_session.assert_success(
  11. api_session.post(
  12. "/staff",
  13. json={
  14. "name": "联调测试员",
  15. "role": "operator",
  16. "storeId": 1,
  17. "phone": phone,
  18. },
  19. ),
  20. status=201,
  21. )
  22. staff_id = created["id"]
  23. api_session.assert_success(
  24. api_session.put(
  25. f"/staff/{staff_id}",
  26. json={"guid": live_guid, "status": 1},
  27. )
  28. )
  29. return staff_id
  30. def test_live_notification_template(api, live_room_ids, live_rooms_seeded):
  31. room = api.assert_success(api.get(f"/rooms/{live_room_ids[0]}"))
  32. tpl = api.assert_success(
  33. api.get(
  34. "/notifications/template",
  35. params={"issueType": "missing_doc", "roomName": room["roomName"]},
  36. )
  37. )
  38. assert room["roomName"] in tpl["content"]
  39. assert "整改" in tpl["title"]
  40. def test_live_send_notification_via_qiwe(api, live_guid, live_staff_id, live_room_ids):
  41. env = load_backend_env()
  42. if env.get("QIWEI_LIVE_NOTIFY", "").strip() != "1":
  43. pytest.skip(
  44. "未开启真实企微通知:backend/.env 设置 QIWEI_LIVE_NOTIFY=1 "
  45. "(会向 QIWEI_ROOM_IDS 第一个群发送一条测试文案)"
  46. )
  47. room = api.assert_success(api.get(f"/rooms/{live_room_ids[0]}"))
  48. tpl = api.assert_success(
  49. api.get(
  50. "/notifications/template",
  51. params={"issueType": "missing_doc", "roomName": room["roomName"]},
  52. )
  53. )
  54. sent = api.assert_success(
  55. api.post(
  56. "/notifications/send",
  57. json={
  58. "type": "compliance",
  59. "receiverId": live_staff_id,
  60. "title": tpl["title"],
  61. "content": tpl["content"] + " [自动化联调]",
  62. "refId": 1,
  63. "guid": live_guid,
  64. "toId": live_room_ids[0],
  65. },
  66. ),
  67. status=201,
  68. )
  69. assert sent["channel"] == "qiwe"
  70. assert sent["status"] == "sent", sent
  71. listed = api.assert_success(
  72. api.get("/notifications", params={"receiverId": live_staff_id, "type": "compliance"})
  73. )
  74. assert any(n["id"] == sent["id"] for n in listed)
  75. logs = api.assert_success(api.get("/audit-logs", params={"action": "notify", "limit": 5}))
  76. assert any("QiWe" in (log.get("detail") or "") for log in logs)