Skip to content

Commit

Permalink
test: deflake tests (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Dec 24, 2023
1 parent 532770a commit 3cf8d5d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 61 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use strict'

const fp = require('fastify-plugin')
const { readFile } = require('node:fs/promises')
const fsPromises = require('node:fs/promises')
const path = require('node:path')
const fp = require('fastify-plugin')
const csp = require('./static/csp.json')

async function fastifySwaggerUi (fastify, opts) {
fastify.decorate('swaggerCSP', require('./static/csp.json'))
const logoContent = await fsPromises.readFile(path.join(__dirname, './static/logo.svg'))

fastify.decorate('swaggerCSP', csp)

await fastify.register(require('./lib/routes'), {
prefix: opts.routePrefix || '/documentation',
uiConfig: opts.uiConfig || {},
initOAuth: opts.initOAuth || {},
hooks: opts.uiHooks,
theme: opts.theme || {},
logo: opts.logo || { type: 'image/svg+xml', content: await readFile(path.join(__dirname, './static/logo.svg')) },
logo: opts.logo || { type: 'image/svg+xml', content: logoContent },
...opts
})
}
Expand Down
4 changes: 2 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const staticPrefix = '/static'
function getRedirectPathForTheRootRoute (url) {
let redirectPath

if (url.substr(-1) === '/') {
if (url.length !== 0 && url[url.length - 1] === '/') {
redirectPath = `.${staticPrefix}/index.html`
} else {
const urlPathParts = url.split('/')
Expand All @@ -26,7 +26,7 @@ function getRedirectPathForTheRootRoute (url) {
function fastifySwagger (fastify, opts, done) {
let staticCSP = false
if (opts.staticCSP === true) {
const csp = require('../static/csp.json')
const csp = fastify.swaggerCSP
staticCSP = `default-src 'self'; base-uri 'self'; font-src 'self' https: data:; frame-ancestors 'self'; img-src 'self' data: validator.swagger.io; object-src 'none'; script-src 'self' ${csp.script.join(' ')}; script-src-attr 'none'; style-src 'self' https: ${csp.style.join(' ')}; upgrade-insecure-requests;`
}
if (typeof opts.staticCSP === 'string') {
Expand Down
5 changes: 3 additions & 2 deletions test/decorator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')

test('fastify.swaggerCSP should exist', async (t) => {
t.plan(1)
t.plan(3)
const fastify = Fastify()

await fastify.register(fastifySwagger)
await fastify.register(fastifySwaggerUi)

await fastify.ready()
t.ok(fastify.swaggerCSP)
t.ok(Array.isArray(fastify.swaggerCSP.script))
t.ok(Array.isArray(fastify.swaggerCSP.style))
})
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)
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
75 changes: 40 additions & 35 deletions test/static.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
'use strict'

const fs = require('node:fs')
const resolve = require('node:path').resolve
const { test } = require('tap')
const yaml = require('yaml')
const Fastify = require('fastify')
const fastifySwagger = require('@fastify/swagger')
const fastifySwaggerUi = require('../index')
const yaml = require('yaml')
const readFileSync = require('node:fs').readFileSync
const resolve = require('node:path').resolve

const oauthRedirectHtml = fs.readFileSync(resolve(__dirname, '..', 'static', 'oauth2-redirect.html'), 'utf8')
const exampleStaticSpecificationYaml = fs.readFileSync(
resolve(__dirname, '..', 'examples', 'example-static-specification.yaml'),
'utf8'
)

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 +41,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 +67,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 +80,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 +98,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 +115,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 +130,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,8 +143,7 @@ test('/documentation/:file should serve static file from the location of main sp
baseDir: resolve(__dirname, '..', 'examples')
}

t.plan(4)
const fastify = new Fastify()
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi, uiConfig)

Expand All @@ -152,13 +163,7 @@ test('/documentation/:file should serve static file from the location of main sp
})

t.equal(res.statusCode, 200)
t.equal(
readFileSync(
resolve(__dirname, '..', 'examples', 'example-static-specification.yaml'),
'utf8'
),
res.payload
)
t.equal(exampleStaticSpecificationYaml, res.payload)
}

{
Expand All @@ -172,15 +177,16 @@ 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 = new Fastify()
const fastify = Fastify()
await fastify.register(fastifySwagger, config)
await fastify.register(fastifySwaggerUi)
fastify.setNotFoundHandler((request, reply) => {
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,8 +216,7 @@ test('/documentation/:file should be served from custom location', async (t) =>
baseDir: resolve(__dirname, '..', 'static')
}

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

Expand All @@ -218,15 +225,13 @@ test('/documentation/:file should be served from custom location', async (t) =>
url: '/documentation/oauth2-redirect.html'
})

const fileContent = readFileSync(resolve(__dirname, '..', 'static', 'oauth2-redirect.html'), 'utf8')
t.equal(res.statusCode, 200)
t.equal(
fileContent,
res.payload
)
t.equal(oauthRedirectHtml, res.payload)
})

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,8 +243,7 @@ test('/documentation/:file should be served from custom location with trailing s
baseDir: resolve(__dirname, '..', 'static') + '/'
}

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

Expand All @@ -249,21 +253,19 @@ test('/documentation/:file should be served from custom location with trailing s
})

t.equal(res.statusCode, 200)
t.equal(
readFileSync(resolve(__dirname, '..', 'static', 'oauth2-redirect.html'), 'utf8'),
res.payload
)
t.equal(oauthRedirectHtml, res.payload)
})

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 +296,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 +333,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 +371,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
Loading

0 comments on commit 3cf8d5d

Please sign in to comment.