136 lines
4.0 KiB
JavaScript
Raw Normal View History

2026-07-30 16:49:41 +08:00
jest.mock("../../utils/sm4-util", () => ({
encryptDataForSm3: jest.fn(() => "encrypted-card-data"),
2026-07-31 16:22:09 +08:00
decryptDataForSm3: jest.fn(() => JSON.stringify({
CanOpen: true,
Success: true,
IDNum: "330601199001011234",
Name: "张三",
CardNum: "DB5712345",
CardIDCode: "3306012345678901A",
})),
2026-07-30 16:49:41 +08:00
}));
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();
});
2026-07-31 16:22:09 +08:00
test("decrypts a scanned card that belongs to the current store", async () => {
const findOne = jest.fn().mockResolvedValue({
encryptedCardData: "encrypted-card-data",
encryption: "SM4",
corpId: "corp-1",
storeIds: ["store-1"],
});
const db = {
collection: jest.fn(() => ({ findOne })),
};
const result = await scannedCardArchive({
type: "getScannedCard",
id: "507f1f77bcf86cd799439011",
corpId: "corp-1",
storeId: "store-1",
}, db);
expect(result.success).toBe(true);
expect(result.data).toEqual(expect.objectContaining({
IDNum: "330601199001011234",
Name: "张三",
CardNum: "DB5712345",
CardIDCode: "3306012345678901A",
}));
expect(Sm4Util.decryptDataForSm3).toHaveBeenCalledWith("encrypted-card-data");
});
test("rejects invalid ids and records from another store", async () => {
const db = {
collection: jest.fn(() => ({
findOne: jest.fn().mockResolvedValue({
encryptedCardData: "encrypted-card-data",
encryption: "SM4",
corpId: "corp-1",
storeIds: ["store-2"],
}),
})),
};
await expect(scannedCardArchive({
type: "getScannedCard",
id: "invalid",
}, db)).resolves.toEqual({ success: false, message: "二维码无效" });
await expect(scannedCardArchive({
type: "getScannedCard",
id: "507f1f77bcf86cd799439011",
corpId: "corp-1",
storeId: "store-1",
}, db)).resolves.toEqual({ success: false, message: "无权读取本次扫码信息" });
});
2026-07-30 16:49:41 +08:00
});