modpacksio-common
Version:
Common code for Modpacks.io services
53 lines (47 loc) • 2.33 kB
JavaScript
require('chai').should();
describe('Permissions', () => {
const { hasPermission } = require('../').permission;
// Emptiness
it('should never return true when perm or perms is blank', () => {
hasPermission().should.be.false;
hasPermission([]).should.be.false;
hasPermission([], '').should.be.false;
hasPermission(null, 'a.b.c').should.be.false;
hasPermission([], 'a.b.c').should.be.false;
hasPermission(['x.y.z', '*'], '').should.be.false;
});
// Exact Matching
it('should return true when perms contains an exact match', () => {
hasPermission(['a.b.c'], 'a.b.c').should.be.true;
hasPermission(['x.y.z', 'a.b.c'], 'a.b.c').should.be.true;
hasPermission(['x.y.z', 'a.b.c', 'z.y.x'], 'a.b.c').should.be.true;
});
it('should never return true when perms does not contain a valid match', () => {
hasPermission(['x.y.z'], 'a.b.c').should.be.false;
hasPermission(['x.y.z', 'z.y.x'], 'a.b.c').should.be.false;
});
// Partial Matching
it('should return true when perm is a partial match to a value of perms', () => {
hasPermission(['a.b.c'], 'a.b').should.be.true;
hasPermission(['x.y.z', 'a.b.c'], 'a.b').should.be.true;
hasPermission(['x.y.z', 'a.b.c', 'z.y.x'], 'a.b').should.be.true;
});
it('should never return true when perms contains a partial match to perm', () => {
hasPermission(['a.b'], 'a.b.c').should.be.false;
hasPermission(['x.y.z', 'a.b'], 'a.b.c').should.be.false;
hasPermission(['x.y.z', 'a.b', 'z.y.x'], 'a.b.c').should.be.false;
});
// Wildcards
it('should always return true when perms contains a wildcard', () => {
hasPermission(['*'], 'a.b.c').should.be.true;
hasPermission(['x.y.z', '*'], 'a.b.c').should.be.true;
hasPermission(['x.y.z', '*', 'z.y.x'], 'a.b.c').should.be.true;
});
it('should return true when perms contains a partial wildcard', () => {
hasPermission(['a.*'], 'a.b.c').should.be.true;
hasPermission(['a.b.*'], 'a.b.c').should.be.true;
hasPermission(['a.b.c.*'], 'a.b.c').should.be.true;
hasPermission(['x.y.z', 'a.b.*'], 'a.b.c').should.be.true;
hasPermission(['x.y.z', 'a.b.*', 'z.y.x'], 'a.b.c').should.be.true;
});
});