|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + performCreateCredential: vi.fn(), |
| 8 | + getEffectiveDecryptedEnv: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/credentials/orchestration', () => ({ |
| 12 | + performCreateCredential: mocks.performCreateCredential, |
| 13 | +})) |
| 14 | +vi.mock('@/lib/environment/utils', () => ({ |
| 15 | + getEffectiveDecryptedEnv: mocks.getEffectiveDecryptedEnv, |
| 16 | +})) |
| 17 | + |
| 18 | +import { executeConnectSlackBot } from './connect-slack-bot' |
| 19 | + |
| 20 | +const context = { userId: 'user-1', workspaceId: 'ws-1' } as never |
| 21 | + |
| 22 | +const validParams = { |
| 23 | + displayName: 'Elder Bot', |
| 24 | + signingSecretEnvVar: 'SLACK_SIGNING_SECRET', |
| 25 | + botTokenEnvVar: 'SLACK_BOT_TOKEN', |
| 26 | +} |
| 27 | + |
| 28 | +beforeEach(() => { |
| 29 | + vi.clearAllMocks() |
| 30 | + mocks.getEffectiveDecryptedEnv.mockResolvedValue({ |
| 31 | + SLACK_SIGNING_SECRET: 'shhh', |
| 32 | + SLACK_BOT_TOKEN: 'xoxb-123', |
| 33 | + }) |
| 34 | + mocks.performCreateCredential.mockResolvedValue({ |
| 35 | + success: true, |
| 36 | + created: true, |
| 37 | + credential: { id: 'cred-1', displayName: 'Elder Bot' }, |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('executeConnectSlackBot', () => { |
| 42 | + it('resolves env vars server-side and mints the credential with the request URL', async () => { |
| 43 | + const result = await executeConnectSlackBot(validParams, context) |
| 44 | + |
| 45 | + expect(mocks.performCreateCredential).toHaveBeenCalledWith( |
| 46 | + expect.objectContaining({ |
| 47 | + workspaceId: 'ws-1', |
| 48 | + userId: 'user-1', |
| 49 | + type: 'service_account', |
| 50 | + providerId: 'slack-custom-bot', |
| 51 | + displayName: 'Elder Bot', |
| 52 | + signingSecret: 'shhh', |
| 53 | + botToken: 'xoxb-123', |
| 54 | + }) |
| 55 | + ) |
| 56 | + expect(result.success).toBe(true) |
| 57 | + expect(result.output).toMatchObject({ |
| 58 | + credentialId: 'cred-1', |
| 59 | + created: true, |
| 60 | + requestUrl: expect.stringContaining('/api/webhooks/slack/custom/cred-1'), |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('names the missing env vars without leaking any values', async () => { |
| 65 | + mocks.getEffectiveDecryptedEnv.mockResolvedValue({ SLACK_SIGNING_SECRET: 'shhh' }) |
| 66 | + |
| 67 | + const result = await executeConnectSlackBot(validParams, context) |
| 68 | + |
| 69 | + expect(result.success).toBe(false) |
| 70 | + expect(result.error).toContain('SLACK_BOT_TOKEN') |
| 71 | + expect(result.error).not.toContain('shhh') |
| 72 | + expect(mocks.performCreateCredential).not.toHaveBeenCalled() |
| 73 | + }) |
| 74 | + |
| 75 | + it('requires displayName and both env var names', async () => { |
| 76 | + const missingName = await executeConnectSlackBot( |
| 77 | + { signingSecretEnvVar: 'A', botTokenEnvVar: 'B' }, |
| 78 | + context |
| 79 | + ) |
| 80 | + expect(missingName.success).toBe(false) |
| 81 | + expect(missingName.error).toContain('displayName') |
| 82 | + |
| 83 | + const missingVars = await executeConnectSlackBot({ displayName: 'Bot' }, context) |
| 84 | + expect(missingVars.success).toBe(false) |
| 85 | + expect(missingVars.error).toContain('signingSecretEnvVar') |
| 86 | + }) |
| 87 | + |
| 88 | + it('surfaces orchestration failures (e.g. auth.test rejection or name conflict)', async () => { |
| 89 | + mocks.performCreateCredential.mockResolvedValue({ |
| 90 | + success: false, |
| 91 | + error: 'Slack rejected the bot token', |
| 92 | + }) |
| 93 | + |
| 94 | + const result = await executeConnectSlackBot(validParams, context) |
| 95 | + |
| 96 | + expect(result.success).toBe(false) |
| 97 | + expect(result.error).toContain('Slack rejected the bot token') |
| 98 | + }) |
| 99 | + |
| 100 | + it('requires workspace scope', async () => { |
| 101 | + const result = await executeConnectSlackBot(validParams, { userId: 'user-1' } as never) |
| 102 | + expect(result.success).toBe(false) |
| 103 | + expect(result.error).toContain('Workspace') |
| 104 | + }) |
| 105 | +}) |
0 commit comments