test_content_writes.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. """内容模块写接口自测:帮助 / 文章分类 / 文章。
  3. 新增 -> 校验 -> 编辑 -> 删除,逐项校验文案;末尾全部清理。
  4. """
  5. import json
  6. import requests
  7. BASE = "http://127.0.0.1:18080"
  8. TOKEN = "testsession123"
  9. S = requests.Session()
  10. S.trust_env = False
  11. H = {"Access-Token": TOKEN, "Content-Type": "application/json"}
  12. ok_count = 0
  13. fail_count = 0
  14. def call(path, payload=None, method="post", params=None):
  15. url = BASE + path
  16. if method == "get":
  17. return S.get(url, headers=H, params=params or {}, timeout=60).json()
  18. return S.post(url, headers=H, data=json.dumps(payload or {}), timeout=60).json()
  19. def check(name, cond, detail=""):
  20. global ok_count, fail_count
  21. if cond:
  22. ok_count += 1
  23. print(f" [OK] {name}")
  24. else:
  25. fail_count += 1
  26. print(f" [FAIL] {name} {detail}")
  27. def expect_ok(name, resp, message=None):
  28. check(name, resp.get("status") == 200 and (message is None or resp.get("message") == message)
  29. and resp.get("data") == [],
  30. f"-> {json.dumps(resp, ensure_ascii=False)[:160]}")
  31. def expect_fail(name, resp, message):
  32. check(name, resp.get("status") == 500 and resp.get("message") == message,
  33. f"-> {json.dumps(resp, ensure_ascii=False)[:160]}")
  34. def main():
  35. print("== 帮助中心 CRUD")
  36. expect_ok("新增帮助", call("/store/content.help/add",
  37. {"form": {"title": "自测帮助", "content": "<p>自测内容</p>", "sort": 99}}), "添加成功")
  38. helps = call("/store/content.help/list", method="get", params={"page": 1})["data"]["list"]["data"]
  39. help_row = [h for h in helps if h["title"] == "自测帮助"]
  40. check("帮助已落库", len(help_row) == 1 and help_row[0]["content"] == "<p>自测内容</p>",
  41. json.dumps(help_row, ensure_ascii=False)[:200])
  42. help_id = help_row[0]["help_id"] if help_row else None
  43. expect_ok("编辑帮助", call("/store/content.help/edit",
  44. {"helpId": help_id, "form": {"title": "自测帮助改", "content": "<p>改</p>", "sort": 98}}),
  45. "更新成功")
  46. helps = call("/store/content.help/list", method="get", params={"page": 1})["data"]["list"]["data"]
  47. check("帮助编辑生效", any(h["help_id"] == help_id and h["title"] == "自测帮助改" for h in helps))
  48. print("== 文章分类 CRUD")
  49. expect_ok("新增文章分类", call("/store/content.article.category/add",
  50. {"form": {"name": "自测分类", "status": 1, "sort": 99}}), "添加成功")
  51. cats = call("/store/content.article.category/list", method="get")["data"]["list"]
  52. cat_row = [c for c in cats if c["name"] == "自测分类"]
  53. check("文章分类已落库", len(cat_row) == 1 and cat_row[0]["status"] == 1,
  54. json.dumps(cat_row, ensure_ascii=False)[:200])
  55. cat_id = cat_row[0]["category_id"] if cat_row else None
  56. print("== 文章 CRUD")
  57. files = call("/store/files/list", method="get", params={"page": 1})["data"]["list"]["data"]
  58. image_id = files[0]["file_id"] if files else None
  59. check("取到可用封面图", image_id is not None)
  60. expect_fail("新增文章-缺封面图", call("/store/content.article/add", {"form": {
  61. "title": "自测文章", "category_id": cat_id, "content": "<p>正文</p>", "status": 1, "sort": 99}}),
  62. "请上传封面图")
  63. expect_fail("新增文章-缺内容", call("/store/content.article/add", {"form": {
  64. "title": "自测文章", "category_id": cat_id, "image_id": image_id, "content": "", "status": 1, "sort": 99}}),
  65. "请输入文章内容")
  66. expect_ok("新增文章", call("/store/content.article/add", {"form": {
  67. "title": "自测文章", "show_type": 10, "category_id": cat_id, "image_id": image_id,
  68. "content": "<p>正文 &amp; 说明</p>", "status": 1, "sort": 99, "virtual_views": 7}}), "添加成功")
  69. arts = call("/store/content.article/list", method="get", params={"page": 1, "title": "自测文章"})["data"]["list"]["data"]
  70. art = [a for a in arts if a["title"] == "自测文章"]
  71. check("文章已落库", len(art) == 1 and art[0]["category_id"] == cat_id, json.dumps(art, ensure_ascii=False)[:240])
  72. article_id = art[0]["article_id"] if art else None
  73. check("列表含 image_url 与 show_views", art and art[0].get("show_views") == 7 and art[0].get("image_url"),
  74. json.dumps(art[0], ensure_ascii=False)[:240] if art else "missing")
  75. check("列表不含 content", art and "content" not in art[0])
  76. detail = call("/store/content.article/detail", method="get", params={"articleId": article_id})["data"]["detail"]
  77. check("详情 content 已解码", detail["content"] == "<p>正文 & 说明</p>", repr(detail["content"]))
  78. check("详情带 image 对象与分类", detail.get("image", {}).get("file_id") == image_id
  79. and detail.get("category", {}).get("category_id") == cat_id,
  80. json.dumps({"image": detail.get("image"), "category": detail.get("category")}, ensure_ascii=False)[:240])
  81. expect_fail("删除被文章引用的分类", call("/store/content.article.category/delete", {"categoryId": cat_id}),
  82. "该分类下存在1个文章,不允许删除")
  83. expect_ok("编辑文章", call("/store/content.article/edit", {"articleId": article_id, "form": {
  84. "title": "自测文章改", "show_type": 20, "category_id": cat_id, "image_id": image_id,
  85. "content": "<p>改后正文</p>", "status": 0, "sort": 97, "virtual_views": 9}}), "更新成功")
  86. detail = call("/store/content.article/detail", method="get", params={"articleId": article_id})["data"]["detail"]
  87. check("文章编辑生效", detail["title"] == "自测文章改" and detail["show_type"] == 20
  88. and detail["status"] == 0 and detail["show_views"] == 9 and detail["content"] == "<p>改后正文</p>",
  89. json.dumps({k: detail[k] for k in ("title", "show_type", "status", "show_views")}, ensure_ascii=False))
  90. print("== 清理")
  91. if article_id:
  92. expect_ok("删除文章", call("/store/content.article/delete", {"articleId": article_id}), "删除成功")
  93. if cat_id:
  94. expect_ok("删除文章分类", call("/store/content.article.category/delete", {"categoryId": cat_id}), "删除成功")
  95. if help_id:
  96. expect_ok("删除帮助", call("/store/content.help/delete", {"helpId": help_id}), "删除成功")
  97. print("== 删除后复核")
  98. helps = call("/store/content.help/list", method="get", params={"page": 1})["data"]["list"]["data"]
  99. check("帮助已不可见", all(h["help_id"] != help_id for h in helps))
  100. cats = call("/store/content.article.category/list", method="get")["data"]["list"]
  101. check("文章分类已删除", all(c["category_id"] != cat_id for c in cats))
  102. arts = call("/store/content.article/list", method="get", params={"page": 1})["data"]["list"]["data"]
  103. check("文章已不可见", all(a["article_id"] != article_id for a in arts))
  104. print(f"\n汇总:通过 {ok_count},失败 {fail_count}")
  105. if __name__ == "__main__":
  106. main()