Skip to content

Commit d09fd6c

Browse files
authored
fix(import): fixed trigger save on export/import flow (#2239)
* fix(import): fixed trigger save on export/import flow * optimized test runners * ack PR comments
1 parent 434d129 commit d09fd6c

File tree

71 files changed

+870
-1003
lines changed

Some content is hidden

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

71 files changed

+870
-1003
lines changed

apps/sim/app/api/auth/oauth/utils.test.ts

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,57 @@
55
*/
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
77

8-
describe('OAuth Utils', () => {
9-
const mockSession = { user: { id: 'test-user-id' } }
10-
const mockDb = {
8+
const mockSession = { user: { id: 'test-user-id' } }
9+
const mockGetSession = vi.fn()
10+
11+
vi.mock('@/lib/auth', () => ({
12+
getSession: () => mockGetSession(),
13+
}))
14+
15+
vi.mock('@sim/db', () => ({
16+
db: {
1117
select: vi.fn().mockReturnThis(),
1218
from: vi.fn().mockReturnThis(),
1319
where: vi.fn().mockReturnThis(),
1420
limit: vi.fn().mockReturnValue([]),
1521
update: vi.fn().mockReturnThis(),
1622
set: vi.fn().mockReturnThis(),
1723
orderBy: vi.fn().mockReturnThis(),
18-
}
19-
const mockRefreshOAuthToken = vi.fn()
20-
const mockLogger = {
24+
},
25+
}))
26+
27+
vi.mock('@/lib/oauth/oauth', () => ({
28+
refreshOAuthToken: vi.fn(),
29+
}))
30+
31+
vi.mock('@/lib/logs/console/logger', () => ({
32+
createLogger: vi.fn().mockReturnValue({
2133
info: vi.fn(),
2234
warn: vi.fn(),
2335
error: vi.fn(),
2436
debug: vi.fn(),
25-
}
37+
}),
38+
}))
39+
40+
import { db } from '@sim/db'
41+
import { createLogger } from '@/lib/logs/console/logger'
42+
import { refreshOAuthToken } from '@/lib/oauth/oauth'
43+
import {
44+
getCredential,
45+
getUserId,
46+
refreshAccessTokenIfNeeded,
47+
refreshTokenIfNeeded,
48+
} from '@/app/api/auth/oauth/utils'
49+
50+
const mockDb = db as any
51+
const mockRefreshOAuthToken = refreshOAuthToken as any
52+
const mockLogger = (createLogger as any)()
2653

54+
describe('OAuth Utils', () => {
2755
beforeEach(() => {
28-
vi.resetModules()
29-
30-
vi.doMock('@/lib/auth', () => ({
31-
getSession: vi.fn().mockResolvedValue(mockSession),
32-
}))
33-
34-
vi.doMock('@sim/db', () => ({
35-
db: mockDb,
36-
}))
37-
38-
vi.doMock('@/lib/oauth/oauth', () => ({
39-
refreshOAuthToken: mockRefreshOAuthToken,
40-
}))
41-
42-
vi.doMock('@/lib/logs/console/logger', () => ({
43-
createLogger: vi.fn().mockReturnValue(mockLogger),
44-
}))
56+
vi.clearAllMocks()
57+
mockGetSession.mockResolvedValue(mockSession)
58+
mockDb.limit.mockReturnValue([])
4559
})
4660

4761
afterEach(() => {
@@ -50,8 +64,6 @@ describe('OAuth Utils', () => {
5064

5165
describe('getUserId', () => {
5266
it('should get user ID from session when no workflowId is provided', async () => {
53-
const { getUserId } = await import('@/app/api/auth/oauth/utils')
54-
5567
const userId = await getUserId('request-id')
5668

5769
expect(userId).toBe('test-user-id')
@@ -60,8 +72,6 @@ describe('OAuth Utils', () => {
6072
it('should get user ID from workflow when workflowId is provided', async () => {
6173
mockDb.limit.mockReturnValueOnce([{ userId: 'workflow-owner-id' }])
6274

63-
const { getUserId } = await import('@/app/api/auth/oauth/utils')
64-
6575
const userId = await getUserId('request-id', 'workflow-id')
6676

6777
expect(mockDb.select).toHaveBeenCalled()
@@ -72,11 +82,7 @@ describe('OAuth Utils', () => {
7282
})
7383

7484
it('should return undefined if no session is found', async () => {
75-
vi.doMock('@/lib/auth', () => ({
76-
getSession: vi.fn().mockResolvedValue(null),
77-
}))
78-
79-
const { getUserId } = await import('@/app/api/auth/oauth/utils')
85+
mockGetSession.mockResolvedValueOnce(null)
8086

8187
const userId = await getUserId('request-id')
8288

@@ -87,8 +93,6 @@ describe('OAuth Utils', () => {
8793
it('should return undefined if workflow is not found', async () => {
8894
mockDb.limit.mockReturnValueOnce([])
8995

90-
const { getUserId } = await import('@/app/api/auth/oauth/utils')
91-
9296
const userId = await getUserId('request-id', 'nonexistent-workflow-id')
9397

9498
expect(userId).toBeUndefined()
@@ -101,8 +105,6 @@ describe('OAuth Utils', () => {
101105
const mockCredential = { id: 'credential-id', userId: 'test-user-id' }
102106
mockDb.limit.mockReturnValueOnce([mockCredential])
103107

104-
const { getCredential } = await import('@/app/api/auth/oauth/utils')
105-
106108
const credential = await getCredential('request-id', 'credential-id', 'test-user-id')
107109

108110
expect(mockDb.select).toHaveBeenCalled()
@@ -116,8 +118,6 @@ describe('OAuth Utils', () => {
116118
it('should return undefined when credential is not found', async () => {
117119
mockDb.limit.mockReturnValueOnce([])
118120

119-
const { getCredential } = await import('@/app/api/auth/oauth/utils')
120-
121121
const credential = await getCredential('request-id', 'nonexistent-id', 'test-user-id')
122122

123123
expect(credential).toBeUndefined()
@@ -135,8 +135,6 @@ describe('OAuth Utils', () => {
135135
providerId: 'google',
136136
}
137137

138-
const { refreshTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
139-
140138
const result = await refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
141139

142140
expect(mockRefreshOAuthToken).not.toHaveBeenCalled()
@@ -159,8 +157,6 @@ describe('OAuth Utils', () => {
159157
refreshToken: 'new-refresh-token',
160158
})
161159

162-
const { refreshTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
163-
164160
const result = await refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
165161

166162
expect(mockRefreshOAuthToken).toHaveBeenCalledWith('google', 'refresh-token')
@@ -183,8 +179,6 @@ describe('OAuth Utils', () => {
183179

184180
mockRefreshOAuthToken.mockResolvedValueOnce(null)
185181

186-
const { refreshTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
187-
188182
await expect(
189183
refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
190184
).rejects.toThrow('Failed to refresh token')
@@ -201,8 +195,6 @@ describe('OAuth Utils', () => {
201195
providerId: 'google',
202196
}
203197

204-
const { refreshTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
205-
206198
const result = await refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
207199

208200
expect(mockRefreshOAuthToken).not.toHaveBeenCalled()
@@ -222,8 +214,6 @@ describe('OAuth Utils', () => {
222214
}
223215
mockDb.limit.mockReturnValueOnce([mockCredential])
224216

225-
const { refreshAccessTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
226-
227217
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')
228218

229219
expect(mockRefreshOAuthToken).not.toHaveBeenCalled()
@@ -247,8 +237,6 @@ describe('OAuth Utils', () => {
247237
refreshToken: 'new-refresh-token',
248238
})
249239

250-
const { refreshAccessTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
251-
252240
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')
253241

254242
expect(mockRefreshOAuthToken).toHaveBeenCalledWith('google', 'refresh-token')
@@ -260,8 +248,6 @@ describe('OAuth Utils', () => {
260248
it('should return null if credential not found', async () => {
261249
mockDb.limit.mockReturnValueOnce([])
262250

263-
const { refreshAccessTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
264-
265251
const token = await refreshAccessTokenIfNeeded('nonexistent-id', 'test-user-id', 'request-id')
266252

267253
expect(token).toBeNull()
@@ -281,8 +267,6 @@ describe('OAuth Utils', () => {
281267

282268
mockRefreshOAuthToken.mockResolvedValueOnce(null)
283269

284-
const { refreshAccessTokenIfNeeded } = await import('@/app/api/auth/oauth/utils')
285-
286270
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')
287271

288272
expect(token).toBeNull()

0 commit comments

Comments
 (0)