|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * The raw `/api/table/**` routes that authenticate with |
| 5 | + * `checkSessionOrInternalAuth` accept an internal executor JWT, whose `userId` |
| 6 | + * is the subject the executor embedded rather than a person asking for |
| 7 | + * anything. Reading it bare applies that person's permission group to a |
| 8 | + * delegation the executor exemption deliberately passes ungated — so these pin |
| 9 | + * the derivation (`capabilityGovernedAuthUserId`) at each gate, on a group |
| 10 | + * whose config would refuse if it were consulted. |
| 11 | + */ |
| 12 | +import { |
| 13 | + hybridAuthMockFns, |
| 14 | + permissionGroupScopeMock, |
| 15 | + permissionGroupScopeMockFns, |
| 16 | + resetPermissionGroupScopeMock, |
| 17 | +} from '@sim/testing' |
| 18 | +import { NextRequest } from 'next/server' |
| 19 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 20 | + |
| 21 | +const mocks = vi.hoisted(() => ({ |
| 22 | + listWorkspaceExportJobs: vi.fn(), |
| 23 | + checkWorkspaceAccess: vi.fn(), |
| 24 | + getUserEntityPermissions: vi.fn(), |
| 25 | + createTable: vi.fn(), |
| 26 | + listTables: vi.fn(), |
| 27 | + getWorkspaceTableLimits: vi.fn(), |
| 28 | + findActiveFolder: vi.fn(), |
| 29 | + getUserSettings: vi.fn(), |
| 30 | + runDetached: vi.fn(), |
| 31 | + runTableImport: vi.fn(), |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('@/lib/permission-groups/config-scope.server', () => permissionGroupScopeMock) |
| 35 | +vi.mock('@/lib/table/jobs/service', () => ({ |
| 36 | + listWorkspaceExportJobs: mocks.listWorkspaceExportJobs, |
| 37 | +})) |
| 38 | +vi.mock('@/lib/workspaces/permissions/utils', () => ({ |
| 39 | + checkWorkspaceAccess: mocks.checkWorkspaceAccess, |
| 40 | + getUserEntityPermissions: mocks.getUserEntityPermissions, |
| 41 | +})) |
| 42 | +vi.mock('@/lib/table', () => ({ |
| 43 | + createTable: mocks.createTable, |
| 44 | + deleteTable: vi.fn(), |
| 45 | + getWorkspaceTableLimits: mocks.getWorkspaceTableLimits, |
| 46 | + listTables: mocks.listTables, |
| 47 | + releaseJobClaim: vi.fn(), |
| 48 | + sanitizeName: (name: string) => name, |
| 49 | + TABLE_LIMITS: { MAX_TABLE_NAME_LENGTH: 64 }, |
| 50 | +})) |
| 51 | +vi.mock('@/lib/table/import-runner', () => ({ runTableImport: mocks.runTableImport })) |
| 52 | +vi.mock('@/lib/folders/queries', () => ({ findActiveFolder: mocks.findActiveFolder })) |
| 53 | +vi.mock('@/lib/users/queries', () => ({ getUserSettings: mocks.getUserSettings })) |
| 54 | +vi.mock('@/lib/core/utils/background', () => ({ runDetached: mocks.runDetached })) |
| 55 | +vi.mock('@/lib/core/config/env-flags', () => ({ isTriggerDevEnabled: false })) |
| 56 | +vi.mock('@/lib/posthog/server', () => ({ captureServerEvent: vi.fn() })) |
| 57 | + |
| 58 | +import { DEFAULT_PERMISSION_GROUP_CONFIG } from '@/lib/permission-groups/fields' |
| 59 | +import { POST as importAsync } from '@/app/api/table/import-async/route' |
| 60 | +import { GET as listJobs } from '@/app/api/table/jobs/route' |
| 61 | + |
| 62 | +const WORKSPACE_ID = '11111111-1111-4111-8111-111111111111' |
| 63 | +const TABLE_ID = '22222222-2222-4222-8222-222222222222' |
| 64 | +const ACTOR_ID = 'run-actor' |
| 65 | + |
| 66 | +/** The run's actor, embedded in the executor's internal JWT. */ |
| 67 | +function authenticateAsExecutor() { |
| 68 | + hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ |
| 69 | + success: true, |
| 70 | + userId: ACTOR_ID, |
| 71 | + authType: 'internal_jwt', |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +/** The same person, calling the same route from their own browser session. */ |
| 76 | +function authenticateAsSession() { |
| 77 | + hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ |
| 78 | + success: true, |
| 79 | + userId: ACTOR_ID, |
| 80 | + authType: 'session', |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +function getExportJobs() { |
| 85 | + return listJobs( |
| 86 | + new NextRequest(`http://localhost/api/table/jobs?workspaceId=${WORKSPACE_ID}&type=export`) |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +function startImport() { |
| 91 | + return importAsync( |
| 92 | + new NextRequest('http://localhost/api/table/import-async', { |
| 93 | + method: 'POST', |
| 94 | + body: JSON.stringify({ |
| 95 | + workspaceId: WORKSPACE_ID, |
| 96 | + fileKey: `workspace/${WORKSPACE_ID}/upload.csv`, |
| 97 | + fileName: 'upload.csv', |
| 98 | + }), |
| 99 | + headers: { 'content-type': 'application/json' }, |
| 100 | + }) |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +describe('the subject the raw table routes gate on', () => { |
| 105 | + beforeEach(() => { |
| 106 | + vi.clearAllMocks() |
| 107 | + resetPermissionGroupScopeMock() |
| 108 | + mocks.checkWorkspaceAccess.mockResolvedValue({ hasAccess: true }) |
| 109 | + mocks.getUserEntityPermissions.mockResolvedValue('admin') |
| 110 | + mocks.listWorkspaceExportJobs.mockResolvedValue([{ id: 'job-1' }]) |
| 111 | + mocks.listTables.mockResolvedValue([]) |
| 112 | + mocks.getWorkspaceTableLimits.mockResolvedValue({ maxTables: 100 }) |
| 113 | + mocks.getUserSettings.mockResolvedValue({ timezone: 'UTC' }) |
| 114 | + mocks.createTable.mockResolvedValue({ id: TABLE_ID }) |
| 115 | + permissionGroupScopeMockFns.mockResolvePermissionGroupConfig.mockResolvedValue({ |
| 116 | + ...DEFAULT_PERMISSION_GROUP_CONFIG, |
| 117 | + hideTablesTab: true, |
| 118 | + disableTableExport: true, |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe('an executor delegation carrying the actor’s id', () => { |
| 123 | + beforeEach(authenticateAsExecutor) |
| 124 | + |
| 125 | + it('lists the workspace’s export jobs without consulting the actor’s group', async () => { |
| 126 | + const response = await getExportJobs() |
| 127 | + |
| 128 | + expect(await response.json()).toEqual({ success: true, data: { jobs: [{ id: 'job-1' }] } }) |
| 129 | + expect(permissionGroupScopeMockFns.mockResolvePermissionGroupConfig).not.toHaveBeenCalled() |
| 130 | + }) |
| 131 | + |
| 132 | + it('starts an import without consulting the actor’s group', async () => { |
| 133 | + const response = await startImport() |
| 134 | + |
| 135 | + expect(response.status).toBe(200) |
| 136 | + expect(mocks.createTable).toHaveBeenCalled() |
| 137 | + expect(permissionGroupScopeMockFns.mockResolvePermissionGroupConfig).not.toHaveBeenCalled() |
| 138 | + }) |
| 139 | + }) |
| 140 | + |
| 141 | + describe('the same person on their own session', () => { |
| 142 | + beforeEach(authenticateAsSession) |
| 143 | + |
| 144 | + it('is handed an empty export tray', async () => { |
| 145 | + const response = await getExportJobs() |
| 146 | + |
| 147 | + expect(await response.json()).toEqual({ success: true, data: { jobs: [] } }) |
| 148 | + expect(mocks.listWorkspaceExportJobs).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | + |
| 151 | + it('is refused the import, and no table is created', async () => { |
| 152 | + const response = await startImport() |
| 153 | + |
| 154 | + expect(response.status).toBe(403) |
| 155 | + expect(mocks.createTable).not.toHaveBeenCalled() |
| 156 | + }) |
| 157 | + }) |
| 158 | +}) |
0 commit comments