Skip to content

Commit fb06e40

Browse files
committed
consolidated utils & testing mocks
1 parent 409672c commit fb06e40

File tree

69 files changed

+2561
-4016
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+2561
-4016
lines changed

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

Lines changed: 0 additions & 1565 deletions
This file was deleted.

apps/sim/app/api/auth/forget-password/route.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,60 @@
33
*
44
* @vitest-environment node
55
*/
6+
import {
7+
createMockRequest,
8+
mockConsoleLogger,
9+
mockCryptoUuid,
10+
mockDrizzleOrm,
11+
mockUuid,
12+
setupCommonApiMocks,
13+
} from '@sim/testing'
614
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockRequest, setupAuthApiMocks } from '@/app/api/__test-utils__/utils'
815

916
vi.mock('@/lib/core/utils/urls', () => ({
1017
getBaseUrl: vi.fn(() => 'https://app.example.com'),
1118
}))
1219

20+
/** Setup auth API mocks for testing authentication routes */
21+
function setupAuthApiMocks(
22+
options: {
23+
operations?: {
24+
forgetPassword?: { success?: boolean; error?: string }
25+
resetPassword?: { success?: boolean; error?: string }
26+
}
27+
} = {}
28+
) {
29+
setupCommonApiMocks()
30+
mockUuid()
31+
mockCryptoUuid()
32+
mockConsoleLogger()
33+
mockDrizzleOrm()
34+
35+
const { operations = {} } = options
36+
const defaultOperations = {
37+
forgetPassword: { success: true, error: 'Forget password error', ...operations.forgetPassword },
38+
resetPassword: { success: true, error: 'Reset password error', ...operations.resetPassword },
39+
}
40+
41+
const createAuthMethod = (config: { success?: boolean; error?: string }) => {
42+
return vi.fn().mockImplementation(() => {
43+
if (config.success) {
44+
return Promise.resolve()
45+
}
46+
return Promise.reject(new Error(config.error))
47+
})
48+
}
49+
50+
vi.doMock('@/lib/auth', () => ({
51+
auth: {
52+
api: {
53+
forgetPassword: createAuthMethod(defaultOperations.forgetPassword),
54+
resetPassword: createAuthMethod(defaultOperations.resetPassword),
55+
},
56+
},
57+
}))
58+
}
59+
1360
describe('Forget Password API Route', () => {
1461
beforeEach(() => {
1562
vi.resetModules()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* @vitest-environment node
55
*/
6+
import { createMockLogger, createMockRequest } from '@sim/testing'
67
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'
88

99
describe('OAuth Connections API Route', () => {
1010
const mockGetSession = vi.fn()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* @vitest-environment node
55
*/
66

7+
import { createMockLogger } from '@sim/testing'
78
import { NextRequest } from 'next/server'
89
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
9-
import { createMockLogger } from '@/app/api/__test-utils__/utils'
1010

1111
describe('OAuth Credentials API Route', () => {
1212
const mockGetSession = vi.fn()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* @vitest-environment node
55
*/
6+
import { createMockLogger, createMockRequest } from '@sim/testing'
67
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'
88

99
describe('OAuth Disconnect API Route', () => {
1010
const mockGetSession = vi.fn()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* @vitest-environment node
55
*/
6+
import { createMockLogger, createMockRequest } from '@sim/testing'
67
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'
88

99
describe('OAuth Token API Routes', () => {
1010
const mockGetUserId = vi.fn()

apps/sim/app/api/auth/reset-password/route.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,55 @@
33
*
44
* @vitest-environment node
55
*/
6+
import {
7+
createMockRequest,
8+
mockConsoleLogger,
9+
mockCryptoUuid,
10+
mockDrizzleOrm,
11+
mockUuid,
12+
setupCommonApiMocks,
13+
} from '@sim/testing'
614
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
7-
import { createMockRequest, setupAuthApiMocks } from '@/app/api/__test-utils__/utils'
15+
16+
/** Setup auth API mocks for testing authentication routes */
17+
function setupAuthApiMocks(
18+
options: {
19+
operations?: {
20+
forgetPassword?: { success?: boolean; error?: string }
21+
resetPassword?: { success?: boolean; error?: string }
22+
}
23+
} = {}
24+
) {
25+
setupCommonApiMocks()
26+
mockUuid()
27+
mockCryptoUuid()
28+
mockConsoleLogger()
29+
mockDrizzleOrm()
30+
31+
const { operations = {} } = options
32+
const defaultOperations = {
33+
forgetPassword: { success: true, error: 'Forget password error', ...operations.forgetPassword },
34+
resetPassword: { success: true, error: 'Reset password error', ...operations.resetPassword },
35+
}
36+
37+
const createAuthMethod = (config: { success?: boolean; error?: string }) => {
38+
return vi.fn().mockImplementation(() => {
39+
if (config.success) {
40+
return Promise.resolve()
41+
}
42+
return Promise.reject(new Error(config.error))
43+
})
44+
}
45+
46+
vi.doMock('@/lib/auth', () => ({
47+
auth: {
48+
api: {
49+
forgetPassword: createAuthMethod(defaultOperations.forgetPassword),
50+
resetPassword: createAuthMethod(defaultOperations.resetPassword),
51+
},
52+
},
53+
}))
54+
}
855

956
describe('Reset Password API Route', () => {
1057
beforeEach(() => {

apps/sim/app/api/chat/[identifier]/route.test.ts

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,34 @@
55
*/
66
import { loggerMock } from '@sim/testing'
77
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
8-
import { createMockRequest } from '@/app/api/__test-utils__/utils'
8+
9+
/**
10+
* Creates a mock NextRequest with cookies support for testing.
11+
*/
12+
function createMockNextRequest(
13+
method = 'GET',
14+
body?: unknown,
15+
headers: Record<string, string> = {},
16+
url = 'http://localhost:3000/api/test'
17+
): any {
18+
const headersObj = new Headers({
19+
'Content-Type': 'application/json',
20+
...headers,
21+
})
22+
23+
return {
24+
method,
25+
headers: headersObj,
26+
cookies: {
27+
get: vi.fn().mockReturnValue(undefined),
28+
},
29+
json:
30+
body !== undefined
31+
? vi.fn().mockResolvedValue(body)
32+
: vi.fn().mockRejectedValue(new Error('No body')),
33+
url,
34+
}
35+
}
936

1037
const createMockStream = () => {
1138
return new ReadableStream({
@@ -71,10 +98,15 @@ vi.mock('@/lib/core/utils/request', () => ({
7198
generateRequestId: vi.fn().mockReturnValue('test-request-id'),
7299
}))
73100

101+
vi.mock('@/lib/core/security/encryption', () => ({
102+
decryptSecret: vi.fn().mockResolvedValue({ decrypted: 'test-password' }),
103+
}))
104+
74105
describe('Chat Identifier API Route', () => {
75106
const mockAddCorsHeaders = vi.fn().mockImplementation((response) => response)
76107
const mockValidateChatAuth = vi.fn().mockResolvedValue({ authorized: true })
77108
const mockSetChatAuthCookie = vi.fn()
109+
const mockValidateAuthToken = vi.fn().mockReturnValue(false)
78110

79111
const mockChatResult = [
80112
{
@@ -114,11 +146,16 @@ describe('Chat Identifier API Route', () => {
114146
beforeEach(() => {
115147
vi.resetModules()
116148

117-
vi.doMock('@/app/api/chat/utils', () => ({
149+
vi.doMock('@/lib/core/security/deployment', () => ({
118150
addCorsHeaders: mockAddCorsHeaders,
151+
validateAuthToken: mockValidateAuthToken,
152+
setDeploymentAuthCookie: vi.fn(),
153+
isEmailAllowed: vi.fn().mockReturnValue(false),
154+
}))
155+
156+
vi.doMock('@/app/api/chat/utils', () => ({
119157
validateChatAuth: mockValidateChatAuth,
120158
setChatAuthCookie: mockSetChatAuthCookie,
121-
validateAuthToken: vi.fn().mockReturnValue(true),
122159
}))
123160

124161
// Mock logger - use loggerMock from @sim/testing
@@ -175,7 +212,7 @@ describe('Chat Identifier API Route', () => {
175212

176213
describe('GET endpoint', () => {
177214
it('should return chat info for a valid identifier', async () => {
178-
const req = createMockRequest('GET')
215+
const req = createMockNextRequest('GET')
179216
const params = Promise.resolve({ identifier: 'test-chat' })
180217

181218
const { GET } = await import('@/app/api/chat/[identifier]/route')
@@ -206,7 +243,7 @@ describe('Chat Identifier API Route', () => {
206243
}
207244
})
208245

209-
const req = createMockRequest('GET')
246+
const req = createMockNextRequest('GET')
210247
const params = Promise.resolve({ identifier: 'nonexistent' })
211248

212249
const { GET } = await import('@/app/api/chat/[identifier]/route')
@@ -240,7 +277,7 @@ describe('Chat Identifier API Route', () => {
240277
}
241278
})
242279

243-
const req = createMockRequest('GET')
280+
const req = createMockNextRequest('GET')
244281
const params = Promise.resolve({ identifier: 'inactive-chat' })
245282

246283
const { GET } = await import('@/app/api/chat/[identifier]/route')
@@ -261,7 +298,7 @@ describe('Chat Identifier API Route', () => {
261298
error: 'auth_required_password',
262299
}))
263300

264-
const req = createMockRequest('GET')
301+
const req = createMockNextRequest('GET')
265302
const params = Promise.resolve({ identifier: 'password-protected-chat' })
266303

267304
const { GET } = await import('@/app/api/chat/[identifier]/route')
@@ -282,7 +319,7 @@ describe('Chat Identifier API Route', () => {
282319

283320
describe('POST endpoint', () => {
284321
it('should handle authentication requests without input', async () => {
285-
const req = createMockRequest('POST', { password: 'test-password' })
322+
const req = createMockNextRequest('POST', { password: 'test-password' })
286323
const params = Promise.resolve({ identifier: 'password-protected-chat' })
287324

288325
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -298,7 +335,7 @@ describe('Chat Identifier API Route', () => {
298335
})
299336

300337
it('should return 400 for requests without input', async () => {
301-
const req = createMockRequest('POST', {})
338+
const req = createMockNextRequest('POST', {})
302339
const params = Promise.resolve({ identifier: 'test-chat' })
303340

304341
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -319,7 +356,7 @@ describe('Chat Identifier API Route', () => {
319356
error: 'Authentication required',
320357
}))
321358

322-
const req = createMockRequest('POST', { input: 'Hello' })
359+
const req = createMockNextRequest('POST', { input: 'Hello' })
323360
const params = Promise.resolve({ identifier: 'protected-chat' })
324361

325362
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -350,7 +387,7 @@ describe('Chat Identifier API Route', () => {
350387
},
351388
})
352389

353-
const req = createMockRequest('POST', { input: 'Hello' })
390+
const req = createMockNextRequest('POST', { input: 'Hello' })
354391
const params = Promise.resolve({ identifier: 'test-chat' })
355392

356393
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -369,7 +406,10 @@ describe('Chat Identifier API Route', () => {
369406
})
370407

371408
it('should return streaming response for valid chat messages', async () => {
372-
const req = createMockRequest('POST', { input: 'Hello world', conversationId: 'conv-123' })
409+
const req = createMockNextRequest('POST', {
410+
input: 'Hello world',
411+
conversationId: 'conv-123',
412+
})
373413
const params = Promise.resolve({ identifier: 'test-chat' })
374414

375415
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -401,7 +441,7 @@ describe('Chat Identifier API Route', () => {
401441
}, 10000)
402442

403443
it('should handle streaming response body correctly', async () => {
404-
const req = createMockRequest('POST', { input: 'Hello world' })
444+
const req = createMockNextRequest('POST', { input: 'Hello world' })
405445
const params = Promise.resolve({ identifier: 'test-chat' })
406446

407447
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -431,7 +471,7 @@ describe('Chat Identifier API Route', () => {
431471
throw new Error('Execution failed')
432472
})
433473

434-
const req = createMockRequest('POST', { input: 'Trigger error' })
474+
const req = createMockNextRequest('POST', { input: 'Trigger error' })
435475
const params = Promise.resolve({ identifier: 'test-chat' })
436476

437477
const { POST } = await import('@/app/api/chat/[identifier]/route')
@@ -470,7 +510,7 @@ describe('Chat Identifier API Route', () => {
470510
})
471511

472512
it('should pass conversationId to streaming execution when provided', async () => {
473-
const req = createMockRequest('POST', {
513+
const req = createMockNextRequest('POST', {
474514
input: 'Hello world',
475515
conversationId: 'test-conversation-123',
476516
})
@@ -492,7 +532,7 @@ describe('Chat Identifier API Route', () => {
492532
})
493533

494534
it('should handle missing conversationId gracefully', async () => {
495-
const req = createMockRequest('POST', { input: 'Hello world' })
535+
const req = createMockNextRequest('POST', { input: 'Hello world' })
496536
const params = Promise.resolve({ identifier: 'test-chat' })
497537

498538
const { POST } = await import('@/app/api/chat/[identifier]/route')

apps/sim/app/api/copilot/api-keys/route.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*
44
* @vitest-environment node
55
*/
6+
import { mockAuth, mockCryptoUuid, setupCommonApiMocks } from '@sim/testing'
67
import { NextRequest } from 'next/server'
78
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
8-
import { mockAuth, mockCryptoUuid, setupCommonApiMocks } from '@/app/api/__test-utils__/utils'
99

1010
describe('Copilot API Keys API Route', () => {
1111
const mockFetch = vi.fn()

apps/sim/app/api/copilot/chat/delete/route.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
*
44
* @vitest-environment node
55
*/
6+
import { createMockRequest, mockAuth, mockCryptoUuid, setupCommonApiMocks } from '@sim/testing'
67
import { NextRequest } from 'next/server'
78
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
8-
import {
9-
createMockRequest,
10-
mockAuth,
11-
mockCryptoUuid,
12-
setupCommonApiMocks,
13-
} from '@/app/api/__test-utils__/utils'
149

1510
describe('Copilot Chat Delete API Route', () => {
1611
const mockDelete = vi.fn()

0 commit comments

Comments
 (0)