-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathdeploy.test.ts
More file actions
361 lines (313 loc) · 14.3 KB
/
Copy pathdeploy.test.ts
File metadata and controls
361 lines (313 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* @vitest-environment node
*/
import { account, credential } from '@sim/db/schema'
import { queueTableRows, resetDbChainMock } from '@sim/testing'
import { eq } from 'drizzle-orm'
import { afterAll, beforeEach, describe, expect, it, type Mock, vi } from 'vitest'
import type { SubBlockConfig } from '@/blocks/types'
import type { BlockState } from '@/stores/workflows/workflow/types'
// deploy.ts pulls in the trigger/block/provider registries at module load; none are exercised by
// buildProviderConfig (a pure function), so stub them to keep this unit test fast and isolated.
vi.mock('@/blocks', () => ({ getBlock: vi.fn() }))
vi.mock('@/triggers', () => ({ getTrigger: vi.fn(), isTriggerValid: vi.fn(() => true) }))
vi.mock('@/lib/webhooks/providers', () => ({ getProviderHandler: vi.fn() }))
vi.mock('@/lib/webhooks/provider-subscriptions', () => ({
cleanupExternalWebhook: vi.fn(),
createExternalWebhookSubscription: vi.fn(),
hasWebhookConfigChanged: vi.fn(),
}))
vi.mock('@/lib/webhooks/utils.server', () => ({
findConflictingWebhookPathOwner: vi.fn(),
}))
vi.mock('@/lib/webhooks/pending-verification', () => ({
PendingWebhookVerificationTracker: vi.fn(),
}))
const {
mockGetSlackBotCredential,
mockResolveOAuthAccountId,
mockRefreshAccessTokenIfNeeded,
mockFetchSlackTeamId,
} = vi.hoisted(() => ({
mockGetSlackBotCredential: vi.fn(),
mockResolveOAuthAccountId: vi.fn(),
mockRefreshAccessTokenIfNeeded: vi.fn(),
mockFetchSlackTeamId: vi.fn(),
}))
vi.mock('@/app/api/auth/oauth/utils', () => ({
getSlackBotCredential: mockGetSlackBotCredential,
resolveOAuthAccountId: mockResolveOAuthAccountId,
refreshAccessTokenIfNeeded: mockRefreshAccessTokenIfNeeded,
}))
vi.mock('@/lib/webhooks/providers/slack', () => ({
fetchSlackTeamId: mockFetchSlackTeamId,
}))
import {
buildProviderConfig,
resolveTriggerCredentialId,
resolveWebhookConfigForBlock,
} from '@/lib/webhooks/deploy'
import { getBlock } from '@/blocks'
import { getTrigger } from '@/triggers'
afterAll(resetDbChainMock)
const trigger = (subBlocks: Partial<SubBlockConfig>[]): { subBlocks: SubBlockConfig[] } => ({
subBlocks: subBlocks as SubBlockConfig[],
})
const driveTrigger = trigger([
{
id: 'triggerCredentials',
mode: 'trigger',
canonicalParamId: 'oauthCredential',
serviceId: 'google-drive',
},
{ id: 'folderId', mode: 'trigger', canonicalParamId: 'folderId', required: false },
{ id: 'manualFolderId', mode: 'trigger-advanced', canonicalParamId: 'folderId', required: false },
])
const tableTrigger = trigger([
{ id: 'tableSelector', mode: 'trigger', canonicalParamId: 'tableId', required: true },
{ id: 'manualTableId', mode: 'trigger-advanced', canonicalParamId: 'tableId', required: true },
])
const slackTrigger = trigger([
{ id: 'eventType', mode: 'trigger', required: true },
{
id: 'customBotCredential',
mode: 'trigger',
canonicalParamId: 'botCredential',
serviceId: 'slack',
required: true,
},
{
id: 'manualBotCredential',
mode: 'trigger-advanced',
canonicalParamId: 'botCredential',
required: true,
},
])
function makeBlock(
type: string,
subBlockValues: Record<string, unknown>,
canonicalModes?: Record<string, 'basic' | 'advanced'>
): BlockState {
const subBlocks: Record<string, { value: unknown }> = {}
for (const [key, value] of Object.entries(subBlockValues)) subBlocks[key] = { value }
return {
id: 'block-1',
type,
subBlocks,
...(canonicalModes ? { data: { canonicalModes } } : {}),
} as unknown as BlockState
}
beforeEach(() => {
vi.clearAllMocks()
resetDbChainMock()
})
describe('buildProviderConfig canonical collapse', () => {
it('writes the basic value under the canonical key in basic mode', () => {
const block = makeBlock('google_drive_poller', { folderId: 'BASIC' })
const { providerConfig } = buildProviderConfig(block, 'google_drive_poller', driveTrigger)
expect(providerConfig.folderId).toBe('BASIC')
})
it('returns the credential reference and OAuth service for deploy validation', () => {
const block = makeBlock('google_drive_poller', { triggerCredentials: 'credential-1' })
const result = buildProviderConfig(block, 'google_drive_poller', driveTrigger)
expect(result.credentialReference).toBe('credential-1')
expect(result.credentialServiceId).toBe('google-drive')
expect(result.providerConfig.credentialId).toBeUndefined()
})
it('writes the active (advanced) value under the canonical key when only advanced is set', () => {
const block = makeBlock('google_drive_poller', { manualFolderId: 'ADVANCED' })
const { providerConfig } = buildProviderConfig(block, 'google_drive_poller', driveTrigger)
// Heuristic: empty basic + populated advanced => advanced is active.
expect(providerConfig.folderId).toBe('ADVANCED')
// Raw advanced key kept for transitional readers.
expect(providerConfig.manualFolderId).toBe('ADVANCED')
})
it('collapses a drift block (stale basic + active advanced via override) to the active value', () => {
const block = makeBlock(
'google_drive_poller',
{ folderId: 'STALE', manualFolderId: 'ACTIVE' },
{ folderId: 'advanced' }
)
const { providerConfig } = buildProviderConfig(block, 'google_drive_poller', driveTrigger)
// The canonical key collapses to the active (advanced) value, not the stale basic value.
expect(providerConfig.folderId).toBe('ACTIVE')
expect(providerConfig.manualFolderId).toBe('ACTIVE')
})
it('honors a basic-mode override even when advanced is populated', () => {
const block = makeBlock(
'google_drive_poller',
{ folderId: 'BASIC', manualFolderId: 'ADVANCED' },
{ folderId: 'basic' }
)
const { providerConfig } = buildProviderConfig(block, 'google_drive_poller', driveTrigger)
expect(providerConfig.folderId).toBe('BASIC')
})
it('omits the canonical key when the active value is empty (optional field)', () => {
const block = makeBlock('google_drive_poller', {})
const { providerConfig } = buildProviderConfig(block, 'google_drive_poller', driveTrigger)
expect(providerConfig.folderId).toBeUndefined()
})
it('writes a distinct canonical key (tableId) for the table trigger', () => {
const block = makeBlock('table_new_row', { tableSelector: 'TBL' })
const { providerConfig } = buildProviderConfig(block, 'table_new_row', tableTrigger)
expect(providerConfig.tableId).toBe('TBL')
// Raw basic key kept for transitional readers.
expect(providerConfig.tableSelector).toBe('TBL')
})
it('collapses a drift table block to the active value under tableId', () => {
const block = makeBlock(
'table_new_row',
{ tableSelector: 'STALE', manualTableId: 'ACTIVE' },
{ tableId: 'advanced' }
)
const { providerConfig } = buildProviderConfig(block, 'table_new_row', tableTrigger)
expect(providerConfig.tableId).toBe('ACTIVE')
})
it('collapses the slack bot credential pair under botCredential for the routing branch', () => {
const block = makeBlock('slack_v2', {
eventType: 'message',
customBotCredential: 'cred_bot_1',
})
const result = buildProviderConfig(block, 'slack_oauth', slackTrigger)
expect(result.providerConfig.botCredential).toBe('cred_bot_1')
expect(result.providerConfig.eventType).toBe('message')
// The slack trigger has no generic triggerCredentials field — the routing
// branch resolves botCredential itself.
expect(result.credentialReference).toBeUndefined()
expect(result.credentialServiceId).toBeUndefined()
})
it('reports a missing required slack bot credential as a missing field', () => {
const block = makeBlock('slack_v2', { eventType: 'message' })
const result = buildProviderConfig(block, 'slack_oauth', slackTrigger)
expect(result.missingFields.length).toBeGreaterThan(0)
})
})
describe('resolveTriggerCredentialId', () => {
it('canonicalizes an OAuth service alias at the credential lookup boundary', async () => {
await resolveTriggerCredentialId('credential-1', 'workspace-1', 'gmail')
expect(eq).toHaveBeenCalledWith(credential.workspaceId, 'workspace-1')
expect(eq).toHaveBeenCalledWith(credential.type, 'oauth')
expect(eq).toHaveBeenCalledWith(credential.providerId, 'google-email')
expect(eq).toHaveBeenCalledWith(credential.id, 'credential-1')
expect(eq).toHaveBeenCalledWith(credential.accountId, 'credential-1')
})
})
describe('resolveWebhookConfigForBlock — slack_oauth routing', () => {
const slackTriggerDef = {
provider: 'slack_app',
name: 'Slack',
subBlocks: [
{ id: 'eventType', mode: 'trigger', required: true },
{
id: 'customBotCredential',
mode: 'trigger',
canonicalParamId: 'botCredential',
serviceId: 'slack',
required: true,
},
{
id: 'manualBotCredential',
mode: 'trigger-advanced',
canonicalParamId: 'botCredential',
required: true,
},
],
}
function resolveSlack(
values: Record<string, unknown>,
workflow: Record<string, unknown> = { workspaceId: 'ws-1' }
) {
;(getBlock as unknown as Mock).mockReturnValue({ category: 'triggers' })
;(getTrigger as unknown as Mock).mockReturnValue(slackTriggerDef)
return resolveWebhookConfigForBlock({
block: makeBlock('slack_oauth', values),
workflow,
userId: 'deployer-1',
requestId: 'req-1',
})
}
it('routes a custom bot credential by credential id on the slack provider', async () => {
mockGetSlackBotCredential.mockResolvedValue({ workspaceId: 'ws-1', botUserId: 'BUSER' })
const result = await resolveSlack({ eventType: 'message', customBotCredential: 'cred_bot_1' })
expect(result?.success).toBe(true)
if (!result?.success) throw new Error('expected success')
expect(result.config.provider).toBe('slack')
expect(result.config.routingKey).toBe('cred_bot_1')
expect(result.config.triggerPath).toBeNull()
expect(result.config.providerConfig.bot_user_id).toBe('BUSER')
})
it('rejects a custom bot credential from another workspace', async () => {
mockGetSlackBotCredential.mockResolvedValue({ workspaceId: 'other-ws', botUserId: 'BUSER' })
const result = await resolveSlack({ eventType: 'message', customBotCredential: 'cred_bot_1' })
expect(result?.success).toBe(false)
if (result?.success) throw new Error('expected failure')
expect(result?.error?.status).toBe(400)
expect(result?.error?.message).toContain('not available in this workspace')
})
it('rejects a deleted or secretless custom bot credential as an invalid bot', async () => {
mockGetSlackBotCredential.mockResolvedValue(null)
mockResolveOAuthAccountId.mockResolvedValue({ credentialType: 'service_account' })
const result = await resolveSlack({ eventType: 'message', customBotCredential: 'cred_bot_x' })
expect(result?.success).toBe(false)
if (result?.success) throw new Error('expected failure')
expect(result?.error?.status).toBe(400)
expect(result?.error?.message).toContain('bot credential is missing or invalid')
expect(mockRefreshAccessTokenIfNeeded).not.toHaveBeenCalled()
})
it('rejects an OAuth credential not resolvable in the workflow workspace', async () => {
mockGetSlackBotCredential.mockResolvedValue(null)
mockResolveOAuthAccountId.mockResolvedValue({ accountId: 'acct-1' })
// No credential row queued → resolveTriggerCredentialId returns null.
const result = await resolveSlack({ eventType: 'message', customBotCredential: 'cred_foreign' })
expect(result?.success).toBe(false)
if (result?.success) throw new Error('expected failure')
expect(result?.error?.status).toBe(400)
expect(result?.error?.message).toContain('not available in this workspace')
expect(mockRefreshAccessTokenIfNeeded).not.toHaveBeenCalled()
})
it('rejects a non-simSubscribed event on the native Sim app (OAuth account)', async () => {
mockGetSlackBotCredential.mockResolvedValue(null)
mockResolveOAuthAccountId.mockResolvedValue({ accountId: 'acct-1' })
queueTableRows(credential, [{ id: 'cred_oauth_1' }])
const result = await resolveSlack({
eventType: 'file_shared',
customBotCredential: 'cred_oauth_1',
})
expect(result?.success).toBe(false)
if (result?.success) throw new Error('expected failure')
expect(result?.error?.status).toBe(400)
expect(result?.error?.message).toContain('not available on the Sim Slack app')
expect(mockRefreshAccessTokenIfNeeded).not.toHaveBeenCalled()
})
it('routes an OAuth account by team_id on the slack_app provider', async () => {
mockGetSlackBotCredential.mockResolvedValue(null)
mockResolveOAuthAccountId.mockResolvedValue({ accountId: 'acct-1' })
queueTableRows(credential, [{ id: 'cred_oauth_1' }])
queueTableRows(account, [{ userId: 'owner-1' }])
mockRefreshAccessTokenIfNeeded.mockResolvedValue('xoxb-token')
mockFetchSlackTeamId.mockResolvedValue({ teamId: 'T123', userId: 'UBOT' })
const result = await resolveSlack({ eventType: 'message', customBotCredential: 'cred_oauth_1' })
expect(result?.success).toBe(true)
if (!result?.success) throw new Error('expected success')
expect(result.config.provider).toBe('slack_app')
expect(result.config.routingKey).toBe('T123')
expect(result.config.triggerPath).toBeNull()
expect(result.config.providerConfig.bot_user_id).toBe('UBOT')
// Runtime token resolution + disconnect cleanup key slack_app rows on this.
expect(result.config.providerConfig.credentialId).toBe('cred_oauth_1')
// Owner's token, not the deploying actor's.
expect(mockRefreshAccessTokenIfNeeded).toHaveBeenCalledWith('cred_oauth_1', 'owner-1', 'req-1')
})
it('fails when the connected Slack account token cannot be resolved', async () => {
mockGetSlackBotCredential.mockResolvedValue(null)
mockResolveOAuthAccountId.mockResolvedValue({ accountId: '' })
queueTableRows(credential, [{ id: 'cred_oauth_1' }])
mockRefreshAccessTokenIfNeeded.mockResolvedValue(null)
const result = await resolveSlack({ eventType: 'message', customBotCredential: 'cred_oauth_1' })
expect(result?.success).toBe(false)
if (result?.success) throw new Error('expected failure')
expect(result?.error?.status).toBe(400)
expect(result?.error?.message).toContain('Could not access the connected Slack account')
expect(mockFetchSlackTeamId).not.toHaveBeenCalled()
})
})