feat: Agent 工作区初始提交(源码+文档+Skills)

包含 CodeAgent 前后端源码、SKILLS、MCP、demo 代码与工作区文档。
排除依赖缓存、运行数据、备份与二进制附件,规则见 .gitignore。

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-11-13 13:48:41 +08:00
co-authored by Claude Code
commit 26e13ff032
190 changed files with 47632 additions and 0 deletions
+166
View File
@@ -0,0 +1,166 @@
# -*- coding: utf-8 -*-
"""
禅道 Token 换取测试
==================
锁定两个行为:
1. POST /api.php/v1/tokens 返回 201 Created 时应判定为登录成功(禅道 REST 语义);
2. 报错信息不得回显完整 Token / 口令,只允许脱敏后的前 6 位。
"""
from __future__ import annotations
import json
from pathlib import Path
import pytest
from app.services.zentao_service import ZentaoService, _redact_body
class _FakeResp:
"""httpx.Response 的最小替身。"""
def __init__(self, status_code: int, payload: dict | str) -> None:
self.status_code = status_code
self._payload = payload
self.text = payload if isinstance(payload, str) else json.dumps(payload, ensure_ascii=False)
def json(self):
if isinstance(self._payload, str):
raise ValueError("非 JSON")
return self._payload
def _make_service(tmp_path: Path) -> ZentaoService:
"""构造一个只依赖内存配置的 Service,不读真实 .env,也不落真实缓存。"""
svc = ZentaoService.__new__(ZentaoService)
svc._lock = __import__("threading").Lock()
svc._base_url = "http://zentao.test/zentao"
svc._account = "test_acct"
svc._password = "fake_password_123"
svc._static_token = ""
svc._token_ttl = 518400
svc._cache_path = tmp_path / "zentao_token.json"
svc._token = ""
svc._expire_at = 0.0
return svc
# ---------- 用例 ----------
def test_login_accepts_http_201(tmp_path, monkeypatch):
"""禅道建 Token 成功返回 201,必须判定 success=True 并吐出脱敏 token_masked。"""
import httpx
svc = _make_service(tmp_path)
raw_token = "00001111222233334444555566667777"
def fake_post(url, json=None, timeout=None):
assert url.endswith("/api.php/v1/tokens")
return _FakeResp(201, {"token": raw_token})
monkeypatch.setattr(httpx, "post", fake_post)
result = svc.refresh_token(force=True)
assert result["success"] is True, result
assert result["token_masked"] == f"{raw_token[:6]}..."
assert raw_token not in str(result)
assert svc._token == raw_token
def test_login_still_accepts_http_200(tmp_path, monkeypatch):
"""兼容既返回 200 的禅道版本。"""
import httpx
svc = _make_service(tmp_path)
raw_token = "deadbeefcafebabe0000000011111111"
monkeypatch.setattr(httpx, "post", lambda *a, **k: _FakeResp(200, {"token": raw_token}))
result = svc.refresh_token(force=True)
assert result["success"] is True, result
assert svc._token == raw_token
def test_login_error_message_does_not_leak_token(tmp_path, monkeypatch):
"""即使登录失败,报错文案也不得回显完整 Token。"""
import httpx
svc = _make_service(tmp_path)
raw_token = "00001111222233334444555566667777"
# 500 + 响应体里夹带 token 字段,模拟最坏情况
monkeypatch.setattr(httpx, "post", lambda *a, **k: _FakeResp(500, {"token": raw_token, "error": "boom"}))
result = svc.refresh_token(force=True)
assert result["success"] is False
assert raw_token not in result["message"]
assert raw_token[:6] in result["message"] # 仍保留前 6 位便于定位
def test_login_json_parse_error_does_not_leak_token(tmp_path, monkeypatch):
"""201/200 但响应非 JSON 时,同样不得回显完整 Token。"""
import httpx
svc = _make_service(tmp_path)
raw_token = "00001111222233334444555566667777"
monkeypatch.setattr(httpx, "post", lambda *a, **k: _FakeResp(200, f'{{"token":"{raw_token}"'))
result = svc.refresh_token(force=True)
assert result["success"] is False
assert raw_token not in result["message"]
def test_redact_body_masks_sensitive_fields():
"""_redact_body 只遮敏感字段值,保留可读结构。"""
raw = '{"token":"00001111222233334444555566667777","password":"fake_password_123","msg":"ok"}'
out = _redact_body(raw)
assert "00001111222233334444555566667777" not in out
assert "fake_password_123" not in out
# _mask 只保留前 6 位并追加省略号
assert out.startswith('{"token":"000011...')
assert out.endswith('","password":"fake_p...","msg":"ok"}')
assert '"msg":"ok"' in out
def test_redact_body_masks_token_at_truncation_boundary():
"""token 值被 limit 截断在中间(无闭合引号)时,同样不得回显完整 token。"""
prefix = "x" * 290
raw_token = "00001111222233334444555566667777"
raw = prefix + '{"token":"' + raw_token + '"}'
# limit=320 恰好切在 token 值中间
out = _redact_body(raw, limit=320)
assert raw_token not in out
assert "000011..." in out
def test_request_raw_field_does_not_leak_token(tmp_path, monkeypatch):
"""data 解析失败时回传的 raw 字段同样必须脱敏(它会流进日志/接口响应)。"""
import httpx
import time as _time
svc = _make_service(tmp_path)
# 预置有效 Token,跳过登录,直接测 _request 的返回结构
svc._token = "cachedtokencachedtokencachedtoken"
svc._expire_at = _time.time() + 3600
raw_token = "00001111222233334444555566667777"
# 非 JSON 响应体里夹带 token,迫使 data=None 走 raw 分支
monkeypatch.setattr(httpx, "request", lambda *a, **k: _FakeResp(500, f'oops {{"token":"{raw_token}"'))
result = svc._request("GET", "/bugs")
assert result["success"] is False
assert result["raw"] is not None, "非 JSON 时应回传 raw 供排查"
assert raw_token not in result["raw"]
assert raw_token not in result["message"]
assert "000011..." in result["raw"]