Skip to content

Commit

Permalink
chore: migrate from tap to node:test and c8 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
dancastillo authored Sep 6, 2024
1 parent 85fd6f0 commit 45f4669
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 198 deletions.
2 changes: 0 additions & 2 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lint:fix": "standard --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},
"precommit": [
"lint",
Expand All @@ -34,9 +34,9 @@
"@fastify/auth": "^5.0.0",
"@fastify/pre-commit": "^2.1.0",
"@types/node": "^22.0.0",
"c8": "^10.1.2",
"fastify": "^5.0.0-alpha.4",
"standard": "^17.1.0",
"tap": "^18.7.2",
"tsd": "^0.31.0"
},
"dependencies": {
Expand Down
14 changes: 7 additions & 7 deletions test/decorate-with-logger.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const stream = require('node:stream')
const Fastify = require('fastify')
const plugin = require('..')
Expand Down Expand Up @@ -29,8 +29,8 @@ test('verifyBearerAuth with debug log', async (t) => {

await fastify.ready()

t.ok(fastify.verifyBearerAuth)
t.ok(fastify.verifyBearerAuthFactory)
t.assert.ok(fastify.verifyBearerAuth)
t.assert.ok(fastify.verifyBearerAuthFactory)

const response = await fastify.inject({
method: 'GET',
Expand All @@ -43,10 +43,10 @@ test('verifyBearerAuth with debug log', async (t) => {
// Debug level is equal to 20 so we search for an entry with a level of 20
const failure = logs.find((entry) => entry.level && entry.level === 20)

t.equal(failure.level, 20)
t.equal(failure.msg, 'unauthorized: invalid authorization header')
t.assert.strictEqual(failure.level, 20)
t.assert.strictEqual(failure.msg, 'unauthorized: invalid authorization header')

t.equal(response.statusCode, 401)
t.assert.strictEqual(response.statusCode, 401)
})

test('register with invalid log level', async (t) => {
Expand All @@ -58,6 +58,6 @@ test('register with invalid log level', async (t) => {
try {
await fastify.register(plugin, { addHook: false, keys: new Set(['123456']), verifyErrorLogLevel: invalidLogLevel })
} catch (err) {
t.equal(err.message, `fastify.log does not have level '${invalidLogLevel}'`)
t.assert.strictEqual(err.message, `fastify.log does not have level '${invalidLogLevel}'`)
}
})
34 changes: 18 additions & 16 deletions test/decorate.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
'use strict'

const tap = require('tap')
const test = tap.test
const { test } = require('node:test')
const fastify = require('fastify')()
const plugin = require('../')

fastify.register(plugin, { addHook: false, keys: new Set(['123456']) })

test('verifyBearerAuth', (t) => {
test('verifyBearerAuth', async (t) => {
t.plan(1)
fastify.ready(() => {
t.ok(fastify.verifyBearerAuth)
})
await fastify.ready()
t.assert.ok(fastify.verifyBearerAuth)
})

test('verifyBearerAuthFactory', (t) => {
test('verifyBearerAuthFactory', async (t) => {
t.plan(1)
fastify.ready(() => {
t.ok(fastify.verifyBearerAuthFactory)
})
await fastify.ready()
t.assert.ok(fastify.verifyBearerAuthFactory)
})

test('verifyBearerAuthFactory', (t) => {
t.plan(1)
fastify.ready(() => {
const keys = { keys: new Set([123456]) }
t.throws(() => fastify.verifyBearerAuthFactory(keys), /keys has to contain only string entries/)
})
test('verifyBearerAuthFactory', async (t) => {
t.plan(2)
await fastify.ready()
const keys = { keys: new Set([123456]) }
await t.assert.rejects(
async () => fastify.verifyBearerAuthFactory(keys),
(err) => {
t.assert.strictEqual(err.message, 'options.keys has to contain only string entries')
return true
}
)
})
87 changes: 35 additions & 52 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const tap = require('tap')
const test = tap.test
const { test } = require('node:test')
const fastify = require('fastify')()
const plugin = require('../')

Expand All @@ -11,67 +10,53 @@ fastify.get('/test', (req, res) => {
res.send({ hello: 'world' })
})

test('success route succeeds', (t) => {
test('success route succeeds', async (t) => {
t.plan(2)
fastify.inject({
const response = await fastify.inject({
method: 'GET',
url: '/test',
headers: {
authorization: 'Bearer 123456'
}
}).then(response => {
t.equal(response.statusCode, 200)
t.same(JSON.parse(response.body), { hello: 'world' })
}).catch(err => {
t.error(err)
})
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})

test('invalid key route fails correctly', (t) => {
test('invalid key route fails correctly', async (t) => {
t.plan(2)
fastify.inject({
const response = await fastify.inject({
method: 'GET',
url: '/test',
headers: {
authorization: 'Bearer 987654'
}
}).then(response => {
t.equal(response.statusCode, 401)
t.match(JSON.parse(response.body).error, /invalid authorization header/)
}).catch(err => {
t.error(err)
})
t.assert.strictEqual(response.statusCode, 401)
t.assert.strictEqual(JSON.parse(response.body).error, 'invalid authorization header')
})

test('missing space between bearerType and key fails correctly', (t) => {
test('missing space between bearerType and key fails correctly', async (t) => {
t.plan(2)
fastify.inject({
const response = await fastify.inject({
method: 'GET',
url: '/test',
headers: {
authorization: 'Bearer123456'
}
}).then(response => {
t.equal(response.statusCode, 401)
t.match(JSON.parse(response.body).error, /invalid authorization header/)
}).catch(err => {
t.error(err)
})
t.assert.strictEqual(response.statusCode, 401)
t.assert.strictEqual(JSON.parse(response.body).error, 'invalid authorization header')
})

test('missing header route fails correctly', (t) => {
test('missing header route fails correctly', async (t) => {
t.plan(2)
fastify.inject({ method: 'GET', url: '/test' }).then(response => {
t.equal(response.statusCode, 401)
t.match(JSON.parse(response.body).error, /missing authorization header/)
}).catch(err => {
t.error(err)
})
const response = await fastify.inject({ method: 'GET', url: '/test' })
t.assert.strictEqual(response.statusCode, 401)
t.assert.strictEqual(JSON.parse(response.body).error, 'missing authorization header')
})

test('integration with @fastify/auth', async (t) => {
t.plan(3)

const fastify = require('fastify')()
await fastify.register(plugin, { addHook: false, keys: new Set(['123456']) })
fastify.decorate('allowAnonymous', function (request, _, done) {
Expand All @@ -96,14 +81,14 @@ test('integration with @fastify/auth', async (t) => {

await fastify.ready()

t.test('anonymous should pass', async (t) => {
await test('anonymous should pass', async (t) => {
t.plan(2)
const res = await fastify.inject({ method: 'GET', url: '/anonymous' })
t.equal(res.statusCode, 200)
t.match(JSON.parse(res.body).hello, 'world')
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(JSON.parse(res.body).hello, 'world')
})

t.test('bearer auth should pass', async (t) => {
await test('bearer auth should pass', async (t) => {
t.plan(2)
const res = await fastify.inject({
method: 'GET',
Expand All @@ -112,11 +97,11 @@ test('integration with @fastify/auth', async (t) => {
authorization: 'Bearer 123456'
}
})
t.equal(res.statusCode, 200)
t.match(JSON.parse(res.body).hello, 'world')
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(JSON.parse(res.body).hello, 'world')
})

t.test('bearer auth should fail, so fastify.auth fails', async (t) => {
await test('bearer auth should fail, so fastify.auth fails', async (t) => {
t.plan(2)
const res = await fastify.inject({
method: 'GET',
Expand All @@ -125,14 +110,12 @@ test('integration with @fastify/auth', async (t) => {
authorization: 'Bearer fail'
}
})
t.equal(res.statusCode, 401)
t.match(JSON.parse(res.body).error, /Unauthorized/)
t.assert.strictEqual(res.statusCode, 401)
t.assert.strictEqual(JSON.parse(res.body).error, 'Unauthorized')
})
})

test('integration with @fastify/auth; not the last auth option', async (t) => {
t.plan(3)

const fastify = require('fastify')()
await fastify.register(plugin, { addHook: false, keys: new Set(['123456']) })
fastify.decorate('alwaysValidAuth', function (request, _, done) {
Expand All @@ -154,7 +137,7 @@ test('integration with @fastify/auth; not the last auth option', async (t) => {

await fastify.ready()

t.test('bearer auth should pass so fastify.auth should pass', async (t) => {
await test('bearer auth should pass so fastify.auth should pass', async (t) => {
t.plan(2)
const res = await fastify.inject({
method: 'GET',
Expand All @@ -163,11 +146,11 @@ test('integration with @fastify/auth; not the last auth option', async (t) => {
authorization: 'Bearer 123456'
}
})
t.equal(res.statusCode, 200)
t.match(JSON.parse(res.body).hello, 'world')
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(JSON.parse(res.body).hello, 'world')
})

t.test('bearer should fail but fastify.auth should pass', async (t) => {
await test('bearer should fail but fastify.auth should pass', async (t) => {
t.plan(2)
const res = await fastify.inject({
method: 'GET',
Expand All @@ -176,18 +159,18 @@ test('integration with @fastify/auth; not the last auth option', async (t) => {
authorization: 'Bearer fail'
}
})
t.equal(res.statusCode, 200)
t.match(JSON.parse(res.body).hello, 'world')
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(JSON.parse(res.body).hello, 'world')
})

t.test('bearer should fail but fastify.auth should pass', async (t) => {
await test('bearer should fail but fastify.auth should pass', async (t) => {
t.plan(2)
const res = await fastify.inject({
method: 'GET',
url: '/bearer-first',
headers: {}
})
t.equal(res.statusCode, 200)
t.match(JSON.parse(res.body).hello, 'world')
t.assert.strictEqual(res.statusCode, 200)
t.assert.strictEqual(JSON.parse(res.body).hello, 'world')
})
})
14 changes: 9 additions & 5 deletions test/spec-compliance-invalid.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
'use strict'

const tap = require('tap')
const test = tap.test
const { test } = require('node:test')
const Fastify = require('fastify')
const plugin = require('../')
const { FST_BEARER_AUTH_INVALID_SPEC } = require('../lib/errors')

test('throws FST_BEARER_AUTH_INVALID_SPEC when invalid value for specCompliance was used', async (t) => {
t.plan(1)
t.plan(2)

const fastify = Fastify()

t.rejects(async () => fastify.register(plugin, { keys: new Set(['123456']), specCompliance: 'invalid' }), new FST_BEARER_AUTH_INVALID_SPEC())
await t.assert.rejects(
async () => fastify.register(plugin, { keys: new Set(['123456']), specCompliance: 'invalid' }),
(err) => {
t.assert.strictEqual(err.name, 'FastifyError')
return true
}
)
})
23 changes: 11 additions & 12 deletions test/spec-compliance-rfc-6749.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const tap = require('tap')
const test = tap.test
const { test } = require('node:test')
const fastify = require('fastify')()
const plugin = require('../')

Expand All @@ -22,8 +21,8 @@ test('bearerType starting with capital letter', async (t) => {
}
})

t.equal(response.statusCode, 200)
t.same(JSON.parse(response.body), { hello: 'world' })
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})

test('bearerType all lowercase', async (t) => {
Expand All @@ -37,8 +36,8 @@ test('bearerType all lowercase', async (t) => {
}
})

t.equal(response.statusCode, 200)
t.same(JSON.parse(response.body), { hello: 'world' })
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})

test('bearerType all uppercase', async (t) => {
Expand All @@ -52,8 +51,8 @@ test('bearerType all uppercase', async (t) => {
}
})

t.equal(response.statusCode, 200)
t.same(JSON.parse(response.body), { hello: 'world' })
t.assert.strictEqual(response.statusCode, 200)
t.assert.deepStrictEqual(JSON.parse(response.body), { hello: 'world' })
})

test('invalid key route fails correctly', async (t) => {
Expand All @@ -66,8 +65,8 @@ test('invalid key route fails correctly', async (t) => {
}
})

t.equal(response.statusCode, 401)
t.match(JSON.parse(response.body).error, /invalid authorization header/)
t.assert.strictEqual(response.statusCode, 401)
t.assert.strictEqual(JSON.parse(response.body).error, 'invalid authorization header')
})

test('missing space between bearerType and key fails correctly', async (t) => {
Expand All @@ -80,6 +79,6 @@ test('missing space between bearerType and key fails correctly', async (t) => {
authorization: 'bearer123456'
}
})
t.equal(response.statusCode, 401)
t.match(JSON.parse(response.body).error, /invalid authorization header/)
t.assert.strictEqual(response.statusCode, 401)
t.assert.strictEqual(JSON.parse(response.body).error, 'invalid authorization header')
})
Loading

0 comments on commit 45f4669

Please sign in to comment.