You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.0 KiB

import { describe, expect, it } from "vitest";
import {
decryptServiceToken,
encryptServiceToken,
generateServiceToken,
hashServiceToken,
tokenKeyPrefix,
} from "@/modules/customer/token-crypto";
describe("token-crypto", () => {
it("可加密并解密 token 原文", () => {
process.env.JWT_SECRET = "unit-test-secret";
const token = generateServiceToken();
const ciphertext = encryptServiceToken(token);
expect(ciphertext).not.toBe(token);
expect(decryptServiceToken(ciphertext)).toBe(token);
});
it("生成 chj_ 前缀 token", () => {
const token = generateServiceToken();
expect(token.startsWith("chj_")).toBe(true);
expect(token.length).toBeGreaterThan(20);
});
it("hash 稳定", () => {
const token = "chj_test_token";
expect(hashServiceToken(token)).toHaveLength(64);
expect(hashServiceToken(token)).toBe(hashServiceToken(token));
});
it("key prefix 用于列表展示", () => {
const token = generateServiceToken();
expect(tokenKeyPrefix(token).length).toBeGreaterThan(0);
});
});