Files
wmailhub/tests/settings.test.ts
T
bigcode fec96e49cc feat: v2.0.0 邮件接收架构改造 —— 移除 Node,Email Worker 直连 Console(docs/11)
- 两阶段接收:POST /internal/email/authorize(accept/reject/temporary_error)+ POST /internal/email/ingest(raw MIME 直传)
- Worker-Console 协议:Authorization: WmailHub-Worker + timestamp/nonce/body-hash/HMAC,nonce 防重放,凭据撤销即时生效
- 数据模型:nodes/node_credentials -> cloudflare_accounts/worker_credentials(迁移 0003),emails/domains 归属 account_id
- 接入流程 Connect Cloudflare Account:OAuth 自动同步域名 + 部署账号级 Email Worker + 签发 Worker 凭据;rotate 自动重部署
- 动态邮箱:创建/删除/禁用下一封邮件立即生效;拒绝码 DOMAIN_*/MAILBOX_*;超时/5xx 不伪装成邮箱不存在
- 删除 node/ 目录、旧 /api/internal/nodes 协议、ingress/enrollment 概念;dev.ts 简化为 console + email worker 双进程
- 前端:账号页 Email Worker/凭据管理,设置窗口移除默认 Ingress,仪表盘/文案同步;版本 2.0.0
- 测试 62 项全过(新增 authorize 决策矩阵 + Worker 两阶段临时错误语义);本机虚拟 E2E 12 场景全通
- docs 00-10 同步改写;README 更新
2026-09-30 13:10:26 +08:00

94 lines
3.9 KiB
TypeScript

/** 设置测试:全局 Webhook 读写与 key 语义、修改密码 */
import { describe, expect, test } from "bun:test";
import { authedFetch, loginCookie, makeTestEnv } from "./helpers.ts";
describe("设置(/api/settings)", () => {
test("全局 Webhook:保存 -> 读取(仅回 last4);key 缺省保留、空格清除", async () => {
const t = await makeTestEnv();
const cookie = await loginCookie(t);
// 保存(带 key)
const save = await authedFetch(t, cookie, "http://localhost/api/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ webhook: { url: "https://hooks.example.com/a", headers: { "X-A": "1" }, key: "wmh_key_abcdefgh" } }),
});
expect(save.status).toBe(200);
let info = (await (
await authedFetch(t, cookie, "http://localhost/api/settings")
).json()) as { webhook: { url: string; has_key: boolean; key_last4: string; headers: Record<string, string> } };
expect(info.webhook.url).toBe("https://hooks.example.com/a");
expect(info.webhook.has_key).toBe(true);
expect(info.webhook.key_last4).toBe("efgh");
expect(info.webhook.headers).toEqual({ "X-A": "1" });
// 再次保存不带 key:key 保留
await authedFetch(t, cookie, "http://localhost/api/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ webhook: { url: "https://hooks.example.com/b" } }),
});
info = (await (await authedFetch(t, cookie, "http://localhost/api/settings")).json()) as typeof info;
expect(info.webhook.url).toBe("https://hooks.example.com/b");
expect(info.webhook.has_key).toBe(true);
// key 传空格:清除 key
await authedFetch(t, cookie, "http://localhost/api/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ webhook: { url: "https://hooks.example.com/b", key: " " } }),
});
info = (await (await authedFetch(t, cookie, "http://localhost/api/settings")).json()) as typeof info;
expect(info.webhook.has_key).toBe(false);
// url 置空:停用全局 webhook
await authedFetch(t, cookie, "http://localhost/api/settings", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ webhook: { url: "" } }),
});
info = (await (await authedFetch(t, cookie, "http://localhost/api/settings")).json()) as typeof info;
expect(info.webhook.url).toBe("");
});
test("修改密码:原密码错误被拒;成功后新密码可登录", async () => {
const t = await makeTestEnv();
const cookie = await loginCookie(t);
const bad = await authedFetch(t, cookie, "http://localhost/api/settings/password", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ old_password: "wrong", new_password: "newpass123" }),
});
expect(bad.status).toBe(400);
const ok = await authedFetch(t, cookie, "http://localhost/api/settings/password", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ old_password: "admin", new_password: "newpass123" }),
});
expect(ok.status).toBe(200);
// 旧密码登录失败,新密码登录成功
const oldLogin = await t.app.fetch(
new Request("http://localhost/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: "admin", password: "admin" }),
}),
t.env,
);
expect(oldLogin.status).toBe(401);
const newLogin = await t.app.fetch(
new Request("http://localhost/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: "admin", password: "newpass123" }),
}),
t.env,
);
expect(newLogin.status).toBe(200);
});
});