# -*- coding: utf-8 -*- """内容模块写接口自测:帮助 / 文章分类 / 文章。 新增 -> 校验 -> 编辑 -> 删除,逐项校验文案;末尾全部清理。 """ import json import requests BASE = "http://127.0.0.1:18080" TOKEN = "testsession123" S = requests.Session() S.trust_env = False H = {"Access-Token": TOKEN, "Content-Type": "application/json"} ok_count = 0 fail_count = 0 def call(path, payload=None, method="post", params=None): url = BASE + path if method == "get": return S.get(url, headers=H, params=params or {}, timeout=60).json() return S.post(url, headers=H, data=json.dumps(payload or {}), timeout=60).json() def check(name, cond, detail=""): global ok_count, fail_count if cond: ok_count += 1 print(f" [OK] {name}") else: fail_count += 1 print(f" [FAIL] {name} {detail}") def expect_ok(name, resp, message=None): check(name, resp.get("status") == 200 and (message is None or resp.get("message") == message) and resp.get("data") == [], f"-> {json.dumps(resp, ensure_ascii=False)[:160]}") def expect_fail(name, resp, message): check(name, resp.get("status") == 500 and resp.get("message") == message, f"-> {json.dumps(resp, ensure_ascii=False)[:160]}") def main(): print("== 帮助中心 CRUD") expect_ok("新增帮助", call("/store/content.help/add", {"form": {"title": "自测帮助", "content": "

自测内容

", "sort": 99}}), "添加成功") helps = call("/store/content.help/list", method="get", params={"page": 1})["data"]["list"]["data"] help_row = [h for h in helps if h["title"] == "自测帮助"] check("帮助已落库", len(help_row) == 1 and help_row[0]["content"] == "

自测内容

", json.dumps(help_row, ensure_ascii=False)[:200]) help_id = help_row[0]["help_id"] if help_row else None expect_ok("编辑帮助", call("/store/content.help/edit", {"helpId": help_id, "form": {"title": "自测帮助改", "content": "

改

", "sort": 98}}), "更新成功") helps = call("/store/content.help/list", method="get", params={"page": 1})["data"]["list"]["data"] check("帮助编辑生效", any(h["help_id"] == help_id and h["title"] == "自测帮助改" for h in helps)) print("== 文章分类 CRUD") expect_ok("新增文章分类", call("/store/content.article.category/add", {"form": {"name": "自测分类", "status": 1, "sort": 99}}), "添加成功") cats = call("/store/content.article.category/list", method="get")["data"]["list"] cat_row = [c for c in cats if c["name"] == "自测分类"] check("文章分类已落库", len(cat_row) == 1 and cat_row[0]["status"] == 1, json.dumps(cat_row, ensure_ascii=False)[:200]) cat_id = cat_row[0]["category_id"] if cat_row else None print("== 文章 CRUD") files = call("/store/files/list", method="get", params={"page": 1})["data"]["list"]["data"] image_id = files[0]["file_id"] if files else None check("取到可用封面图", image_id is not None) expect_fail("新增文章-缺封面图", call("/store/content.article/add", {"form": { "title": "自测文章", "category_id": cat_id, "content": "

正文

", "status": 1, "sort": 99}}), "请上传封面图") expect_fail("新增文章-缺内容", call("/store/content.article/add", {"form": { "title": "自测文章", "category_id": cat_id, "image_id": image_id, "content": "", "status": 1, "sort": 99}}), "请输入文章内容") expect_ok("新增文章", call("/store/content.article/add", {"form": { "title": "自测文章", "show_type": 10, "category_id": cat_id, "image_id": image_id, "content": "

正文 & 说明

", "status": 1, "sort": 99, "virtual_views": 7}}), "添加成功") arts = call("/store/content.article/list", method="get", params={"page": 1, "title": "自测文章"})["data"]["list"]["data"] art = [a for a in arts if a["title"] == "自测文章"] check("文章已落库", len(art) == 1 and art[0]["category_id"] == cat_id, json.dumps(art, ensure_ascii=False)[:240]) article_id = art[0]["article_id"] if art else None check("列表含 image_url 与 show_views", art and art[0].get("show_views") == 7 and art[0].get("image_url"), json.dumps(art[0], ensure_ascii=False)[:240] if art else "missing") check("列表不含 content", art and "content" not in art[0]) detail = call("/store/content.article/detail", method="get", params={"articleId": article_id})["data"]["detail"] check("详情 content 已解码", detail["content"] == "

正文 & 说明

", repr(detail["content"])) check("详情带 image 对象与分类", detail.get("image", {}).get("file_id") == image_id and detail.get("category", {}).get("category_id") == cat_id, json.dumps({"image": detail.get("image"), "category": detail.get("category")}, ensure_ascii=False)[:240]) expect_fail("删除被文章引用的分类", call("/store/content.article.category/delete", {"categoryId": cat_id}), "该分类下存在1个文章,不允许删除") expect_ok("编辑文章", call("/store/content.article/edit", {"articleId": article_id, "form": { "title": "自测文章改", "show_type": 20, "category_id": cat_id, "image_id": image_id, "content": "

改后正文

", "status": 0, "sort": 97, "virtual_views": 9}}), "更新成功") detail = call("/store/content.article/detail", method="get", params={"articleId": article_id})["data"]["detail"] check("文章编辑生效", detail["title"] == "自测文章改" and detail["show_type"] == 20 and detail["status"] == 0 and detail["show_views"] == 9 and detail["content"] == "

改后正文

", json.dumps({k: detail[k] for k in ("title", "show_type", "status", "show_views")}, ensure_ascii=False)) print("== 清理") if article_id: expect_ok("删除文章", call("/store/content.article/delete", {"articleId": article_id}), "删除成功") if cat_id: expect_ok("删除文章分类", call("/store/content.article.category/delete", {"categoryId": cat_id}), "删除成功") if help_id: expect_ok("删除帮助", call("/store/content.help/delete", {"helpId": help_id}), "删除成功") print("== 删除后复核") helps = call("/store/content.help/list", method="get", params={"page": 1})["data"]["list"]["data"] check("帮助已不可见", all(h["help_id"] != help_id for h in helps)) cats = call("/store/content.article.category/list", method="get")["data"]["list"] check("文章分类已删除", all(c["category_id"] != cat_id for c in cats)) arts = call("/store/content.article/list", method="get", params={"page": 1})["data"]["list"]["data"] check("文章已不可见", all(a["article_id"] != article_id for a in arts)) print(f"\n汇总:通过 {ok_count},失败 {fail_count}") if __name__ == "__main__": main()