2013-09-06 20:07:25 +04:00
|
|
|
/*globals describe, before, beforeEach, afterEach, it*/
|
2013-11-07 17:26:47 +04:00
|
|
|
var testUtils = require('../utils'),
|
2013-06-25 15:43:15 +04:00
|
|
|
should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2013-09-06 20:07:25 +04:00
|
|
|
when = require('when'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require("lodash"),
|
2013-07-11 23:02:18 +04:00
|
|
|
errors = require('../../server/errorHandling'),
|
2013-09-06 20:07:25 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2013-07-11 23:02:18 +04:00
|
|
|
permissions = require('../../server/permissions'),
|
|
|
|
Models = require('../../server/models'),
|
2013-06-25 15:43:15 +04:00
|
|
|
UserProvider = Models.User,
|
|
|
|
PermissionsProvider = Models.Permission,
|
|
|
|
PostProvider = Models.Post;
|
|
|
|
|
2013-10-08 05:39:33 +04:00
|
|
|
describe('Permissions', function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
before(function (done) {
|
2013-11-24 18:29:36 +04:00
|
|
|
testUtils.clearData().then(function () {
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
beforeEach(function (done) {
|
2013-09-06 20:07:25 +04:00
|
|
|
testUtils.initData()
|
2013-11-24 18:29:36 +04:00
|
|
|
.then(testUtils.insertDefaultUser).then(function () {
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function (done) {
|
2013-09-06 20:07:25 +04:00
|
|
|
testUtils.clearData()
|
2013-08-25 00:51:58 +04:00
|
|
|
.then(function () {
|
|
|
|
done();
|
|
|
|
}, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-11-24 18:29:36 +04:00
|
|
|
after(function (done) {
|
|
|
|
testUtils.clearData().then(function () {
|
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
var testPerms = [
|
|
|
|
{ act: "edit", obj: "post" },
|
|
|
|
{ act: "edit", obj: "tag" },
|
|
|
|
{ act: "edit", obj: "user" },
|
|
|
|
{ act: "edit", obj: "page" },
|
|
|
|
{ act: "add", obj: "post" },
|
|
|
|
{ act: "add", obj: "user" },
|
|
|
|
{ act: "add", obj: "page" },
|
|
|
|
{ act: "remove", obj: "post" },
|
|
|
|
{ act: "remove", obj: "user" }
|
|
|
|
],
|
|
|
|
currTestPermId = 1,
|
|
|
|
// currTestUserId = 1,
|
2013-09-12 02:04:49 +04:00
|
|
|
// createTestUser = function (email) {
|
|
|
|
// if (!email) {
|
2013-06-25 15:43:15 +04:00
|
|
|
// currTestUserId += 1;
|
2013-09-12 02:04:49 +04:00
|
|
|
// email = "test" + currTestPermId + "@test.com";
|
2013-06-25 15:43:15 +04:00
|
|
|
// }
|
|
|
|
|
|
|
|
// var newUser = {
|
|
|
|
// id: currTestUserId,
|
2013-09-12 02:04:49 +04:00
|
|
|
// email: email,
|
2013-06-25 15:43:15 +04:00
|
|
|
// password: "testing123"
|
|
|
|
// };
|
|
|
|
|
|
|
|
// return UserProvider.add(newUser);
|
|
|
|
// },
|
|
|
|
createPermission = function (name, act, obj) {
|
|
|
|
if (!name) {
|
|
|
|
currTestPermId += 1;
|
|
|
|
name = "test" + currTestPermId;
|
|
|
|
}
|
|
|
|
|
|
|
|
var newPerm = {
|
|
|
|
name: name,
|
|
|
|
action_type: act,
|
|
|
|
object_type: obj
|
|
|
|
};
|
|
|
|
|
|
|
|
return PermissionsProvider.add(newPerm);
|
|
|
|
},
|
|
|
|
createTestPermissions = function () {
|
|
|
|
var createActions = _.map(testPerms, function (testPerm) {
|
|
|
|
return createPermission(null, testPerm.act, testPerm.obj);
|
2013-06-15 02:12:04 +04:00
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return when.all(createActions);
|
|
|
|
};
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can load an actions map from existing permissions', function (done) {
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
createTestPermissions()
|
|
|
|
.then(permissions.init)
|
|
|
|
.then(function (actionsMap) {
|
|
|
|
should.exist(actionsMap);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-09-27 15:52:31 +04:00
|
|
|
actionsMap.edit.sort().should.eql(['post', 'tag', 'user', 'page'].sort());
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
actionsMap.should.equal(permissions.actionsMap);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can add user to role', function (done) {
|
|
|
|
var existingUserRoles;
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
UserProvider.read({id: 1}, { withRelated: ['roles'] }).then(function (foundUser) {
|
|
|
|
var testRole = new Models.Role({
|
|
|
|
name: 'testrole1',
|
|
|
|
description: 'testrole1 description'
|
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(foundUser);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(foundUser.roles());
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
existingUserRoles = foundUser.related('roles').length;
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return testRole.save().then(function () {
|
|
|
|
return foundUser.roles().attach(testRole);
|
|
|
|
});
|
|
|
|
}).then(function () {
|
|
|
|
return UserProvider.read({id: 1}, { withRelated: ['roles'] });
|
|
|
|
}).then(function (updatedUser) {
|
|
|
|
should.exist(updatedUser);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
updatedUser.related('roles').length.should.equal(existingUserRoles + 1);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can add user permissions', function (done) {
|
|
|
|
Models.User.read({id: 1}, { withRelated: ['permissions']}).then(function (testUser) {
|
|
|
|
var testPermission = new Models.Permission({
|
|
|
|
name: "test edit posts",
|
|
|
|
action_type: 'edit',
|
|
|
|
object_type: 'post'
|
|
|
|
});
|
|
|
|
|
|
|
|
testUser.related('permissions').length.should.equal(0);
|
|
|
|
|
|
|
|
return testPermission.save().then(function () {
|
|
|
|
return testUser.permissions().attach(testPermission);
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function () {
|
|
|
|
return Models.User.read({id: 1}, { withRelated: ['permissions']});
|
|
|
|
}).then(function (updatedUser) {
|
|
|
|
should.exist(updatedUser);
|
|
|
|
|
|
|
|
updatedUser.related('permissions').length.should.equal(1);
|
|
|
|
|
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can add role permissions', function (done) {
|
|
|
|
var testRole = new Models.Role({
|
|
|
|
name: "test2",
|
|
|
|
description: "test2 description"
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
testRole.save()
|
|
|
|
.then(function () {
|
|
|
|
return testRole.load('permissions');
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
var rolePermission = new Models.Permission({
|
2013-06-05 07:47:11 +04:00
|
|
|
name: "test edit posts",
|
|
|
|
action_type: 'edit',
|
|
|
|
object_type: 'post'
|
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
testRole.related('permissions').length.should.equal(0);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return rolePermission.save().then(function () {
|
|
|
|
return testRole.permissions().attach(rolePermission);
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return Models.Role.read({id: testRole.id}, { withRelated: ['permissions']});
|
|
|
|
})
|
|
|
|
.then(function (updatedRole) {
|
|
|
|
should.exist(updatedRole);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
updatedRole.related('permissions').length.should.equal(1);
|
2013-06-05 07:47:11 +04:00
|
|
|
|
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('does not allow edit post without permission', function (done) {
|
|
|
|
var fakePage = {
|
|
|
|
id: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
createTestPermissions()
|
|
|
|
.then(permissions.init)
|
|
|
|
.then(function () {
|
|
|
|
return Models.User.read({id: 1});
|
|
|
|
})
|
|
|
|
.then(function (foundUser) {
|
|
|
|
var canThisResult = permissions.canThis(foundUser);
|
|
|
|
|
|
|
|
should.exist(canThisResult.edit);
|
|
|
|
should.exist(canThisResult.edit.post);
|
|
|
|
|
|
|
|
return canThisResult.edit.page(fakePage);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
errors.logError(new Error("Allowed edit post without permission"));
|
2013-07-11 16:50:31 +04:00
|
|
|
}, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('allows edit post with permission', function (done) {
|
|
|
|
var fakePost = {
|
|
|
|
id: "1"
|
|
|
|
};
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
createTestPermissions()
|
|
|
|
.then(permissions.init)
|
|
|
|
.then(function () {
|
|
|
|
return Models.User.read({id: 1});
|
|
|
|
})
|
|
|
|
.then(function (foundUser) {
|
|
|
|
var newPerm = new Models.Permission({
|
|
|
|
name: "test3 edit post",
|
|
|
|
action_type: "edit",
|
|
|
|
object_type: "post"
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return newPerm.save().then(function () {
|
|
|
|
return foundUser.permissions().attach(newPerm);
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
return Models.User.read({id: 1}, { withRelated: ['permissions']});
|
|
|
|
})
|
|
|
|
.then(function (updatedUser) {
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// TODO: Verify updatedUser.related('permissions') has the permission?
|
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
var canThisResult = permissions.canThis(updatedUser.id);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
should.exist(canThisResult.edit);
|
|
|
|
should.exist(canThisResult.edit.post);
|
|
|
|
|
|
|
|
return canThisResult.edit.post(fakePost);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can use permissable function on Model to allow something', function (done) {
|
|
|
|
var testUser,
|
|
|
|
permissableStub = sinon.stub(PostProvider, 'permissable', function () {
|
|
|
|
return when.resolve();
|
|
|
|
});
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// createTestUser()
|
|
|
|
UserProvider.browse()
|
|
|
|
.then(function (foundUser) {
|
|
|
|
testUser = foundUser.models[0];
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return permissions.canThis(testUser).edit.post(123);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
permissableStub.restore();
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
permissableStub.calledWith(123, testUser.id, 'edit').should.equal(true);
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.otherwise(function () {
|
|
|
|
permissableStub.restore();
|
|
|
|
errors.logError(new Error("Did not allow testUser"));
|
2013-07-11 16:50:31 +04:00
|
|
|
|
|
|
|
done();
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
|
|
|
});
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can use permissable function on Model to forbid something', function (done) {
|
|
|
|
var testUser,
|
|
|
|
permissableStub = sinon.stub(PostProvider, 'permissable', function () {
|
|
|
|
return when.reject();
|
|
|
|
});
|
2013-06-15 02:12:04 +04:00
|
|
|
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// createTestUser()
|
|
|
|
UserProvider.browse()
|
|
|
|
.then(function (foundUser) {
|
|
|
|
testUser = foundUser.models[0];
|
2013-06-09 03:39:24 +04:00
|
|
|
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return permissions.canThis(testUser).edit.post(123);
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
permissableStub.restore();
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
errors.logError(new Error("Allowed testUser to edit post"));
|
|
|
|
})
|
|
|
|
.otherwise(function () {
|
|
|
|
permissableStub.restore();
|
|
|
|
permissableStub.calledWith(123, testUser.id, 'edit').should.equal(true);
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
|
|
|
});
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|