|
4 | 4 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
5 | 5 | import { WorkflowRunAlreadyTerminalError } from '@/lib/execution/workflow-run-already-terminal-error' |
6 | 6 | 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' |
7 | 9 | import type { CancelWorkflowRunParams } from '@/lib/mothership/tools/handlers/param-types' |
8 | 10 |
|
9 | 11 | const { mocks } = vi.hoisted(() => ({ |
@@ -51,10 +53,16 @@ vi.mock('@/lib/core/telemetry', () => ({ |
51 | 53 | PlatformEvents: { apiKeyGenerated: vi.fn() }, |
52 | 54 | })) |
53 | 55 |
|
| 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 | + |
54 | 63 | import { |
55 | 64 | executeCancelWorkflowRun, |
56 | 65 | executeCreateWorkflow, |
57 | | - executeGenerateApiKey, |
58 | 66 | executeMoveWorkflow, |
59 | 67 | executeRunBlock, |
60 | 68 | executeRunFromBlock, |
@@ -448,18 +456,35 @@ describe('workflow mutation Copilot adapters', () => { |
448 | 456 | key: { id: 'key-1', name: 'Copilot key', key: 'secret-key' }, |
449 | 457 | }) |
450 | 458 |
|
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 | + ) |
452 | 465 |
|
453 | 466 | expect(result.success).toBe(true) |
454 | 467 | expect(mocks.apiKey).toHaveBeenCalledWith( |
455 | | - context, |
| 468 | + expect.objectContaining({ userId: context.userId, workspaceId: context.workspaceId }), |
456 | 469 | expect.objectContaining({ |
457 | 470 | operation: expect.objectContaining({ id: 'api_keys.copilot.create' }), |
458 | 471 | }), |
459 | 472 | { workspaceId: 'workspace-1', name: 'Copilot key' } |
460 | 473 | ) |
461 | 474 | }) |
462 | 475 |
|
| 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 | + |
463 | 488 | it('logs the full unknown run failure but returns a generic model-visible error', async () => { |
464 | 489 | mocks.executeWorkflowUseCase.mockRejectedValueOnce(new Error('postgres password=secret')) |
465 | 490 |
|
|
0 commit comments