Skip to content

Commit 71693c3

Browse files
committed
fix tests
1 parent faada90 commit 71693c3

File tree

7 files changed

+9531
-2
lines changed

7 files changed

+9531
-2
lines changed

apps/sim/app/api/auth/oauth/disconnect/route.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/ut
88

99
describe('OAuth Disconnect API Route', () => {
1010
const mockGetSession = vi.fn()
11+
const mockSelectChain = {
12+
from: vi.fn().mockReturnThis(),
13+
innerJoin: vi.fn().mockReturnThis(),
14+
where: vi.fn().mockResolvedValue([]),
15+
}
1116
const mockDb = {
1217
delete: vi.fn().mockReturnThis(),
1318
where: vi.fn(),
19+
select: vi.fn().mockReturnValue(mockSelectChain),
1420
}
1521
const mockLogger = createMockLogger()
22+
const mockSyncAllWebhooksForCredentialSet = vi.fn().mockResolvedValue({})
1623

1724
const mockUUID = 'mock-uuid-12345678-90ab-cdef-1234-567890abcdef'
1825

@@ -33,6 +40,13 @@ describe('OAuth Disconnect API Route', () => {
3340

3441
vi.doMock('@sim/db/schema', () => ({
3542
account: { userId: 'userId', providerId: 'providerId' },
43+
credentialSetMember: {
44+
id: 'id',
45+
credentialSetId: 'credentialSetId',
46+
userId: 'userId',
47+
status: 'status',
48+
},
49+
credentialSet: { id: 'id', providerId: 'providerId' },
3650
}))
3751

3852
vi.doMock('drizzle-orm', () => ({
@@ -45,6 +59,14 @@ describe('OAuth Disconnect API Route', () => {
4559
vi.doMock('@sim/logger', () => ({
4660
createLogger: vi.fn().mockReturnValue(mockLogger),
4761
}))
62+
63+
vi.doMock('@/lib/core/utils/request', () => ({
64+
generateRequestId: vi.fn().mockReturnValue('test-request-id'),
65+
}))
66+
67+
vi.doMock('@/lib/webhooks/utils.server', () => ({
68+
syncAllWebhooksForCredentialSet: mockSyncAllWebhooksForCredentialSet,
69+
}))
4870
})
4971

5072
afterEach(() => {

apps/sim/app/api/auth/oauth/token/route.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ describe('OAuth Token API Routes', () => {
138138
const data = await response.json()
139139

140140
expect(response.status).toBe(400)
141-
expect(data).toHaveProperty('error', 'Credential ID is required')
141+
expect(data).toHaveProperty(
142+
'error',
143+
'Either credentialId or (credentialAccountUserId + providerId) is required'
144+
)
142145
expect(mockLogger.warn).toHaveBeenCalled()
143146
})
144147

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

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,106 @@ vi.mock('@/lib/workflows/persistence/utils', () => ({
172172
blockExistsInDeployment: vi.fn().mockResolvedValue(true),
173173
}))
174174

175+
vi.mock('@/lib/webhooks/processor', () => ({
176+
findAllWebhooksForPath: vi.fn().mockImplementation(async (options: { path: string }) => {
177+
// Filter webhooks by path from globalMockData
178+
const matchingWebhooks = globalMockData.webhooks.filter(
179+
(wh) => wh.path === options.path && wh.isActive
180+
)
181+
182+
if (matchingWebhooks.length === 0) {
183+
return []
184+
}
185+
186+
// Return array of {webhook, workflow} objects
187+
return matchingWebhooks.map((wh) => {
188+
const matchingWorkflow = globalMockData.workflows.find((w) => w.id === wh.workflowId) || {
189+
id: wh.workflowId || 'test-workflow-id',
190+
userId: 'test-user-id',
191+
workspaceId: 'test-workspace-id',
192+
}
193+
return {
194+
webhook: wh,
195+
workflow: matchingWorkflow,
196+
}
197+
})
198+
}),
199+
parseWebhookBody: vi.fn().mockImplementation(async (request: any) => {
200+
try {
201+
const cloned = request.clone()
202+
const rawBody = await cloned.text()
203+
const body = rawBody ? JSON.parse(rawBody) : {}
204+
return { body, rawBody }
205+
} catch {
206+
return { body: {}, rawBody: '' }
207+
}
208+
}),
209+
handleProviderChallenges: vi.fn().mockResolvedValue(null),
210+
handleProviderReachabilityTest: vi.fn().mockReturnValue(null),
211+
verifyProviderAuth: vi
212+
.fn()
213+
.mockImplementation(
214+
async (
215+
foundWebhook: any,
216+
_foundWorkflow: any,
217+
request: any,
218+
_rawBody: string,
219+
_requestId: string
220+
) => {
221+
// Implement generic webhook auth verification for tests
222+
if (foundWebhook.provider === 'generic') {
223+
const providerConfig = foundWebhook.providerConfig || {}
224+
if (providerConfig.requireAuth) {
225+
const configToken = providerConfig.token
226+
const secretHeaderName = providerConfig.secretHeaderName
227+
228+
if (configToken) {
229+
let isTokenValid = false
230+
231+
if (secretHeaderName) {
232+
// Custom header auth
233+
const headerValue = request.headers.get(secretHeaderName.toLowerCase())
234+
if (headerValue === configToken) {
235+
isTokenValid = true
236+
}
237+
} else {
238+
// Bearer token auth
239+
const authHeader = request.headers.get('authorization')
240+
if (authHeader?.toLowerCase().startsWith('bearer ')) {
241+
const token = authHeader.substring(7)
242+
if (token === configToken) {
243+
isTokenValid = true
244+
}
245+
}
246+
}
247+
248+
if (!isTokenValid) {
249+
const { NextResponse } = await import('next/server')
250+
return new NextResponse('Unauthorized - Invalid authentication token', {
251+
status: 401,
252+
})
253+
}
254+
} else {
255+
// Auth required but no token configured
256+
const { NextResponse } = await import('next/server')
257+
return new NextResponse('Unauthorized - Authentication required but not configured', {
258+
status: 401,
259+
})
260+
}
261+
}
262+
}
263+
return null
264+
}
265+
),
266+
checkWebhookPreprocessing: vi.fn().mockResolvedValue(null),
267+
queueWebhookExecution: vi.fn().mockImplementation(async () => {
268+
// Call processWebhookMock so tests can verify it was called
269+
processWebhookMock()
270+
const { NextResponse } = await import('next/server')
271+
return NextResponse.json({ message: 'Webhook processed' })
272+
}),
273+
}))
274+
175275
vi.mock('drizzle-orm/postgres-js', () => ({
176276
drizzle: vi.fn().mockReturnValue({}),
177277
}))
@@ -180,6 +280,10 @@ vi.mock('postgres', () => vi.fn().mockReturnValue({}))
180280

181281
vi.mock('@sim/logger', () => loggerMock)
182282

283+
vi.mock('@/lib/core/utils/request', () => ({
284+
generateRequestId: vi.fn().mockReturnValue('test-request-id'),
285+
}))
286+
183287
process.env.DATABASE_URL = 'postgresql://test:test@localhost:5432/test'
184288

185289
import { POST } from '@/app/api/webhooks/trigger/[path]/route'

apps/sim/triggers/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export const SYSTEM_SUBBLOCK_IDS: string[] = [
1313
'setupScript', // Setup script code (e.g., Apps Script)
1414
'triggerId', // Stored trigger ID
1515
'selectedTriggerId', // Selected trigger from dropdown (multi-trigger blocks)
16-
'triggerConfig', // Aggregated trigger config (derived from individual fields)
1716
]
1817

1918
/**
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
CREATE TYPE "public"."credential_set_invitation_status" AS ENUM('pending', 'accepted', 'expired', 'cancelled');--> statement-breakpoint
2+
CREATE TYPE "public"."credential_set_member_status" AS ENUM('active', 'pending', 'revoked');--> statement-breakpoint
3+
CREATE TABLE "credential_set" (
4+
"id" text PRIMARY KEY NOT NULL,
5+
"organization_id" text NOT NULL,
6+
"name" text NOT NULL,
7+
"description" text,
8+
"provider_id" text NOT NULL,
9+
"created_by" text NOT NULL,
10+
"created_at" timestamp DEFAULT now() NOT NULL,
11+
"updated_at" timestamp DEFAULT now() NOT NULL
12+
);
13+
--> statement-breakpoint
14+
CREATE TABLE "credential_set_invitation" (
15+
"id" text PRIMARY KEY NOT NULL,
16+
"credential_set_id" text NOT NULL,
17+
"email" text,
18+
"token" text NOT NULL,
19+
"invited_by" text NOT NULL,
20+
"status" "credential_set_invitation_status" DEFAULT 'pending' NOT NULL,
21+
"expires_at" timestamp NOT NULL,
22+
"accepted_at" timestamp,
23+
"accepted_by_user_id" text,
24+
"created_at" timestamp DEFAULT now() NOT NULL,
25+
CONSTRAINT "credential_set_invitation_token_unique" UNIQUE("token")
26+
);
27+
--> statement-breakpoint
28+
CREATE TABLE "credential_set_member" (
29+
"id" text PRIMARY KEY NOT NULL,
30+
"credential_set_id" text NOT NULL,
31+
"user_id" text NOT NULL,
32+
"status" "credential_set_member_status" DEFAULT 'pending' NOT NULL,
33+
"joined_at" timestamp,
34+
"invited_by" text,
35+
"created_at" timestamp DEFAULT now() NOT NULL,
36+
"updated_at" timestamp DEFAULT now() NOT NULL
37+
);
38+
--> statement-breakpoint
39+
ALTER TABLE "webhook" ADD COLUMN "credential_set_id" text;--> statement-breakpoint
40+
ALTER TABLE "credential_set" ADD CONSTRAINT "credential_set_organization_id_organization_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organization"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
41+
ALTER TABLE "credential_set" ADD CONSTRAINT "credential_set_created_by_user_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
42+
ALTER TABLE "credential_set_invitation" ADD CONSTRAINT "credential_set_invitation_credential_set_id_credential_set_id_fk" FOREIGN KEY ("credential_set_id") REFERENCES "public"."credential_set"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
43+
ALTER TABLE "credential_set_invitation" ADD CONSTRAINT "credential_set_invitation_invited_by_user_id_fk" FOREIGN KEY ("invited_by") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
44+
ALTER TABLE "credential_set_invitation" ADD CONSTRAINT "credential_set_invitation_accepted_by_user_id_user_id_fk" FOREIGN KEY ("accepted_by_user_id") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
45+
ALTER TABLE "credential_set_member" ADD CONSTRAINT "credential_set_member_credential_set_id_credential_set_id_fk" FOREIGN KEY ("credential_set_id") REFERENCES "public"."credential_set"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
46+
ALTER TABLE "credential_set_member" ADD CONSTRAINT "credential_set_member_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
47+
ALTER TABLE "credential_set_member" ADD CONSTRAINT "credential_set_member_invited_by_user_id_fk" FOREIGN KEY ("invited_by") REFERENCES "public"."user"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
48+
CREATE INDEX "credential_set_organization_id_idx" ON "credential_set" USING btree ("organization_id");--> statement-breakpoint
49+
CREATE INDEX "credential_set_created_by_idx" ON "credential_set" USING btree ("created_by");--> statement-breakpoint
50+
CREATE UNIQUE INDEX "credential_set_org_name_unique" ON "credential_set" USING btree ("organization_id","name");--> statement-breakpoint
51+
CREATE INDEX "credential_set_provider_id_idx" ON "credential_set" USING btree ("provider_id");--> statement-breakpoint
52+
CREATE INDEX "credential_set_invitation_set_id_idx" ON "credential_set_invitation" USING btree ("credential_set_id");--> statement-breakpoint
53+
CREATE INDEX "credential_set_invitation_token_idx" ON "credential_set_invitation" USING btree ("token");--> statement-breakpoint
54+
CREATE INDEX "credential_set_invitation_status_idx" ON "credential_set_invitation" USING btree ("status");--> statement-breakpoint
55+
CREATE INDEX "credential_set_invitation_expires_at_idx" ON "credential_set_invitation" USING btree ("expires_at");--> statement-breakpoint
56+
CREATE INDEX "credential_set_member_set_id_idx" ON "credential_set_member" USING btree ("credential_set_id");--> statement-breakpoint
57+
CREATE INDEX "credential_set_member_user_id_idx" ON "credential_set_member" USING btree ("user_id");--> statement-breakpoint
58+
CREATE UNIQUE INDEX "credential_set_member_unique" ON "credential_set_member" USING btree ("credential_set_id","user_id");--> statement-breakpoint
59+
CREATE INDEX "credential_set_member_status_idx" ON "credential_set_member" USING btree ("status");--> statement-breakpoint
60+
ALTER TABLE "webhook" ADD CONSTRAINT "webhook_credential_set_id_credential_set_id_fk" FOREIGN KEY ("credential_set_id") REFERENCES "public"."credential_set"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
61+
CREATE INDEX "webhook_credential_set_id_idx" ON "webhook" USING btree ("credential_set_id");

0 commit comments

Comments
 (0)