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 3 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
5 changes: 2 additions & 3 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 Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,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')

Check failure on line 3 in test/casbinRest.test.js

View workflow job for this annotation

GitHub Actions / Node 20

A space is required after '{'

Check failure on line 3 in test/casbinRest.test.js

View workflow job for this annotation

GitHub Actions / Node 20

A space is required before '}'
ilteoood marked this conversation as resolved.
Show resolved Hide resolved
const Fastify = require('fastify')
const sinon = require('sinon')
const fp = require('fastify-plugin')
Expand All @@ -26,26 +25,20 @@

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 @@
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 @@

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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@
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 @@

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 @@

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 @@
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