sign.spec.js 1.9 KB
const sign = require("@/libs/rop/signCore");
const buildRopPayload = require("@/libs/rop/buildRopPayload");

describe("rop signCore", () => {
	it("相同参数应生成稳定签名", () => {
		const secret = "test-secret";
		const params = buildRopPayload("appkey", "dj.api.user.login", {
			userName: "demo",
			password: "abc",
		}, null);
		params.timestamp = 1700000000000;
		const first = sign(params, [], secret, "sha1");
		const second = sign({ ...params }, [], secret, "sha1");
		expect(first.sign).toBe(second.sign);
		expect(first.sign).toMatch(/^[A-F0-9]{40}$/);
	});

	it("修改参数后签名应变化", () => {
		const secret = "test-secret";
		const base = buildRopPayload("appkey", "dj.api.test", { foo: "bar" });
		base.timestamp = 1700000000000;
		const signedA = sign(base, [], secret, "sha1");
		const signedB = sign({ ...base, foo: "baz" }, [], secret, "sha1");
		expect(signedA.sign).not.toBe(signedB.sign);
	});

	it("recordList 数组应返回扁平化参数(与 axios 发送形态一致)", () => {
		const secret = "test-secret";
		const params = buildRopPayload(
			"appkey",
			"clue.api.burial.point.record.batch.insert",
			{
				recordList: [
					{ loginAccount: "demo", uuid: "u1", userType: 1 },
				],
			},
		);
		params.timestamp = 1700000000000;
		const signed = sign(params, [], secret, "sha1");

		expect(signed.recordList).toBeUndefined();
		expect(signed["recordList[0].loginAccount"]).toBe("demo");
		expect(signed["recordList[0].uuid"]).toBe("u1");
		expect(signed.sign).toMatch(/^[A-F0-9]{40}$/);

		const again = sign({ ...params }, [], secret, "sha1");
		expect(again.sign).toBe(signed.sign);
	});

	it("md5 签名方式应可用", () => {
		const secret = "test-secret";
		const params = buildRopPayload("appkey", "glsx.api.test", { foo: "bar" });
		params.timestamp = 1700000000000;
		const signed = sign(params, [], secret, "md5");
		expect(signed.sign).toMatch(/^[A-F0-9]{32}$/);
	});
});