Skip to content

Commit

Permalink
test: fix openapi test & add missing swagger test
Browse files Browse the repository at this point in the history
  • Loading branch information
beryxz committed Aug 23, 2024
1 parent b1982bd commit 0571b62
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test/spec/openapi/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ test('security headers ignored when declared in multiple required security objec
}
}
},
security: [{ apiKey: [] }, { securityKey: [] }]
security: [{ apiKey: [], securityKey: [] }]
}
})

Expand Down
121 changes: 121 additions & 0 deletions test/spec/swagger/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,127 @@ test('security querystrings ignored when declared in multiple alternative securi
t.ok(api.paths['/address3/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})

test('security querystrings ignored when declared in multiple required security objects', async (t) => {
t.plan(10)
const fastify = Fastify()

await fastify.register(fastifySwagger, {
swagger: {
securityDefinitions: {
apiKey: {
type: 'apiKey',
name: 'apiKey',
in: 'query'
},
securityKey: {
type: 'apiKey',
name: 'securityKey',
in: 'query'
}
},
security: [
{ apiKey: [], securityKey: [] }
]
}
})

fastify.get('/address1/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
},
querystring: {
type: 'object',
properties: {
apiKey: {
type: 'string',
description: 'api token'
},
securityKey: {
type: 'string',
description: 'security api token'
},
somethingElse: {
type: 'string',
description: 'common field'
}
}
}
}
}, () => {})

fastify.get('/address2/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
},
querystring: {
type: 'object',
properties: {
authKey: {
type: 'string',
description: 'auth token'
},
securityKey: {
type: 'string',
description: 'security api token'
},
somethingElse: {
type: 'string',
description: 'common field'
}
}
}
}
}, () => {})

fastify.get('/address3/:id', {
schema: {
params: {
type: 'object',
properties: {
id: { type: 'string' }
}
},
querystring: {
type: 'object',
properties: {
authKey: {
type: 'string',
description: 'auth token'
},
somethingElse: {
type: 'string',
description: 'common field'
}
}
}
}
}, () => {})

await fastify.ready()

const swaggerObject = fastify.swagger()
t.equal(typeof swaggerObject, 'object')

const api = await Swagger.validate(swaggerObject)
t.pass('valid swagger object')
t.ok(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'apiKey')))
t.notOk(api.paths['/address1/{id}'].get.parameters.find(({ name }) => (name === 'securityKey')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
t.notOk(api.paths['/address2/{id}'].get.parameters.find(({ name }) => (name === 'securityKey')))
t.ok(api.paths['/address3/{id}'].get.parameters.find(({ name }) => (name === 'id')))
t.ok(api.paths['/address3/{id}'].get.parameters.find(({ name }) => (name === 'authKey')))
})

test('verify generated path param definition with route prefixing', async (t) => {
const opts = {
schema: {}
Expand Down

0 comments on commit 0571b62

Please sign in to comment.