|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + getSession: vi.fn(), |
| 9 | + resolveContext: vi.fn(), |
| 10 | + resolvePermission: vi.fn(), |
| 11 | + getConnector: vi.fn(), |
| 12 | + createEnrollmentLink: vi.fn(), |
| 13 | + requireAvailable: vi.fn(), |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@/lib/auth', () => ({ getSession: mocks.getSession })) |
| 17 | +vi.mock('@sim/platform-authz/workspace', () => ({ |
| 18 | + resolveEffectiveWorkspacePermission: mocks.resolvePermission, |
| 19 | + permissionSatisfies: (actual: string | null) => actual !== null, |
| 20 | +})) |
| 21 | +vi.mock('@/lib/knowledge/application/contexts', () => ({ |
| 22 | + resolveActiveKnowledgeConnectorContext: mocks.resolveContext, |
| 23 | +})) |
| 24 | +vi.mock('@/lib/knowledge/application/connectors', () => ({ |
| 25 | + requireConnectorWorkspaceId: (context: { workspaceId: string }) => context.workspaceId, |
| 26 | + requireSuccessfulOutcome: vi.fn(), |
| 27 | + resolveConnectorCredentialAccessToken: vi.fn(), |
| 28 | +})) |
| 29 | +vi.mock('@/lib/knowledge/orchestration/connectors', () => ({ |
| 30 | + getKnowledgeConnector: mocks.getConnector, |
| 31 | +})) |
| 32 | +vi.mock('@/lib/knowledge/orchestration/connector-access', () => ({ |
| 33 | + performUpdateKnowledgeConnectorAccess: vi.fn(), |
| 34 | + resolveKnowledgeConnectorMembersBinding: vi.fn(), |
| 35 | +})) |
| 36 | +vi.mock('@/lib/knowledge/connectors/member-provisioning', () => ({ |
| 37 | + createViewerConnectorEnrollmentLink: mocks.createEnrollmentLink, |
| 38 | +})) |
| 39 | +vi.mock('@/lib/knowledge/access/availability', () => ({ |
| 40 | + requireKnowledgeMemberAccessAvailable: mocks.requireAvailable, |
| 41 | +})) |
| 42 | +vi.mock('@/lib/credential-groups/enrollments', () => ({ |
| 43 | + CredentialGroupEnrollmentError: class extends Error { |
| 44 | + constructor( |
| 45 | + message: string, |
| 46 | + readonly status: 400 | 404 | 409 | 502 |
| 47 | + ) { |
| 48 | + super(message) |
| 49 | + } |
| 50 | + }, |
| 51 | +})) |
| 52 | + |
| 53 | +import { OrchestrationError } from '@/lib/core/orchestration/types' |
| 54 | +import { CredentialGroupEnrollmentError } from '@/lib/credential-groups/enrollments' |
| 55 | +import { POST } from '@/app/api/knowledge/[id]/connectors/[connectorId]/enroll/route' |
| 56 | + |
| 57 | +const context = { params: Promise.resolve({ id: 'kb-1', connectorId: 'connector-1' }) } |
| 58 | +const request = () => createMockRequest('POST') |
| 59 | + |
| 60 | +describe('knowledge connector member enrollment', () => { |
| 61 | + beforeEach(() => { |
| 62 | + vi.clearAllMocks() |
| 63 | + mocks.getSession.mockResolvedValue({ user: { id: 'user-1' }, session: { id: 'session-1' } }) |
| 64 | + mocks.resolvePermission.mockResolvedValue('read') |
| 65 | + mocks.resolveContext.mockResolvedValue({ |
| 66 | + workspaceId: 'ws-1', |
| 67 | + workspaceOrganizationId: null, |
| 68 | + allowPersonalApiKeys: true, |
| 69 | + knowledgeBaseId: 'kb-1', |
| 70 | + connectorId: 'connector-1', |
| 71 | + }) |
| 72 | + mocks.getConnector.mockResolvedValue({ accessMode: 'members', credentialGroupId: 'group-1' }) |
| 73 | + mocks.createEnrollmentLink.mockResolvedValue('https://example.com/enroll') |
| 74 | + }) |
| 75 | + |
| 76 | + it.each([ |
| 77 | + { status: 404 as const, message: 'Credential group not found' }, |
| 78 | + { status: 409 as const, message: 'Credential group is disabled' }, |
| 79 | + { |
| 80 | + status: 409 as const, |
| 81 | + message: 'Add an account type or OAuth MCP server before inviting people', |
| 82 | + }, |
| 83 | + ])('returns $status for $message', async ({ status, message }) => { |
| 84 | + mocks.createEnrollmentLink.mockRejectedValue( |
| 85 | + new CredentialGroupEnrollmentError(message, status) |
| 86 | + ) |
| 87 | + |
| 88 | + const response = await POST(request(), context) |
| 89 | + |
| 90 | + expect(response.status).toBe(status) |
| 91 | + expect(await response.json()).toMatchObject({ error: message }) |
| 92 | + }) |
| 93 | + |
| 94 | + it('preserves the revoked-enrollment refusal', async () => { |
| 95 | + mocks.createEnrollmentLink.mockRejectedValue( |
| 96 | + new OrchestrationError('forbidden', 'A workspace admin removed your access to this connector') |
| 97 | + ) |
| 98 | + |
| 99 | + const response = await POST(request(), context) |
| 100 | + |
| 101 | + expect(response.status).toBe(403) |
| 102 | + expect(await response.json()).toMatchObject({ |
| 103 | + error: 'A workspace admin removed your access to this connector', |
| 104 | + }) |
| 105 | + }) |
| 106 | + |
| 107 | + it('does not disclose an unexpected enrollment failure', async () => { |
| 108 | + mocks.createEnrollmentLink.mockRejectedValue(new Error('Database connection failed')) |
| 109 | + |
| 110 | + const response = await POST(request(), context) |
| 111 | + |
| 112 | + expect(response.status).toBe(500) |
| 113 | + expect(await response.json()).toMatchObject({ error: 'Internal server error' }) |
| 114 | + }) |
| 115 | + |
| 116 | + it('issues the current member an enrollment link', async () => { |
| 117 | + const response = await POST(request(), context) |
| 118 | + |
| 119 | + expect(response.status).toBe(200) |
| 120 | + expect(await response.json()).toEqual({ |
| 121 | + success: true, |
| 122 | + data: { url: 'https://example.com/enroll' }, |
| 123 | + }) |
| 124 | + expect(mocks.createEnrollmentLink).toHaveBeenCalledWith({ |
| 125 | + userId: 'user-1', |
| 126 | + workspaceId: 'ws-1', |
| 127 | + credentialGroupId: 'group-1', |
| 128 | + }) |
| 129 | + }) |
| 130 | +}) |
0 commit comments