基底:#251 头 174b631(堆叠于 #242 feat-ci-expand 之上), 基线 origin/main aebc6238;本轨不改 #251 已定语义、不动 workflow 门槛、不删检查、不加 skip。 lint(ruff E,F@120):109 -> 10 - F401 43 -> 0:删未用导入;try/except 双绑定的名字出现 “打地鼠”(删 except 分支后 try 分支又被标出),两分支 均已核对正文无引用后清掉;MCP 三模块的 Error 名不删, 改走 importlib 属性绑定(见下),两路径绑定都保留。 - E702 40 -> 0:全在 host.py main() 的 argparse 段,纯分行, tokenize 按分号 token 拆(含 2377 行多行语句只断首个分号); 一次 5 空格缩进失误已回滚重做并以 ast + 全量 host 测试验证。 - E501 13 -> 0:host.py 5 项随 E702 分行连带清掉;mcp_server 3 项把描述拆成相邻字面量(AST 比对 24 条工具描述与原值 完全一致,注意 ruff 按东亚宽度计长,CJK 算 2);test_host 5 项断开 git commit 参数表。 - E402 8 -> 10:生产侧 2 项清掉(db.py _re、github_app _base64 移到顶部,之前无引用、无重名);剩余 10 项全是 tests/ 的 sys.path 前置 import(6 项旧 + 4 个新测试文件 沿用同一既定模式),移上去会破坏单文件直跑,有意保留。 - F841 4 -> 0:只去绑定、不删调用(run_task、DB 入队、 tarfile 上下文、git 提交),各有注释与原测试/新测试覆盖; host dispatch_from_branch 新增真实 git 仓库回归测试。 - F811 1 -> 0:server 本地 _row_get 与 peer_messages.row_get 逐行相同,删本地定义,全部调用点解析到同一 import; 新增 test_server_row_get(同一对象 + 三路行为)。 mypy(生产范围):24 -> 0,零新增 type: ignore - no-redef 9 -> 0:MCP 三模块 try/except 两次 from-import 改 importlib.import_module + 属性绑定(cloud 优先、flat copy 回落、内层 ImportError 同样回落,语义与原来一致; flat-copy 子进程测试证实回落后 __module__ 为顶层名); server _row_get 随 F811 消除。 - arg-type 10 -> 0:harness2_tasks shared 改 TypedDict 逐键精确类型;CodexProbeAdapter 补 no-op validate_tree (_adapter_validate 本就跳过缺失方法,行为一致)。 - var-annotated 3 -> 0:sub2api _SHARED_ADMIN/_login_slots、 verify-multiuser RESULTS 加精确注解。 - assignment/misc 2 -> 0:marker_tools ZoneInfo 先声明 Any 再 try import(草稿先验 mypy 通过)。 bandit:总数 286 不变,High 仍 4 项,全在 host.py,本轨不改 (属第 3 项白名单轨范围),只做 triage: - 1319 run_verify:无插值,整串验测命令,管道/&& 须 shell 语义(新增 test_host_run_verify 断言管道与 && 真实生效)。 - 1497 notify:模板来自算子配置,三处插值全部 shlex.quote, 证据为既有 test_notify_failure_quotes_shell_metacharacters (; touch 与 $(touch ) 不执行)。 - 2099/2103 goal verify:无插值,任务配置验测串,同 run_verify。 - 无新增 High;pip-audit 0 漏洞。 覆盖率(同口径三套件 coverage 计量):75.96% -> 77.87% (22392 行/缺 5385 -> 22437 行/缺 4963,显示 76% -> 78%) - client.py 0% -> 90%(pipe 假子进程 + main 分支) - run_parallel.py 0% -> 97%、migrate 包装 0% -> 95% - playwright_mcp 37% -> 62%、imagegen_mcp 47% -> 79%、 websearch_mcp 51% -> 86%、deploy.py 56% -> 77% - verify-multiuser 0% -> 27%(仅 check/req helper;main() 是主机侧真实多用户实弹验证,按门禁不建生产任务、 不以 mock 冒充,仍为缺口) - host.py 55% -> 57%(文件 2020 -> 2060 行系 E702 分行 的计量口径变化,非代码膨胀) - 未到 85%:门槛保持 75% 不动,不删低覆盖模块出统计口径; 缺口主力仍是 server.py / worker.py / host.py 大文件, 下一阶段须针对性深测,非本轨快赢可及。 验证:三套件 1576 -> 1629 项全过(tests 127、cloud/tests 1449 skipped=4、deploy/tests 53);ruff E9 通过;mypy 0; bandit High 4(同上);pip-audit 0 漏洞;试跑产物已删, 提交后工作树干净。
236 lines
8.3 KiB
Python
236 lines
8.3 KiB
Python
"""webhooks.py 单测:签名校验、事件解析、命令派发、用户映射、通知门禁。
|
||
|
||
只用标准库,不联网、不连真实 GitHub/Slack、不跑真实模型回合。
|
||
签名测试用的 secret 均为单测内临时值,不是生产密钥。
|
||
"""
|
||
|
||
import hashlib
|
||
import hmac
|
||
import os
|
||
import sys
|
||
import tempfile
|
||
import time
|
||
import unittest
|
||
|
||
# 本文件位于 cloud/tests/,被测模块位于 cloud/;cloud/ 目前不是包
|
||
# (协调员统一创建 __init__.py),故把 cloud/ 加到 sys.path 后按
|
||
# 顶层模块导入 webhooks。
|
||
sys.path.insert(
|
||
0, os.path.normpath(os.path.join(os.path.dirname(__file__), ".."))
|
||
)
|
||
|
||
from webhooks import ( # noqa: E402
|
||
ALLOWED_NOTIFICATION_EVENTS,
|
||
NotificationService,
|
||
SlackUserMap,
|
||
parse_github_pr_comment,
|
||
parse_slack_command,
|
||
verify_github_signature,
|
||
verify_slack_signature,
|
||
)
|
||
|
||
|
||
def _github_signature(secret, payload):
|
||
return "sha256=" + hmac.new(
|
||
secret.encode("utf-8"), payload, hashlib.sha256
|
||
).hexdigest()
|
||
|
||
|
||
def _slack_signature(secret, timestamp, body):
|
||
basestring = f"v0:{timestamp}:{body}"
|
||
return "v0=" + hmac.new(
|
||
secret.encode("utf-8"), basestring.encode("utf-8"), hashlib.sha256
|
||
).hexdigest()
|
||
|
||
|
||
class TestGithubSignature(unittest.TestCase):
|
||
def test_correct_signature_passes(self):
|
||
secret = "test-github-secret"
|
||
payload = b'{"action":"created"}'
|
||
header = _github_signature(secret, payload)
|
||
self.assertTrue(verify_github_signature(secret, payload, header))
|
||
|
||
def test_wrong_signature_rejected(self):
|
||
secret = "test-github-secret"
|
||
payload = b'{"action":"created"}'
|
||
header = _github_signature("wrong-secret", payload)
|
||
self.assertFalse(verify_github_signature(secret, payload, header))
|
||
|
||
def test_tampered_payload_rejected(self):
|
||
secret = "test-github-secret"
|
||
header = _github_signature(secret, b'{"action":"created"}')
|
||
self.assertFalse(
|
||
verify_github_signature(secret, b'{"action":"deleted"}', header)
|
||
)
|
||
|
||
def test_missing_header_rejected(self):
|
||
self.assertFalse(
|
||
verify_github_signature("test-github-secret", b"{}", None)
|
||
)
|
||
self.assertFalse(verify_github_signature("test-github-secret", b"{}", ""))
|
||
|
||
def test_missing_secret_rejected(self):
|
||
payload = b"{}"
|
||
self.assertFalse(verify_github_signature("", payload, "sha256=abc"))
|
||
self.assertFalse(verify_github_signature(None, payload, "sha256=abc"))
|
||
|
||
|
||
class TestParseGithubPrComment(unittest.TestCase):
|
||
def _payload(self, body, action="created"):
|
||
return {
|
||
"action": action,
|
||
"repository": {"full_name": "owner/repo"},
|
||
"issue": {"number": 42},
|
||
"comment": {"body": body, "user": {"login": "alice"}},
|
||
}
|
||
|
||
def test_fix_review_comment_parsed(self):
|
||
result = parse_github_pr_comment(
|
||
self._payload("请帮忙 /fix-review 看一下")
|
||
)
|
||
self.assertEqual(
|
||
result,
|
||
{
|
||
"type": "fix_review",
|
||
"repo": "owner/repo",
|
||
"pr_number": 42,
|
||
"comment": "请帮忙 /fix-review 看一下",
|
||
"author": "alice",
|
||
},
|
||
)
|
||
|
||
def test_comment_without_trigger_returns_none(self):
|
||
self.assertIsNone(parse_github_pr_comment(self._payload("好看,LGTM")))
|
||
|
||
def test_non_created_action_returns_none(self):
|
||
self.assertIsNone(
|
||
parse_github_pr_comment(
|
||
self._payload("有 /fix-review 但是已编辑", action="edited")
|
||
)
|
||
)
|
||
|
||
|
||
class TestSlackSignature(unittest.TestCase):
|
||
def test_correct_signature_passes(self):
|
||
secret = "test-slack-secret"
|
||
timestamp = str(int(time.time()))
|
||
body = "command=/codex&text=status"
|
||
header = _slack_signature(secret, timestamp, body)
|
||
self.assertTrue(
|
||
verify_slack_signature(secret, timestamp, body, header)
|
||
)
|
||
|
||
def test_wrong_signature_rejected(self):
|
||
secret = "test-slack-secret"
|
||
timestamp = str(int(time.time()))
|
||
body = "command=/codex&text=status"
|
||
header = _slack_signature("wrong-secret", timestamp, body)
|
||
self.assertFalse(
|
||
verify_slack_signature(secret, timestamp, body, header)
|
||
)
|
||
|
||
def test_expired_timestamp_rejected(self):
|
||
# 超过 300 秒:用正确密钥签一个 301 秒前的时间戳,仍应拒绝。
|
||
secret = "test-slack-secret"
|
||
timestamp = str(int(time.time()) - 301)
|
||
body = "command=/codex&text=status"
|
||
header = _slack_signature(secret, timestamp, body)
|
||
self.assertFalse(
|
||
verify_slack_signature(secret, timestamp, body, header)
|
||
)
|
||
|
||
def test_missing_params_rejected(self):
|
||
timestamp = str(int(time.time()))
|
||
self.assertFalse(
|
||
verify_slack_signature("", timestamp, "body", "v0=abc")
|
||
)
|
||
self.assertFalse(
|
||
verify_slack_signature("test-slack-secret", timestamp, "body", None)
|
||
)
|
||
|
||
|
||
class TestParseSlackCommand(unittest.TestCase):
|
||
def test_dispatch_prompt(self):
|
||
self.assertEqual(
|
||
parse_slack_command("/codex 做一个加法函数"),
|
||
{"command": "codex", "prompt": "做一个加法函数"},
|
||
)
|
||
|
||
def test_dispatch_status(self):
|
||
self.assertEqual(
|
||
parse_slack_command("/codex status my-task"),
|
||
{"command": "status", "task": "my-task"},
|
||
)
|
||
|
||
def test_empty_or_non_codex_returns_none_command(self):
|
||
self.assertEqual(
|
||
parse_slack_command(""), {"command": None, "prompt": ""}
|
||
)
|
||
self.assertEqual(
|
||
parse_slack_command("hello"),
|
||
{"command": None, "prompt": "hello"},
|
||
)
|
||
self.assertEqual(
|
||
parse_slack_command("/other do something"),
|
||
{"command": None, "prompt": "/other do something"},
|
||
)
|
||
|
||
|
||
class TestSlackUserMap(unittest.TestCase):
|
||
def test_map_and_lookup(self):
|
||
with tempfile.TemporaryDirectory() as tmpdir:
|
||
db_path = os.path.join(tmpdir, "slack_users.db")
|
||
user_map = SlackUserMap(db_path)
|
||
try:
|
||
self.assertIsNone(user_map.lookup("U123"))
|
||
user_map.map("U123", "user-1")
|
||
self.assertEqual(user_map.lookup("U123"), "user-1")
|
||
# 覆盖映射
|
||
user_map.map("U123", "user-2")
|
||
self.assertEqual(user_map.lookup("U123"), "user-2")
|
||
# 未映射的 id 仍返回 None
|
||
self.assertIsNone(user_map.lookup("U999"))
|
||
finally:
|
||
user_map.close()
|
||
|
||
|
||
class TestNotificationService(unittest.TestCase):
|
||
def test_allowed_event_sent_and_senders_called(self):
|
||
calls = {"webhook": [], "slack": []}
|
||
service = NotificationService(
|
||
webhook_sender=lambda payload: calls["webhook"].append(payload),
|
||
slack_sender=lambda payload: calls["slack"].append(payload),
|
||
)
|
||
result = service.notify("completed", "task-1", "proj-1", "done")
|
||
self.assertEqual(result, {"sent": True, "channels": ["webhook", "slack"]})
|
||
self.assertEqual(len(calls["webhook"]), 1)
|
||
self.assertEqual(len(calls["slack"]), 1)
|
||
self.assertEqual(calls["webhook"][0]["event_type"], "completed")
|
||
self.assertEqual(calls["webhook"][0]["task_id"], "task-1")
|
||
|
||
def test_disallowed_event_not_sent_and_senders_not_called(self):
|
||
calls = []
|
||
service = NotificationService(
|
||
webhook_sender=lambda payload: calls.append(("webhook", payload)),
|
||
slack_sender=lambda payload: calls.append(("slack", payload)),
|
||
)
|
||
result = service.notify("running", "task-1", "proj-1")
|
||
self.assertEqual(result, {"sent": False, "reason": "event not allowed"})
|
||
self.assertEqual(calls, [])
|
||
|
||
def test_no_senders_does_not_error(self):
|
||
service = NotificationService()
|
||
result = service.notify("completed", "task-1", "proj-1")
|
||
self.assertEqual(result, {"sent": True, "channels": []})
|
||
|
||
def test_allowed_events_set(self):
|
||
self.assertEqual(
|
||
ALLOWED_NOTIFICATION_EVENTS,
|
||
{"completed", "failed", "goal-unmet", "merge-failed",
|
||
"review-requested"},
|
||
)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|