85 lines
3.3 KiB
TypeScript
85 lines
3.3 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 } from "../src/modules/admin/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 plaintext credentials", async () => {
|
|
const credential = { algorithm: "aes-256-gcm" };
|
|
const { service } = createService({
|
|
appId: "wxtestsettings",
|
|
apiBaseUrl: "https://wechat.example.com/api",
|
|
credential,
|
|
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.deepEqual(result.credential, { algorithm: "aes-256-gcm" });
|
|
assert.equal(result.privateKey, undefined);
|
|
});
|
|
|
|
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,
|
|
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);
|
|
});
|