|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockGetSession, mockIsFeatureEnabled, mockHasWorkspaceAdminAccess, mockOperations } = |
| 8 | + vi.hoisted(() => ({ |
| 9 | + mockGetSession: vi.fn(), |
| 10 | + mockIsFeatureEnabled: vi.fn(), |
| 11 | + mockHasWorkspaceAdminAccess: vi.fn(), |
| 12 | + mockOperations: { |
| 13 | + getCustomBlockManageContext: vi.fn(), |
| 14 | + getCustomBlockUsageCounts: vi.fn(), |
| 15 | + }, |
| 16 | + })) |
| 17 | + |
| 18 | +vi.mock('@/lib/auth', () => ({ |
| 19 | + getSession: mockGetSession, |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@/lib/core/config/feature-flags', () => ({ |
| 23 | + isFeatureEnabled: mockIsFeatureEnabled, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
| 27 | + hasWorkspaceAdminAccess: mockHasWorkspaceAdminAccess, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('@/lib/workflows/custom-blocks/operations', () => mockOperations) |
| 31 | + |
| 32 | +import { GET } from '@/app/api/custom-blocks/[id]/usages/route' |
| 33 | + |
| 34 | +const MANAGE_CONTEXT = { |
| 35 | + organizationId: 'org-1', |
| 36 | + sourceWorkspaceId: 'ws-1', |
| 37 | + type: 'custom_block_abc123', |
| 38 | + name: 'Invoice Parser', |
| 39 | +} |
| 40 | + |
| 41 | +const USAGE_COUNTS = { usageCount: 3, deployedUsageCount: 2 } |
| 42 | + |
| 43 | +function callRoute(id = 'cb-1') { |
| 44 | + return GET(createMockRequest('GET'), { params: Promise.resolve({ id }) }) |
| 45 | +} |
| 46 | + |
| 47 | +describe('GET /api/custom-blocks/[id]/usages', () => { |
| 48 | + beforeEach(() => { |
| 49 | + vi.clearAllMocks() |
| 50 | + mockGetSession.mockResolvedValue({ user: { id: 'user-1' } }) |
| 51 | + mockIsFeatureEnabled.mockResolvedValue(true) |
| 52 | + mockHasWorkspaceAdminAccess.mockResolvedValue(true) |
| 53 | + mockOperations.getCustomBlockManageContext.mockResolvedValue(MANAGE_CONTEXT) |
| 54 | + mockOperations.getCustomBlockUsageCounts.mockResolvedValue(USAGE_COUNTS) |
| 55 | + }) |
| 56 | + |
| 57 | + it('returns 401 without a session', async () => { |
| 58 | + mockGetSession.mockResolvedValue(null) |
| 59 | + const response = await callRoute() |
| 60 | + expect(response.status).toBe(401) |
| 61 | + }) |
| 62 | + |
| 63 | + it('returns 404 for an unknown block', async () => { |
| 64 | + mockOperations.getCustomBlockManageContext.mockResolvedValue(null) |
| 65 | + const response = await callRoute() |
| 66 | + expect(response.status).toBe(404) |
| 67 | + }) |
| 68 | + |
| 69 | + it('returns 403 when the feature flag is off', async () => { |
| 70 | + mockIsFeatureEnabled.mockResolvedValue(false) |
| 71 | + const response = await callRoute() |
| 72 | + expect(response.status).toBe(403) |
| 73 | + }) |
| 74 | + |
| 75 | + it('returns 403 for a non-admin of the source workspace', async () => { |
| 76 | + mockHasWorkspaceAdminAccess.mockResolvedValue(false) |
| 77 | + const response = await callRoute() |
| 78 | + expect(response.status).toBe(403) |
| 79 | + expect(mockOperations.getCustomBlockUsageCounts).not.toHaveBeenCalled() |
| 80 | + }) |
| 81 | + |
| 82 | + it('returns the org-scoped usage counts for the block type', async () => { |
| 83 | + const response = await callRoute() |
| 84 | + expect(response.status).toBe(200) |
| 85 | + expect(await response.json()).toEqual(USAGE_COUNTS) |
| 86 | + expect(mockOperations.getCustomBlockUsageCounts).toHaveBeenCalledWith( |
| 87 | + 'org-1', |
| 88 | + 'custom_block_abc123' |
| 89 | + ) |
| 90 | + }) |
| 91 | +}) |
0 commit comments