Files
simstudioai--sim/apps/sim/lib/webhooks/provider-subscriptions.test.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

122 lines
3.8 KiB
TypeScript

/**
* @vitest-environment node
*/
import type { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it, vi } from 'vitest'
const { mockGetEffectiveDecryptedEnv, mockGetProviderHandler } = vi.hoisted(() => ({
mockGetEffectiveDecryptedEnv: vi.fn(),
mockGetProviderHandler: vi.fn(),
}))
vi.mock('@/lib/environment/utils', () => ({
getEffectiveDecryptedEnv: mockGetEffectiveDecryptedEnv,
}))
vi.mock('@/lib/webhooks/providers', () => ({
getProviderHandler: mockGetProviderHandler,
}))
import { createExternalWebhookSubscription } from '@/lib/webhooks/provider-subscriptions'
describe('createExternalWebhookSubscription', () => {
beforeEach(() => {
vi.clearAllMocks()
mockGetEffectiveDecryptedEnv.mockResolvedValue({ ASHBY_API_KEY: 'real-secret-key' })
})
it('resolves {{ENV_VAR}} references in providerConfig before calling the provider', async () => {
const createSubscription = vi.fn().mockResolvedValue({
providerConfigUpdates: { externalId: 'ext-1' },
})
mockGetProviderHandler.mockReturnValue({ createSubscription })
const webhookData = {
provider: 'ashby',
providerConfig: { apiKey: '{{ASHBY_API_KEY}}', triggerId: 'ashby_application_submit' },
}
const workflow = { id: 'wf-1', workspaceId: 'ws-1' }
await createExternalWebhookSubscription(
{} as NextRequest,
webhookData,
workflow,
'user-1',
'req-1'
)
expect(mockGetEffectiveDecryptedEnv).toHaveBeenCalledWith('user-1', 'ws-1')
const passedWebhook = createSubscription.mock.calls[0][0].webhook
expect(passedWebhook.providerConfig.apiKey).toBe('real-secret-key')
})
it('persists the unresolved providerConfig, not the resolved one, back to the caller', async () => {
const createSubscription = vi.fn().mockResolvedValue({
providerConfigUpdates: { externalId: 'ext-1' },
})
mockGetProviderHandler.mockReturnValue({ createSubscription })
const webhookData = {
provider: 'ashby',
providerConfig: { apiKey: '{{ASHBY_API_KEY}}', triggerId: 'ashby_application_submit' },
}
const workflow = { id: 'wf-1', workspaceId: 'ws-1' }
const result = await createExternalWebhookSubscription(
{} as NextRequest,
webhookData,
workflow,
'user-1',
'req-1'
)
expect(result.updatedProviderConfig.apiKey).toBe('{{ASHBY_API_KEY}}')
expect(result.updatedProviderConfig.externalId).toBe('ext-1')
})
it('falls back to personal-only env resolution when workspaceId is not a string', async () => {
const createSubscription = vi.fn().mockResolvedValue({
providerConfigUpdates: { externalId: 'ext-1' },
})
mockGetProviderHandler.mockReturnValue({ createSubscription })
const webhookData = {
provider: 'ashby',
providerConfig: { apiKey: '{{ASHBY_API_KEY}}', triggerId: 'ashby_application_submit' },
}
const workflow = { id: 'wf-1', workspaceId: null }
await createExternalWebhookSubscription(
{} as NextRequest,
webhookData,
workflow,
'user-1',
'req-1'
)
expect(mockGetEffectiveDecryptedEnv).toHaveBeenCalledWith('user-1', undefined)
})
it('skips resolution and provider call entirely when the provider has no createSubscription', async () => {
mockGetProviderHandler.mockReturnValue({})
const webhookData = {
provider: 'slack',
providerConfig: { token: '{{SLACK_TOKEN}}' },
}
const workflow = { id: 'wf-1', workspaceId: 'ws-1' }
const result = await createExternalWebhookSubscription(
{} as NextRequest,
webhookData,
workflow,
'user-1',
'req-1'
)
expect(mockGetEffectiveDecryptedEnv).not.toHaveBeenCalled()
expect(result.externalSubscriptionCreated).toBe(false)
expect(result.updatedProviderConfig.token).toBe('{{SLACK_TOKEN}}')
})
})