Skip to content

Commit

Permalink
chore(openapi): add test for middleware options
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurapov committed Mar 25, 2024
1 parent a54813e commit 4cc8422
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion packages/openapi/src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import Koa from 'koa'
import * as httpMocks from 'node-mocks-http'
import { v4 as uuid } from 'uuid'
import assert from 'assert'
import { createOpenAPI, OpenAPI, HttpMethod } from './'
import {
createOpenAPI,
OpenAPI,
HttpMethod,
RequestValidator,
ResponseValidator
} from './'
import {
createValidatorMiddleware,
OpenAPIValidatorMiddlewareError
Expand Down Expand Up @@ -246,6 +252,62 @@ describe('OpenAPI Validator', (): void => {
expect(next).toHaveBeenCalled()
})

test.each`
validateRequest | validateResponse
${true} | ${true}
${true} | ${false}
${false} | ${true}
${false} | ${false}
`(
'calls validators correctly with validateRequest=$validateRequest and validateResponse=$validateResponse',
async ({ validateRequest, validateResponse }): Promise<void> => {
const ctx = createContext(
{
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
},
{}
)
addTestSignatureHeaders(ctx)
ctx.request['body'] = { walletAddress: WALLET_ADDRESS }

const mockRequestValidator = jest.fn()

jest
.spyOn(openApi, 'createRequestValidator')
.mockReturnValueOnce(
mockRequestValidator as unknown as RequestValidator<any>

Check warning on line 281 in packages/openapi/src/middleware.test.ts

View workflow job for this annotation

GitHub Actions / checkout

Unexpected any. Specify a different type
)

const mockResponseValidator = jest.fn()

jest
.spyOn(openApi, 'createResponseValidator')
.mockReturnValueOnce(
mockResponseValidator as unknown as ResponseValidator<any>

Check warning on line 289 in packages/openapi/src/middleware.test.ts

View workflow job for this annotation

GitHub Actions / checkout

Unexpected any. Specify a different type
)

await createValidatorMiddleware(
openApi,
{
path: PATH,
method: HttpMethod.POST
},
{ validateRequest, validateResponse }
)(ctx, next)

expect(mockRequestValidator).toHaveBeenCalledTimes(
validateRequest ? 1 : 0
)
expect(mockResponseValidator).toHaveBeenCalledTimes(
validateResponse ? 1 : 0
)
expect(next).toHaveBeenCalled()
}
)

const body = {
id: `https://${accountId}/incoming-payments/${uuid()}`,
walletAddress: 'https://openpayments.guide/alice',
Expand Down

0 comments on commit 4cc8422

Please sign in to comment.