Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: support for fastify v5 and node test runner #148

Merged
merged 7 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
- 20
- 22
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v4
Expand All @@ -22,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
Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": "plugin.js",
"types": "plugin.d.ts",
"scripts": {
"test": "tap test/*.test.js && npm run typescript",
"test:ci": "tap --coverage-report=lcov test/*.test.js && npm run typescript",
"test": "node --test && npm run typescript",
"lint": "standard | snazzy",
"lint:fix": "standard --fix | snazzy",
"typescript": "tsd"
Expand Down Expand Up @@ -45,14 +44,13 @@
"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",
"tap": "^16.0.0",
"tsd": "^0.31.0",
"typescript": "^5.0.2"
"standard": "^17.1.2",
"tsd": "^0.31.2",
"typescript": "^5.7.2"
}
}
133 changes: 63 additions & 70 deletions test/casbinRest.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, beforeEach, afterEach } = require('node:test')
const Fastify = require('fastify')
const sinon = require('sinon')
const fp = require('fastify-plugin')
Expand All @@ -26,26 +25,20 @@ 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)
try {
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")
}
})

Expand All @@ -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")
}
})

Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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')
})
Loading