mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-26 11:48:27 -05:00
c03e60ac0b
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: pandeymangg <anshuman.pandey9999@gmail.com>
179 lines
5.4 KiB
TypeScript
179 lines
5.4 KiB
TypeScript
import { hashApiKey } from "@/modules/api/v2/management/lib/utils";
|
|
import { getApiKeyWithPermissions } from "@/modules/organization/settings/api-keys/lib/api-key";
|
|
import { hasPermission } from "@/modules/organization/settings/api-keys/lib/utils";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { prisma } from "@formbricks/database";
|
|
import { TAPIKeyEnvironmentPermission } from "@formbricks/types/auth";
|
|
import { authenticateRequest } from "./auth";
|
|
|
|
vi.mock("@formbricks/database", () => ({
|
|
prisma: {
|
|
apiKey: {
|
|
findUnique: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("@/modules/api/v2/management/lib/utils", () => ({
|
|
hashApiKey: vi.fn(),
|
|
}));
|
|
|
|
describe("getApiKeyWithPermissions", () => {
|
|
it("should return API key data with permissions when valid key is provided", async () => {
|
|
const mockApiKeyData = {
|
|
id: "api-key-id",
|
|
organizationId: "org-id",
|
|
hashedKey: "hashed-key",
|
|
createdAt: new Date(),
|
|
createdBy: "user-id",
|
|
lastUsedAt: null,
|
|
label: "Test API Key",
|
|
apiKeyEnvironments: [
|
|
{
|
|
environmentId: "env-1",
|
|
permission: "manage" as const,
|
|
environment: { id: "env-1" },
|
|
},
|
|
],
|
|
};
|
|
|
|
vi.mocked(hashApiKey).mockReturnValue("hashed-key");
|
|
vi.mocked(prisma.apiKey.findUnique).mockResolvedValue(mockApiKeyData);
|
|
vi.mocked(prisma.apiKey.update).mockResolvedValue(mockApiKeyData);
|
|
|
|
const result = await getApiKeyWithPermissions("test-api-key");
|
|
|
|
expect(result).toEqual(mockApiKeyData);
|
|
expect(prisma.apiKey.update).toHaveBeenCalledWith({
|
|
where: { id: "api-key-id" },
|
|
data: { lastUsedAt: expect.any(Date) },
|
|
});
|
|
});
|
|
|
|
it("should return null when API key is not found", async () => {
|
|
vi.mocked(prisma.apiKey.findUnique).mockResolvedValue(null);
|
|
|
|
const result = await getApiKeyWithPermissions("invalid-key");
|
|
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("hasPermission", () => {
|
|
const permissions: TAPIKeyEnvironmentPermission[] = [
|
|
{
|
|
environmentId: "env-1",
|
|
permission: "manage",
|
|
environmentType: "development",
|
|
projectId: "project-1",
|
|
projectName: "Project 1",
|
|
},
|
|
{
|
|
environmentId: "env-2",
|
|
permission: "write",
|
|
environmentType: "production",
|
|
projectId: "project-2",
|
|
projectName: "Project 2",
|
|
},
|
|
{
|
|
environmentId: "env-3",
|
|
permission: "read",
|
|
environmentType: "development",
|
|
projectId: "project-3",
|
|
projectName: "Project 3",
|
|
},
|
|
];
|
|
|
|
it("should return true for manage permission with any method", () => {
|
|
expect(hasPermission(permissions, "env-1", "GET")).toBe(true);
|
|
expect(hasPermission(permissions, "env-1", "POST")).toBe(true);
|
|
expect(hasPermission(permissions, "env-1", "DELETE")).toBe(true);
|
|
});
|
|
|
|
it("should handle write permission correctly", () => {
|
|
expect(hasPermission(permissions, "env-2", "GET")).toBe(true);
|
|
expect(hasPermission(permissions, "env-2", "POST")).toBe(true);
|
|
expect(hasPermission(permissions, "env-2", "DELETE")).toBe(false);
|
|
});
|
|
|
|
it("should handle read permission correctly", () => {
|
|
expect(hasPermission(permissions, "env-3", "GET")).toBe(true);
|
|
expect(hasPermission(permissions, "env-3", "POST")).toBe(false);
|
|
expect(hasPermission(permissions, "env-3", "DELETE")).toBe(false);
|
|
});
|
|
|
|
it("should return false for non-existent environment", () => {
|
|
expect(hasPermission(permissions, "env-4", "GET")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("authenticateRequest", () => {
|
|
it("should return authentication data for valid API key", async () => {
|
|
const request = new Request("http://localhost", {
|
|
headers: { "x-api-key": "valid-api-key" },
|
|
});
|
|
|
|
const mockApiKeyData = {
|
|
id: "api-key-id",
|
|
organizationId: "org-id",
|
|
hashedKey: "hashed-key",
|
|
createdAt: new Date(),
|
|
createdBy: "user-id",
|
|
lastUsedAt: null,
|
|
label: "Test API Key",
|
|
apiKeyEnvironments: [
|
|
{
|
|
environmentId: "env-1",
|
|
permission: "manage" as const,
|
|
environment: {
|
|
id: "env-1",
|
|
projectId: "project-1",
|
|
project: { name: "Project 1" },
|
|
type: "development",
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
vi.mocked(hashApiKey).mockReturnValue("hashed-key");
|
|
vi.mocked(prisma.apiKey.findUnique).mockResolvedValue(mockApiKeyData);
|
|
vi.mocked(prisma.apiKey.update).mockResolvedValue(mockApiKeyData);
|
|
|
|
const result = await authenticateRequest(request);
|
|
|
|
expect(result).toEqual({
|
|
type: "apiKey",
|
|
environmentPermissions: [
|
|
{
|
|
environmentId: "env-1",
|
|
permission: "manage",
|
|
environmentType: "development",
|
|
projectId: "project-1",
|
|
projectName: "Project 1",
|
|
},
|
|
],
|
|
hashedApiKey: "hashed-key",
|
|
apiKeyId: "api-key-id",
|
|
organizationId: "org-id",
|
|
});
|
|
});
|
|
|
|
it("should return null when no API key is provided", async () => {
|
|
const request = new Request("http://localhost");
|
|
const result = await authenticateRequest(request);
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("should return null when API key is invalid", async () => {
|
|
const request = new Request("http://localhost", {
|
|
headers: { "x-api-key": "invalid-api-key" },
|
|
});
|
|
|
|
vi.mocked(prisma.apiKey.findUnique).mockResolvedValue(null);
|
|
|
|
const result = await authenticateRequest(request);
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|