Skip to content

Commit

Permalink
Test/multi profile w custom val (#7)
Browse files Browse the repository at this point in the history
* Adapt perm to redefinition of hierarchy of user roles

* Add tests for multi customVal
aka AND rule between customVal
Add rule for consistency of the config
  • Loading branch information
Wardormeur authored and DanielBrierton committed Jan 23, 2017
1 parent c4b0fd4 commit 1fb15d3
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
1 change: 0 additions & 1 deletion lib/check_permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ function checkPermissions (args, cb) {
userTypes = [userTypes];
}
var userTypeDepth = getHighestTreeMatch(permissions.userTypeHierarchy, _.toArray(userTypes), maxUserTypeDepth);

if (profileDepth >= userTypeDepth.value && userTypeDepth.value < maxUserTypeDepth) {
allowed = allowed && true;
httpErr = null;
Expand Down
63 changes: 61 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('cp-perms', function () {
var actForAdult = {role: 'cp-test', cmd: 'acting_as_adult'};
var actForPro = {role: 'cp-test', cmd: 'acting_as_pro'};
var actForSchyzo = {role: 'cp-test', cmd: 'acting_as_schyzo'};
var actForCrazy = {role: 'cp-test', cmd: 'acting_as_crazy'};

var expectedResult = {'acting': 'as_normal'};
var customValHandler = function (args, done) {
Expand Down Expand Up @@ -94,8 +95,9 @@ describe('cp-perms', function () {

it('should allow a list of profiles', function (done) {
var basicUser = function (next) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForSchyzo.cmd, user: {roles: ['basic-user'], initUserType: ['parent']}, toTest: 'imabanana'}, function (err, allowance) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForSchyzo.cmd, user: {roles: ['basic-user'], initUserType: ['parent-guardian']}, toTest: 'imabanana'}, function (err, allowance) {
if (err) return done(err);
expect(spied.calledOnce).to.be.true;
expect(allowance).to.be.deep.equal({allowed: true}).and.to.satisfy(isValidFormat);
next();
});
Expand All @@ -118,6 +120,63 @@ describe('cp-perms', function () {

});

// In this scenario, we set a scenario where only the last customVal is valid in order to verify that every customVal is called
it('should allow a list of profiles with multiple customVals', function (done) {
// Setup spies
var customValHandler2 = function (args, done) {
var toTest = args.toTest2;
return done(null, {'allowed': toTest === 'sicksadworld'});
};
var spyHandler2 = spy(customValHandler2);
seneca.add({role: 'cp-test', cmd: 'customValSSW'}, spyHandler2);

var customValHandler3 = function (args, done) {
var toTest = args.toTest3;
return done(null, {'allowed': toTest === 'canihazcheezburger'});
};
var spyHandler3 = spy(customValHandler3);
seneca.add({role: 'cp-test', cmd: 'customValCIHCB'}, spyHandler3);

// Do the calls !
var basicUser = function (next) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForCrazy.cmd, user: {roles: ['basic-user'], initUserType: ['parent-guardian']},
toTest: 'imabanana', toTest2: 'sicksadworld'}, function (err, allowance) {
if (err) return done(err);
expect(spied.calledOnce).to.be.true;
expect(spyHandler2.calledOnce).to.be.true;
expect(allowance).to.be.deep.equal({allowed: true}).and.to.satisfy(isValidFormat);
next();
});
};
var cdfAdmin = function (next) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForCrazy.cmd, user: {roles: ['cdf-admin']}}, function (err, allowance) {
if (err) return done(err);
expect(allowance).to.be.deep.equal({allowed: true}).and.to.satisfy(isValidFormat);
next();
});
};
var mentor = function (next) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForCrazy.cmd, user: {roles: ['basic-user'], initUserType: ['mentor']}, toTest2: 'sicksadworld', toTest3: 'canihazcheezburger'}, function (err, allowance) {
if (err) return done(err);
expect(spyHandler2.callCount).to.be.equal(3); // basicUser 1st profile + this call basic-user 1st profile + this call last profile
expect(spyHandler3.callCount).to.be.equal(1);
expect(allowance).to.be.deep.equal({allowed: true}).and.to.satisfy(isValidFormat);
next();
});
};
async.waterfall([
basicUser,
cdfAdmin,
mentor
], done);

});

it('should not modify the config after all those calls', function (done) {
expect(conf).to.be.deep.equal(seneca.export('cd-permissions/config'));
done();
});

/***** ERROR HANDLING ***/

// TODO : test for missing conf
Expand All @@ -140,7 +199,7 @@ describe('cp-perms', function () {

// Since everything is casted to array, we can allow ourselves to simply send a sigle userType
it('should be allowing a string instead of an array with a proper usertype', function (done) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForAdult.cmd, user: {initUserType: 'parent'}}, function (err, allowance) {
seneca.act({role: 'cp-test', cmd: 'check_permissions', act: actForAdult.cmd, user: {initUserType: 'parent-guardian'}}, function (err, allowance) {
expect(allowance).to.satisfy(isValidFormat);
expect(allowance).to.be.deep.equal({allowed: true});
done();
Expand Down
31 changes: 28 additions & 3 deletions test/permissions-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = function () {
* }
*/

// TODO:50 ensure is_own_dojo for dojo-admin && belongs_to for champion

return {
'cp-test':{
Expand All @@ -21,7 +20,7 @@ module.exports = function () {
userType: 'mentor'
},
{
userType: 'parent'
userType: 'parent-guardian'
}],
'acting_as_pro': [{
role: 'basic-user',
Expand All @@ -32,7 +31,7 @@ module.exports = function () {
}],
'acting_as_schyzo': [{
role: 'basic-user',
userType: 'parent',
userType: 'parent-guardian',
customValidator: [{
role: 'cp-test',
cmd: 'customVal'
Expand All @@ -48,6 +47,32 @@ module.exports = function () {
role: 'cp-test',
cmd: 'customVal'
}]
}],
'acting_as_crazy': [{
role: 'basic-user',
userType: 'parent-guardian',
customValidator: [{
role: 'cp-test',
cmd: 'customVal'
}, {
role: 'cp-test',
cmd: 'customValSSW'
}
]
},
{
role: 'cdf-admin'
},
{
role: 'basic-user',
userType: 'mentor',
customValidator: [{
role: 'cp-test',
cmd: 'customValSSW'
}, {
role: 'cp-test',
cmd: 'customValCIHCB'
}]
}
]
}
Expand Down

0 comments on commit 1fb15d3

Please sign in to comment.