feat: 支持按品牌配置微信登录凭据
This commit is contained in:
1 parent
e75c524343
commit
d0a0eeea8c
23 files changed
+460
-62
No files matched your search
@@ -0,0 +1,133 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { ServiceUnavailableException, UnauthorizedException } from "@nestjs/common";
|
||||
import { encryptMiniAppCredential } from "../src/miniapp-credential-crypto.js";
|
||||
import {
|
||||
WechatAuthService,
|
||||
resolveWechatLoginCredentials,
|
||||
type WechatLoginCredentials,
|
||||
} from "../src/wechat-auth.js";
|
||||
|
||||
process.env.SESSION_SECRET = "wechat-auth-test-secret-1234567890";
|
||||
|
||||
const credentials: WechatLoginCredentials = {
|
||||
appId: "wxbranchappid",
|
||||
appSecret: "0123456789abcdef0123456789abcdef",
|
||||
};
|
||||
|
||||
test("resolveWechatLoginCredentials requires a brand AppID", () => {
|
||||
assert.throws(
|
||||
() => resolveWechatLoginCredentials({}),
|
||||
(error: unknown) =>
|
||||
error instanceof ServiceUnavailableException && error.message.includes("AppID"),
|
||||
);
|
||||
});
|
||||
|
||||
test("resolveWechatLoginCredentials requires a brand AppSecret", () => {
|
||||
assert.throws(
|
||||
() => resolveWechatLoginCredentials({ appId: credentials.appId }),
|
||||
(error: unknown) =>
|
||||
error instanceof ServiceUnavailableException && error.message.includes("AppSecret"),
|
||||
);
|
||||
});
|
||||
|
||||
test("resolveWechatLoginCredentials decrypts the brand AppSecret", () => {
|
||||
const resolved = resolveWechatLoginCredentials({
|
||||
appId: credentials.appId,
|
||||
appSecretCredential: encryptMiniAppCredential(credentials.appSecret),
|
||||
});
|
||||
assert.deepEqual(resolved, credentials);
|
||||
});
|
||||
|
||||
test("resolveWechatLoginCredentials reports unusable encrypted credentials as unavailable", () => {
|
||||
assert.throws(
|
||||
() =>
|
||||
resolveWechatLoginCredentials({
|
||||
appId: credentials.appId,
|
||||
appSecretCredential: { algorithm: "aes-256-gcm", ciphertext: "broken" },
|
||||
}),
|
||||
(error: unknown) =>
|
||||
error instanceof ServiceUnavailableException && error.message.includes("凭据不可用"),
|
||||
);
|
||||
});
|
||||
|
||||
test("exchangeCode uses the credentials supplied for the requested brand", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
let requestedUrl = "";
|
||||
globalThis.fetch = async (input: string | URL | Request) => {
|
||||
requestedUrl =
|
||||
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
openid: "openid-1",
|
||||
session_key: "session-key-1",
|
||||
unionid: "unionid-1",
|
||||
}),
|
||||
{ status: 200, headers: { "content-type": "application/json" } },
|
||||
);
|
||||
};
|
||||
try {
|
||||
const session = await new WechatAuthService().exchangeCode("login-code", credentials);
|
||||
const url = new URL(requestedUrl);
|
||||
assert.equal(url.origin + url.pathname, "https://api.weixin.qq.com/sns/jscode2session");
|
||||
assert.equal(url.searchParams.get("appid"), credentials.appId);
|
||||
assert.equal(url.searchParams.get("secret"), credentials.appSecret);
|
||||
assert.equal(url.searchParams.get("js_code"), "login-code");
|
||||
assert.equal(url.searchParams.get("grant_type"), "authorization_code");
|
||||
assert.deepEqual(session, {
|
||||
openid: "openid-1",
|
||||
sessionKey: "session-key-1",
|
||||
unionid: "unionid-1",
|
||||
});
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test("exchangeCode surfaces WeChat rejection details without treating them as success", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async () =>
|
||||
new Response(JSON.stringify({ errcode: 40029, errmsg: "invalid code" }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
try {
|
||||
await assert.rejects(
|
||||
() => new WechatAuthService().exchangeCode("bad-code", credentials),
|
||||
(error: unknown) =>
|
||||
error instanceof UnauthorizedException && error.message === "invalid code",
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test("exchangeCode reports network and HTTP failures as service unavailable", async () => {
|
||||
const originalFetch = globalThis.fetch;
|
||||
try {
|
||||
globalThis.fetch = async () => {
|
||||
throw new Error("network down");
|
||||
};
|
||||
await assert.rejects(
|
||||
() => new WechatAuthService().exchangeCode("login-code", credentials),
|
||||
ServiceUnavailableException,
|
||||
);
|
||||
|
||||
globalThis.fetch = async () => new Response("upstream error", { status: 502 });
|
||||
await assert.rejects(
|
||||
() => new WechatAuthService().exchangeCode("login-code", credentials),
|
||||
ServiceUnavailableException,
|
||||
);
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
|
||||
test("exchangeCode rejects empty codes and incomplete credentials", async () => {
|
||||
const service = new WechatAuthService();
|
||||
await assert.rejects(() => service.exchangeCode(" ", credentials), UnauthorizedException);
|
||||
await assert.rejects(
|
||||
() => service.exchangeCode("login-code", { appId: "", appSecret: "" }),
|
||||
ServiceUnavailableException,
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user