Skip to content

Commit b8cb7fb

Browse files
committed
fix tests
1 parent 85a8560 commit b8cb7fb

2 files changed

Lines changed: 137 additions & 54 deletions

File tree

apps/sim/app/api/__test-utils__/utils.ts

Lines changed: 87 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -148,60 +148,108 @@ export const sampleWorkflowState = {
148148
}
149149

150150
// Global mock data that can be configured by tests
151+
export interface MockWorkflow {
152+
id: string
153+
userId: string
154+
pinnedApiKeyId?: string | null
155+
[key: string]: any
156+
}
157+
151158
export const globalMockData = {
152159
webhooks: [] as any[],
153-
workflows: [] as any[],
160+
workflows: [] as MockWorkflow[],
154161
schedules: [] as any[],
162+
apiKeys: [] as { id: string; userId: string }[],
155163
shouldThrowError: false,
156164
errorMessage: 'Database error',
157165
}
158166

167+
export const mockWebhookTable = {
168+
id: 'id',
169+
path: 'path',
170+
workflowId: 'workflowId',
171+
isActive: 'isActive',
172+
provider: 'provider',
173+
providerConfig: 'providerConfig',
174+
pinnedApiKeyId: 'pinnedApiKeyId',
175+
}
176+
177+
export const mockWorkflowTable = {
178+
id: 'id',
179+
userId: 'userId',
180+
pinnedApiKeyId: 'pinnedApiKeyId',
181+
}
182+
183+
export const mockApiKeyTable = {
184+
id: 'id',
185+
userId: 'userId',
186+
}
187+
159188
export const mockDb = {
160189
select: vi.fn().mockImplementation(() => {
161190
if (globalMockData.shouldThrowError) {
162191
throw new Error(globalMockData.errorMessage)
163192
}
164193
return {
165-
from: vi.fn().mockImplementation(() => ({
166-
innerJoin: vi.fn().mockImplementation(() => ({
194+
from: vi.fn().mockImplementation((table) => {
195+
const tableName = typeof table === 'string' ? table : (table?.id ?? '')
196+
197+
if (table === mockApiKeyTable || tableName === 'apiKey') {
198+
return {
199+
where: vi.fn().mockImplementation(() => ({
200+
limit: vi.fn().mockImplementation(() => {
201+
if (globalMockData.apiKeys.length > 0) {
202+
return globalMockData.apiKeys
203+
}
204+
return []
205+
}),
206+
})),
207+
}
208+
}
209+
210+
return {
211+
innerJoin: vi.fn().mockImplementation(() => ({
212+
where: vi.fn().mockImplementation(() => ({
213+
limit: vi.fn().mockImplementation(() => {
214+
// Return webhook/workflow join data if available
215+
if (globalMockData.webhooks.length > 0) {
216+
return [
217+
{
218+
webhook: globalMockData.webhooks[0],
219+
workflow: globalMockData.workflows[0] || {
220+
id: 'test-workflow',
221+
userId: 'test-user',
222+
pinnedApiKeyId: null,
223+
},
224+
},
225+
]
226+
}
227+
return []
228+
}),
229+
})),
230+
})),
167231
where: vi.fn().mockImplementation(() => ({
168232
limit: vi.fn().mockImplementation(() => {
169-
// Return webhook/workflow join data if available
170-
if (globalMockData.webhooks.length > 0) {
171-
return [
172-
{
173-
webhook: globalMockData.webhooks[0],
174-
workflow: globalMockData.workflows[0] || {
175-
id: 'test-workflow',
176-
userId: 'test-user',
177-
},
178-
},
179-
]
233+
// Return schedules if available
234+
if (globalMockData.schedules.length > 0) {
235+
return globalMockData.schedules
236+
}
237+
// Return simple workflow data
238+
if (globalMockData.workflows.length > 0) {
239+
return globalMockData.workflows
180240
}
181-
return []
241+
return [
242+
{
243+
id: 'workflow-id',
244+
userId: 'user-id',
245+
pinnedApiKeyId: null,
246+
state: sampleWorkflowState,
247+
},
248+
]
182249
}),
183250
})),
184-
})),
185-
where: vi.fn().mockImplementation(() => ({
186-
limit: vi.fn().mockImplementation(() => {
187-
// Return schedules if available
188-
if (globalMockData.schedules.length > 0) {
189-
return globalMockData.schedules
190-
}
191-
// Return simple workflow data
192-
if (globalMockData.workflows.length > 0) {
193-
return globalMockData.workflows
194-
}
195-
return [
196-
{
197-
id: 'workflow-id',
198-
userId: 'user-id',
199-
state: sampleWorkflowState,
200-
},
201-
]
202-
}),
203-
})),
204-
})),
251+
}
252+
}),
205253
}
206254
}),
207255
update: vi.fn().mockImplementation(() => ({
@@ -393,17 +441,11 @@ export function mockExecutionDependencies() {
393441
}))
394442

395443
vi.mock('@sim/db', () => ({
444+
apiKey: mockApiKeyTable,
396445
db: mockDb,
397446
// Add common schema exports that tests might need
398-
webhook: {
399-
id: 'id',
400-
path: 'path',
401-
workflowId: 'workflowId',
402-
isActive: 'isActive',
403-
provider: 'provider',
404-
providerConfig: 'providerConfig',
405-
},
406-
workflow: { id: 'id', userId: 'userId' },
447+
webhook: mockWebhookTable,
448+
workflow: mockWorkflowTable,
407449
workflowSchedule: {
408450
id: 'id',
409451
workflowId: 'workflowId',

apps/sim/app/api/webhooks/trigger/[path]/route.test.ts

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('Webhook Trigger API Route', () => {
102102
globalMockData.webhooks.length = 0
103103
globalMockData.workflows.length = 0
104104
globalMockData.schedules.length = 0
105+
globalMockData.apiKeys.length = 0
105106

106107
mockExecutionDependencies()
107108
mockTriggerDevSdk()
@@ -222,7 +223,9 @@ describe('Webhook Trigger API Route', () => {
222223
globalMockData.workflows.push({
223224
id: 'test-workflow-id',
224225
userId: 'test-user-id',
226+
pinnedApiKeyId: 'test-pinned-api-key-id',
225227
})
228+
globalMockData.apiKeys.push({ id: 'test-pinned-api-key-id', userId: 'test-user-id' })
226229

227230
const req = createMockRequest('POST', { event: 'test', id: 'test-123' })
228231
const params = Promise.resolve({ path: 'test-path' })
@@ -250,7 +253,12 @@ describe('Webhook Trigger API Route', () => {
250253
providerConfig: { requireAuth: true, token: 'test-token-123' },
251254
workflowId: 'test-workflow-id',
252255
})
253-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
256+
globalMockData.workflows.push({
257+
id: 'test-workflow-id',
258+
userId: 'test-user-id',
259+
pinnedApiKeyId: 'test-pinned-api-key-id',
260+
})
261+
globalMockData.apiKeys.push({ id: 'test-pinned-api-key-id', userId: 'test-user-id' })
254262

255263
const headers = {
256264
'Content-Type': 'application/json',
@@ -281,7 +289,12 @@ describe('Webhook Trigger API Route', () => {
281289
},
282290
workflowId: 'test-workflow-id',
283291
})
284-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
292+
globalMockData.workflows.push({
293+
id: 'test-workflow-id',
294+
userId: 'test-user-id',
295+
pinnedApiKeyId: 'test-pinned-api-key-id',
296+
})
297+
globalMockData.apiKeys.push({ id: 'test-pinned-api-key-id', userId: 'test-user-id' })
285298

286299
const headers = {
287300
'Content-Type': 'application/json',
@@ -308,7 +321,11 @@ describe('Webhook Trigger API Route', () => {
308321
providerConfig: { requireAuth: true, token: 'case-test-token' },
309322
workflowId: 'test-workflow-id',
310323
})
311-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
324+
globalMockData.workflows.push({
325+
id: 'test-workflow-id',
326+
userId: 'test-user-id',
327+
pinnedApiKeyId: 'test-pinned-api-key-id',
328+
})
312329

313330
vi.doMock('@trigger.dev/sdk', () => ({
314331
tasks: {
@@ -354,7 +371,11 @@ describe('Webhook Trigger API Route', () => {
354371
},
355372
workflowId: 'test-workflow-id',
356373
})
357-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
374+
globalMockData.workflows.push({
375+
id: 'test-workflow-id',
376+
userId: 'test-user-id',
377+
pinnedApiKeyId: 'test-pinned-api-key-id',
378+
})
358379

359380
vi.doMock('@trigger.dev/sdk', () => ({
360381
tasks: {
@@ -391,7 +412,11 @@ describe('Webhook Trigger API Route', () => {
391412
providerConfig: { requireAuth: true, token: 'correct-token' },
392413
workflowId: 'test-workflow-id',
393414
})
394-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
415+
globalMockData.workflows.push({
416+
id: 'test-workflow-id',
417+
userId: 'test-user-id',
418+
pinnedApiKeyId: 'test-pinned-api-key-id',
419+
})
395420

396421
const headers = {
397422
'Content-Type': 'application/json',
@@ -424,7 +449,11 @@ describe('Webhook Trigger API Route', () => {
424449
},
425450
workflowId: 'test-workflow-id',
426451
})
427-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
452+
globalMockData.workflows.push({
453+
id: 'test-workflow-id',
454+
userId: 'test-user-id',
455+
pinnedApiKeyId: 'test-pinned-api-key-id',
456+
})
428457

429458
const headers = {
430459
'Content-Type': 'application/json',
@@ -453,7 +482,11 @@ describe('Webhook Trigger API Route', () => {
453482
providerConfig: { requireAuth: true, token: 'required-token' },
454483
workflowId: 'test-workflow-id',
455484
})
456-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
485+
globalMockData.workflows.push({
486+
id: 'test-workflow-id',
487+
userId: 'test-user-id',
488+
pinnedApiKeyId: 'test-pinned-api-key-id',
489+
})
457490

458491
const req = createMockRequest('POST', { event: 'no.auth.test' })
459492
const params = Promise.resolve({ path: 'test-path' })
@@ -482,7 +515,11 @@ describe('Webhook Trigger API Route', () => {
482515
},
483516
workflowId: 'test-workflow-id',
484517
})
485-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
518+
globalMockData.workflows.push({
519+
id: 'test-workflow-id',
520+
userId: 'test-user-id',
521+
pinnedApiKeyId: 'test-pinned-api-key-id',
522+
})
486523

487524
const headers = {
488525
'Content-Type': 'application/json',
@@ -515,7 +552,11 @@ describe('Webhook Trigger API Route', () => {
515552
},
516553
workflowId: 'test-workflow-id',
517554
})
518-
globalMockData.workflows.push({ id: 'test-workflow-id', userId: 'test-user-id' })
555+
globalMockData.workflows.push({
556+
id: 'test-workflow-id',
557+
userId: 'test-user-id',
558+
pinnedApiKeyId: 'test-pinned-api-key-id',
559+
})
519560

520561
const headers = {
521562
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)