Skip to content
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

feat, test: enforce strict logOnly parameters #517

Merged
merged 1 commit into from
Jan 31, 2025
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
4 changes: 4 additions & 0 deletions lib/handlers/get-unimind/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const unimindQueryParamsSchema = Joi.object({
'string.routeInvalid': 'route structure is invalid after parsing'
}),
logOnly: Joi.boolean().optional()
.truthy('true')
.falsy('false')
.sensitive()
// All other values are rejected for a 400 error
})

export type UnimindQueryParams = Omit<QuoteMetadata, 'route'> & {
Expand Down
117 changes: 117 additions & 0 deletions test/unit/handlers/get-unimind/get-unimind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,121 @@ describe('Testing get unimind handler', () => {
const body = JSON.parse(response.body)
expect(body.detail).toContain('route must be a valid JSON string')
})

it('rejects case-sensitive variations of "true" for logOnly', async () => {
const quoteMetadata = {
quoteId: 'test-quote-id',
referencePrice: '4221.21',
priceImpact: 0.01,
pair: 'ETH-USDC',
route: STRINGIFIED_ROUTE,
}

const variations = ['TrUe', 'TRUE', 'True']

for (const valueToTest of variations) {
mockQuoteMetadataRepo.put.mockClear()
mockUnimindParametersRepo.getByPair.mockClear()

const response = await getUnimindHandler.handler(
{
queryStringParameters: { ...quoteMetadata, logOnly: valueToTest },
requestContext: {
requestId: 'test-request-id'
}
} as any,
EVENT_CONTEXT
)

expect(response.statusCode).toBe(400)
expect(mockQuoteMetadataRepo.put).not.toHaveBeenCalled()
expect(mockUnimindParametersRepo.getByPair).not.toHaveBeenCalled()
}
})

it('rejects case-sensitive variations of "false" for logOnly', async () => {
const quoteMetadata = {
quoteId: 'test-quote-id',
referencePrice: '4221.21',
priceImpact: 0.01,
pair: 'ETH-USDC',
route: STRINGIFIED_ROUTE,
}

const variations = ['FALSE', 'False', 'fAlSe']

for (const valueToTest of variations) {
mockQuoteMetadataRepo.put.mockClear()
mockUnimindParametersRepo.getByPair.mockClear()

const response = await getUnimindHandler.handler(
{
queryStringParameters: { ...quoteMetadata, logOnly: valueToTest },
requestContext: {
requestId: 'test-request-id'
}
} as any,
EVENT_CONTEXT
)

expect(response.statusCode).toBe(400)
expect(mockQuoteMetadataRepo.put).not.toHaveBeenCalled()
expect(mockUnimindParametersRepo.getByPair).not.toHaveBeenCalled()
}
})

it('logOnly works when we pass in true', async () => {
const quoteMetadata = {
quoteId: 'test-quote-id',
referencePrice: '4221.21',
priceImpact: 0.01,
pair: 'ETH-USDC',
route: STRINGIFIED_ROUTE,
}
//mock the put as successful
mockQuoteMetadataRepo.put.mockResolvedValue()

const response = await getUnimindHandler.handler(
{
queryStringParameters: { ...quoteMetadata, logOnly: 'true' },
requestContext: {
requestId: 'test-request-id'
}
} as any,
EVENT_CONTEXT
)

expect(response.statusCode).toBe(200)
const body = JSON.parse(response.body)
expect(body).toEqual({
pi: 0,
tau: 0
})
expect(mockQuoteMetadataRepo.put).toHaveBeenCalledTimes(1)
expect(mockUnimindParametersRepo.getByPair).toHaveBeenCalledTimes(0)
})

it('logOnly does not run when we pass in false', async () => {
const quoteMetadata = {
quoteId: 'test-quote-id',
referencePrice: '4221.21',
priceImpact: 0.01,
pair: 'ETH-USDC',
route: STRINGIFIED_ROUTE,
}

const response = await getUnimindHandler.handler(
{
queryStringParameters: { ...quoteMetadata, logOnly: 'false' },
requestContext: {
requestId: 'test-request-id'
}
} as any,
EVENT_CONTEXT
)

expect(response.statusCode).toBe(200)
expect(mockQuoteMetadataRepo.put).toHaveBeenCalledTimes(1)
expect(mockUnimindParametersRepo.getByPair).toHaveBeenCalledTimes(1)
})
})
Loading