#!/usr/bin/env python3 """回归验证:对之前失败的25项接口进行重新测试""" import requests import json from datetime import datetime BASE_URL = "http://localhost:18080" API_PREFIX = "/app/api" ts = int(datetime.now().timestamp()) def api_get(path, token=None, params=None): h = {"Authorization": f"Bearer {token}"} if token else {} return requests.get(f"{BASE_URL}{API_PREFIX}{path}", headers=h, params=params, timeout=10) def api_post(path, token=None, json_data=None): h = {"Authorization": f"Bearer {token}"} if token else {} return requests.post(f"{BASE_URL}{API_PREFIX}{path}", headers=h, json=json_data, timeout=10) def api_put(path, token=None, json_data=None, params=None): h = {"Authorization": f"Bearer {token}"} if token else {} return requests.put(f"{BASE_URL}{API_PREFIX}{path}", headers=h, json=json_data, params=params, timeout=10) passed = 0 failed = 0 failed_list = [] def test(name, method, path, token, data_or_params, expected_check): global passed, failed if method == "GET": resp = api_get(path, token=token, params=data_or_params) elif method == "POST": resp = api_post(path, token=token, json_data=data_or_params) elif method == "PUT": resp = api_put(path, token=token, json_data=data_or_params) else: return ok = expected_check(resp) if ok: passed += 1 print(f" [PASS] {name} | status={resp.status_code}") else: failed += 1 failed_list.append((name, resp)) print(f" [FAIL] {name} | status={resp.status_code} | body={resp.text[:150]}") print("=" * 60) print("接口回归验证 - 25项失败接口") print(f"时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") print("=" * 60) # 0. 登录 resp = api_post("/auth/login", json_data={"username": "admin", "password": "admin123"}) admin_token = resp.json()["data"]["access_token"] # 注册测试用户 resp = api_post("/auth/register", json_data={"username": f"regtest_{ts}", "password": "Test123456", "email": f"reg_{ts}@test.com", "phone": f"138{str(ts)[-8:]}"}) user_token = resp.json()["data"]["access_token"] user_id = resp.json()["data"]["user"]["id"] print("\n[回归1] AI聊天 - 创建对话 (之前500)") def check_200(resp): return resp is not None and resp.status_code == 200 and resp.json().get("code") == 0 test("创建对话", "POST", "/chat/conversations", user_token, {"title": "回归测试对话"}, check_200) print("\n[回归2] 频道 - 创建频道 (之前500)") test("创建频道", "POST", "/channels/", user_token, {"name": "回归测试频道", "type": "public"}, check_200) print("\n[回归3] 知识库 - 创建目录 (之前500)") resp = api_post("/knowledge/bases", token=user_token, json_data={"name": "回归知识库"}) kb_id = resp.json()["data"]["id"] if resp.status_code == 200 else None test("创建目录", "POST", "/knowledge/directories", user_token, {"name": "回归目录", "base_id": kb_id}, check_200) print("\n[回归4] 应用市场 - 上传应用 (之前500)") test("上传应用", "POST", "/market/apps", user_token, {"name": "回归应用", "type": "skill", "price": 0}, check_200) print("\n[回归5] Token驾驶舱 - 充值 (之前500)") test("Token充值", "POST", "/token/recharge", user_token, {"amount": 500}, check_200) print("\n[回归6] 付费中心 - 获取增值服务列表 (之前500)") def check_200_any(resp): return resp is not None and resp.status_code == 200 test("增值服务列表", "GET", "/payment/services", user_token, None, check_200_any) print("\n[回归7] 付费中心 - 获取优惠券列表 (之前500)") test("优惠券列表", "GET", "/payment/coupons", user_token, None, check_200_any) print("\n[回归8] 付费中心 - 创建订单 (之前422)") plans_resp = api_get("/payment/plans", token=user_token) plans = plans_resp.json().get("data", []) if plans_resp.status_code == 200 else [] if plans: test("创建订单", "POST", "/payment/orders", user_token, {"plan_id": plans[0]["id"]}, check_200) else: print(" [SKIP] 无可用会员计划") print("\n[回归9] 个人中心 - 获取个人信息 (之前500)") test("个人信息", "GET", "/profile/", user_token, None, check_200_any) print("\n[回归10] 个人中心 - 更新个人信息 (之前500)") test("更新信息", "PUT", "/profile/", user_token, {"nickname": "回归昵称"}, check_200) print("\n[回归11] 个人中心 - 修改密码 (之前500)") test("修改密码", "PUT", "/profile/password", user_token, {"old_password": "Test123456", "new_password": "NewPwd789"}, check_200) print("\n[回归12] 个人中心 - 获取登录日志 (之前500)") test("登录日志", "GET", "/profile/login-logs", user_token, None, check_200_any) print("\n[回归13] 个人中心 - 创建API密钥 (之前500)") test("创建API密钥", "POST", "/profile/api-keys", user_token, {"name": "回归密钥"}, check_200) print("\n[回归14] 资产管理 - 创建资产 (之前500)") test("创建资产", "POST", "/assets/", user_token, {"name": "回归资产.txt", "description": "test"}, check_200) print("\n[回归15] 资产管理 - 获取分类列表 (之前404)") def check_200_ok(resp): return resp is not None and resp.status_code == 200 test("分类列表", "GET", "/assets/categories", user_token, None, check_200_ok) print("\n[回归16] 用户管理 - 获取注册设置 (之前N/A)") test("注册设置", "GET", "/admin/users/register-settings", admin_token, None, check_200_ok) print("\n[回归17] 核心检查 - Token计费 (创建对话,之前N/A)") # 先获取model_id resp = api_get("/chat/models") models = resp.json().get("data", []) if resp.status_code == 200 else [] model_id = models[0]["id"] if models else None test("Token计费-创建对话", "POST", "/chat/conversations", user_token, {"title": "计费测试", "model_id": model_id}, check_200) # 其他各项:AI聊天系列 (重命名、置顶、发送消息、消息历史、清空、删除、归档 - 之前都是404) print("\n[回归18] AI聊天 - 对话管理系列 (之前404-对话不存在)") # 先创建一个新对话 resp = api_post("/chat/conversations", token=user_token, json_data={"title": "管理测试对话"}) conv_id = resp.json()["data"]["id"] if resp.status_code == 200 else None if conv_id: test("对话重命名", "PUT", f"/chat/conversations/{conv_id}", user_token, {"title": "已重命名"}, check_200) test("对话置顶", "PUT", f"/chat/conversations/{conv_id}", user_token, {"pinned": True}, check_200) test("发送消息", "POST", f"/chat/conversations/{conv_id}/messages", user_token, {"content": "Hello"}, check_200) test("消息历史", "GET", f"/chat/conversations/{conv_id}/messages", user_token, None, check_200_any) test("清空上下文", "POST", f"/chat/conversations/{conv_id}/clear", user_token, None, check_200) test("对话归档", "PUT", f"/chat/conversations/{conv_id}", user_token, {"status": "archived"}, check_200) # 创建一个用于删除的对话 resp = api_post("/chat/conversations", token=user_token, json_data={"title": "待删除对话"}) del_conv_id = resp.json()["data"]["id"] if resp.status_code == 200 else None if del_conv_id: test("删除对话", "DELETE", f"/chat/conversations/{del_conv_id}", user_token, None, check_200) print("\n" + "=" * 60) print(f"回归验证完成: 通过 {passed}, 失败 {failed}") print(f"通过率: {passed/(passed+failed)*100:.1f}%" if (passed+failed) > 0 else "N/A") if failed_list: print("失败项:") for name, resp in failed_list: print(f" - {name}: status={resp.status_code}, body={resp.text[:200]}") print("=" * 60)