|
| 1 | +/** |
| 2 | + * Real PostgreSQL coverage for the workbench's `SIM_ENTITLEMENTS`: a Mothership Function |
| 3 | + * execution carries a Copilot delegation scoped to Function executions, and the availability use |
| 4 | + * cases behind each entitlement authorize it for real. Only the workbench transport (CLI bundle, |
| 5 | + * worker bootstrap, callback endpoint) and the AppConfig fetch are replaced. |
| 6 | + */ |
| 7 | + |
| 8 | +import { db } from '@sim/db' |
| 9 | +import { member, organization, permissions, user, workspace } from '@sim/db/schema' |
| 10 | +import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/workspace-fixtures' |
| 11 | +import { envFlagsMock, resetEnvFlagsMock, setEnvFlags } from '@sim/testing/mocks/env-flags.mock' |
| 12 | +import { |
| 13 | + mothershipAgentUrlMock, |
| 14 | + mothershipAgentUrlMockFns, |
| 15 | +} from '@sim/testing/mocks/mothership-agent-url.mock' |
| 16 | +import { |
| 17 | + mothershipGoFetchMock, |
| 18 | + mothershipGoFetchMockFns, |
| 19 | +} from '@sim/testing/mocks/mothership-go-fetch.mock' |
| 20 | +import { generateId } from '@sim/utils/id' |
| 21 | +import { inArray } from 'drizzle-orm' |
| 22 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest' |
| 23 | + |
| 24 | +const { mockFetchProfile } = vi.hoisted(() => ({ mockFetchProfile: vi.fn() })) |
| 25 | + |
| 26 | +vi.mock('@/lib/core/config/env-flags', () => envFlagsMock) |
| 27 | +vi.mock('@/lib/core/config/appconfig', () => ({ fetchAppConfigProfile: mockFetchProfile })) |
| 28 | +vi.mock('node:fs/promises', () => ({ readFile: async () => 'runtime' })) |
| 29 | +vi.mock('@/lib/mothership/transport/connection', () => ({ |
| 30 | + getSimConnection: () => ({ mode: 'direct' }), |
| 31 | +})) |
| 32 | +vi.mock('@/lib/mothership/request/go/fetch', () => mothershipGoFetchMock) |
| 33 | +vi.mock('@/lib/mothership/request/headers', () => ({ mothershipRequestHeaders: () => ({}) })) |
| 34 | +vi.mock('@/lib/mothership/server/agent-url', () => mothershipAgentUrlMock) |
| 35 | +vi.mock('@/lib/mothership/tools/sandbox-resources', () => ({ |
| 36 | + sandboxResourceEndpoint: async (url: string) => `${url}/api/mothership/sandbox/owned-token`, |
| 37 | + activeSandboxChatOwner: () => undefined, |
| 38 | + activeSandboxFileOwnerProtocol: () => undefined, |
| 39 | + activeSandboxWorkspaceFileCopy: () => undefined, |
| 40 | +})) |
| 41 | + |
| 42 | +import { FUNCTION_EXECUTION_DELEGATION_AUDIENCE } from '@/lib/function-execution/application/authorization' |
| 43 | +import { createTrustedOrganizationCopilotPrincipal } from '@/lib/mothership/auth/application-delegation' |
| 44 | +import { buildMothershipSandboxSession } from '@/lib/mothership/tools/sandbox-session' |
| 45 | + |
| 46 | +const tenant = { orgId: generateId(), ownerId: generateId(), workspaceId: generateId() } |
| 47 | +const notRolledOut = { orgId: generateId(), ownerId: generateId(), workspaceId: generateId() } |
| 48 | +const tenants = [tenant, notRolledOut] |
| 49 | +const chatId = generateId() |
| 50 | + |
| 51 | +/** The principal `lib/internal/function/execute.ts` builds for a workspace Copilot run. */ |
| 52 | +function workspaceExecutionPrincipal(owner = tenant) { |
| 53 | + const issuedAt = new Date() |
| 54 | + return { |
| 55 | + kind: 'delegated' as const, |
| 56 | + serviceId: 'copilot' as const, |
| 57 | + subjectUserId: owner.ownerId, |
| 58 | + workspaceId: owner.workspaceId, |
| 59 | + delegationId: `function-execute:${generateId()}`, |
| 60 | + audience: FUNCTION_EXECUTION_DELEGATION_AUDIENCE, |
| 61 | + issuedAt, |
| 62 | + expiresAt: new Date(issuedAt.getTime() + 60_000), |
| 63 | + resourceScope: { executionId: generateId() }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +describe('workbench entitlements for a Mothership Function execution', () => { |
| 68 | + beforeAll(async () => { |
| 69 | + setEnvFlags({ isAppConfigEnabled: true }) |
| 70 | + mockFetchProfile.mockImplementation(async (_ids: unknown, parse: (doc: unknown) => unknown) => |
| 71 | + parse({ 'workflow-code': { enabled: false, orgIds: [tenant.orgId] } }) |
| 72 | + ) |
| 73 | + mothershipAgentUrlMockFns.mockGetMothershipBaseURL.mockResolvedValue('https://worker.test') |
| 74 | + mothershipGoFetchMockFns.mockFetchGo.mockImplementation(async () => |
| 75 | + Response.json({ version: 1, entrypoint: 'entry' }) |
| 76 | + ) |
| 77 | + const now = new Date() |
| 78 | + await db.insert(user).values( |
| 79 | + tenants.map((owner) => ({ |
| 80 | + id: owner.ownerId, |
| 81 | + name: 'Sandbox entitlements fixture', |
| 82 | + email: `${owner.ownerId}@sandbox-entitlements.invalid`, |
| 83 | + emailVerified: true, |
| 84 | + createdAt: now, |
| 85 | + updatedAt: now, |
| 86 | + })) |
| 87 | + ) |
| 88 | + await db.insert(organization).values( |
| 89 | + tenants.map((owner) => ({ |
| 90 | + id: owner.orgId, |
| 91 | + name: 'Sandbox entitlements', |
| 92 | + slug: owner.orgId, |
| 93 | + createdAt: now, |
| 94 | + })) |
| 95 | + ) |
| 96 | + await db.insert(member).values( |
| 97 | + tenants.map((owner) => ({ |
| 98 | + id: generateId(), |
| 99 | + organizationId: owner.orgId, |
| 100 | + userId: owner.ownerId, |
| 101 | + role: 'owner', |
| 102 | + createdAt: now, |
| 103 | + })) |
| 104 | + ) |
| 105 | + await insertWorkspaceFixture( |
| 106 | + db, |
| 107 | + tenants.map((owner) => ({ |
| 108 | + id: owner.workspaceId, |
| 109 | + organizationId: owner.orgId, |
| 110 | + workspaceMode: 'organization' as const, |
| 111 | + name: 'Sandbox entitlements', |
| 112 | + ownerId: owner.ownerId, |
| 113 | + billedAccountUserId: owner.ownerId, |
| 114 | + })) |
| 115 | + ) |
| 116 | + await db.insert(permissions).values( |
| 117 | + tenants.map((owner) => ({ |
| 118 | + id: generateId(), |
| 119 | + userId: owner.ownerId, |
| 120 | + entityType: 'workspace' as const, |
| 121 | + entityId: owner.workspaceId, |
| 122 | + permissionType: 'admin' as const, |
| 123 | + })) |
| 124 | + ) |
| 125 | + }) |
| 126 | + |
| 127 | + afterAll(async () => { |
| 128 | + const workspaceIds = tenants.map((owner) => owner.workspaceId) |
| 129 | + await db.delete(permissions).where(inArray(permissions.entityId, workspaceIds)) |
| 130 | + await deleteWorkspaceFixture(db, inArray(workspace.id, workspaceIds)) |
| 131 | + await db.delete(organization).where( |
| 132 | + inArray( |
| 133 | + organization.id, |
| 134 | + tenants.map((owner) => owner.orgId) |
| 135 | + ) |
| 136 | + ) |
| 137 | + await db.delete(user).where( |
| 138 | + inArray( |
| 139 | + user.id, |
| 140 | + tenants.map((owner) => owner.ownerId) |
| 141 | + ) |
| 142 | + ) |
| 143 | + resetEnvFlagsMock() |
| 144 | + }) |
| 145 | + |
| 146 | + it('grants a workspace chat workflow-code through its Function delegation', async () => { |
| 147 | + const session = await buildMothershipSandboxSession({ |
| 148 | + sessionKey: chatId, |
| 149 | + workspaceId: tenant.workspaceId, |
| 150 | + userId: tenant.ownerId, |
| 151 | + principal: workspaceExecutionPrincipal(), |
| 152 | + }) |
| 153 | + expect(session.envs?.SIM_WORKSPACE).toBe(tenant.workspaceId) |
| 154 | + expect((session.envs?.SIM_ENTITLEMENTS ?? '').split(',')).toContain('workflow-code') |
| 155 | + }) |
| 156 | + |
| 157 | + it('grants an organization chat workflow-code through its Function delegation', async () => { |
| 158 | + const session = await buildMothershipSandboxSession({ |
| 159 | + sessionKey: chatId, |
| 160 | + organizationId: tenant.orgId, |
| 161 | + userId: tenant.ownerId, |
| 162 | + principal: createTrustedOrganizationCopilotPrincipal( |
| 163 | + { |
| 164 | + userId: tenant.ownerId, |
| 165 | + organizationId: tenant.orgId, |
| 166 | + chatId, |
| 167 | + delegationId: `function-execute:${generateId()}`, |
| 168 | + }, |
| 169 | + { audience: FUNCTION_EXECUTION_DELEGATION_AUDIENCE, ttlMs: 60_000 } |
| 170 | + ), |
| 171 | + }) |
| 172 | + expect(session.envs?.SIM_ORGANIZATION_ID).toBe(tenant.orgId) |
| 173 | + expect((session.envs?.SIM_ENTITLEMENTS ?? '').split(',')).toContain('workflow-code') |
| 174 | + }) |
| 175 | + |
| 176 | + it('omits workflow-code where the workspace is not rolled out', async () => { |
| 177 | + const session = await buildMothershipSandboxSession({ |
| 178 | + sessionKey: chatId, |
| 179 | + workspaceId: notRolledOut.workspaceId, |
| 180 | + userId: notRolledOut.ownerId, |
| 181 | + principal: workspaceExecutionPrincipal(notRolledOut), |
| 182 | + }) |
| 183 | + expect(session.envs?.SIM_WORKSPACE).toBe(notRolledOut.workspaceId) |
| 184 | + expect(session.envs?.SIM_ENTITLEMENTS ?? '').not.toContain('workflow-code') |
| 185 | + }) |
| 186 | +}) |
0 commit comments