2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const testUtils = require('../../../utils');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const models = require('../../../../core/server/models');
|
|
|
|
const actionsMap = require('../../../../core/server/services/permissions/actions-map-cache');
|
|
|
|
const permissions = require('../../../../core/server/services/permissions');
|
2017-09-25 12:17:06 +03:00
|
|
|
|
|
|
|
describe('Permissions', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let fakePermissions = [];
|
|
|
|
let findPostSpy;
|
|
|
|
let findTagSpy;
|
2017-09-25 12:17:06 +03:00
|
|
|
|
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Permission, 'findAll').callsFake(function () {
|
2017-09-25 12:17:06 +03:00
|
|
|
return Promise.resolve(models.Permissions.forge(fakePermissions));
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
findPostSpy = sinon.stub(models.Post, 'findOne').callsFake(function () {
|
2017-09-25 12:17:06 +03:00
|
|
|
return Promise.resolve(models.Post.forge(testUtils.DataGenerator.Content.posts[0]));
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
findTagSpy = sinon.stub(models.Tag, 'findOne').callsFake(function () {
|
2017-09-25 12:17:06 +03:00
|
|
|
return Promise.resolve({});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2017-09-25 12:17:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default test actionMap looks like this:
|
|
|
|
* {
|
|
|
|
* browse: [ 'post' ],
|
|
|
|
* edit: [ 'post', 'tag', 'user', 'page' ],
|
|
|
|
* add: [ 'post', 'user', 'page' ],
|
|
|
|
* destroy: [ 'post', 'user' ]
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @param {object} options
|
|
|
|
* @return {Array|*}
|
|
|
|
*/
|
|
|
|
function loadFakePermissions(options) {
|
|
|
|
options = options || {};
|
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
const fixturePermissions = _.cloneDeep(testUtils.DataGenerator.Content.permissions);
|
|
|
|
|
|
|
|
const extraPerm = {
|
|
|
|
name: 'test',
|
|
|
|
action_type: 'edit',
|
|
|
|
object_type: 'post'
|
|
|
|
};
|
2017-09-25 12:17:06 +03:00
|
|
|
|
|
|
|
if (options.extra) {
|
|
|
|
fixturePermissions.push(extraPerm);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _.map(fixturePermissions, function (testPerm) {
|
|
|
|
return testUtils.DataGenerator.forKnex.createPermission(testPerm);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('No init (no action map)', function () {
|
2017-10-05 22:01:34 +03:00
|
|
|
it('throws an error without actionMap', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(actionsMap, 'empty').returns(true);
|
2017-10-05 22:01:34 +03:00
|
|
|
|
2017-09-25 12:17:06 +03:00
|
|
|
permissions.canThis.should.throw(/No actions map found/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Init (build actions map)', function () {
|
|
|
|
it('can load an actions map from existing permissions', function (done) {
|
|
|
|
fakePermissions = loadFakePermissions();
|
|
|
|
|
|
|
|
permissions.init().then(function (actionsMap) {
|
|
|
|
should.exist(actionsMap);
|
|
|
|
|
2017-10-05 22:01:34 +03:00
|
|
|
permissions.canThis.should.not.throwError();
|
|
|
|
|
2017-09-25 12:17:06 +03:00
|
|
|
_.keys(actionsMap).should.eql(['browse', 'edit', 'add', 'destroy']);
|
|
|
|
|
|
|
|
actionsMap.browse.should.eql(['post']);
|
|
|
|
actionsMap.edit.should.eql(['post', 'tag', 'user', 'page']);
|
|
|
|
actionsMap.add.should.eql(['post', 'user', 'page']);
|
|
|
|
actionsMap.destroy.should.eql(['post', 'user']);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can load an actions map from existing permissions, and deduplicate', function (done) {
|
|
|
|
fakePermissions = loadFakePermissions({extra: true});
|
|
|
|
|
|
|
|
permissions.init().then(function (actionsMap) {
|
|
|
|
should.exist(actionsMap);
|
|
|
|
|
2017-10-05 22:01:34 +03:00
|
|
|
permissions.canThis.should.not.throwError();
|
|
|
|
|
2017-09-25 12:17:06 +03:00
|
|
|
_.keys(actionsMap).should.eql(['browse', 'edit', 'add', 'destroy']);
|
|
|
|
|
|
|
|
actionsMap.browse.should.eql(['post']);
|
|
|
|
actionsMap.edit.should.eql(['post', 'tag', 'user', 'page']);
|
|
|
|
actionsMap.add.should.eql(['post', 'user', 'page']);
|
|
|
|
actionsMap.destroy.should.eql(['post', 'user']);
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).catch(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|