UNPKG

@superawesome/permissions

Version:

Fine grained permissions / access control with ownerships & attribute picking, done right.

138 lines 5.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _ = require("lodash"); const types_1 = require("../types"); const utils_1 = require("../utils"); describe('utils tests', () => { describe('projectPDWithDefaultsToInternal', () => { it(`projects without defaults`, () => { const isOwner = () => { }; const listOwned = () => { }; expect(utils_1.projectPDWithDefaultsToInternal(null, { roles: 'EMPLOYEE', resource: 'document', descr: `descr`, isOwner, listOwned, possession: types_1.EPossession.any, attributes: ['*', '!price', '!confidential'], grant: ['create', 'read', 'update:own', 'delete:own'], })).toEqual({ roles: ['EMPLOYEE'], resource: 'document', descr: `descr`, isOwner, listOwned, grant: { // these inherit their possession 'any' from PD.possession 'create:any': ['*', '!price', '!confidential'], 'read:any': ['*', '!price', '!confidential'], // these retain their possession 'own' 'update:own': ['*', '!price', '!confidential'], 'delete:own': ['*', '!price', '!confidential'], }, }); }); it(`projects with defaults`, () => { const isOwner = () => { }; const listOwned = () => { }; expect(utils_1.projectPDWithDefaultsToInternal({ roles: 'ADMIN', possession: types_1.EPossession.any, resource: 'document', }, { descr: `descr`, isOwner, attributes: ['*', '!price', '!confidential'], grant: ['create', 'read', 'update:own', 'delete:own'], })).toEqual({ roles: ['ADMIN'], resource: 'document', descr: `descr`, isOwner, grant: { // these inherit their possession 'any' from PD.possession 'create:any': ['*', '!price', '!confidential'], 'read:any': ['*', '!price', '!confidential'], // these retain their possession 'own' 'update:own': ['*', '!price', '!confidential'], 'delete:own': ['*', '!price', '!confidential'], }, }); }); }); describe('isArraySetEqual', () => { const refComparator = (a, b) => a === b; _.each([ [[1, 2, 3], [3, 2, 1], true], [[1, 2, 3, 4], [3, 2, 1], false], [[1, 2, 3], [4, 3, 2, 1], false], ], ([a, b, expected]) => it('should Compare Arrays By Reference With Default Comparator', () => expect(utils_1.isArraySetEqual(a, b)).toEqual(expected))); _.each([ [[1, 2, 3], [3, 2, 1], true], [[1, 2, 3, 4], [3, 2, 1], false], [[1, 2, 3], [4, 3, 2, 1], false], ], ([a, b, expected]) => it('should Compare Arrays By Reference With Custom Comparator', () => expect(utils_1.isArraySetEqual(a, b, refComparator)).toEqual(expected))); _.each([ [[{ a: 1 }, { b: 2 }, { c: 3 }], [{ c: 3 }, { b: 2 }, { a: 1 }], false], [ [{ a: 1 }, { b: 2 }, { c: 3 }], [{ d: 4 }, { c: 3 }, { b: 2 }, { a: 1 }], false, _.isEqual, ], [ [{ a: 1 }, { b: 'bbb' }, { c: { d: 4 } }], [{ c: { d: 4 } }, { b: 'bbb' }, { a: 1 }], true, _.isEqual, ], ], ([a, b, expected, comparator]) => it('should Compare Arrays By Value With Custom Comparator', () => expect(utils_1.isArraySetEqual(a, b, comparator)).toEqual(expected))); }); describe('isLike', () => { it.each([ [{}, {}], [1, 1], ['aa', 'aa'], [{ a: 1 }, { a: 1, b: 'doesnt matter' }], [{ a: { a2: 2 } }, { a: { a2: 2, b2: 'doesnt matter' }, b: 'doesnt matter' }], [null, null], [undefined, undefined], [ [1, 2, 3], [1, 2, 3], ], ])('returns true for values that o1 is like o2, %s %s', (o1, o2) => expect(utils_1.isLike(o1, o2)).toBe(true)); it.each([ [{ a: 1 }, { a: 11, b: 2 }], [{ a: { a2: 2, b2: 333 } }, { a: { a2: 2, b2: 'does matter' }, b: 'doesnt matter' }], [1, 2], ['aa', 'bb'], [null, undefined], [ [1, 2, 3], [1, 2, 3, 4], ], ])('returns false for values that o1 is not like o2, %s %s', (o1, o2) => expect(utils_1.isLike(o1, o2)).toBe(false)); }); describe('deleteEmptyArrayKeys', () => { it.each([ [{}, {}], [ { a: 'val', emptyArray: [], b: { some: 'value', emptyArrayNested: [] } }, { a: 'val', b: { some: 'value' } }, ], [1, 1], [[], []], [ [1, 2, 3], [1, 2, 3], ], ])('deletes all keys with an empty array as value %s', (o, expected) => { const actual = utils_1.deleteEmptyArrayKeys(o); expect(actual).toBe(o); expect(actual).toEqual(expected); }); }); }); //# sourceMappingURL=utils.spec.js.map