Skip to content

Commit f182eb5

Browse files
committed
test(mcp): add regression test for agent block JWT userId resolution
1 parent d7b505f commit f182eb5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

apps/sim/tools/index.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
mockGetCustomToolById,
2626
mockListCustomTools,
2727
mockGetCustomToolByIdOrTitle,
28+
mockGenerateInternalToken,
2829
} = vi.hoisted(() => ({
2930
mockIsHosted: { value: false },
3031
mockEnv: { NEXT_PUBLIC_APP_URL: 'http://localhost:3000' } as Record<string, string | undefined>,
@@ -38,6 +39,7 @@ const {
3839
mockGetCustomToolById: vi.fn(),
3940
mockListCustomTools: vi.fn(),
4041
mockGetCustomToolByIdOrTitle: vi.fn(),
42+
mockGenerateInternalToken: vi.fn(),
4143
}))
4244

4345
// Mock feature flags
@@ -65,6 +67,10 @@ vi.mock('@/lib/api-key/byok', () => ({
6567
getBYOKKey: (...args: unknown[]) => mockGetBYOKKey(...args),
6668
}))
6769

70+
vi.mock('@/lib/auth/internal', () => ({
71+
generateInternalToken: (...args: unknown[]) => mockGenerateInternalToken(...args),
72+
}))
73+
6874
vi.mock('@/lib/billing/core/usage-log', () => ({}))
6975

7076
vi.mock('@/lib/core/rate-limiter/hosted-key', () => ({
@@ -1154,6 +1160,34 @@ describe('MCP Tool Execution', () => {
11541160
expect(result.timing).toBeDefined()
11551161
})
11561162

1163+
it('should embed userId in JWT when executionContext is undefined (agent block path)', async () => {
1164+
mockGenerateInternalToken.mockResolvedValue('test-token')
1165+
1166+
global.fetch = Object.assign(
1167+
vi.fn().mockImplementation(async () => ({
1168+
ok: true,
1169+
status: 200,
1170+
json: () =>
1171+
Promise.resolve({
1172+
success: true,
1173+
data: { output: { content: [{ type: 'text', text: 'OK' }] } },
1174+
}),
1175+
})),
1176+
{ preconnect: vi.fn() }
1177+
) as typeof fetch
1178+
1179+
await executeTool('mcp-123-test_tool', {
1180+
query: 'test',
1181+
_context: {
1182+
workspaceId: 'workspace-456',
1183+
workflowId: 'workflow-789',
1184+
userId: 'user-abc',
1185+
},
1186+
})
1187+
1188+
expect(mockGenerateInternalToken).toHaveBeenCalledWith('user-abc')
1189+
})
1190+
11571191
describe('Tool request retries', () => {
11581192
function makeJsonResponse(
11591193
status: number,

0 commit comments

Comments
 (0)