formies/__tests__/unit/utils/apiKeyHelper.test.js
Mohamad.Elsena a3236ae9d5 Refactor environment configuration for PostgreSQL and enhance application structure
- Updated `.env` and `.env.test` files to include PostgreSQL connection settings and Redis configuration.
- Migrated database from SQLite to PostgreSQL, updating relevant queries and connection logic.
- Enhanced error handling and logging throughout the application.
- Added new test utilities for PostgreSQL integration and updated user model methods.
- Introduced new routes for user authentication and form management, ensuring compatibility with the new database structure.
- Created login and registration views in EJS for user interaction.
2025-05-28 16:16:33 +02:00

34 lines
1.1 KiB
JavaScript

// __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
);
});
});
});