test_http.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from __future__ import annotations
  2. from uuid import UUID, uuid4
  3. def test_dms_blueprint_is_registered(flask_app):
  4. rules = {rule.rule for rule in flask_app.url_map.iter_rules()}
  5. assert "/api/v1/health" in rules
  6. def test_only_q2b_dms_routes_are_registered(flask_app):
  7. dms_rules = {
  8. rule.rule for rule in flask_app.url_map.iter_rules()
  9. if rule.rule.startswith("/api/v1")
  10. }
  11. assert dms_rules == {
  12. "/api/v1/health",
  13. "/api/v1/auth/login",
  14. "/api/v1/auth/logout",
  15. "/api/v1/users/me",
  16. "/api/v1/organizations/tree",
  17. "/api/v1/users",
  18. "/api/v1/config/ui-dictionaries",
  19. "/api/v1/categories/tree",
  20. "/api/v1/categories",
  21. "/api/v1/categories/<id>",
  22. "/api/v1/documents",
  23. "/api/v1/documents/<id>",
  24. "/api/v1/documents/batch-import",
  25. "/api/v1/documents/<id>/preview",
  26. "/api/v1/documents/<id>/download",
  27. "/api/v1/main-plans/<id>/sub-plans",
  28. "/api/v1/attachments",
  29. "/api/v1/attachments/<id>",
  30. "/api/v1/attachments/batch-import",
  31. "/api/v1/attachments/<id>/main-plans",
  32. "/api/v1/main-plans/<id>/attachments",
  33. "/api/v1/main-plans/<id>/attachments/bind",
  34. "/api/v1/main-plans/<id>/attachments/<attachmentId>",
  35. "/api/v1/documents/<id>/permissions",
  36. "/api/v1/audit/logs",
  37. "/api/v1/statistics/documents",
  38. "/api/v1/audit/statistics/trend",
  39. "/api/v1/audit/statistics/actions",
  40. "/api/v1/audit/statistics/users",
  41. "/api/v1/recycle-bin/documents",
  42. "/api/v1/recycle-bin/documents/<id>/restore",
  43. }
  44. def test_legacy_health_response_is_unchanged(client):
  45. response = client.get("/api/health")
  46. assert response.status_code == 200
  47. assert response.get_json() == {"status": "ok"}
  48. assert "requestId" not in response.get_json()
  49. def test_dms_health_uses_contract_response(client):
  50. response = client.get("/api/v1/health")
  51. payload = response.get_json()
  52. assert response.status_code == 200
  53. assert payload == {
  54. "code": "OK",
  55. "message": "success",
  56. "data": {"service": "dms", "status": "UP"},
  57. "requestId": payload["requestId"],
  58. }
  59. UUID(payload["requestId"])
  60. assert response.headers["X-Request-Id"] == payload["requestId"]
  61. def test_request_id_is_passed_through(client):
  62. request_id = str(uuid4())
  63. response = client.get(
  64. "/api/v1/health",
  65. headers={"X-Request-Id": request_id},
  66. )
  67. assert response.get_json()["requestId"] == request_id
  68. assert response.headers["X-Request-Id"] == request_id
  69. def test_request_id_is_generated_when_absent(client):
  70. response = client.get("/api/v1/health")
  71. UUID(response.get_json()["requestId"])
  72. def test_invalid_request_id_uses_uniform_error_response(client):
  73. response = client.get(
  74. "/api/v1/health",
  75. headers={"X-Request-Id": "not-a-uuid"},
  76. )
  77. payload = response.get_json()
  78. assert response.status_code == 400
  79. assert payload["code"] == "INVALID_ARGUMENT"
  80. assert payload["details"] is None
  81. assert set(payload) == {"code", "message", "details", "requestId"}
  82. assert response.headers["X-Request-Id"] == payload["requestId"]
  83. UUID(payload["requestId"])
  84. def test_unknown_dms_route_uses_uniform_not_found(client):
  85. request_id = str(uuid4())
  86. response = client.get(
  87. "/api/v1/not-implemented",
  88. headers={"X-Request-Id": request_id},
  89. )
  90. assert response.status_code == 404
  91. assert response.get_json() == {
  92. "code": "RESOURCE_NOT_FOUND",
  93. "message": "请求的资源不存在",
  94. "details": None,
  95. "requestId": request_id,
  96. }
  97. assert response.headers["X-Request-Id"] == request_id