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
17 changes: 10 additions & 7 deletions apps/sim/app/api/__test-utils__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createMockLogger as createSimTestingMockLogger } from '@sim/testing'
import { NextRequest } from 'next/server'
import { vi } from 'vitest'

export { createMockLogger } from '@sim/testing'

export interface MockUser {
id: string
email: string
Expand Down Expand Up @@ -214,12 +217,11 @@ export const mockDb = {
})),
}

export const mockLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}
/**
* Mock logger using @sim/testing createMockLogger.
* This provides a consistent mock logger across all API tests.
*/
export const mockLogger = createSimTestingMockLogger()

export const mockUser = {
id: 'user-123',
Expand Down Expand Up @@ -729,7 +731,8 @@ export function mockKnowledgeSchemas() {
}

/**
* Mock console logger
* Mock console logger using the shared mockLogger instance.
* This ensures tests can assert on the same mockLogger instance exported from this module.
*/
export function mockConsoleLogger() {
vi.doMock('@/lib/logs/console/logger', () => ({
Expand Down
9 changes: 2 additions & 7 deletions apps/sim/app/api/auth/oauth/connections/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @vitest-environment node
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest } from '@/app/api/__test-utils__/utils'
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'

describe('OAuth Connections API Route', () => {
const mockGetSession = vi.fn()
Expand All @@ -14,12 +14,7 @@ describe('OAuth Connections API Route', () => {
where: vi.fn().mockReturnThis(),
limit: vi.fn(),
}
const mockLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}
const mockLogger = createMockLogger()
const mockParseProvider = vi.fn()
const mockEvaluateScopeCoverage = vi.fn()

Expand Down
8 changes: 2 additions & 6 deletions apps/sim/app/api/auth/oauth/credentials/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { NextRequest } from 'next/server'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockLogger } from '@/app/api/__test-utils__/utils'

describe('OAuth Credentials API Route', () => {
const mockGetSession = vi.fn()
Expand All @@ -17,12 +18,7 @@ describe('OAuth Credentials API Route', () => {
where: vi.fn().mockReturnThis(),
limit: vi.fn(),
}
const mockLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}
const mockLogger = createMockLogger()

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

Expand Down
9 changes: 2 additions & 7 deletions apps/sim/app/api/auth/oauth/disconnect/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
* @vitest-environment node
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest } from '@/app/api/__test-utils__/utils'
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'

describe('OAuth Disconnect API Route', () => {
const mockGetSession = vi.fn()
const mockDb = {
delete: vi.fn().mockReturnThis(),
where: vi.fn(),
}
const mockLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}
const mockLogger = createMockLogger()

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

Expand Down
9 changes: 2 additions & 7 deletions apps/sim/app/api/auth/oauth/token/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @vitest-environment node
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createMockRequest } from '@/app/api/__test-utils__/utils'
import { createMockLogger, createMockRequest } from '@/app/api/__test-utils__/utils'

describe('OAuth Token API Routes', () => {
const mockGetUserId = vi.fn()
Expand All @@ -13,12 +13,7 @@ describe('OAuth Token API Routes', () => {
const mockAuthorizeCredentialUse = vi.fn()
const mockCheckHybridAuth = vi.fn()

const mockLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}
const mockLogger = createMockLogger()

const mockUUID = 'mock-uuid-12345678-90ab-cdef-1234-567890abcdef'
const mockRequestId = mockUUID.slice(0, 8)
Expand Down
71 changes: 33 additions & 38 deletions apps/sim/app/api/auth/oauth/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*
* @vitest-environment node
*/

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

const mockSession = { user: { id: 'test-user-id' } }
const mockSession = createSession({ userId: 'test-user-id' })
const mockGetSession = vi.fn()

vi.mock('@/lib/auth', () => ({
Expand All @@ -29,14 +31,7 @@ vi.mock('@/lib/oauth/oauth', () => ({
OAUTH_PROVIDERS: {},
}))

vi.mock('@/lib/logs/console/logger', () => ({
createLogger: vi.fn().mockReturnValue({
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}),
}))
vi.mock('@/lib/logs/console/logger', () => loggerMock)

import { db } from '@sim/db'
import { refreshOAuthToken } from '@/lib/oauth'
Expand All @@ -47,14 +42,14 @@ import {
refreshTokenIfNeeded,
} from '@/app/api/auth/oauth/utils'

const mockDb = db as any
const mockDbTyped = db as any
const mockRefreshOAuthToken = refreshOAuthToken as any

describe('OAuth Utils', () => {
beforeEach(() => {
vi.clearAllMocks()
mockGetSession.mockResolvedValue(mockSession)
mockDb.limit.mockReturnValue([])
mockDbTyped.limit.mockReturnValue([])
})

afterEach(() => {
Expand All @@ -69,14 +64,14 @@ describe('OAuth Utils', () => {
})

it('should get user ID from workflow when workflowId is provided', async () => {
mockDb.limit.mockReturnValueOnce([{ userId: 'workflow-owner-id' }])
mockDbTyped.limit.mockReturnValueOnce([{ userId: 'workflow-owner-id' }])

const userId = await getUserId('request-id', 'workflow-id')

expect(mockDb.select).toHaveBeenCalled()
expect(mockDb.from).toHaveBeenCalled()
expect(mockDb.where).toHaveBeenCalled()
expect(mockDb.limit).toHaveBeenCalledWith(1)
expect(mockDbTyped.select).toHaveBeenCalled()
expect(mockDbTyped.from).toHaveBeenCalled()
expect(mockDbTyped.where).toHaveBeenCalled()
expect(mockDbTyped.limit).toHaveBeenCalledWith(1)
expect(userId).toBe('workflow-owner-id')
})

Expand All @@ -89,7 +84,7 @@ describe('OAuth Utils', () => {
})

it('should return undefined if workflow is not found', async () => {
mockDb.limit.mockReturnValueOnce([])
mockDbTyped.limit.mockReturnValueOnce([])

const userId = await getUserId('request-id', 'nonexistent-workflow-id')

Expand All @@ -100,20 +95,20 @@ describe('OAuth Utils', () => {
describe('getCredential', () => {
it('should return credential when found', async () => {
const mockCredential = { id: 'credential-id', userId: 'test-user-id' }
mockDb.limit.mockReturnValueOnce([mockCredential])
mockDbTyped.limit.mockReturnValueOnce([mockCredential])

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

expect(mockDb.select).toHaveBeenCalled()
expect(mockDb.from).toHaveBeenCalled()
expect(mockDb.where).toHaveBeenCalled()
expect(mockDb.limit).toHaveBeenCalledWith(1)
expect(mockDbTyped.select).toHaveBeenCalled()
expect(mockDbTyped.from).toHaveBeenCalled()
expect(mockDbTyped.where).toHaveBeenCalled()
expect(mockDbTyped.limit).toHaveBeenCalledWith(1)

expect(credential).toEqual(mockCredential)
})

it('should return undefined when credential is not found', async () => {
mockDb.limit.mockReturnValueOnce([])
mockDbTyped.limit.mockReturnValueOnce([])

const credential = await getCredential('request-id', 'nonexistent-id', 'test-user-id')

Expand All @@ -127,7 +122,7 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'valid-token',
refreshToken: 'refresh-token',
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000), // 1 hour in the future
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000),
providerId: 'google',
}

Expand All @@ -142,7 +137,7 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'expired-token',
refreshToken: 'refresh-token',
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
providerId: 'google',
}

Expand All @@ -155,8 +150,8 @@ describe('OAuth Utils', () => {
const result = await refreshTokenIfNeeded('request-id', mockCredential, 'credential-id')

expect(mockRefreshOAuthToken).toHaveBeenCalledWith('google', 'refresh-token')
expect(mockDb.update).toHaveBeenCalled()
expect(mockDb.set).toHaveBeenCalled()
expect(mockDbTyped.update).toHaveBeenCalled()
expect(mockDbTyped.set).toHaveBeenCalled()
expect(result).toEqual({ accessToken: 'new-token', refreshed: true })
})

Expand All @@ -165,7 +160,7 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'expired-token',
refreshToken: 'refresh-token',
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
providerId: 'google',
}

Expand All @@ -181,7 +176,7 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'token',
refreshToken: null,
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
providerId: 'google',
}

Expand All @@ -198,11 +193,11 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'valid-token',
refreshToken: 'refresh-token',
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000), // 1 hour in the future
accessTokenExpiresAt: new Date(Date.now() + 3600 * 1000),
providerId: 'google',
userId: 'test-user-id',
}
mockDb.limit.mockReturnValueOnce([mockCredential])
mockDbTyped.limit.mockReturnValueOnce([mockCredential])

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

Expand All @@ -215,11 +210,11 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'expired-token',
refreshToken: 'refresh-token',
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
providerId: 'google',
userId: 'test-user-id',
}
mockDb.limit.mockReturnValueOnce([mockCredential])
mockDbTyped.limit.mockReturnValueOnce([mockCredential])

mockRefreshOAuthToken.mockResolvedValueOnce({
accessToken: 'new-token',
Expand All @@ -230,13 +225,13 @@ describe('OAuth Utils', () => {
const token = await refreshAccessTokenIfNeeded('credential-id', 'test-user-id', 'request-id')

expect(mockRefreshOAuthToken).toHaveBeenCalledWith('google', 'refresh-token')
expect(mockDb.update).toHaveBeenCalled()
expect(mockDb.set).toHaveBeenCalled()
expect(mockDbTyped.update).toHaveBeenCalled()
expect(mockDbTyped.set).toHaveBeenCalled()
expect(token).toBe('new-token')
})

it('should return null if credential not found', async () => {
mockDb.limit.mockReturnValueOnce([])
mockDbTyped.limit.mockReturnValueOnce([])

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

Expand All @@ -248,11 +243,11 @@ describe('OAuth Utils', () => {
id: 'credential-id',
accessToken: 'expired-token',
refreshToken: 'refresh-token',
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000), // 1 hour in the past
accessTokenExpiresAt: new Date(Date.now() - 3600 * 1000),
providerId: 'google',
userId: 'test-user-id',
}
mockDb.limit.mockReturnValueOnce([mockCredential])
mockDbTyped.limit.mockReturnValueOnce([mockCredential])

mockRefreshOAuthToken.mockResolvedValueOnce(null)

Expand Down
Loading
Loading