Skip to content

Commit 99d9265

Browse files
committed
improvement(oauth): coalesce token refresh + cache terminal failures
1 parent 3f7698c commit 99d9265

11 files changed

Lines changed: 761 additions & 187 deletions

File tree

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
* @vitest-environment node
55
*/
66

7+
import { redisConfigMock, redisConfigMockFns } from '@sim/testing'
78
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
89

910
vi.mock('@/lib/oauth/oauth', () => ({
1011
refreshOAuthToken: vi.fn(),
1112
OAUTH_PROVIDERS: {},
1213
}))
1314

15+
vi.mock('@/lib/core/config/redis', () => redisConfigMock)
16+
1417
import { db } from '@sim/db'
18+
import { __resetCoalesceLocallyForTests } from '@/lib/concurrency/singleflight'
1519
import { refreshOAuthToken } from '@/lib/oauth'
1620
import {
1721
getCredential,
@@ -49,6 +53,10 @@ function mockUpdateChain() {
4953
describe('OAuth Utils', () => {
5054
beforeEach(() => {
5155
vi.clearAllMocks()
56+
__resetCoalesceLocallyForTests()
57+
redisConfigMockFns.mockGetRedisClient.mockReturnValue(null)
58+
redisConfigMockFns.mockAcquireLock.mockResolvedValue(true)
59+
redisConfigMockFns.mockReleaseLock.mockResolvedValue(true)
5260
})
5361

5462
afterEach(() => {
@@ -107,6 +115,7 @@ describe('OAuth Utils', () => {
107115
}
108116

109117
mockRefreshOAuthToken.mockResolvedValueOnce({
118+
ok: true,
110119
accessToken: 'new-token',
111120
expiresIn: 3600,
112121
refreshToken: 'new-refresh-token',
@@ -130,7 +139,11 @@ describe('OAuth Utils', () => {
130139
providerId: 'google',
131140
}
132141

133-
mockRefreshOAuthToken.mockResolvedValueOnce(null)
142+
mockRefreshOAuthToken.mockResolvedValueOnce({
143+
ok: false,
144+
errorCode: 'invalid_grant',
145+
message: 'Failed',
146+
})
134147

135148
await expect(
136149
refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
@@ -198,6 +211,7 @@ describe('OAuth Utils', () => {
198211
mockUpdateChain()
199212

200213
mockRefreshOAuthToken.mockResolvedValueOnce({
214+
ok: true,
201215
accessToken: 'new-token',
202216
expiresIn: 3600,
203217
refreshToken: 'new-refresh-token',
@@ -237,7 +251,11 @@ describe('OAuth Utils', () => {
237251
mockSelectChain([mockResolvedCredential])
238252
mockSelectChain([mockAccountRow])
239253

240-
mockRefreshOAuthToken.mockResolvedValueOnce(null)
254+
mockRefreshOAuthToken.mockResolvedValueOnce({
255+
ok: false,
256+
errorCode: 'invalid_grant',
257+
message: 'Failed',
258+
})
241259

242260
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')
243261

0 commit comments

Comments
 (0)