Skip to content

Commit

Permalink
test(openapi): add quote validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BlairCurrey committed Feb 28, 2024
1 parent e48673c commit 78626fb
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions packages/openapi/src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,23 @@ describe('OpenAPI Validator', (): void => {
)

describe('Quote', (): void => {
let validateQuotePostMiddleware: AppMiddleware

beforeAll((): void => {
validateQuotePostMiddleware = createValidatorMiddleware(openApi, {
path: '/quotes',
method: HttpMethod.POST
})
})

test.each`
body | message | description
${{ receiver: 'ht999tp://something.com/incoming-payments' }} | ${'body.receiver must match pattern "^(https|http):..(.+).incoming-payments.(.+)$"'} | ${'invalid receiver, unknown protocol'}
${{ receiver: 'http://something.com/incoming-payments' }} | ${'body.receiver must match pattern "^(https|http):..(.+).incoming-payments.(.+)$"'} | ${'invalid receiver, missing incoming payment id'}
${{ receiver: 'http://something.com/connections/c3a0d182-b221-4612-a500-07ad106b5f5d' }} | ${'body.receiver must match pattern "^(https|http):..(.+).incoming-payments.(.+)$"'} | ${'invalid receiver, wrong path'}
body | description
${{ receiver: 'ht999tp://something.com/incoming-payments' }} | ${'invalid receiver, unknown protocol'}
${{ receiver: 'http://something.com/incoming-payments' }} | ${'invalid receiver, missing incoming payment id'}
${{ receiver: 'http://something.com/connections/c3a0d182-b221-4612-a500-07ad106b5f5d' }} | ${'invalid receiver, wrong path'}
`(
'returns 400 on invalid quote body ($description)',
async ({ body, message }): Promise<void> => {
const validateQuotePostMiddleware = createValidatorMiddleware(
openApi,
{
path: '/quotes',
method: HttpMethod.POST
}
)

async ({ body }): Promise<void> => {
const ctx = createContext(
{
headers: {
Expand All @@ -293,11 +294,65 @@ describe('OpenAPI Validator', (): void => {
validateQuotePostMiddleware(ctx, next)
).rejects.toMatchObject({
status: 400,
message
message:
'body.receiver must match pattern "^(https|http):..(.+).incoming-payments.(.+)$"'
})
expect(next).not.toHaveBeenCalled()
}
)
test.each`
receiver | description
${'http://something.com/incoming-payments/c3a0d182-b221-4612-a500-07ad106b5f5d'} | ${'accepts http and uuid'}
${'https://something.com/incoming-payments/123'} | ${'accepts http and non uuid id format'}
`(
'calls next on valid request: ($description)',
async ({ receiver }): Promise<void> => {
const ctx = createContext(
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
url: '/quotes'
},
{}
)
addTestSignatureHeaders(ctx)
ctx.request.body = {
receiver,
walletAddress: WALLET_ADDRESS,
method: 'ilp'
}
const next = jest.fn().mockImplementation(() => {
expect(ctx.request.body.receiver).toEqual(receiver)
ctx.status = 201
ctx.response.body = {
id: 'https://something-else/quotes/3b461206-daae-4d97-88b0-abffbcaa6f96',
walletAddress: 'https://something-else/accounts/someone',
receiveAmount: {
value: '100',
assetCode: 'USD',
assetScale: 2
},
debitAmount: {
value: '205',
assetCode: 'USD',
assetScale: 2
},
receiver,
expiresAt: '2024-02-28T16:26:32.444Z',
createdAt: '2024-02-28T16:21:32.444Z',
method: 'ilp'
}
})

await expect(
validateQuotePostMiddleware(ctx, next)
).resolves.toBeUndefined()

expect(next).toHaveBeenCalled()
}
)
})
})
})
Expand Down

0 comments on commit 78626fb

Please sign in to comment.