// __tests__/unit/utils/apiKeyHelper.test.js const { generateApiKeyParts, hashApiKeySecret, compareApiKeySecret, API_KEY_IDENTIFIER_PREFIX, } = require("../../../src/utils/apiKeyHelper"); // Adjust path describe("API Key Helper", () => { describe("generateApiKeyParts", () => { it("should generate an API key with correct prefix, identifier, and secret", () => { const { fullApiKey, identifier, secret } = generateApiKeyParts(); expect(identifier).toMatch( new RegExp(`^${API_KEY_IDENTIFIER_PREFIX}_[a-f0-9]{12}$`) ); expect(secret).toMatch(/^[a-f0-9]{64}$/); // 32 bytes = 64 hex chars expect(fullApiKey).toBe(`${identifier}_${secret}`); }); }); describe("hashApiKeySecret and compareApiKeySecret", () => { it("should correctly hash and compare a secret", async () => { const secret = "mySuperSecretApiKeyPart"; const hashedSecret = await hashApiKeySecret(secret); expect(hashedSecret).not.toBe(secret); expect(await compareApiKeySecret(secret, hashedSecret)).toBe(true); expect(await compareApiKeySecret("wrongSecret", hashedSecret)).toBe( false ); }); }); });