Ctrl K
ComunidadGuías…

tests/helpers/fixtures.ts

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
import type { FastifyInstance } from 'fastify'

import { API_KEY_TYPES } from '../../src/auth/constants.js'
import type { Db } from '../../src/db/index.js'
import { apiKeys } from '../../src/db/schema.js'
import { generateKey } from '../../src/plugins/auth.js'

export async function createRootKey(db: Db) {
  const generated = generateKey()
  await db.insert(apiKeys).values({
    keyHash: generated.hash,
    keyPrefix: generated.prefix,
    type: API_KEY_TYPES.ADMIN,
    projectId: null,
    environmentId: null,
    description: 'Test root key',
  })
  return generated.plaintext
}

export async function createProject(app: FastifyInstance, rootKey: string, slug = 'test-project') {
  const response = await app.inject({
    method: 'POST',
    url: '/api/v1/admin/projects',
    headers: { authorization: rootKey },
    payload: { name: 'Test Project', slug },
  })
  if (response.statusCode !== 201) {
    throw new Error(response.body)
  }
  return response.json<{ id: string; slug: string }>()
}

export async function createAdminKey(app: FastifyInstance, rootKey: string, projectId: string) {
  const response = await app.inject({
    method: 'POST',
    url: `/api/v1/admin/projects/${projectId}/keys`,
    headers: { authorization: rootKey },
    payload: { type: API_KEY_TYPES.ADMIN, description: 'Project admin' },
  })
  if (response.statusCode !== 201) {
    throw new Error(response.body)
  }
  return response.json<{ key: string; id: string }>().key
}

export async function createClientKey(
  app: FastifyInstance,
  adminKey: string,
  projectId: string,
  environmentId: string,
) {
  const response = await app.inject({
    method: 'POST',
    url: `/api/v1/admin/projects/${projectId}/keys`,
    headers: { authorization: adminKey },
    payload: { type: API_KEY_TYPES.CLIENT, environmentId, description: 'Client' },
  })
  if (response.statusCode !== 201) {
    throw new Error(response.body)
  }
  return response.json<{ key: string; id: string }>().key
}