Files
persionalBrand/apps/api/test/admin-miniapp-settings.service.test.ts
que01 d0a0eeea8c
CI / Browser smoke (pull_request) Successful in 58s
CI / Static checks (pull_request) Successful in 1m32s
CI / API integration (pull_request) Successful in 1m27s
CI / Production build (pull_request) Successful in 2m7s
feat: 支持按品牌配置微信登录凭据
2026-10-23 22:07:39 +08:00

151 lines
5.7 KiB
TypeScript

import assert from "node:assert/strict";
import { test } from "node:test";
import { BadRequestException } from "@nestjs/common";
import { generateKeyPairSync } from "node:crypto";
import { AdminMiniAppSettingsService } from "../src/modules/admin/admin-miniapp-settings.service.js";
import type { AdminMiniAppReleaseService } from "../src/modules/admin/admin-miniapp-release.service.js";
import {
decryptMiniAppCredential,
encryptMiniAppCredential,
} from "../src/miniapp-credential-crypto.js";
process.env.SESSION_SECRET = "settings-service-test-secret-1234567890";
const privateKey = generateKeyPairSync("rsa", {
modulusLength: 2048,
privateKeyEncoding: { type: "pkcs8", format: "pem" },
publicKeyEncoding: { type: "spki", format: "pem" },
}).privateKey;
function createService(settings: Record<string, unknown> = {}) {
const saved: Record<string, unknown>[] = [];
const releases = {
settings: async () => structuredClone(settings),
saveSettings: async (_brandId: string, value: Record<string, unknown>) => {
saved.push(value);
},
} as unknown as AdminMiniAppReleaseService;
return { service: new AdminMiniAppSettingsService(releases), saved };
}
test("settings presentation never exposes encrypted credential payloads", async () => {
const credential = { algorithm: "aes-256-gcm" };
const appSecretCredential = {
algorithm: "aes-256-gcm",
ciphertext: "encrypted-secret",
iv: "iv",
authTag: "tag",
};
const { service } = createService({
appId: "wxtestsettings",
apiBaseUrl: "https://wechat.example.com/api",
credential,
appSecretCredential,
build: { keep: true },
});
const result = (await service.get("brand-1")) as Record<string, unknown>;
assert.equal(result.appId, "wxtestsettings");
assert.equal(result.apiBaseUrl, "https://wechat.example.com/api");
assert.equal(result.requestDomain, "wechat.example.com");
assert.equal(result.hasStoredCredentials, true);
assert.equal(result.hasStoredAppSecret, true);
assert.equal(result.loginConfigured, true);
assert.deepEqual(result.credential, { algorithm: "aes-256-gcm" });
assert.equal(result.privateKey, undefined);
assert.equal("appSecretCredential" in result, false);
assert.equal(JSON.stringify(result).includes("encrypted-secret"), false);
});
test("update validates URL and private key before persistence", async () => {
const { service } = createService();
await assert.rejects(
() => service.update("brand-1", { apiBaseUrl: "http://wechat.example.com/api" }),
BadRequestException,
);
await assert.rejects(
() =>
service.update("brand-1", {
privateKey: "-----BEGIN PRIVATE KEY-----\nMIIB\n-----END PRIVATE KEY-----",
}),
BadRequestException,
);
});
test("update encrypts a valid private key and preserves existing settings", async () => {
const { service, saved } = createService({
appId: "wxold",
apiBaseUrl: "https://old.example.com/api",
build: { keep: true },
});
const result = (await service.update("brand-1", {
appId: "wxnew",
apiBaseUrl: "https://new.example.com/api/",
privateKey,
appSecret: "0123456789abcdef0123456789abcdef",
build: { extra: true },
})) as Record<string, unknown>;
assert.equal(saved.length, 1);
assert.equal(saved[0]?.appId, "wxnew");
assert.equal(saved[0]?.apiBaseUrl, "https://new.example.com/api");
assert.deepEqual((saved[0]?.build as Record<string, unknown>) ?? {}, {
keep: true,
extra: true,
});
const credential = saved[0]?.credential as Record<string, string>;
assert.equal(credential.algorithm, "aes-256-gcm");
assert.equal(decryptMiniAppCredential(credential), privateKey);
assert.ok(!JSON.stringify(saved[0]).includes(privateKey));
assert.equal(result.hasStoredCredentials, true);
});
test("update encrypts the WeChat login AppSecret without exposing it", async () => {
const appSecret = "0123456789abcdef0123456789abcdef";
const { service, saved } = createService({
appId: "wxtestsettings",
apiBaseUrl: "https://wechat.example.com/api",
});
const result = (await service.update("brand-1", { appSecret })) as Record<string, unknown>;
assert.equal(saved.length, 1);
const appSecretCredential = saved[0]?.appSecretCredential as Record<string, string>;
assert.equal(appSecretCredential.algorithm, "aes-256-gcm");
assert.equal(decryptMiniAppCredential(appSecretCredential), appSecret);
assert.equal(JSON.stringify(saved[0]).includes(appSecret), false);
assert.equal(result.hasStoredAppSecret, true);
assert.equal(result.loginConfigured, true);
assert.equal("appSecretCredential" in result, false);
});
test("update rejects malformed WeChat login AppSecret", async () => {
const { service, saved } = createService();
await assert.rejects(
() => service.update("brand-1", { appSecret: "not-a-valid-secret!" }),
BadRequestException,
);
assert.equal(saved.length, 0);
});
test("changing AppID requires the matching AppSecret", async () => {
const appSecret = "0123456789abcdef0123456789abcdef";
const { service, saved } = createService({
appId: "wxold",
appSecretCredential: encryptMiniAppCredential(appSecret),
});
await assert.rejects(
() => service.update("brand-1", { appId: "wxnew" }),
/必须同时填写该 AppID 对应的 AppSecret/,
);
assert.equal(saved.length, 0);
});
test("leaving AppSecret empty preserves the stored credential", async () => {
const appSecret = "0123456789abcdef0123456789abcdef";
const storedCredential = encryptMiniAppCredential(appSecret);
const { service, saved } = createService({
appId: "wxtestsettings",
appSecretCredential: storedCredential,
});
await service.update("brand-1", { appId: "wxtestsettings" });
assert.equal(saved.length, 1);
assert.deepEqual(saved[0]?.appSecretCredential, storedCredential);
});