|
| 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 | + authenticate: vi.fn(), |
| 9 | + resolveContext: vi.fn(), |
| 10 | + validateDatabaseHost: vi.fn(), |
| 11 | + imapConstruct: vi.fn(), |
| 12 | + imapConnect: vi.fn(), |
| 13 | + imapList: vi.fn(), |
| 14 | + imapLogout: vi.fn(), |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/lib/selectors/server/resolve-authorized-context', () => ({ |
| 18 | + authenticateSelectorRequest: mocks.authenticate, |
| 19 | + resolveAuthorizedSelectorContext: mocks.resolveContext, |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('@/lib/core/security/input-validation.server', () => ({ |
| 23 | + validateDatabaseHost: mocks.validateDatabaseHost, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('imapflow', () => ({ |
| 27 | + ImapFlow: class MockImapFlow { |
| 28 | + constructor(config: unknown) { |
| 29 | + mocks.imapConstruct(config) |
| 30 | + } |
| 31 | + |
| 32 | + connect = mocks.imapConnect |
| 33 | + list = mocks.imapList |
| 34 | + logout = mocks.imapLogout |
| 35 | + }, |
| 36 | +})) |
| 37 | + |
| 38 | +import { POST as listMailboxes } from '@/app/api/tools/imap/mailboxes/route' |
| 39 | + |
| 40 | +function request(body: unknown) { |
| 41 | + return createMockRequest('POST', body, {}, 'http://localhost:3000/api/tools/imap/mailboxes') |
| 42 | +} |
| 43 | + |
| 44 | +const wireBody = { |
| 45 | + workflowId: 'workflow-1', |
| 46 | + host: '{{IMAP_HOST}}', |
| 47 | + port: '{{IMAP_PORT}}', |
| 48 | + secure: '{{IMAP_TLS}}', |
| 49 | + username: '{{IMAP_USERNAME}}', |
| 50 | + password: '{{IMAP_PASSWORD}}', |
| 51 | +} |
| 52 | + |
| 53 | +describe('server-resolved IMAP mailbox selector', () => { |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks() |
| 56 | + mocks.authenticate.mockResolvedValue({ |
| 57 | + ok: true, |
| 58 | + principal: { kind: 'session', userId: 'viewer-1', sessionId: 'session-1' }, |
| 59 | + }) |
| 60 | + mocks.resolveContext.mockResolvedValue({ |
| 61 | + ok: true, |
| 62 | + context: { |
| 63 | + host: 'imap.example.com', |
| 64 | + port: '993', |
| 65 | + secure: 'true', |
| 66 | + username: 'mailbox@example.com', |
| 67 | + password: 'resolved-password', |
| 68 | + }, |
| 69 | + requesterUserId: 'viewer-1', |
| 70 | + workspaceId: 'workspace-1', |
| 71 | + }) |
| 72 | + mocks.validateDatabaseHost.mockResolvedValue({ |
| 73 | + isValid: true, |
| 74 | + resolvedIP: '203.0.113.10', |
| 75 | + }) |
| 76 | + mocks.imapConnect.mockResolvedValue(undefined) |
| 77 | + mocks.imapList.mockResolvedValue([ |
| 78 | + { path: 'Archive', name: 'Archive', delimiter: '/' }, |
| 79 | + { path: 'INBOX', name: 'INBOX', delimiter: '/' }, |
| 80 | + ]) |
| 81 | + mocks.imapLogout.mockResolvedValue(undefined) |
| 82 | + }) |
| 83 | + |
| 84 | + it('authenticates before parsing malformed requests', async () => { |
| 85 | + mocks.authenticate.mockResolvedValue({ ok: false, status: 401, error: 'Unauthorized' }) |
| 86 | + |
| 87 | + const response = await listMailboxes(request({})) |
| 88 | + |
| 89 | + expect(response.status).toBe(401) |
| 90 | + expect(mocks.resolveContext).not.toHaveBeenCalled() |
| 91 | + }) |
| 92 | + |
| 93 | + it('passes every raw connection field to the resolver and maps sorted mailboxes', async () => { |
| 94 | + const response = await listMailboxes(request(wireBody)) |
| 95 | + |
| 96 | + expect(await response.json()).toEqual({ |
| 97 | + success: true, |
| 98 | + mailboxes: [ |
| 99 | + { path: 'INBOX', name: 'INBOX', delimiter: '/' }, |
| 100 | + { path: 'Archive', name: 'Archive', delimiter: '/' }, |
| 101 | + ], |
| 102 | + }) |
| 103 | + expect(mocks.resolveContext).toHaveBeenCalledWith(expect.anything(), { |
| 104 | + workflowId: 'workflow-1', |
| 105 | + context: { |
| 106 | + host: '{{IMAP_HOST}}', |
| 107 | + port: '{{IMAP_PORT}}', |
| 108 | + secure: '{{IMAP_TLS}}', |
| 109 | + username: '{{IMAP_USERNAME}}', |
| 110 | + password: '{{IMAP_PASSWORD}}', |
| 111 | + }, |
| 112 | + }) |
| 113 | + expect(mocks.validateDatabaseHost).toHaveBeenCalledWith('imap.example.com', 'host', { |
| 114 | + logFailureDetails: false, |
| 115 | + }) |
| 116 | + expect(mocks.imapConstruct).toHaveBeenCalledWith( |
| 117 | + expect.objectContaining({ |
| 118 | + host: '203.0.113.10', |
| 119 | + servername: 'imap.example.com', |
| 120 | + port: 993, |
| 121 | + secure: true, |
| 122 | + auth: { user: 'mailbox@example.com', pass: 'resolved-password' }, |
| 123 | + }) |
| 124 | + ) |
| 125 | + expect(mocks.imapLogout).toHaveBeenCalledOnce() |
| 126 | + }) |
| 127 | + |
| 128 | + it('short-circuits inaccessible references before DNS or provider access', async () => { |
| 129 | + mocks.resolveContext.mockResolvedValue({ |
| 130 | + ok: false, |
| 131 | + status: 400, |
| 132 | + error: 'Unable to resolve selector configuration', |
| 133 | + }) |
| 134 | + |
| 135 | + const response = await listMailboxes(request(wireBody)) |
| 136 | + |
| 137 | + expect(response.status).toBe(400) |
| 138 | + expect(await response.json()).toEqual({ |
| 139 | + success: false, |
| 140 | + message: 'Unable to resolve selector configuration', |
| 141 | + }) |
| 142 | + expect(mocks.validateDatabaseHost).not.toHaveBeenCalled() |
| 143 | + expect(mocks.imapConstruct).not.toHaveBeenCalled() |
| 144 | + }) |
| 145 | + |
| 146 | + it('rejects invalid resolved port/TLS values before DNS or provider access', async () => { |
| 147 | + mocks.resolveContext.mockResolvedValue({ |
| 148 | + ok: true, |
| 149 | + context: { |
| 150 | + host: 'imap.example.com', |
| 151 | + port: 'invalid', |
| 152 | + secure: 'invalid', |
| 153 | + username: 'mailbox@example.com', |
| 154 | + password: 'resolved-password', |
| 155 | + }, |
| 156 | + requesterUserId: 'viewer-1', |
| 157 | + workspaceId: 'workspace-1', |
| 158 | + }) |
| 159 | + |
| 160 | + const response = await listMailboxes(request(wireBody)) |
| 161 | + |
| 162 | + expect(response.status).toBe(400) |
| 163 | + expect(mocks.validateDatabaseHost).not.toHaveBeenCalled() |
| 164 | + expect(mocks.imapConstruct).not.toHaveBeenCalled() |
| 165 | + }) |
| 166 | + |
| 167 | + it('sanitizes provider connection failures and logs out best-effort', async () => { |
| 168 | + mocks.imapConnect.mockRejectedValue( |
| 169 | + new Error('connection failed with resolved-password and imap.example.com') |
| 170 | + ) |
| 171 | + |
| 172 | + const response = await listMailboxes(request(wireBody)) |
| 173 | + const body = await response.json() |
| 174 | + |
| 175 | + expect(response.status).toBe(500) |
| 176 | + expect(body).toEqual({ |
| 177 | + success: false, |
| 178 | + message: 'Failed to connect to IMAP server. Please check your connection settings.', |
| 179 | + }) |
| 180 | + expect(mocks.imapLogout).toHaveBeenCalledOnce() |
| 181 | + }) |
| 182 | +}) |
0 commit comments