-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extra): adds
rulesToAST
that converts rules into @ucast AST
Relates to #350
- Loading branch information
Showing
6 changed files
with
121 additions
and
48 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 |
---|---|---|
|
@@ -76,6 +76,6 @@ | |
"extra" | ||
], | ||
"dependencies": { | ||
"@ucast/mongo2js": "^1.1.1" | ||
"@ucast/mongo2js": "^1.2.0" | ||
} | ||
} |
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,58 @@ | ||
import { defineAbility } from '../src' | ||
import { rulesToAST } from '../src/extra' | ||
|
||
describe('rulesToAST', () => { | ||
it('returns empty "and" `Condition` if there are no conditions in `Ability`', () => { | ||
const ability = defineAbility(can => can('read', 'Post')) | ||
const ast = rulesToAST(ability, 'read', 'Post') | ||
|
||
expect(ast.operator).to.equal('and') | ||
expect(ast.value).to.be.an('array').that.is.empty | ||
}) | ||
|
||
it('returns `null` if there is no ability to do an action', () => { | ||
const ability = defineAbility(can => can('read', 'Post')) | ||
const ast = rulesToAST(ability, 'update', 'Post') | ||
|
||
expect(ast).to.be.null | ||
}) | ||
|
||
it('returns only "or" `Condition` if there are no forbidden rules', () => { | ||
const ability = defineAbility((can) => { | ||
can('read', 'Post', { author: 1 }) | ||
can('read', 'Post', { private: false }) | ||
}) | ||
const ast = rulesToAST(ability, 'read', 'Post') | ||
|
||
expect(ast).to.deep.equal({ | ||
operator: 'or', | ||
value: [ | ||
{ operator: 'eq', field: 'private', value: false }, | ||
{ operator: 'eq', field: 'author', value: 1 }, | ||
] | ||
}) | ||
}) | ||
|
||
it('returns "and" condition that includes "or" if there are forbidden and regular rules', () => { | ||
const ability = defineAbility((can, cannot) => { | ||
can('read', 'Post', { author: 1 }) | ||
can('read', 'Post', { sharedWith: 1 }) | ||
cannot('read', 'Post', { private: true }) | ||
}) | ||
const ast = rulesToAST(ability, 'read', 'Post') | ||
|
||
expect(ast).to.deep.equal({ | ||
operator: 'and', | ||
value: [ | ||
{ operator: 'eq', field: 'private', value: true }, | ||
{ | ||
operator: 'or', | ||
value: [ | ||
{ operator: 'eq', field: 'sharedWith', value: 1 }, | ||
{ operator: 'eq', field: 'author', value: 1 }, | ||
] | ||
} | ||
] | ||
}) | ||
}) | ||
}) |
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.