2018-04-25 12:56:45 +03:00
|
|
|
const should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
2018-10-06 23:13:52 +03:00
|
|
|
Promise = require('bluebird'),
|
2018-04-25 12:56:45 +03:00
|
|
|
common = require('../../../server/lib/common'),
|
|
|
|
models = require('../../../server/models'),
|
|
|
|
settingsCache = require('../../../server/services/settings/cache'),
|
|
|
|
testUtils = require('../../utils'),
|
|
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
|
|
|
|
describe('Unit: models/invite', function () {
|
|
|
|
before(function () {
|
|
|
|
models.init();
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2018-04-25 12:56:45 +03:00
|
|
|
sandbox.stub(settingsCache, 'get').withArgs('db_hash').returns('12345678');
|
|
|
|
});
|
|
|
|
|
2018-10-06 23:13:52 +03:00
|
|
|
afterEach(function () {
|
2018-04-25 12:56:45 +03:00
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2018-06-12 21:26:16 +03:00
|
|
|
before(testUtils.teardown);
|
2018-04-25 12:56:45 +03:00
|
|
|
|
2018-06-12 21:26:16 +03:00
|
|
|
describe('add', function () {
|
|
|
|
beforeEach(testUtils.setup('roles'));
|
|
|
|
afterEach(testUtils.teardown);
|
2018-04-25 12:56:45 +03:00
|
|
|
|
|
|
|
it('default', function () {
|
|
|
|
return models.Invite.add({email: 'invited@test.org', role_id: testUtils.DataGenerator.forKnex.roles[1].id})
|
|
|
|
.then(function (invite) {
|
|
|
|
invite.get('status').should.eql('pending');
|
|
|
|
invite.get('email').should.eql('invited@test.org');
|
|
|
|
should.exist(invite.get('token'));
|
|
|
|
should.exist(invite.get('expires'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set status with none internal context', function () {
|
|
|
|
return models.Invite.add({
|
|
|
|
email: 'invited@test.org',
|
|
|
|
role_id: testUtils.DataGenerator.forKnex.roles[1].id,
|
|
|
|
status: 'sent'
|
|
|
|
}).then(function (invite) {
|
|
|
|
invite.get('status').should.eql('pending');
|
|
|
|
invite.get('email').should.eql('invited@test.org');
|
|
|
|
should.exist(invite.get('token'));
|
|
|
|
should.exist(invite.get('expires'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set status with internal context', function () {
|
|
|
|
return models.Invite.add({
|
|
|
|
email: 'invited@test.org',
|
|
|
|
role_id: testUtils.DataGenerator.forKnex.roles[1].id,
|
|
|
|
status: 'sent'
|
|
|
|
}, testUtils.context.internal).then(function (invite) {
|
|
|
|
invite.get('status').should.eql('sent');
|
|
|
|
invite.get('email').should.eql('invited@test.org');
|
|
|
|
should.exist(invite.get('token'));
|
|
|
|
should.exist(invite.get('expires'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('[error] no role passed', function () {
|
|
|
|
return models.Invite.add({email: 'invited@test.org'})
|
|
|
|
.then(function () {
|
|
|
|
'Should fail'.should.be.true();
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
(err[0] instanceof common.errors.ValidationError).should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
describe('permissible', function () {
|
|
|
|
describe('action: add', function () {
|
|
|
|
let inviteModel;
|
|
|
|
let context;
|
|
|
|
let unsafeAttrs;
|
|
|
|
let roleModel;
|
|
|
|
let loadedPermissions;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
inviteModel = {};
|
|
|
|
context = {};
|
|
|
|
unsafeAttrs = {role_id: 'role_id'};
|
|
|
|
roleModel = sandbox.stub();
|
|
|
|
roleModel.get = sandbox.stub();
|
|
|
|
loadedPermissions = {
|
|
|
|
user: {
|
|
|
|
roles: []
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('role does not exist', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(null);
|
|
|
|
|
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs)
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NotFoundError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite owner', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Owner');
|
|
|
|
|
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs)
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as owner', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
loadedPermissions.user.roles = [{name: 'Owner'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite administrator', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Administrator');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite editor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Editor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite author', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Author');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite contributor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Contributor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as administrator', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
loadedPermissions.user.roles = [{name: 'Administrator'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite administrator', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Administrator');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite editor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Editor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite author', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Author');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite contributor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Contributor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as editor', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
loadedPermissions.user.roles = [{name: 'Editor'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite administrator', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Administrator');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite editor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Editor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite author', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Author');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('invite contributor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Contributor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, true, true);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as author', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
loadedPermissions.user.roles = [{name: 'Author'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite administrator', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Administrator');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite editor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Editor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite author', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Author');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite contributor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Contributor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('as contributor', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
loadedPermissions.user.roles = [{name: 'Contributor'}];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite administrator', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Administrator');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite editor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Editor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite author', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Author');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('invite contributor', function () {
|
|
|
|
sandbox.stub(models.Role, 'findOne').withArgs({id: 'role_id'}).resolves(roleModel);
|
|
|
|
roleModel.get.withArgs('name').returns('Contributor');
|
|
|
|
|
2018-10-16 19:00:05 +03:00
|
|
|
return models.Invite.permissible(inviteModel, 'add', context, unsafeAttrs, loadedPermissions, false, false)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
|
|
|
(err instanceof common.errors.NoPermissionError).should.eql(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-04-25 12:56:45 +03:00
|
|
|
});
|