Skip to content

Added unit test for validateKey util #4800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions packages/server/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getRunningExpressApp } from '../src/utils/getRunningExpressApp'
import { organizationUserRouteTest } from './routes/v1/organization-user.route.test'
import { userRouteTest } from './routes/v1/user.route.test'
import { apiKeyTest } from './utils/api-key.util.test'
import { validateKeyTest } from './utils/validateKey.util.test'

// ⏱️ Extend test timeout to 6 minutes for long setups (increase as tests grow)
jest.setTimeout(360000)
Expand All @@ -25,4 +26,5 @@ describe('Routes Test', () => {

describe('Utils Test', () => {
apiKeyTest()
validateKeyTest()
})
110 changes: 110 additions & 0 deletions packages/server/test/utils/validateKey.util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { validateChatflowAPIKey, validateAPIKey, getAPIKeyWorkspaceID } from '../../src/utils/validateKey'
import apikeyService from '../../src/services/apikey'
import { compareKeys } from '../../src/utils/apiKey'
import { Request } from 'express'

jest.mock('../../src/services/apikey')
jest.mock('../../src/utils/apiKey')

const mockedCompareKeys = compareKeys as jest.Mock
const mockedApikeyService = apikeyService as jest.Mocked<typeof apikeyService>

const createMockRequest = (headers: Record<string, string> = {}): Request => {
return {
headers,
cookies: {},
query: {},
params: {},
body: {},
get: jest.fn()
} as unknown as Request
}

export function validateKeyTest() {
describe('Validate Key Utils', () => {
describe('validateChatflowAPIKey', () => {
it('should return true if chatflow has no apikeyid', async () => {
const req = createMockRequest()
const result = await validateChatflowAPIKey(req, { apikeyid: null } as any)
expect(result).toBe(true)
})

it('should return false if apikeyid exists but no Authorization header', async () => {
const req = createMockRequest()
const result = await validateChatflowAPIKey(req, { apikeyid: 'abc' } as any)
expect(result).toBe(false)
})

it('should return false if no matching key is found', async () => {
mockedApikeyService.getAllApiKeys.mockResolvedValueOnce([])
const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await validateChatflowAPIKey(req, { apikeyid: 'abc' } as any)
expect(result).toBe(false)
})

it('should return false if key does not match', async () => {
mockedApikeyService.getAllApiKeys.mockResolvedValueOnce([{ id: 'abc', apiSecret: 'secret' }])
mockedCompareKeys.mockReturnValueOnce(false)

const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await validateChatflowAPIKey(req, { apikeyid: 'abc' } as any)
expect(result).toBe(false)
})

it('should return true if key matches', async () => {
mockedApikeyService.getAllApiKeys.mockResolvedValueOnce([{ id: 'abc', apiSecret: 'secret' }])
mockedCompareKeys.mockReturnValueOnce(true)

const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await validateChatflowAPIKey(req, { apikeyid: 'abc' } as any)
expect(result).toBe(true)
})
})

describe('validateAPIKey', () => {
it('should return false if no Authorization header', async () => {
const req = createMockRequest()
const result = await validateAPIKey(req)
expect(result).toBe(false)
})

it('should return false if no matching apiKey', async () => {
mockedApikeyService.getAllApiKeys.mockResolvedValueOnce([])
const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await validateAPIKey(req)
expect(result).toBe(false)
})

it('should return false if apiKey does not match', async () => {
mockedApikeyService.getAllApiKeys.mockResolvedValueOnce([{ apiKey: 'someKey', apiSecret: 'secret' }])
mockedCompareKeys.mockReturnValueOnce(false)
const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await validateAPIKey(req)
expect(result).toBe(false)
})

it('should return true if apiKey matches', async () => {
mockedApikeyService.getAllApiKeys.mockResolvedValueOnce([{ apiKey: 'someKey', apiSecret: 'secret' }])
mockedCompareKeys.mockReturnValueOnce(true)
const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await validateAPIKey(req)
expect(result).toBe(true)
})
})

describe('getAPIKeyWorkspaceID', () => {
it('should return undefined if no Authorization header', async () => {
const req = createMockRequest()
const result = await getAPIKeyWorkspaceID(req)
expect(result).toBeUndefined()
})

it('should return undefined if key is not found', async () => {
mockedApikeyService.getApiKey.mockResolvedValueOnce(undefined)
const req = createMockRequest({ authorization: 'Bearer someKey' })
const result = await getAPIKeyWorkspaceID(req)
expect(result).toBeUndefined()
})
})
})
}