Skip to content

Commit

Permalink
restructure some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Dec 24, 2023
1 parent 922567d commit 676c150
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
8 changes: 3 additions & 5 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,15 @@ test('catch all added schema', async t => {

fastify.addSchema({ $id: 'Root', type: 'object', properties: {} })

fastify.register(function (instance, _, done) {
fastify.register(async function (instance) {
instance.addSchema({ $id: 'Instance', type: 'object', properties: {} })

instance.register(function (instance, _, done) {
await instance.register(async function (instance) {
instance.addSchema({ $id: 'Sub-Instance', type: 'object', properties: {} })
done()
})
done()
})

await fastify.ready()
const openapi = await fastify.swagger()
const openapi = fastify.swagger()
t.same(Object.keys(openapi.components.schemas), ['Root', 'Instance', 'Sub-Instance'])
})
19 changes: 10 additions & 9 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ test('fastify will response swagger csp', async (t) => {

await fastify.register(fastifySwagger)
await fastify.register(fastifySwaggerUi)
await fastify.register(fastifyHelmet, instance => {
return {
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
imgSrc: ["'self'", 'data:', 'validator.swagger.io'],
scriptSrc: ["'self'"].concat(instance.swaggerCSP.script),
styleSrc: ["'self'", 'https:'].concat(instance.swaggerCSP.style)
}

const scriptSrc = ["'self'"].concat(fastify.swaggerCSP.script)
const styleSrc = ["'self'", 'https:'].concat(fastify.swaggerCSP.style)
await fastify.register(fastifyHelmet, {
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
imgSrc: ["'self'", 'data:', 'validator.swagger.io'],
scriptSrc,
styleSrc
}
}
})
Expand Down
36 changes: 24 additions & 12 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const readFileSync = require('node:fs').readFileSync
const resolve = require('node:path').resolve

test('swagger route returns yaml', async (t) => {
t.plan(3)

const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}

t.plan(3)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand All @@ -34,14 +35,15 @@ test('swagger route returns yaml', async (t) => {
})

test('swagger route returns json', async (t) => {
t.plan(3)

const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.json'
}
}

t.plan(3)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand All @@ -59,6 +61,8 @@ test('swagger route returns json', async (t) => {
})

test('postProcessor works, swagger route returns updated yaml', async (t) => {
t.plan(4)

const config = {
mode: 'static',
specification: {
Expand All @@ -70,7 +74,6 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => {
}
}

t.plan(4)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand All @@ -89,6 +92,8 @@ test('postProcessor works, swagger route returns updated yaml', async (t) => {
})

test('swagger route returns explicitly passed doc', async (t) => {
t.plan(2)

const document = {
info: {
title: 'Test swagger',
Expand All @@ -104,7 +109,6 @@ test('swagger route returns explicitly passed doc', async (t) => {
}
}

t.plan(2)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)

Expand All @@ -120,6 +124,8 @@ test('swagger route returns explicitly passed doc', async (t) => {
})

test('/documentation/:file should serve static file from the location of main specification file', async (t) => {
t.plan(4)

const config = {
mode: 'static',
specification: {
Expand All @@ -131,7 +137,6 @@ test('/documentation/:file should serve static file from the location of main sp
baseDir: resolve(__dirname, '..', 'examples')
}

t.plan(4)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, uiConfig)
Expand Down Expand Up @@ -172,14 +177,15 @@ test('/documentation/:file should serve static file from the location of main sp
})

test('/documentation/non-existing-file calls custom NotFoundHandler', async (t) => {
t.plan(1)

const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}

t.plan(1)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand All @@ -196,6 +202,8 @@ test('/documentation/non-existing-file calls custom NotFoundHandler', async (t)
})

test('/documentation/:file should be served from custom location', async (t) => {
t.plan(2)

const config = {
mode: 'static',
specification: {
Expand All @@ -208,7 +216,6 @@ test('/documentation/:file should be served from custom location', async (t) =>
baseDir: resolve(__dirname, '..', 'static')
}

t.plan(2)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, uiConfig)
Expand All @@ -227,6 +234,8 @@ test('/documentation/:file should be served from custom location', async (t) =>
})

test('/documentation/:file should be served from custom location with trailing slash(es)', async (t) => {
t.plan(2)

const config = {
mode: 'static',
specification: {
Expand All @@ -238,7 +247,6 @@ test('/documentation/:file should be served from custom location with trailing s
baseDir: resolve(__dirname, '..', 'static') + '/'
}

t.plan(2)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, uiConfig)
Expand All @@ -256,14 +264,15 @@ test('/documentation/:file should be served from custom location with trailing s
})

test('/documentation/yaml returns cache.swaggerString on second request in static mode', async (t) => {
t.plan(6)

const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.yaml'
}
}

t.plan(6)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand Down Expand Up @@ -294,14 +303,15 @@ test('/documentation/yaml returns cache.swaggerString on second request in stati
})

test('/documentation/json returns cache.swaggerObject on second request in static mode', async (t) => {
t.plan(6)

const config = {
mode: 'static',
specification: {
path: './examples/example-static-specification.json'
}
}

t.plan(6)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand Down Expand Up @@ -330,13 +340,14 @@ test('/documentation/json returns cache.swaggerObject on second request in stati
})

test('/documentation/yaml returns cache.swaggerString on second request in dynamic mode', async (t) => {
t.plan(6)

const config = {
specification: {
path: './examples/example-static-specification.yaml'
}
}

t.plan(6)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand Down Expand Up @@ -367,13 +378,14 @@ test('/documentation/yaml returns cache.swaggerString on second request in dynam
})

test('/documentation/json returns cache.swaggerObject on second request in dynamic mode', async (t) => {
t.plan(6)

const config = {
specification: {
path: './examples/example-static-specification.json'
}
}

t.plan(6)
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
Expand Down

0 comments on commit 676c150

Please sign in to comment.