-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set up a test in CI to verify whether all Casbin engines return…
… the same response in all scenarios. (#185) * chore: add Jest configuration and update package.json for testing setup * feat: add Casbin engine cross-engine enforcement consistency tests * test: enhance error handling and logging in Casbin engine tests * refactor: simplify error handling and remove redundant try-catch in casbinEngine tests * chore: add cross-engine test workflow for Casbin * chore: add `add_jest_configuration` branch to cross-engine-test workflow triggers * chore: remove add_jest_configuration branch from cross-engine-test workflow
- Loading branch information
1 parent
7353505
commit 1bd5e26
Showing
7 changed files
with
1,774 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Casbin Cross-Engine Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
workflow_dispatch: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20.8.1' | ||
|
||
- name: Install dependencies | ||
run: yarn install | ||
|
||
- name: Run tests | ||
run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,3 @@ next-env.d.ts | |
|
||
# editor | ||
.idea | ||
test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
moduleNameMapper: { | ||
'^@/(.*)$': '<rootDir>/$1', | ||
}, | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'], | ||
transform: { | ||
'^.+\\.(ts|tsx)$': 'ts-jest', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { newEnforcer, newModel, StringAdapter } from 'casbin'; | ||
import { RemoteCasbinEngine } from '../app/components/editor/CasbinEngine'; | ||
import { example } from '../app/components/editor/casbin-mode/example'; | ||
|
||
describe('Casbin Engine Tests', () => { | ||
describe('Cross-engine enforcement consistency', () => { | ||
Object.entries(example).forEach(([key, testCase]) => { | ||
test(`should return consistent enforcement result for ${testCase.name}`, async () => { | ||
const nodeEnforcer = await newEnforcer(newModel(testCase.model), new StringAdapter(testCase.policy || ' ')); | ||
|
||
const remoteEngines = { | ||
java: new RemoteCasbinEngine('java'), | ||
go: new RemoteCasbinEngine('go'), | ||
}; | ||
|
||
const requests = testCase.request.split('\n').filter(Boolean); | ||
|
||
for (const request of requests) { | ||
const requestParams = request.split(',').map((param) => {return param.trim()}); | ||
const nodeResult = await nodeEnforcer.enforce(...requestParams); | ||
|
||
for (const [engineType, engine] of Object.entries(remoteEngines)) { | ||
try { | ||
const adjustedRequestParams = [...requestParams]; | ||
if (engineType === 'go' && adjustedRequestParams.length < 3) { | ||
adjustedRequestParams.push(''); | ||
} | ||
|
||
const remoteResult = await engine.enforce({ | ||
model: testCase.model, | ||
policy: testCase.policy || ' ', | ||
request: adjustedRequestParams.join(','), | ||
}); | ||
|
||
if (remoteResult.error) { | ||
throw new Error(`${engineType} engine error: ${remoteResult.error}`); | ||
} | ||
|
||
console.log(`${testCase.name} - ${engineType} complete response:`, { | ||
request: adjustedRequestParams, | ||
response: remoteResult, | ||
nodeResult: nodeResult, | ||
}); | ||
|
||
expect(remoteResult.allowed).toBe(nodeResult); | ||
} catch (engineError: any) { | ||
console.error(`${testCase.name} - ${engineType} engine error:`, { | ||
error: engineError.message, | ||
request: requestParams, | ||
model: testCase.model, | ||
policy: testCase.policy, | ||
}); | ||
throw engineError; | ||
} | ||
} | ||
} | ||
}, 10000); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.