Skip to content

Commit

Permalink
BREAKING CHANGE: fastify v5 and node test runner (#137)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: fastify v5

* BREAKING CHANGE: node test runner
  • Loading branch information
ilteoood authored Dec 9, 2024
1 parent f026b40 commit fc0fad3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
- 20
- 22
name: Node ${{ matrix.node }}
steps:
- uses: actions/checkout@v4
Expand All @@ -20,7 +19,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
16 changes: 7 additions & 9 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 @@ -39,16 +38,15 @@
"casbin": ">=5.1.6"
},
"devDependencies": {
"@types/node": "^22.0.0",
"casbin": "^5.1.6",
"fastify": "^4.0.2",
"@types/node": "^22.10.1",
"casbin": "^5.36.0",
"fastify": "^5.1.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"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"
}
}
67 changes: 39 additions & 28 deletions test/casbin.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict'

const path = require('path')
const tap = require('tap')
const test = tap.test
const { test } = require('node:test')
const Fastify = require('fastify')
const proxyquire = require('proxyquire')
const sinon = require('sinon')
const { Model, FileAdapter } = require('casbin')
const fs = require('fs')
const { Model, FileAdapter, setDefaultFileSystem } = require('casbin')

const plugin = require('../')

const modelPath = path.join(__dirname, 'fixtures', 'basic_model.conf')
const policyPath = path.join(__dirname, 'fixtures', 'basic_policy.csv')

test('casbin should exist', t => {
setDefaultFileSystem(fs)

test('casbin should exist', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand All @@ -24,14 +26,15 @@ test('casbin should exist', t => {
})

fastify.ready(err => {
t.error(err)
t.ok(fastify.casbin)
t.assert.ok(!err)
t.assert.ok(fastify.casbin)

fastify.close()
done()
})
})

test('preloaded model and adapter should be accepted', t => {
test('preloaded model and adapter should be accepted', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand All @@ -45,14 +48,15 @@ test('preloaded model and adapter should be accepted', t => {
})

fastify.ready(err => {
t.error(err)
t.ok(fastify.casbin)
t.assert.ok(!err)
t.assert.ok(fastify.casbin)

fastify.close()
done()
})
})

test('adapter can be omitted for in-memory storage', t => {
test('adapter can be omitted for in-memory storage', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand All @@ -64,14 +68,15 @@ test('adapter can be omitted for in-memory storage', t => {
})

fastify.ready(err => {
t.error(err)
t.ok(fastify.casbin)
t.assert.ok(!err)
t.assert.ok(fastify.casbin)

fastify.close()
done()
})
})

test('casbinJsGetPermissionForUser should exist', t => {
test('casbinJsGetPermissionForUser should exist', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand All @@ -82,14 +87,15 @@ test('casbinJsGetPermissionForUser should exist', t => {
})

fastify.ready(err => {
t.error(err)
t.ok(fastify.casbin.casbinJsGetPermissionForUser)
t.assert.ok(!err)
t.assert.ok(fastify.casbin.casbinJsGetPermissionForUser)

fastify.close()
done()
})
})

test('calls loadPolicy on enforcer', t => {
test('calls loadPolicy on enforcer', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand All @@ -112,14 +118,15 @@ test('calls loadPolicy on enforcer', t => {
})

fastify.ready(err => {
t.error(err)
t.ok(enforcer.loadPolicy.called)
t.assert.ok(!err)
t.assert.ok(enforcer.loadPolicy.called)

fastify.close()
done()
})
})

test('calls casbinJsGetPermissionForUser with enforcer', t => {
test('calls casbinJsGetPermissionForUser with enforcer', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand All @@ -145,17 +152,18 @@ test('calls casbinJsGetPermissionForUser with enforcer', t => {
})

fastify.ready(err => {
t.error(err)
t.assert.ok(!err)

fastify.casbin.casbinJsGetPermissionForUser('user')

t.ok(casbinJsGetPermissionForUser.calledWith(enforcer, 'user'))
t.assert.ok(casbinJsGetPermissionForUser.calledWith(enforcer, 'user'))

fastify.close()
done()
})
})

test('sets watcher on enforcer when provided', t => {
test('sets watcher on enforcer when provided', (t, done) => {
t.plan(2)

const fastify = Fastify()
Expand Down Expand Up @@ -183,14 +191,15 @@ test('sets watcher on enforcer when provided', t => {
})

fastify.ready(err => {
t.error(err)
t.ok(enforcer.setWatcher.calledWith(watcher))
t.assert.ok(!err)
t.assert.ok(enforcer.setWatcher.calledWith(watcher))

fastify.close()
done()
})
})

test('closes adapter and watcher', t => {
test('closes adapter and watcher', (t, done) => {
t.plan(3)

const fastify = Fastify()
Expand Down Expand Up @@ -224,11 +233,13 @@ test('closes adapter and watcher', t => {
})

fastify.ready(err => {
t.error(err)
t.assert.ok(!err)

fastify.close(() => {
t.ok(adapter.close.called)
t.ok(watcher.close.called)
t.assert.ok(adapter.close.called)
t.assert.ok(watcher.close.called)

done()
})
})
})
3 changes: 0 additions & 3 deletions test/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ interface CasbinJsGetPermissionsForUser {

const server = fastify()

// no options
server.register(fastifyCasbin)

// string adapter
server.register(fastifyCasbin, {
model: 'some file',
Expand Down

0 comments on commit fc0fad3

Please sign in to comment.