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
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import { createMockRequest } from '@sim/testing'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const { mockGetSession, mockIsFeatureEnabled, mockHasWorkspaceAdminAccess, mockOperations } =
|
|
vi.hoisted(() => ({
|
|
mockGetSession: vi.fn(),
|
|
mockIsFeatureEnabled: vi.fn(),
|
|
mockHasWorkspaceAdminAccess: vi.fn(),
|
|
mockOperations: {
|
|
getCustomBlockManageContext: vi.fn(),
|
|
getCustomBlockUsageCounts: vi.fn(),
|
|
},
|
|
}))
|
|
|
|
vi.mock('@/lib/auth', () => ({
|
|
getSession: mockGetSession,
|
|
}))
|
|
|
|
vi.mock('@/lib/core/config/feature-flags', () => ({
|
|
isFeatureEnabled: mockIsFeatureEnabled,
|
|
}))
|
|
|
|
vi.mock('@/lib/workspaces/permissions/utils', () => ({
|
|
hasWorkspaceAdminAccess: mockHasWorkspaceAdminAccess,
|
|
}))
|
|
|
|
vi.mock('@/lib/workflows/custom-blocks/operations', () => mockOperations)
|
|
|
|
import { GET } from '@/app/api/custom-blocks/[id]/usages/route'
|
|
|
|
const MANAGE_CONTEXT = {
|
|
organizationId: 'org-1',
|
|
sourceWorkspaceId: 'ws-1',
|
|
type: 'custom_block_abc123',
|
|
name: 'Invoice Parser',
|
|
}
|
|
|
|
const USAGE_COUNTS = { usageCount: 3, deployedUsageCount: 2 }
|
|
|
|
function callRoute(id = 'cb-1') {
|
|
return GET(createMockRequest('GET'), { params: Promise.resolve({ id }) })
|
|
}
|
|
|
|
describe('GET /api/custom-blocks/[id]/usages', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
mockGetSession.mockResolvedValue({ user: { id: 'user-1' } })
|
|
mockIsFeatureEnabled.mockResolvedValue(true)
|
|
mockHasWorkspaceAdminAccess.mockResolvedValue(true)
|
|
mockOperations.getCustomBlockManageContext.mockResolvedValue(MANAGE_CONTEXT)
|
|
mockOperations.getCustomBlockUsageCounts.mockResolvedValue(USAGE_COUNTS)
|
|
})
|
|
|
|
it('returns 401 without a session', async () => {
|
|
mockGetSession.mockResolvedValue(null)
|
|
const response = await callRoute()
|
|
expect(response.status).toBe(401)
|
|
})
|
|
|
|
it('returns 404 for an unknown block', async () => {
|
|
mockOperations.getCustomBlockManageContext.mockResolvedValue(null)
|
|
const response = await callRoute()
|
|
expect(response.status).toBe(404)
|
|
})
|
|
|
|
it('returns 403 when the feature flag is off', async () => {
|
|
mockIsFeatureEnabled.mockResolvedValue(false)
|
|
const response = await callRoute()
|
|
expect(response.status).toBe(403)
|
|
})
|
|
|
|
it('returns 403 for a non-admin of the source workspace', async () => {
|
|
mockHasWorkspaceAdminAccess.mockResolvedValue(false)
|
|
const response = await callRoute()
|
|
expect(response.status).toBe(403)
|
|
expect(mockOperations.getCustomBlockUsageCounts).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('returns the org-scoped usage counts for the block type', async () => {
|
|
const response = await callRoute()
|
|
expect(response.status).toBe(200)
|
|
expect(await response.json()).toEqual(USAGE_COUNTS)
|
|
expect(mockOperations.getCustomBlockUsageCounts).toHaveBeenCalledWith(
|
|
'org-1',
|
|
'custom_block_abc123'
|
|
)
|
|
})
|
|
})
|