sign.spec.js
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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}$/);
});
});