|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { dbChainMockFns, hybridAuthMockFns, resetDbChainMock } from '@sim/testing' |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockListAccessibleWorkspaceRowsForUser } = vi.hoisted(() => ({ |
| 9 | + mockListAccessibleWorkspaceRowsForUser: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/lib/workspaces/utils', () => ({ |
| 13 | + listAccessibleWorkspaceRowsForUser: mockListAccessibleWorkspaceRowsForUser, |
| 14 | +})) |
| 15 | + |
| 16 | +import { GET } from '@/app/api/mcp/discover/route' |
| 17 | + |
| 18 | +function workspaceRow(id: string) { |
| 19 | + return { |
| 20 | + workspace: { id, name: `${id} name`, archivedAt: null }, |
| 21 | + permissionType: 'write' as const, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function serverRow( |
| 26 | + id: string, |
| 27 | + { isPublic = false, workspaceAllowsPersonalApiKeys = true } = {} |
| 28 | +): Record<string, unknown> { |
| 29 | + return { |
| 30 | + id, |
| 31 | + name: `${id} name`, |
| 32 | + description: null, |
| 33 | + workspaceId: 'ws-1', |
| 34 | + workspaceName: 'ws-1 name', |
| 35 | + isPublic, |
| 36 | + workspaceAllowsPersonalApiKeys, |
| 37 | + createdAt: new Date('2026-01-01T00:00:00.000Z'), |
| 38 | + toolCount: 1, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function discoverRequest() { |
| 43 | + return new NextRequest('http://localhost:3000/api/mcp/discover', { |
| 44 | + method: 'GET', |
| 45 | + headers: { 'X-API-Key': 'sk_test_123' }, |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +function authAs(auth: Record<string, unknown>) { |
| 50 | + hybridAuthMockFns.mockCheckHybridAuth.mockResolvedValueOnce({ |
| 51 | + success: true, |
| 52 | + userId: 'user-1', |
| 53 | + ...auth, |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +describe('MCP Discover Route', () => { |
| 58 | + beforeEach(() => { |
| 59 | + vi.clearAllMocks() |
| 60 | + resetDbChainMock() |
| 61 | + mockListAccessibleWorkspaceRowsForUser.mockResolvedValue([workspaceRow('ws-1')]) |
| 62 | + }) |
| 63 | + |
| 64 | + it('hides private servers whose workspace disallows personal api keys', async () => { |
| 65 | + authAs({ authType: 'api_key', apiKeyType: 'personal' }) |
| 66 | + dbChainMockFns.orderBy.mockResolvedValueOnce([ |
| 67 | + serverRow('allowed', { workspaceAllowsPersonalApiKeys: true }), |
| 68 | + serverRow('blocked', { workspaceAllowsPersonalApiKeys: false }), |
| 69 | + ]) |
| 70 | + |
| 71 | + const response = await GET(discoverRequest()) |
| 72 | + const body = await response.json() |
| 73 | + |
| 74 | + expect(response.status).toBe(200) |
| 75 | + expect(body.servers.map((server: { id: string }) => server.id)).toEqual(['allowed']) |
| 76 | + }) |
| 77 | + |
| 78 | + it('keeps public servers listed for a personal key even when the workspace disallows them', async () => { |
| 79 | + authAs({ authType: 'api_key', apiKeyType: 'personal' }) |
| 80 | + dbChainMockFns.orderBy.mockResolvedValueOnce([ |
| 81 | + serverRow('public', { isPublic: true, workspaceAllowsPersonalApiKeys: false }), |
| 82 | + serverRow('private', { isPublic: false, workspaceAllowsPersonalApiKeys: false }), |
| 83 | + ]) |
| 84 | + |
| 85 | + const response = await GET(discoverRequest()) |
| 86 | + const body = await response.json() |
| 87 | + |
| 88 | + expect(response.status).toBe(200) |
| 89 | + expect(body.servers.map((server: { id: string }) => server.id)).toEqual(['public']) |
| 90 | + }) |
| 91 | + |
| 92 | + it('does not filter for a session caller', async () => { |
| 93 | + authAs({ authType: 'session' }) |
| 94 | + dbChainMockFns.orderBy.mockResolvedValueOnce([ |
| 95 | + serverRow('blocked', { workspaceAllowsPersonalApiKeys: false }), |
| 96 | + ]) |
| 97 | + |
| 98 | + const response = await GET(discoverRequest()) |
| 99 | + const body = await response.json() |
| 100 | + |
| 101 | + expect(response.status).toBe(200) |
| 102 | + expect(body.servers).toHaveLength(1) |
| 103 | + }) |
| 104 | + |
| 105 | + it('does not filter for a workspace key', async () => { |
| 106 | + authAs({ authType: 'api_key', apiKeyType: 'workspace', workspaceId: 'ws-1' }) |
| 107 | + dbChainMockFns.orderBy.mockResolvedValueOnce([ |
| 108 | + serverRow('blocked', { workspaceAllowsPersonalApiKeys: false }), |
| 109 | + ]) |
| 110 | + |
| 111 | + const response = await GET(discoverRequest()) |
| 112 | + const body = await response.json() |
| 113 | + |
| 114 | + expect(response.status).toBe(200) |
| 115 | + expect(body.servers).toHaveLength(1) |
| 116 | + }) |
| 117 | +}) |
0 commit comments