|
| 1 | +/** @vitest-environment node */ |
| 2 | +import { authMockFns, createMockRequest } from '@sim/testing' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ read: vi.fn(), retry: vi.fn() })) |
| 6 | +vi.mock('@/lib/knowledge/application/slack-search/onboarding', () => { |
| 7 | + const read = { |
| 8 | + id: 'knowledge.slack.onboarding.read', |
| 9 | + capability: 'none', |
| 10 | + principalKinds: ['session'], |
| 11 | + } |
| 12 | + const retry = { |
| 13 | + id: 'knowledge.slack.onboarding.retry', |
| 14 | + capability: 'knowledge.use', |
| 15 | + principalKinds: ['session'], |
| 16 | + } |
| 17 | + return { |
| 18 | + slackSearchOnboardingOperations: { read, retry }, |
| 19 | + getSlackSearchOnboarding: { operation: read, execute: mocks.read }, |
| 20 | + retrySlackSearchOnboarding: { operation: retry, execute: mocks.retry }, |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +import { OrchestrationError } from '@/lib/core/orchestration/types' |
| 25 | +import { POST } from '@/app/api/knowledge/slack/onboarding/retry/route' |
| 26 | +import { GET } from '@/app/api/knowledge/slack/onboarding/route' |
| 27 | + |
| 28 | +const token = '11111111-1111-4111-8111-111111111111' |
| 29 | +const url = `http://localhost/api/knowledge/slack/onboarding?token=${token}` |
| 30 | + |
| 31 | +beforeEach(() => { |
| 32 | + vi.clearAllMocks() |
| 33 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 34 | + user: { id: 'sender' }, |
| 35 | + session: { id: 'session' }, |
| 36 | + }) |
| 37 | + mocks.read.mockResolvedValue({ status: 'membership_required' }) |
| 38 | + mocks.retry.mockResolvedValue({ slackUrl: 'https://example.slack.com/archives/D1/p1' }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('Slack onboarding routes', () => { |
| 42 | + it('authenticates both routes before parsing invalid input', async () => { |
| 43 | + authMockFns.mockGetSession.mockResolvedValue(null) |
| 44 | + expect((await GET(createMockRequest('GET'))).status).toBe(401) |
| 45 | + expect((await POST(createMockRequest('POST', {}))).status).toBe(401) |
| 46 | + expect(mocks.read).not.toHaveBeenCalled() |
| 47 | + expect(mocks.retry).not.toHaveBeenCalled() |
| 48 | + }) |
| 49 | + |
| 50 | + it('rejects malformed tokens before entering the application', async () => { |
| 51 | + expect((await GET(createMockRequest('GET'))).status).toBe(400) |
| 52 | + expect((await POST(createMockRequest('POST', { token: 'invalid' }))).status).toBe(400) |
| 53 | + expect(mocks.read).not.toHaveBeenCalled() |
| 54 | + expect(mocks.retry).not.toHaveBeenCalled() |
| 55 | + }) |
| 56 | + |
| 57 | + it('projects only the blocked view and keeps the response private', async () => { |
| 58 | + mocks.read.mockResolvedValue({ |
| 59 | + status: 'membership_required', |
| 60 | + question: 'private question', |
| 61 | + organizationId: 'private organization', |
| 62 | + email: 'private@example.test', |
| 63 | + }) |
| 64 | + const response = await GET(createMockRequest('GET', undefined, {}, url)) |
| 65 | + expect(response.status).toBe(200) |
| 66 | + expect(await response.json()).toEqual({ status: 'membership_required' }) |
| 67 | + expect(response.headers.get('Cache-Control')).toBe('private, no-store') |
| 68 | + expect(response.headers.get('Referrer-Policy')).toBe('no-referrer') |
| 69 | + expect(mocks.read).toHaveBeenCalledWith( |
| 70 | + expect.objectContaining({ |
| 71 | + principal: { kind: 'session', userId: 'sender', sessionId: 'session' }, |
| 72 | + input: { token }, |
| 73 | + }) |
| 74 | + ) |
| 75 | + expect(mocks.retry).not.toHaveBeenCalled() |
| 76 | + }) |
| 77 | + |
| 78 | + it('passes explicit retries to the same signed-in principal', async () => { |
| 79 | + const response = await POST(createMockRequest('POST', { token })) |
| 80 | + expect(response.status).toBe(200) |
| 81 | + expect(await response.json()).toEqual({ |
| 82 | + slackUrl: 'https://example.slack.com/archives/D1/p1', |
| 83 | + }) |
| 84 | + expect(mocks.retry).toHaveBeenCalledWith( |
| 85 | + expect.objectContaining({ |
| 86 | + principal: { kind: 'session', userId: 'sender', sessionId: 'session' }, |
| 87 | + input: { token }, |
| 88 | + }) |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + it('preserves authorization denial without returning a question', async () => { |
| 93 | + mocks.retry.mockRejectedValue(new OrchestrationError('forbidden', 'Complete account setup')) |
| 94 | + const response = await POST(createMockRequest('POST', { token })) |
| 95 | + expect(response.status).toBe(403) |
| 96 | + expect(await response.json()).not.toHaveProperty('slackUrl') |
| 97 | + }) |
| 98 | + |
| 99 | + it('conceals unexpected infrastructure errors', async () => { |
| 100 | + mocks.read.mockRejectedValue(new Error('private database connection')) |
| 101 | + const response = await GET(createMockRequest('GET', undefined, {}, url)) |
| 102 | + expect(response.status).toBe(500) |
| 103 | + expect(await response.json()).toMatchObject({ error: 'Internal server error' }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments