Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions apps/sim/app/api/auth/oauth/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
* @vitest-environment node
*/

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

vi.mock('@/lib/oauth/oauth', () => ({
refreshOAuthToken: vi.fn(),
OAUTH_PROVIDERS: {},
}))

vi.mock('@/lib/core/config/redis', () => redisConfigMock)

import { db } from '@sim/db'
import { __resetCoalesceLocallyForTests } from '@/lib/concurrency/singleflight'
import { refreshOAuthToken } from '@/lib/oauth'
import {
getCredential,
Expand Down Expand Up @@ -49,6 +53,10 @@ function mockUpdateChain() {
describe('OAuth Utils', () => {
beforeEach(() => {
vi.clearAllMocks()
__resetCoalesceLocallyForTests()
redisConfigMockFns.mockGetRedisClient.mockReturnValue(null)
redisConfigMockFns.mockAcquireLock.mockResolvedValue(true)
redisConfigMockFns.mockReleaseLock.mockResolvedValue(true)
})

afterEach(() => {
Expand Down Expand Up @@ -107,6 +115,7 @@ describe('OAuth Utils', () => {
}

mockRefreshOAuthToken.mockResolvedValueOnce({
ok: true,
accessToken: 'new-token',
expiresIn: 3600,
refreshToken: 'new-refresh-token',
Expand All @@ -130,7 +139,11 @@ describe('OAuth Utils', () => {
providerId: 'google',
}

mockRefreshOAuthToken.mockResolvedValueOnce(null)
mockRefreshOAuthToken.mockResolvedValueOnce({
ok: false,
errorCode: 'invalid_grant',
message: 'Failed',
})

await expect(
refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')
Expand Down Expand Up @@ -198,6 +211,7 @@ describe('OAuth Utils', () => {
mockUpdateChain()

mockRefreshOAuthToken.mockResolvedValueOnce({
ok: true,
accessToken: 'new-token',
expiresIn: 3600,
refreshToken: 'new-refresh-token',
Expand Down Expand Up @@ -237,7 +251,11 @@ describe('OAuth Utils', () => {
mockSelectChain([mockResolvedCredential])
mockSelectChain([mockAccountRow])

mockRefreshOAuthToken.mockResolvedValueOnce(null)
mockRefreshOAuthToken.mockResolvedValueOnce({
ok: false,
errorCode: 'invalid_grant',
message: 'Failed',
})

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

Expand Down
Loading
Loading