Skip to content

Commit 973d01a

Browse files
committed
fix(mothership): restore workspace API key dispatch
1 parent 1a18bff commit 973d01a

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

‎apps/sim/lib/mothership/tool-executor/register-handlers.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import {
33
CancelWorkflowRun,
4+
GenerateApiKey,
45
RunBlock,
56
RunFromBlock,
67
RunWorkflow,
@@ -13,6 +14,7 @@ import { executeRunCode } from '../tools/handlers/run-code'
1314
import { executeSimCli } from '../tools/handlers/sim-cli'
1415
import {
1516
executeCancelWorkflowRun,
17+
executeGenerateApiKey,
1618
executeRunBlock,
1719
executeRunFromBlock,
1820
executeRunWorkflow,
@@ -50,6 +52,7 @@ function h(fn: (params: any, context: any) => Promise<any>): ToolHandler {
5052
function buildHandlerMap(): Record<string, ToolHandler> {
5153
return {
5254
[CancelWorkflowRun.id]: h(executeCancelWorkflowRun),
55+
[GenerateApiKey.id]: h(executeGenerateApiKey),
5356
[RunWorkflow.id]: h(executeRunWorkflow),
5457
[RunWorkflowUntilBlock.id]: h(executeRunWorkflowUntilBlock),
5558
[RunFromBlock.id]: h(executeRunFromBlock),

‎apps/sim/lib/mothership/tools/handlers/workflow/mutations.test.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import { beforeEach, describe, expect, it, vi } from 'vitest'
55
import { WorkflowRunAlreadyTerminalError } from '@/lib/execution/workflow-run-already-terminal-error'
66
import type { ExecutionContext } from '@/lib/mothership/request/types'
7+
import { executeTool } from '@/lib/mothership/tool-executor/executor'
8+
import { ensureHandlersRegistered } from '@/lib/mothership/tool-executor/register-handlers'
79
import type { CancelWorkflowRunParams } from '@/lib/mothership/tools/handlers/param-types'
810

911
const { mocks } = vi.hoisted(() => ({
@@ -51,10 +53,16 @@ vi.mock('@/lib/core/telemetry', () => ({
5153
PlatformEvents: { apiKeyGenerated: vi.fn() },
5254
}))
5355

56+
vi.mock('@/lib/mothership/tools/handlers/function-execute', () => ({
57+
executeFunctionExecute: vi.fn(),
58+
}))
59+
vi.mock('@/lib/mothership/tools/handlers/run-code', () => ({ executeRunCode: vi.fn() }))
60+
vi.mock('@/lib/mothership/tools/handlers/sim-cli', () => ({ executeSimCli: vi.fn() }))
61+
vi.mock('@/lib/mothership/tools/server/router', () => ({ getRegisteredServerToolNames: () => [] }))
62+
5463
import {
5564
executeCancelWorkflowRun,
5665
executeCreateWorkflow,
57-
executeGenerateApiKey,
5866
executeMoveWorkflow,
5967
executeRunBlock,
6068
executeRunFromBlock,
@@ -448,18 +456,35 @@ describe('workflow mutation Copilot adapters', () => {
448456
key: { id: 'key-1', name: 'Copilot key', key: 'secret-key' },
449457
})
450458

451-
const result = await executeGenerateApiKey({ name: ' Copilot key ' }, context)
459+
ensureHandlersRegistered()
460+
const result = await executeTool(
461+
'generate_api_key',
462+
{ name: ' Copilot key ' },
463+
{ ...context, userPermission: 'admin' }
464+
)
452465

453466
expect(result.success).toBe(true)
454467
expect(mocks.apiKey).toHaveBeenCalledWith(
455-
context,
468+
expect.objectContaining({ userId: context.userId, workspaceId: context.workspaceId }),
456469
expect.objectContaining({
457470
operation: expect.objectContaining({ id: 'api_keys.copilot.create' }),
458471
}),
459472
{ workspaceId: 'workspace-1', name: 'Copilot key' }
460473
)
461474
})
462475

476+
it('refuses API-key creation before calling the handler when the actor lacks admin access', async () => {
477+
ensureHandlersRegistered()
478+
const result = await executeTool(
479+
'generate_api_key',
480+
{ name: 'Copilot key' },
481+
{ ...context, userPermission: 'write' }
482+
)
483+
expect(result.success).toBe(false)
484+
expect(result.error).toContain('requires admin access')
485+
expect(mocks.apiKey).not.toHaveBeenCalled()
486+
})
487+
463488
it('logs the full unknown run failure but returns a generic model-visible error', async () => {
464489
mocks.executeWorkflowUseCase.mockRejectedValueOnce(new Error('postgres password=secret'))
465490

0 commit comments

Comments
 (0)