R81 补充企微回包字段取证

这个提交包含在:
2026-05-04 18:10:55 +08:00
父节点 9dbac63744
当前提交 849fd35638
共修改 3 个文件,包含 90 行新增和 7 行删除
+68 -5
查看文件
@@ -31,8 +31,8 @@ function buildPrisma(overrides = {}) {
};
}
async function buildApp({ config = {}, prisma = buildPrisma() } = {}) {
const app = Fastify();
async function buildApp({ config = {}, prisma = buildPrisma(), fastifyOptions = {} } = {}) {
const app = Fastify(fastifyOptions);
await app.register(fastifyCookie);
app.decorate("config", {
appOrigin: "https://oa.hyinda.com",
@@ -87,6 +87,18 @@ function mockWecomFetch({ userId = "wecom-user-1", openId = "wecom-open-1" } = {
});
}
function mockWecomFetchWithPayload(payload) {
global.fetch = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: async () => ({ errcode: 0, access_token: "access-token" }),
})
.mockResolvedValueOnce({
ok: true,
json: async () => payload,
});
}
afterEach(() => {
global.fetch = originalFetch;
vi.restoreAllMocks();
@@ -178,8 +190,26 @@ describe("wecom auth routes R27", () => {
});
it("returns structured diagnostics when wecom callback has only open id", async () => {
mockWecomFetch({ userId: "", openId: "wecom-open-only" });
const app = await buildApp();
const logs = [];
mockWecomFetchWithPayload({
errcode: 0,
errmsg: "ok",
openid: "lowercase-openid-value",
user_ticket: "ticket-value",
corpid: "corp-id-value",
});
const app = await buildApp({
fastifyOptions: {
logger: {
level: "warn",
stream: {
write(message) {
logs.push(JSON.parse(message));
},
},
},
},
});
const state = signPayload({
exp: Date.now() + 60_000,
nonce: "nonce-openid-only",
@@ -198,7 +228,40 @@ describe("wecom auth routes R27", () => {
message: "企业微信认证失败",
details: expect.objectContaining({
wecom: "missing_user_id",
hasOpenId: true,
hasOpenId: false,
mode: "login",
}),
}));
expect(logs.some((entry) => (
entry.msg === "wecom callback missing internal user id"
&& entry.mode === "login"
&& entry.hasUserId === false
&& entry.hasOpenId === false
&& entry.hasDeviceId === false
&& entry.wecomErrCode === 0
&& entry.wecomErrMsg === "ok"
&& Array.isArray(entry.payloadKeys)
&& entry.payloadKeys.join(",") === "corpid,errcode,errmsg,openid,user_ticket"
&& entry.fieldPresence?.UserId === false
&& entry.fieldPresence?.userid === false
&& entry.fieldPresence?.OpenId === false
&& entry.fieldPresence?.openid === true
&& entry.fieldPresence?.external_userid === false
&& entry.fieldPresence?.user_ticket === true
&& entry.fieldPresence?.corpid === true
))).toBe(true);
expect(logs.some((entry) => (
entry.msg === "wecom callback missing internal user id"
&& typeof entry.requestId === "string"
))).toBe(true);
expect(response.json().error).toEqual(expect.objectContaining({
code: "AUTH_003",
message: "企业微信认证失败",
details: expect.objectContaining({
wecom: "missing_user_id",
hasOpenId: false,
mode: "login",
}),
}));