d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
115 lines
3.5 KiB
TypeScript
115 lines
3.5 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { createMockRequest } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const {
|
|
mockFlags,
|
|
mockDbLimit,
|
|
mockCheckInternalApiKey,
|
|
mockCheckServerSideUsageLimits,
|
|
mockCheckOrgMemberUsageLimit,
|
|
} = vi.hoisted(() => ({
|
|
mockFlags: { isHosted: true },
|
|
mockDbLimit: vi.fn(),
|
|
mockCheckInternalApiKey: vi.fn(),
|
|
mockCheckServerSideUsageLimits: vi.fn(),
|
|
mockCheckOrgMemberUsageLimit: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@sim/db', () => ({
|
|
db: {
|
|
select: () => ({ from: () => ({ where: () => ({ limit: mockDbLimit }) }) }),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@/lib/billing/calculations/usage-monitor', () => ({
|
|
checkServerSideUsageLimits: mockCheckServerSideUsageLimits,
|
|
checkOrgMemberUsageLimit: mockCheckOrgMemberUsageLimit,
|
|
}))
|
|
|
|
vi.mock('@/lib/copilot/request/http', () => ({
|
|
checkInternalApiKey: mockCheckInternalApiKey,
|
|
}))
|
|
|
|
vi.mock('@/lib/copilot/request/otel', () => ({
|
|
withIncomingGoSpan: (
|
|
_headers: unknown,
|
|
_span: unknown,
|
|
_attrs: unknown,
|
|
fn: (span: { setAttribute: () => void; setAttributes: () => void }) => unknown
|
|
) => fn({ setAttribute: vi.fn(), setAttributes: vi.fn() }),
|
|
}))
|
|
|
|
vi.mock('@/lib/core/config/env-flags', () => ({
|
|
get isHosted() {
|
|
return mockFlags.isHosted
|
|
},
|
|
}))
|
|
|
|
import { POST } from '@/app/api/copilot/api-keys/validate/route'
|
|
|
|
function request(body: Record<string, unknown>) {
|
|
return createMockRequest('POST', body, { 'x-api-key': 'internal' })
|
|
}
|
|
|
|
describe('POST /api/copilot/api-keys/validate — per-member enforcement', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockFlags.isHosted = true
|
|
mockCheckInternalApiKey.mockReturnValue({ success: true })
|
|
mockDbLimit.mockResolvedValue([{ id: 'user-1' }])
|
|
mockCheckServerSideUsageLimits.mockResolvedValue({
|
|
isExceeded: false,
|
|
currentUsage: 0,
|
|
limit: 100,
|
|
})
|
|
mockCheckOrgMemberUsageLimit.mockResolvedValue({
|
|
isExceeded: false,
|
|
currentUsage: 0,
|
|
limit: null,
|
|
})
|
|
})
|
|
|
|
it('returns 402 when the pooled/personal limit is exceeded (existing behavior)', async () => {
|
|
mockCheckServerSideUsageLimits.mockResolvedValue({
|
|
isExceeded: true,
|
|
currentUsage: 200,
|
|
limit: 100,
|
|
})
|
|
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
|
|
expect(res.status).toBe(402)
|
|
expect(mockCheckOrgMemberUsageLimit).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns 402 when the per-member org-workspace cap is exceeded', async () => {
|
|
mockCheckOrgMemberUsageLimit.mockResolvedValue({
|
|
isExceeded: true,
|
|
currentUsage: 5,
|
|
limit: 4,
|
|
})
|
|
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
|
|
expect(res.status).toBe(402)
|
|
expect(mockCheckOrgMemberUsageLimit).toHaveBeenCalledWith('user-1', 'ws-1')
|
|
})
|
|
|
|
it('returns 200 when under both limits', async () => {
|
|
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
|
|
expect(res.status).toBe(200)
|
|
})
|
|
|
|
it('rejects with 400 when workspaceId is omitted (contract-required, fail closed)', async () => {
|
|
const res = await POST(request({ userId: 'user-1' }))
|
|
expect(res.status).toBe(400)
|
|
expect(mockCheckOrgMemberUsageLimit).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('skips the per-member check when not hosted', async () => {
|
|
mockFlags.isHosted = false
|
|
const res = await POST(request({ userId: 'user-1', workspaceId: 'ws-1' }))
|
|
expect(res.status).toBe(200)
|
|
expect(mockCheckOrgMemberUsageLimit).not.toHaveBeenCalled()
|
|
})
|
|
})
|