2026-07-30 16:49:41 +08:00

75 lines
2.2 KiB
JavaScript

jest.mock("../../utils/sm4-util", () => ({
encryptDataForSm3: jest.fn(() => "encrypted-card-data"),
}));
const Sm4Util = require("../../utils/sm4-util");
const scannedCardArchive = require("./index");
describe("scanned-card-archive", () => {
test("encrypts the full normalized card payload and only stores ciphertext", async () => {
const insertOne = jest.fn().mockResolvedValue({
insertedId: { toString: () => "archive-id" },
});
const db = {
collection: jest.fn(() => ({ insertOne })),
};
const cardData = {
CanOpen: true,
CardIDCode: "3306012345678901A",
CardNum: "DB5712345",
CardType: "3",
Gender: "",
IDNum: "330601199001011234",
Name: "张三",
PsamTN: "330412345678",
Success: true,
flag: "1",
ylbxtcqbm: "330600",
ignoredField: "must-not-be-saved",
};
const result = await scannedCardArchive({
type: "saveScannedCard",
cardData,
readerType: "ZJ_HZ_310000",
corpId: "corp-1",
storeIds: ["store-1"],
}, db);
expect(result).toEqual({
success: true,
id: "archive-id",
message: "卡片数据保存成功",
});
expect(Sm4Util.encryptDataForSm3).toHaveBeenCalledTimes(1);
const encryptedInput = JSON.parse(Sm4Util.encryptDataForSm3.mock.calls[0][0]);
expect(encryptedInput).toEqual(expect.objectContaining({
IDNum: cardData.IDNum,
Name: cardData.Name,
Success: true,
}));
expect(encryptedInput).not.toHaveProperty("ignoredField");
const stored = insertOne.mock.calls[0][0];
expect(stored.encryptedCardData).toBe("encrypted-card-data");
expect(stored).not.toHaveProperty("IDNum");
expect(stored).not.toHaveProperty("Name");
expect(stored).not.toHaveProperty("cardData");
});
test("rejects unsuccessful or malformed card payloads", async () => {
const db = { collection: jest.fn() };
const result = await scannedCardArchive({
type: "saveScannedCard",
cardData: {
CanOpen: false,
Success: false,
Message: "-2无卡",
},
}, db);
expect(result).toEqual({ success: false, message: "卡片数据无效" });
expect(db.collection).not.toHaveBeenCalled();
});
});