🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
import {
makeZodVerifiedAPICall,
makeAPICall,
} from "@/src/__tests__/test-utils";
import { z } from "zod";
import {
createApiKey,
createBasicAuthHeader,
createOrgProjectAndApiKey,
invalidateCachedOrgApiKeys,
} from "@langfuse/shared/src/server";
import { prisma } from "@langfuse/shared/src/db";
import {
OrganizationId,
ProjectId,
SystemRoleId,
UserId,
} from "@langfuse/shared/rbac";
import { randomUUID } from "crypto";
import { type NextApiRequest, type NextApiResponse } from "next";
import { createMocks } from "node-mocks-http";
import projectApiKeysHandler from "@/src/pages/api/public/projects/[projectId]/apiKeys";
import projectApiKeyHandler from "@/src/pages/api/public/projects/[projectId]/apiKeys/[apiKeyId]";
import projectByIdHandler from "@/src/pages/api/public/projects/[projectId]";
import { authenticator } from "@/src/features/apiKey/authenticator";
import { type AuthorizationContext } from "@/src/features/auth/policy/types";
import {
organizationApiKeyCreationSchema,
projectApiKeyCreationSchema,
} from "@/src/ee/features/admin-api/server/apiKeys";
import { env } from "@/src/env.mjs";
vi.mock("@/src/env.mjs", async (importOriginal) => {
const actual = await importOriginal<{ env: typeof env }>();
return { ...actual, env: { ...actual.env } };
});
// Schema for project response
const ProjectResponseSchema = z.object({
data: z.array(
z.object({
id: z.string(),
name: z.string(),
organization: z.object({
id: z.string(),
name: z.string(),
}),
metadata: z.object({}).optional(),
retentionDays: z.number().nullable().optional(),
}),
),
});
// Schema for project creation response
const ProjectCreationResponseSchema = z.object({
id: z.string(),
name: z.string(),
metadata: z.object({}).optional(),
retentionDays: z.number().nullable().optional(),
});
// Schema for project update response
const ProjectUpdateResponseSchema = z.object({
id: z.string(),
name: z.string(),
metadata: z.object({}).optional(),
retentionDays: z.number().nullable().optional(),
});
// Schema for project deletion response
const ProjectDeletionResponseSchema = z.object({
success: z.boolean(),
message: z.string(),
});
// Schema for API keys response
const ApiKeysResponseSchema = z.object({
apiKeys: z.array(
z.object({
id: z.string(),
createdAt: z.string().or(z.date()),
expiresAt: z.string().or(z.date()).nullable(),
lastUsedAt: z.string().or(z.date()).nullable(),
name: z.string().nullable(),
note: z.string().nullable(),
role: z.string().nullable(),
publicKey: z.string(),
displaySecretKey: z.string().nullable(),
}),
),
});
// Schema for API key creation response
const ApiKeyCreationResponseSchema = z.object({
id: z.string(),
publicKey: z.string(),
secretKey: z.string(),
displaySecretKey: z.string(),
name: z.string().nullable(),
note: z.string().nullable(),
role: z.string(),
createdAt: z.string().or(z.date()),
expiresAt: z.iso.datetime().nullable(),
});
// Schema for API key deletion response
const ApiKeyDeletionResponseSchema = z.object({
success: z.boolean(),
});
describe("REST API key creation schemas", () => {
const originalFlags = {
API_AUTH_MIGRATION: env.API_AUTH_MIGRATION,
API_KEY_PROJECT_ROLES_ENABLE: env.API_KEY_PROJECT_ROLES_ENABLE,
API_KEY_ORG_ROLES_ENABLE: env.API_KEY_ORG_ROLES_ENABLE,
};
afterEach(() => {
Object.assign(env, originalFlags);
});
describe.each([
{
scope: "project",
schema: projectApiKeyCreationSchema,
legacyRole: "LEGACY_PROJECT_API_KEY",
roles: ["ADMIN", "VIEWER", "INGEST", "SCORES_INGEST"],
invalidRoles: [
"LEGACY_PROJECT_API_KEY",
"LEGACY_ORGANIZATION_API_KEY",
"AI_GATEWAY",
"OWNER",
"",
"INVALID",
[],
],
},
{
scope: "organization",
schema: organizationApiKeyCreationSchema,
legacyRole: "LEGACY_ORGANIZATION_API_KEY",
roles: ["ADMIN", "VIEWER", "INGEST", "SCORES_INGEST", "AI_GATEWAY"],
invalidRoles: [
"LEGACY_PROJECT_API_KEY",
"LEGACY_ORGANIZATION_API_KEY",
"OWNER",
"",
"INVALID",
[],
],
},
])("$scope", ({ scope, schema, legacyRole, roles, invalidRoles }) => {
describe.each([
{ migration: "legacy", projectEnabled: "true", orgEnabled: "true" },
{ migration: "shadow", projectEnabled: "true", orgEnabled: "true" },
{ migration: "enforce", projectEnabled: "false", orgEnabled: "false" },
{ migration: "enforce", projectEnabled: "true", orgEnabled: "false" },
{ migration: "enforce", projectEnabled: "false", orgEnabled: "true" },
{ migration: "enforce", projectEnabled: "true", orgEnabled: "true" },
])(
"$migration, project roles $projectEnabled, organization roles $orgEnabled",
({ migration, projectEnabled, orgEnabled }) => {
beforeEach(() => {
Object.assign(env, {
API_AUTH_MIGRATION: migration,
API_KEY_PROJECT_ROLES_ENABLE: projectEnabled,
API_KEY_ORG_ROLES_ENABLE: orgEnabled,
});
});
it.each([
{ label: "omitted", body: {} },
{ label: "undefined", body: { role: undefined } },
{ label: "null", body: { role: null } },
])("defaults $label role to the stored legacy role", ({ body }) => {
expect(schema.parse(body).role).toBe(legacyRole);
});
it.each(roles)("gates %s on role exposure", (role) => {
const result = schema.safeParse({ role });
const rolesEnabled =
migration === "enforce" &&
(scope === "project" ? projectEnabled : orgEnabled) === "true";
if (rolesEnabled) {
expect(result.success).toBe(true);
expect(result.data?.role).toBe(role);
} else {
expect(result.success).toBe(false);
}
});
it.each(invalidRoles.map((role) => ({ role })))(
"rejects role $role",
({ role }) => {
expect(schema.safeParse({ role }).success).toBe(false);
},
);
},
);
});
});
describe("Projects API", () => {
// Test variables
const projectId = "7a88fb47-b4e2-43b8-a06c-a5ce950dc53a";
const projectName = "Seed Project";
const projectApiKey = "pk-lf-1234567890";
const projectSecretKey = "sk-lf-1234567890";
const invalidApiKey = "pk-lf-invalid";
const invalidSecretKey = "sk-lf-invalid";
const orgApiKey = `pk-lf-org-${randomUUID().substring(0, 8)}`;
const orgSecretKey = `sk-lf-org-${randomUUID().substring(0, 8)}`;
let orgApiKeyId: string;
beforeAll(async () => {
const keyCreator = await prisma.user.create({
data: { email: `apikey-creator-${randomUUID()}@example.com` },
});
const orgKey = await createApiKey(prisma, {
tenant: OrganizationId("seed-org-id"),
scope: OrganizationId("seed-org-id"),
role: SystemRoleId("LEGACY_ORGANIZATION_API_KEY"),
createdBy: UserId(keyCreator.id),
predefinedKeys: {
publicKey: orgApiKey,
secretKey: orgSecretKey,
},
});
orgApiKeyId = orgKey.id;
});
describe("GET /api/public/projects", () => {
it("should return project data with valid project API key authentication", async () => {
const response = await makeZodVerifiedAPICall(
ProjectResponseSchema,
"GET",
"/api/public/projects",
undefined,
createBasicAuthHeader(projectApiKey, projectSecretKey),
200, // Expected status code is 200 OK
);
expect(response.status).toBe(200);
expect(response.body.data).toHaveLength(1);
expect(response.body.data[0]).toMatchObject({
id: projectId,
name: projectName,
organization: {
id: expect.any(String),
name: expect.any(String),
},
metadata: {},
});
});
it("should return 401 when invalid API keys are provided", async () => {
const result = await makeAPICall<{ message: string }>(
"GET",
"/api/public/projects",
undefined,
createBasicAuthHeader(invalidApiKey, invalidSecretKey),
);
expect(result.status).toBe(401);
expect(result.body.message).toBeDefined();
});
it("should return 405 for non-GET methods", async () => {
const result = await makeAPICall<{ message: string }>(
"PUT", // Changed from POST to PUT since we now support POST
"/api/public/projects",
{},
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(405);
expect(result.body.message).toContain("Method not allowed");
});
});
describe("POST /api/public/projects", () => {
// Clean up test projects after each test
afterEach(async () => {
// Delete any test projects created during tests
await prisma.project.deleteMany({
where: {
orgId: "seed-org-id",
name: {
startsWith: "Test Project",
},
},
});
});
it("should create a new project with valid organization API key", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const metadata = { plan: "pro", features: ["all"] };
const response = await makeZodVerifiedAPICall(
ProjectCreationResponseSchema,
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
metadata: metadata,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
201, // Expected status code is 201 Created
);
expect(response.status).toBe(201);
expect(response.body).toMatchObject({
name: uniqueProjectName,
});
expect(response.body.metadata).toEqual(metadata);
expect(response.body.id).toBeDefined();
// Verify the project was actually created in the database
const project = await prisma.project.findUnique({
where: { id: response.body.id },
});
expect(project).not.toBeNull();
expect(project?.name).toBe(uniqueProjectName);
expect(project?.metadata).toEqual(metadata);
});
it("should parse metadata sent as a JSON string", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const metadata = { plan: "pro", features: ["all"] };
const response = await makeZodVerifiedAPICall(
ProjectCreationResponseSchema,
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
metadata: JSON.stringify(metadata),
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
201,
);
expect(response.status).toBe(201);
expect(response.body.metadata).toEqual(metadata);
const project = await prisma.project.findUnique({
where: { id: response.body.id },
});
expect(project?.metadata).toEqual(metadata);
});
it.each([
["JSON-stringified null", '"null"' /* JSON.stringify(null) */],
["JSON-stringified array", JSON.stringify([1, 2, 3])],
["JSON-stringified number", JSON.stringify(42)],
["JSON-stringified string", JSON.stringify("foo")],
])(
"should reject create when metadata is %s",
async (_label, metadataValue) => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
metadata: metadataValue,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain("valid JSON object");
},
);
it("should allow create without metadata and return empty object", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const response = await makeZodVerifiedAPICall(
ProjectCreationResponseSchema,
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
201,
);
expect(response.status).toBe(201);
expect(response.body.metadata).toEqual({});
const project = await prisma.project.findUnique({
where: { id: response.body.id },
});
expect(project?.metadata).toBeNull();
});
it("should reject create when metadata is a literal null", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
metadata: null,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain("valid JSON object");
});
it("should create a new project with retention days", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const response = await makeZodVerifiedAPICall(
ProjectCreationResponseSchema,
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
retention: 0, // Setting retention to 0 (no retention)
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
201, // Expected status code is 201 Created
);
expect(response.status).toBe(201);
expect(response.body).toMatchObject({
name: uniqueProjectName,
});
// Verify the project was created with the correct retention days
const project = await prisma.project.findUnique({
where: { id: response.body.id },
});
expect(project).not.toBeNull();
expect(project?.retentionDays).toBe(0);
});
it("should validate retention days", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
// Test with invalid retention days (less than 3 and not 0)
const invalidResult = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
retention: 2, // Invalid: less than 3 and not 0
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(invalidResult.status).toBe(400);
expect(invalidResult.body.message).toContain("Invalid retention value");
});
it("should return 403 when using project API key instead of organization API key", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
},
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(403);
});
it("should return 401 when invalid API keys are provided", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
},
createBasicAuthHeader(invalidApiKey, invalidSecretKey),
);
expect(result.status).toBe(401);
expect(result.body.message).toContain("Invalid credentials");
});
it("should return 400 when project name is invalid", async () => {
// Test with a name that's too short
const shortNameResult = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: "AB", // Too short
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(shortNameResult.status).toBe(400);
expect(shortNameResult.body.message).toContain("Invalid project name");
// Test with a name that's too long
const longNameResult = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: "A".repeat(61), // Too long (more than 60 characters)
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(longNameResult.status).toBe(400);
expect(longNameResult.body.message).toContain("Invalid project name");
});
it("should return 409 when project name already exists in the organization", async () => {
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
// First create a project
await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
// Try to create another project with the same name
const duplicateResult = await makeAPICall<{ message: string }>(
"POST",
"/api/public/projects",
{
name: uniqueProjectName,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(duplicateResult.status).toBe(409);
expect(duplicateResult.body.message).toContain("already exists");
});
});
describe("PUT /api/public/projects/[projectId]", () => {
let testProjectId: string;
beforeEach(async () => {
// Create a test project to update
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const project = await prisma.project.create({
data: {
name: uniqueProjectName,
orgId: "seed-org-id", // Same org ID used for the API key
metadata: { plan: "free", features: ["basic"] },
},
});
testProjectId = project.id;
// The raw create bypasses the invalidation real create paths run, so the
// org key's cached context would keep a projectIds snapshot without it.
await invalidateCachedOrgApiKeys("seed-org-id");
});
afterEach(async () => {
// Clean up test projects
await prisma.project.deleteMany({
where: {
id: testProjectId,
},
});
});
it("should update project retention days with valid organization API key", async () => {
const response = await makeZodVerifiedAPICall(
ProjectUpdateResponseSchema,
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
retention: 7, // Valid retention value
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
200, // Expected status code is 200 OK
);
expect(response.status).toBe(200);
expect(response.body.name).toBe("Updated Project Name");
expect(response.body.retentionDays).toBe(7);
// Verify the project was updated in the database
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project).not.toBeNull();
expect(project?.name).toBe("Updated Project Name");
expect(project?.retentionDays).toBe(7);
});
it("should validate retention days on update", async () => {
// Test with invalid retention days (less than 3 and not 0)
const invalidResult = await makeAPICall<{ message: string }>(
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
retention: 2, // Invalid: less than 3 and not 0
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(invalidResult.status).toBe(400);
expect(invalidResult.body.message).toContain("Invalid retention value");
});
it("should allow setting retention to 0", async () => {
const response = await makeZodVerifiedAPICall(
ProjectUpdateResponseSchema,
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
retention: 0, // Valid: 0 means no retention
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
200, // Expected status code is 200 OK
);
expect(response.status).toBe(200);
// Retentions with value 0 are not returned.
expect(response.body.retentionDays).toBeUndefined();
});
it("should update project metadata", async () => {
const newMetadata = { plan: "enterprise", features: ["all", "custom"] };
const response = await makeZodVerifiedAPICall(
ProjectUpdateResponseSchema,
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
metadata: newMetadata,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
200, // Expected status code is 200 OK
);
expect(response.status).toBe(200);
expect(response.body.name).toBe("Updated Project Name");
expect(response.body.metadata).toEqual(newMetadata);
// Verify the project was updated in the database
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project).not.toBeNull();
expect(project?.name).toBe("Updated Project Name");
expect(project?.metadata).toEqual(newMetadata);
});
it("should parse update metadata sent as a JSON string", async () => {
const newMetadata = { plan: "enterprise", features: ["all", "custom"] };
const response = await makeZodVerifiedAPICall(
ProjectUpdateResponseSchema,
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
metadata: JSON.stringify(newMetadata),
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
200,
);
expect(response.status).toBe(200);
expect(response.body.metadata).toEqual(newMetadata);
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project?.metadata).toEqual(newMetadata);
});
it.each([
["JSON-stringified null", '"null"'],
["JSON-stringified array", JSON.stringify([1, 2, 3])],
["JSON-stringified number", JSON.stringify(42)],
["JSON-stringified string", JSON.stringify("foo")],
])(
"should reject update and preserve existing metadata when metadata is %s",
async (_label, metadataValue) => {
const seedMetadata = { plan: "pro", features: ["all"] };
await prisma.project.update({
where: { id: testProjectId },
data: { metadata: seedMetadata },
});
const result = await makeAPICall<{ message: string }>(
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
metadata: metadataValue,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain("valid JSON object");
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project?.metadata).toEqual(seedMetadata);
},
);
it("should allow update without metadata and preserve existing metadata", async () => {
const seedMetadata = { plan: "pro", features: ["all"] };
await prisma.project.update({
where: { id: testProjectId },
data: { metadata: seedMetadata },
});
const response = await makeZodVerifiedAPICall(
ProjectUpdateResponseSchema,
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Renamed Project",
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
200,
);
expect(response.status).toBe(200);
expect(response.body.name).toBe("Renamed Project");
expect(response.body.metadata).toEqual(seedMetadata);
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project?.metadata).toEqual(seedMetadata);
});
it("should reject update when metadata is a literal null and preserve existing metadata", async () => {
const seedMetadata = { plan: "pro", features: ["all"] };
await prisma.project.update({
where: { id: testProjectId },
data: { metadata: seedMetadata },
});
const result = await makeAPICall<{ message: string }>(
"PUT",
`/api/public/projects/${testProjectId}`,
{
name: "Updated Project Name",
metadata: null,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain("valid JSON object");
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project?.metadata).toEqual(seedMetadata);
});
it("should return 403 when using project API key instead of organization API key", async () => {
const result = await makeAPICall<{ message: string }>(
"PUT",
`/api/public/projects/${testProjectId}`,
{
retention: 7,
},
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(403);
});
it("should return 404 when project does not exist", async () => {
const nonExistentProjectId = randomUUID();
const result = await makeAPICall<{ message: string }>(
"PUT",
`/api/public/projects/${nonExistentProjectId}`,
{
retention: 7,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(404);
expect(result.body.message).toContain("Project not found");
});
});
describe("DELETE /api/public/projects/[projectId]", () => {
let testProjectId: string;
beforeEach(async () => {
// Create a test project to delete
const uniqueProjectName = `Test Project ${randomUUID().substring(0, 8)}`;
const project = await prisma.project.create({
data: {
name: uniqueProjectName,
orgId: "seed-org-id", // Same org ID used for the API key
},
});
testProjectId = project.id;
// The raw create bypasses the invalidation real create paths run, so the
// org key's cached context would keep a projectIds snapshot without it.
await invalidateCachedOrgApiKeys("seed-org-id");
});
afterEach(async () => {
// Clean up any remaining test projects
await prisma.project.deleteMany({
where: {
id: testProjectId,
},
});
});
it("should delete a project with valid organization API key", async () => {
const response = await makeZodVerifiedAPICall(
ProjectDeletionResponseSchema,
"DELETE",
`/api/public/projects/${testProjectId}`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
202, // Expected status code is 202 Accepted
);
expect(response.status).toBe(202);
expect(response.body.success).toBe(true);
expect(response.body.message).toContain("being processed asynchronously");
// Verify the project was marked as deleted in the database
const project = await prisma.project.findUnique({
where: { id: testProjectId },
});
expect(project).not.toBeNull();
expect(project?.deletedAt).not.toBeNull();
});
it("turns the gateway off when deleting its ingestion project", async () => {
const { orgId, projectId: ingestionProjectId } =
await createOrgProjectAndApiKey({ plan: "Enterprise" });
const orgKey = await createApiKey(prisma, {
tenant: OrganizationId(orgId),
scope: OrganizationId(orgId),
role: SystemRoleId("LEGACY_ORGANIZATION_API_KEY"),
createdBy: "system",
});
await prisma.gatewayConfig.create({
data: {
organizationId: orgId,
defaultIngestionProjectId: ingestionProjectId,
ingestionMode: "USAGE",
},
});
const result = await makeAPICall<{ success: boolean }>(
"DELETE",
`/api/public/projects/${ingestionProjectId}`,
undefined,
createBasicAuthHeader(orgKey.publicKey, orgKey.secretKey),
);
expect(result.status).toBe(202);
await expect(
prisma.gatewayConfig.findUnique({ where: { organizationId: orgId } }),
).resolves.toMatchObject({ defaultIngestionProjectId: null });
await expect(
prisma.project.findUnique({ where: { id: ingestionProjectId } }),
).resolves.toMatchObject({ deletedAt: expect.any(Date) });
});
describe("gateway ingestion project in enforce mode", () => {
const originalMode = env.API_AUTH_MIGRATION;
let orgId: string;
let ingestionProjectId: string;
let auth: string;
let context: AuthorizationContext;
beforeEach(async () => {
Object.assign(env, { API_AUTH_MIGRATION: "enforce" });
({ orgId, projectId: ingestionProjectId } =
await createOrgProjectAndApiKey({ plan: "Enterprise" }));
const orgKey = await createApiKey(prisma, {
tenant: OrganizationId(orgId),
scope: OrganizationId(orgId),
role: SystemRoleId("LEGACY_ORGANIZATION_API_KEY"),
createdBy: "system",
});
auth = createBasicAuthHeader(orgKey.publicKey, orgKey.secretKey);
const authentication = await authenticator.authenticate({
headers: { authorization: auth },
});
if (!authentication.success) throw authentication.error;
context = authentication.context;
await prisma.gatewayConfig.create({
data: {
organizationId: orgId,
defaultIngestionProjectId: ingestionProjectId,
ingestionMode: "USAGE",
},
});
});
afterEach(() => {
vi.restoreAllMocks();
Object.assign(env, { API_AUTH_MIGRATION: originalMode });
});
// No system role grants project:delete without gateway:manage, so the
// principal is the org key's real context with that one action removed.
const deleteIngestionProject = async (ctx: AuthorizationContext) => {
vi.spyOn(authenticator, "authenticate").mockResolvedValueOnce({
success: true,
context: ctx,
});
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: "DELETE",
query: { projectId: ingestionProjectId },
headers: { authorization: auth },
});
await projectByIdHandler(req, res);
return res;
};
it("rejects a key with project:delete but without gateway:manage", async () => {
const policies = context.policies.map((policy) => ({
...policy,
actions: policy.actions.filter(
(action) => action !== "gateway:manage",
),
}));
const res = await deleteIngestionProject({ ...context, policies });
expect(res._getStatusCode()).toBe(403);
expect(res._getJSONData().message).toContain("gateway:manage");
await expect(
prisma.gatewayConfig.findUnique({ where: { organizationId: orgId } }),
).resolves.toMatchObject({
defaultIngestionProjectId: ingestionProjectId,
ingestionMode: "USAGE",
});
await expect(
prisma.project.findUnique({ where: { id: ingestionProjectId } }),
).resolves.toMatchObject({ deletedAt: null });
});
});
it("should return 403 when using project API key instead of organization API key", async () => {
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${testProjectId}`,
undefined,
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(403);
});
it("should return 401 when invalid API keys are provided", async () => {
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${testProjectId}`,
undefined,
createBasicAuthHeader(invalidApiKey, invalidSecretKey),
);
expect(result.status).toBe(401);
expect(result.body.message).toBeDefined();
});
it("should return 404 when project does not exist", async () => {
const nonExistentProjectId = randomUUID();
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${nonExistentProjectId}`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(404);
expect(result.body.message).toContain("Project not found");
});
});
describe("GET /api/public/projects/[projectId]/apiKeys", () => {
it("should return API keys with valid organization API key authentication", async () => {
const response = await makeZodVerifiedAPICall(
ApiKeysResponseSchema,
"GET",
`/api/public/projects/${projectId}/apiKeys`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
200, // Expected status code is 200 OK
);
expect(response.status).toBe(200);
expect(response.body.apiKeys.length).toBeGreaterThanOrEqual(1);
expect(response.body.apiKeys[0]).toHaveProperty("id");
expect(response.body.apiKeys[0]).toHaveProperty("publicKey");
});
it("should return 401 when invalid API keys are provided", async () => {
const result = await makeAPICall<{ message: string }>(
"GET",
`/api/public/projects/${projectId}/apiKeys`,
undefined,
createBasicAuthHeader(invalidApiKey, invalidSecretKey),
);
expect(result.status).toBe(401);
expect(result.body.message).toBeDefined();
});
it("should return 403 when using project API key instead of organization API key", async () => {
const result = await makeAPICall<{ message: string }>(
"GET",
`/api/public/projects/${projectId}/apiKeys`,
undefined,
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(403);
});
it("should return 404 when project does not exist", async () => {
const nonExistentProjectId = randomUUID();
const result = await makeAPICall<{ message: string }>(
"GET",
`/api/public/projects/${nonExistentProjectId}/apiKeys`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(404);
expect(result.body.message).toContain("Project not found");
});
it("should return 405 for non-GET/POST methods", async () => {
const result = await makeAPICall<{ message: string }>(
"PUT",
`/api/public/projects/${projectId}/apiKeys`,
{},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(405);
expect(result.body.message).toContain("Method Not Allowed");
});
});
describe("POST /api/public/projects/[projectId]/apiKeys", () => {
let createdApiKeyId: string;
afterEach(async () => {
// Clean up any API keys created during tests
if (createdApiKeyId) {
await prisma.apiKey.deleteMany({
where: {
id: createdApiKeyId,
},
});
createdApiKeyId = "";
}
});
it("attributes project-key creation to the authenticating API key", async () => {
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: "POST",
query: { projectId },
headers: {
authorization: createBasicAuthHeader(orgApiKey, orgSecretKey),
},
body: { note: "Audit attribution" },
});
await projectApiKeysHandler(req, res);
expect(res._getStatusCode()).toBe(201);
createdApiKeyId = res._getJSONData().id;
const audit = await prisma.auditLog.findFirstOrThrow({
where: { projectId, resourceId: createdApiKeyId, action: "create" },
});
expect(audit).toMatchObject({
apiKeyId: orgApiKeyId,
orgId: "seed-org-id",
projectId,
resourceType: "apiKey",
resourceId: createdApiKeyId,
});
});
it.each([
{
label: "omitted",
role: undefined,
expiresAt: undefined,
name: "Named key",
},
{ label: "null", role: null, expiresAt: null, name: "Named key" },
{
label: "omitted with empty name",
role: undefined,
expiresAt: undefined,
name: "",
},
{
label: "null with expiration",
name: undefined,
role: null,
expiresAt: new Date(Date.now() + 86_400_000).toISOString(),
},
])(
"provisions a key with $label role and lists its expiration",
async ({ role, expiresAt, name }) => {
const response = await makeAPICall<
z.infer<typeof ApiKeyCreationResponseSchema>
>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{ name, role, expiresAt },
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
createdApiKeyId = response.body.id;
expect(response.status).toBe(201);
expect(response.body).toMatchObject({
name: name ?? null,
note: name ?? null,
expiresAt: expiresAt ?? null,
role: "LEGACY_PROJECT_API_KEY",
});
const stored = await prisma.apiKey.findUniqueOrThrow({
where: { id: response.body.id },
include: { roleAssignments: true },
});
expect(stored.note).toBe(name ?? null);
expect(stored.expiresAt?.toISOString() ?? null).toBe(expiresAt ?? null);
expect(stored.roleAssignments).toMatchObject([
{ systemRole: "LEGACY_PROJECT_API_KEY" },
]);
const listed = await makeAPICall<z.infer<typeof ApiKeysResponseSchema>>(
"GET",
`/api/public/projects/${projectId}/apiKeys`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(listed.status).toBe(200);
ApiKeysResponseSchema.parse(listed.body);
const listedKey = listed.body.apiKeys.find(
(key) => key.id === stored.id,
);
expect(listedKey).toMatchObject({
name: name ?? null,
note: name ?? null,
expiresAt: expiresAt ?? null,
role: "LEGACY_PROJECT_API_KEY",
});
for (const field of [
"secretKey",
"hashedSecretKey",
"fastHashedSecretKey",
"roleAssignments",
]) {
expect(listedKey).not.toHaveProperty(field);
}
},
);
it.each([
{ label: "both name and note", body: { name: "same", note: "same" } },
{ label: "empty name and note", body: { name: "", note: "" } },
{ label: "empty name with note", body: { name: "", note: "alias" } },
{ label: "name with empty note", body: { name: "name", note: "" } },
{ label: "null name", body: { name: null } },
{ label: "past expiration", body: { expiresAt: "2000-01-01T00:00:00Z" } },
{ label: "invalid expiration", body: { expiresAt: "not-a-date" } },
{ label: "numeric expiration", body: { expiresAt: 4_102_444_800_000 } },
{ label: "empty role", body: { role: "" } },
{
label: "explicit legacy role",
body: { role: "LEGACY_PROJECT_API_KEY" },
},
{
label: "wrong-scope legacy role",
body: { role: "LEGACY_ORGANIZATION_API_KEY" },
},
{ label: "gateway role", body: { role: "AI_GATEWAY" } },
{ label: "role array", body: { role: [] } },
])("rejects $label without creating a key", async ({ body }) => {
const before = await prisma.apiKey.count({ where: { projectId } });
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
body,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toEqual(expect.any(String));
expect(await prisma.apiKey.count({ where: { projectId } })).toBe(before);
});
it("provisions a key with a selected role under the default env", async () => {
const response = await makeAPICall<
z.infer<typeof ApiKeyCreationResponseSchema>
>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{ role: "INGEST" },
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
createdApiKeyId = response.body.id;
expect(response.status).toBe(201);
expect(response.body.role).toBe("INGEST");
const stored = await prisma.apiKey.findUniqueOrThrow({
where: { id: response.body.id },
include: { roleAssignments: true },
});
expect(stored.roleAssignments).toMatchObject([{ systemRole: "INGEST" }]);
});
it("should create a new API key with valid organization API key", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const response = await makeZodVerifiedAPICall(
ApiKeyCreationResponseSchema,
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
201, // Expected status code is 201 Created
);
expect(response.status).toBe(201);
expect(response.body).toHaveProperty("id");
expect(response.body).toHaveProperty("publicKey");
expect(response.body).toHaveProperty("secretKey");
expect(response.body.note).toBe(note);
expect(response.body.name).toBe(note);
expect(response.body.expiresAt).toBeNull();
expect(response.body.role).toBe("LEGACY_PROJECT_API_KEY");
// Store the created API key ID for cleanup
createdApiKeyId = response.body.id;
// Verify the API key was actually created in the database
const apiKey = await prisma.apiKey.findUnique({
where: { id: response.body.id },
});
expect(apiKey).not.toBeNull();
expect(apiKey?.note).toBe(note);
expect(apiKey?.projectId).toBe(projectId);
expect(apiKey?.scope).toBe("PROJECT");
// Verify the creating org API key is referenced
const orgKeyRecord = await prisma.apiKey.findUnique({
where: { publicKey: orgApiKey },
});
expect(apiKey?.createdByApiKeyId).toBe(orgKeyRecord?.id);
expect(apiKey?.createdByUserId).toBeNull();
});
it("should return 403 when using project API key instead of organization API key", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
},
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(403);
});
it("should return 401 when invalid API keys are provided", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
},
createBasicAuthHeader(invalidApiKey, invalidSecretKey),
);
expect(result.status).toBe(401);
expect(result.body.message).toBeDefined();
});
it("should return 404 when project does not exist", async () => {
const nonExistentProjectId = randomUUID();
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${nonExistentProjectId}/apiKeys`,
{
note,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(404);
expect(result.body.message).toContain("Project not found");
});
it("should create an API key with predefined keys", async () => {
const note = `Test API Key with Predefined Keys ${randomUUID().substring(0, 8)}`;
const predefinedPublicKey = `pk-lf-${randomUUID()}`;
const predefinedSecretKey = `sk-lf-${randomUUID()}`;
const response = await makeZodVerifiedAPICall(
ApiKeyCreationResponseSchema,
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
publicKey: predefinedPublicKey,
secretKey: predefinedSecretKey,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
201,
);
expect(response.status).toBe(201);
expect(response.body).toHaveProperty("id");
expect(response.body.publicKey).toBe(predefinedPublicKey);
expect(response.body.secretKey).toBe(predefinedSecretKey);
expect(response.body.note).toBe(note);
// Store for cleanup
createdApiKeyId = response.body.id;
// Verify the API key was created with the predefined keys
const apiKey = await prisma.apiKey.findUnique({
where: { id: response.body.id },
});
expect(apiKey).not.toBeNull();
expect(apiKey?.publicKey).toBe(predefinedPublicKey);
});
it("should return 400 when only publicKey is provided without secretKey", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const predefinedPublicKey = `pk-lf-${randomUUID()}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
publicKey: predefinedPublicKey,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain(
"Both publicKey and secretKey must be provided together",
);
});
it("should return 400 when only secretKey is provided without publicKey", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const predefinedSecretKey = `sk-lf-${randomUUID()}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
secretKey: predefinedSecretKey,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain(
"Both publicKey and secretKey must be provided together",
);
});
it("should return 400 when publicKey does not start with pk-lf-", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const invalidPublicKey = `invalid-${randomUUID()}`;
const predefinedSecretKey = `sk-lf-${randomUUID()}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
publicKey: invalidPublicKey,
secretKey: predefinedSecretKey,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain("publicKey must start with");
});
it("should return 400 when secretKey does not start with sk-lf-", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const predefinedPublicKey = `pk-lf-${randomUUID()}`;
const invalidSecretKey = `invalid-${randomUUID()}`;
const result = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
publicKey: predefinedPublicKey,
secretKey: invalidSecretKey,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(400);
expect(result.body.message).toContain("secretKey must start with");
});
it("should return 409 when predefined publicKey already exists", async () => {
const note = `Test API Key ${randomUUID().substring(0, 8)}`;
const duplicatePublicKey = `pk-lf-${randomUUID()}`;
const secretKey1 = `sk-lf-${randomUUID()}`;
const secretKey2 = `sk-lf-${randomUUID()}`;
// Create first API key with predefined keys
const firstResponse = await makeAPICall<{ id: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note,
publicKey: duplicatePublicKey,
secretKey: secretKey1,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(firstResponse.status).toBe(201);
createdApiKeyId = firstResponse.body.id;
// Try to create second API key with same publicKey but different secretKey
const secondResponse = await makeAPICall<{ message: string }>(
"POST",
`/api/public/projects/${projectId}/apiKeys`,
{
note: `${note} duplicate`,
publicKey: duplicatePublicKey,
secretKey: secretKey2,
},
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(secondResponse.status).toBe(409);
expect(secondResponse.body.message).toContain("already exists");
});
});
describe("DELETE /api/public/projects/[projectId]/apiKeys/[apiKeyId]", () => {
let deleteTestApiKeyId: string;
beforeEach(async () => {
// Create a test API key to delete
const keyCreator = await prisma.user.create({
data: { email: `apikey-creator-${randomUUID()}@example.com` },
});
const apiKeyMeta = await createApiKey(prisma, {
tenant: OrganizationId("seed-org-id"),
scope: ProjectId(projectId),
role: SystemRoleId("LEGACY_PROJECT_API_KEY"),
createdBy: UserId(keyCreator.id),
name: `Delete Test API Key ${randomUUID().substring(0, 8)}`,
});
deleteTestApiKeyId = apiKeyMeta.id;
});
afterEach(async () => {
// Clean up any remaining test API keys
try {
await prisma.apiKey.deleteMany({
where: {
id: deleteTestApiKeyId,
},
});
} catch {
// Ignore errors if the API key was already deleted by the test
}
});
it("attributes project-key deletion to the authenticating API key", async () => {
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: "DELETE",
query: { projectId, apiKeyId: deleteTestApiKeyId },
headers: {
authorization: createBasicAuthHeader(orgApiKey, orgSecretKey),
},
});
await projectApiKeyHandler(req, res);
expect(res._getStatusCode()).toBe(200);
expect(
await prisma.apiKey.findUnique({ where: { id: deleteTestApiKeyId } }),
).toBeNull();
const audit = await prisma.auditLog.findFirstOrThrow({
where: { projectId, resourceId: deleteTestApiKeyId, action: "delete" },
});
expect(audit).toMatchObject({
apiKeyId: orgApiKeyId,
orgId: "seed-org-id",
projectId,
resourceType: "apiKey",
resourceId: deleteTestApiKeyId,
});
});
it("should delete an API key with valid organization API key", async () => {
const response = await makeZodVerifiedAPICall(
ApiKeyDeletionResponseSchema,
"DELETE",
`/api/public/projects/${projectId}/apiKeys/${deleteTestApiKeyId}`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
200, // Expected status code is 200 OK
);
expect(response.status).toBe(200);
expect(response.body.success).toBe(true);
// Verify the API key was actually deleted from the database
const apiKey = await prisma.apiKey.findUnique({
where: { id: deleteTestApiKeyId },
});
expect(apiKey).toBeNull();
});
it("should return 403 when using project API key instead of organization API key", async () => {
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${projectId}/apiKeys/${deleteTestApiKeyId}`,
undefined,
createBasicAuthHeader(projectApiKey, projectSecretKey),
);
expect(result.status).toBe(403);
});
it("should return 401 when invalid API keys are provided", async () => {
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${projectId}/apiKeys/${deleteTestApiKeyId}`,
undefined,
createBasicAuthHeader(invalidApiKey, invalidSecretKey),
);
expect(result.status).toBe(401);
expect(result.body.message).toBeDefined();
});
it("should return 404 when API key does not exist", async () => {
const nonExistentApiKeyId = randomUUID();
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${projectId}/apiKeys/${nonExistentApiKeyId}`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(404);
expect(result.body.message).toContain("API key not found");
});
it("should return 404 when project does not exist", async () => {
const nonExistentProjectId = randomUUID();
const result = await makeAPICall<{ message: string }>(
"DELETE",
`/api/public/projects/${nonExistentProjectId}/apiKeys/${deleteTestApiKeyId}`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(404);
expect(result.body.message).toContain("Project not found");
});
});
it("should return 405 for unallowed methods", async () => {
const result = await makeAPICall<{ message: string }>(
"PATCH",
`/api/public/projects/${projectId}`,
undefined,
createBasicAuthHeader(orgApiKey, orgSecretKey),
);
expect(result.status).toBe(405);
expect(result.body.message).toContain("Method not allowed");
});
});