From ac34d81f52dd17bdf04cec77849ff07d73f030de Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 21:42:33 +0100 Subject: [PATCH 1/7] BREAKING CHANGE: fastify v5 --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 5eb5b67..0be800d 100644 --- a/package.json +++ b/package.json @@ -45,14 +45,14 @@ "http-errors": "^2.0.0" }, "devDependencies": { - "@types/node": "^22.0.0", - "fastify": "^4.0.2", + "@types/node": "^22.10.1", + "fastify": "^5.1.0", "pre-commit": "^1.2.2", "sinon": "^19.0.2", "snazzy": "^9.0.0", - "standard": "^17.0.0", + "standard": "^17.1.2", "tap": "^16.0.0", - "tsd": "^0.31.0", - "typescript": "^5.0.2" + "tsd": "^0.31.2", + "typescript": "^5.7.2" } } From 3f349c41b8b8500078caf255995654a266d8e0fe Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 21:46:04 +0100 Subject: [PATCH 2/7] chore: migrate to node test runner --- package.json | 1 - test/casbinRest.test.js | 133 +++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 71 deletions(-) diff --git a/package.json b/package.json index 0be800d..4fefe03 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "sinon": "^19.0.2", "snazzy": "^9.0.0", "standard": "^17.1.2", - "tap": "^16.0.0", "tsd": "^0.31.2", "typescript": "^5.7.2" } diff --git a/test/casbinRest.test.js b/test/casbinRest.test.js index 198b92e..c895f2d 100644 --- a/test/casbinRest.test.js +++ b/test/casbinRest.test.js @@ -1,7 +1,6 @@ 'use strict' -const tap = require('tap') -const test = tap.test +const {test, beforeEach, afterEach} = require('node:test') const Fastify = require('fastify') const sinon = require('sinon') const fp = require('fastify-plugin') @@ -26,18 +25,12 @@ function makeStubCasbin () { let fastify -tap.beforeEach(async () => { +beforeEach(() => { fastify = new Fastify() - await fastify.register(makeStubCasbin()) + return fastify.register(makeStubCasbin()) }) -tap.afterEach(async () => { - try { - await fastify.close() - } catch (error) { - tap.error(error) - } -}) +afterEach(() => fastify.close()) test('throws if no casbin decorator exists', async t => { t.plan(1) @@ -45,7 +38,7 @@ test('throws if no casbin decorator exists', async t => { const buggyFastify = new Fastify() await buggyFastify.register(plugin) } catch (err) { - t.equal(err.message, "The decorator 'casbin' required by 'fastify-casbin-rest' is not present in Fastify") + t.assert.equal(err.message, "The decorator 'casbin' required by 'fastify-casbin-rest' is not present in Fastify") } }) @@ -56,7 +49,7 @@ test('throws if fastify-casbin plugin is not registered', async t => { buggyFastify.decorate('casbin', sinon.stub()) await buggyFastify.register(plugin) } catch (err) { - t.equal(err.message, "The dependency 'fastify-casbin' of plugin 'fastify-casbin-rest' is not registered") + t.assert.equal(err.message, "The dependency 'fastify-casbin' of plugin 'fastify-casbin-rest' is not registered") } }) @@ -77,11 +70,11 @@ test('ignores routes where plugin is not enabled', async t => { await fastify.ready() - t.equal((await fastify.inject('/no-options')).body, 'ok') - t.equal((await fastify.inject('/no-casbin-rest')).body, 'ok') - t.equal((await fastify.inject('/false-casbin-rest')).body, 'ok') + t.assert.equal((await fastify.inject('/no-options')).body, 'ok') + t.assert.equal((await fastify.inject('/no-casbin-rest')).body, 'ok') + t.assert.equal((await fastify.inject('/false-casbin-rest')).body, 'ok') - t.notOk(fastify.casbin.enforce.called) + t.assert.ok(!fastify.casbin.enforce.called) }) test('allows route where plugin is enabled and enforce resolves true', async t => { @@ -92,13 +85,13 @@ test('allows route where plugin is enabled and enforce resolves true', async t = fastify.casbin.enforce.resolves(true) await fastify.ready() - t.equal((await fastify.inject('/')).body, 'ok') - t.ok(fastify.casbin.enforce.called) + t.assert.equal((await fastify.inject('/')).body, 'ok') + t.assert.ok(fastify.casbin.enforce.called) const [sub, obj, act] = fastify.casbin.enforce.getCall(0).args - t.equal(sub, undefined) - t.equal(obj, '/') - t.equal(act, 'GET') + t.assert.equal(sub, undefined) + t.assert.equal(obj, '/') + t.assert.equal(act, 'GET') }) test('allows route where plugin is enabled and enforce resolves true with dom resolver enabled', async t => { @@ -109,14 +102,14 @@ test('allows route where plugin is enabled and enforce resolves true with dom re fastify.casbin.enforce.resolves(true) await fastify.ready() - t.equal((await fastify.inject('/')).body, 'ok') - t.ok(fastify.casbin.enforce.called) + t.assert.equal((await fastify.inject('/')).body, 'ok') + t.assert.ok(fastify.casbin.enforce.called) const [sub, dom, obj, act] = fastify.casbin.enforce.getCall(0).args - t.equal(sub, undefined) - t.equal(dom, 'domain') - t.equal(obj, '/') - t.equal(act, 'GET') + t.assert.equal(sub, undefined) + t.assert.equal(dom, 'domain') + t.assert.equal(obj, '/') + t.assert.equal(act, 'GET') }) test('invokes onAllow callback if defined', async t => { @@ -128,20 +121,20 @@ test('invokes onAllow callback if defined', async t => { fastify.casbin.enforce.resolves(true) await fastify.ready() - t.equal((await fastify.inject('/')).body, 'ok') + t.assert.equal((await fastify.inject('/')).body, 'ok') - t.ok(onAllow.called) + t.assert.ok(onAllow.called) const [reply, argsFromCallback] = onAllow.getCall(0).args - t.ok(reply) - t.equal(argsFromCallback.sub, undefined) - t.equal(argsFromCallback.obj, '/') - t.equal(argsFromCallback.act, 'GET') + t.assert.ok(reply) + t.assert.equal(argsFromCallback.sub, undefined) + t.assert.equal(argsFromCallback.obj, '/') + t.assert.equal(argsFromCallback.act, 'GET') - t.ok(fastify.casbin.enforce.called) + t.assert.ok(fastify.casbin.enforce.called) const [sub, obj, act] = fastify.casbin.enforce.getCall(0).args - t.equal(sub, undefined) - t.equal(obj, '/') - t.equal(act, 'GET') + t.assert.equal(sub, undefined) + t.assert.equal(obj, '/') + t.assert.equal(act, 'GET') }) test('forbids route where plugin is enabled and enforce resolves false', async t => { @@ -152,8 +145,8 @@ test('forbids route where plugin is enabled and enforce resolves false', async t fastify.casbin.enforce.resolves(false) await fastify.ready() - t.equal((await fastify.inject('/')).statusCode, 403) - t.ok(fastify.casbin.enforce.called) + t.assert.equal((await fastify.inject('/')).statusCode, 403) + t.assert.ok(fastify.casbin.enforce.called) }) test('forbids route where plugin is enabled and enforce resolves false with dom resolver enabled', async t => { @@ -164,8 +157,8 @@ test('forbids route where plugin is enabled and enforce resolves false with dom fastify.casbin.enforce.resolves(false) await fastify.ready() - t.equal((await fastify.inject('/')).statusCode, 403) - t.ok(fastify.casbin.enforce.called) + t.assert.equal((await fastify.inject('/')).statusCode, 403) + t.assert.ok(fastify.casbin.enforce.called) }) test('works correctly if there is an existing preHandler hook', async t => { @@ -177,9 +170,9 @@ test('works correctly if there is an existing preHandler hook', async t => { fastify.casbin.enforce.resolves(false) await fastify.ready() - t.equal((await fastify.inject('/')).statusCode, 403) - t.ok(fastify.casbin.enforce.called) - t.ok(preHandler.calledOnce) + t.assert.equal((await fastify.inject('/')).statusCode, 403) + t.assert.ok(fastify.casbin.enforce.called) + t.assert.ok(preHandler.calledOnce) }) test('supports specifying custom hooks', async t => { @@ -191,10 +184,10 @@ test('supports specifying custom hooks', async t => { fastify.casbin.enforce.resolves(false) await fastify.ready() - t.equal((await fastify.inject('/')).statusCode, 403) + t.assert.equal((await fastify.inject('/')).statusCode, 403) - t.ok(fastify.casbin.enforce.called) - t.notOk(preParsing.called) + t.assert.ok(fastify.casbin.enforce.called) + t.assert.ok(!preParsing.called) }) test('supports specifying custom logger', async t => { @@ -208,21 +201,21 @@ test('supports specifying custom logger', async t => { fastify.casbin.enforce.resolves(true) await fastify.ready() - t.equal((await fastify.inject('/')).statusCode, 200) + t.assert.equal((await fastify.inject('/')).statusCode, 200) - t.ok(log.called) + t.assert.ok(log.called) const [_fastify, _request, argsFromCallback] = log.getCall(0).args - t.ok(_fastify) - t.ok(_request) + t.assert.ok(_fastify) + t.assert.ok(_request) - t.ok(getSub.called) - t.equal(argsFromCallback.sub, 'custom sub') + t.assert.ok(getSub.called) + t.assert.equal(argsFromCallback.sub, 'custom sub') - t.ok(getObj.called) - t.equal(argsFromCallback.obj, 'custom obj') + t.assert.ok(getObj.called) + t.assert.equal(argsFromCallback.obj, 'custom obj') - t.ok(getAct.called) - t.equal(argsFromCallback.act, 'custom act') + t.assert.ok(getAct.called) + t.assert.equal(argsFromCallback.act, 'custom act') }) test('supports overriding plugin rules on route level', async t => { @@ -246,11 +239,11 @@ test('supports overriding plugin rules on route level', async t => { await fastify.inject('/') - t.ok(fastify.casbin.enforce.called) + t.assert.ok(fastify.casbin.enforce.called) const [sub, obj, act] = fastify.casbin.enforce.getCall(0).args - t.equal(sub, 'GET') - t.equal(obj, undefined) - t.equal(act, '/') + t.assert.equal(sub, 'GET') + t.assert.equal(obj, undefined) + t.assert.equal(act, '/') }) test('supports passing constants as extractor params without domain', async t => { @@ -274,11 +267,11 @@ test('supports passing constants as extractor params without domain', async t => await fastify.inject('/') - t.ok(fastify.casbin.enforce.called) + t.assert.ok(fastify.casbin.enforce.called) const [sub, obj, act] = fastify.casbin.enforce.getCall(0).args - t.equal(sub, 'a') - t.equal(obj, 'b') - t.equal(act, 'c') + t.assert.equal(sub, 'a') + t.assert.equal(obj, 'b') + t.assert.equal(act, 'c') }) test('supports passing constants as extractor params with domain', async t => { @@ -305,8 +298,8 @@ test('supports passing constants as extractor params with domain', async t => { await fastify.inject('/') const [sub, dom, obj, act] = fastify.casbin.enforce.getCall(0).args - t.equal(sub, 'a') - t.equal(dom, 'users') - t.equal(obj, 'b') - t.equal(act, 'c') + t.assert.equal(sub, 'a') + t.assert.equal(dom, 'users') + t.assert.equal(obj, 'b') + t.assert.equal(act, 'c') }) From f9792853468ff74ef43f1ce3fef1a0e36e8c380d Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 21:46:21 +0100 Subject: [PATCH 3/7] BREAKING CHANGE: min version node 20 --- .github/workflows/ci.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33af588..775bb52 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,9 +10,8 @@ jobs: strategy: matrix: node: - - 14 - - 16 - - 18 + - 20 + - 22 name: Node ${{ matrix.node }} steps: - uses: actions/checkout@v4 From 301ac38ce7ba742d98f8b02616613af7c82e1b03 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 21:04:12 +0000 Subject: [PATCH 4/7] fix: lint --- test/casbinRest.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/casbinRest.test.js b/test/casbinRest.test.js index c895f2d..9feb8c5 100644 --- a/test/casbinRest.test.js +++ b/test/casbinRest.test.js @@ -1,6 +1,6 @@ 'use strict' -const {test, beforeEach, afterEach} = require('node:test') +const { test, beforeEach, afterEach } = require('node:test') const Fastify = require('fastify') const sinon = require('sinon') const fp = require('fastify-plugin') From b9539836b4e35c3c27dba09a578589bd0b1a40e8 Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 21:06:32 +0000 Subject: [PATCH 5/7] fix: test command --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4fefe03..9633bea 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "plugin.js", "types": "plugin.d.ts", "scripts": { - "test": "tap test/*.test.js && npm run typescript", + "test": "node --test && npm run typescript", "test:ci": "tap --coverage-report=lcov test/*.test.js && npm run typescript", "lint": "standard | snazzy", "lint:fix": "standard --fix | snazzy", From a269c26e16be82482d76976deb4958c965e91efc Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 21:08:17 +0000 Subject: [PATCH 6/7] fix: test --- .github/workflows/ci.yml | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 775bb52..0970ee6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: node-version: ${{ matrix.node }} - run: npm install - run: npm run lint - - run: npm run test:ci + - run: npm run test - run: bash <(curl -s https://codecov.io/bash) automerge: needs: build diff --git a/package.json b/package.json index 9633bea..2951fcb 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "types": "plugin.d.ts", "scripts": { "test": "node --test && npm run typescript", - "test:ci": "tap --coverage-report=lcov test/*.test.js && npm run typescript", + "lint": "standard | snazzy", "lint:fix": "standard --fix | snazzy", "typescript": "tsd" From 6399242484ab7d039b8b4845e063e38b9c301c0d Mon Sep 17 00:00:00 2001 From: Matteo Pietro Dazzi Date: Mon, 9 Dec 2024 22:08:38 +0100 Subject: [PATCH 7/7] fix: package json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 2951fcb..aa0634a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "types": "plugin.d.ts", "scripts": { "test": "node --test && npm run typescript", - "lint": "standard | snazzy", "lint:fix": "standard --fix | snazzy", "typescript": "tsd"