36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
|
|
import { createHash } from "crypto";
|
||
|
|
import { describe, expect, test } from "vitest";
|
||
|
|
import { buildSignedRequest, dreameSign, stringifyBody } from "../app/services/signature.server";
|
||
|
|
|
||
|
|
describe("dreame signature", () => {
|
||
|
|
test("generates stable 32-char lowercase md5", () => {
|
||
|
|
const bodyString = '{"activity_id":12}';
|
||
|
|
const timestamp = "1776901800000";
|
||
|
|
const secret = "abc123secret";
|
||
|
|
const expected = createHash("md5")
|
||
|
|
.update(Buffer.from(`${bodyString}${timestamp}${secret}`, "utf8"))
|
||
|
|
.digest("hex");
|
||
|
|
|
||
|
|
const sign = dreameSign(bodyString, timestamp, secret);
|
||
|
|
|
||
|
|
expect(sign).toBe(expected);
|
||
|
|
expect(sign).toMatch(/^[a-f0-9]{32}$/);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("uses the same serialized body for signing and fetch", () => {
|
||
|
|
const payload = { activity_id: 12 };
|
||
|
|
const bodyString = stringifyBody(payload);
|
||
|
|
const signed = buildSignedRequest(payload, {
|
||
|
|
apiAppId: "app-id",
|
||
|
|
apiSecret: "secret",
|
||
|
|
timestamp: "1776901800000",
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(signed.bodyString).toBe(bodyString);
|
||
|
|
expect(signed.bodyString).toBe('{"activity_id":12}');
|
||
|
|
expect(signed.headers["dreame-api-sign"]).toBe(
|
||
|
|
dreameSign(bodyString, "1776901800000", "secret"),
|
||
|
|
);
|
||
|
|
});
|
||
|
|
});
|