2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const Promise = require('bluebird');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-04-29 18:44:27 +03:00
|
|
|
const models = require('../../../core/server/models');
|
|
|
|
const permissions = require('../../../core/server/services/permissions');
|
2021-06-15 13:45:37 +03:00
|
|
|
const schema = require('../../../core/server/data/schema');
|
2020-08-11 16:01:16 +03:00
|
|
|
const security = require('@tryghost/security');
|
2020-04-29 18:44:27 +03:00
|
|
|
const testUtils = require('../../utils');
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2018-02-16 02:49:15 +03:00
|
|
|
describe('Unit: models/user', function () {
|
2018-02-07 12:46:22 +03:00
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2018-02-15 22:15:43 +03:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-02-15 22:15:43 +03:00
|
|
|
});
|
|
|
|
|
2018-11-13 14:27:10 +03:00
|
|
|
describe('updateLastSeen method', function () {
|
|
|
|
it('exists', function () {
|
|
|
|
should.equal(typeof models.User.prototype.updateLastSeen, 'function');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets the last_seen property to new Date and returns a call to save', function () {
|
|
|
|
const instance = {
|
2019-01-21 19:53:44 +03:00
|
|
|
set: sinon.spy(),
|
|
|
|
save: sinon.stub().resolves()
|
2018-11-13 14:27:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const clock = sinon.useFakeTimers(now.getTime());
|
|
|
|
|
|
|
|
const returnVal = models.User.prototype.updateLastSeen.call(instance);
|
|
|
|
|
|
|
|
should.deepEqual(instance.set.args[0][0], {
|
|
|
|
last_seen: now
|
|
|
|
});
|
|
|
|
|
|
|
|
should.equal(returnVal, instance.save.returnValues[0]);
|
|
|
|
|
|
|
|
clock.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-15 22:15:43 +03:00
|
|
|
describe('validation', function () {
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(security.password, 'hash').resolves('$2a$10$we16f8rpbrFZ34xWj0/ZC.LTPUux8ler7bcdTs5qIleN6srRHhilG');
|
2018-02-15 22:15:43 +03:00
|
|
|
});
|
|
|
|
|
2018-02-16 02:49:15 +03:00
|
|
|
describe('blank', function () {
|
|
|
|
it('name cannot be blank', function () {
|
|
|
|
return models.User.add({email: 'test@ghost.org'})
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('expected ValidationError');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
2020-05-22 21:22:20 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2018-02-16 02:49:15 +03:00
|
|
|
err.message.should.match(/users\.name/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('email cannot be blank', function () {
|
|
|
|
let data = {name: 'name'};
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.User, 'findOne').resolves(null);
|
2018-02-16 02:49:15 +03:00
|
|
|
|
|
|
|
return models.User.add(data)
|
|
|
|
.then(function () {
|
|
|
|
throw new Error('expected ValidationError');
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
err.should.be.an.Array();
|
2020-05-22 21:22:20 +03:00
|
|
|
(err[0] instanceof errors.ValidationError).should.eql(true);
|
2018-02-16 02:49:15 +03:00
|
|
|
err[0].message.should.match(/users\.email/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fn: check', function () {
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(security.password, 'hash').resolves('$2a$10$we16f8rpbrFZ34xWj0/ZC.LTPUux8ler7bcdTs5qIleN6srRHhilG');
|
2018-02-16 02:49:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('user status is warn', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(security.password, 'compare').resolves(true);
|
2018-02-16 02:49:15 +03:00
|
|
|
|
|
|
|
// NOTE: Add a user with a broken field to ensure we only validate changed fields on login
|
2021-06-15 13:45:37 +03:00
|
|
|
sinon.stub(schema, 'validate').resolves();
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
const user = models.User.forge(testUtils.DataGenerator.forKnex.createUser({
|
2018-02-16 02:49:15 +03:00
|
|
|
status: 'warn-1',
|
|
|
|
email: 'test-9@example.de',
|
|
|
|
website: '!!!!!this-is-not-a-website!!!!'
|
2019-01-22 15:36:30 +03:00
|
|
|
}));
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
sinon.stub(models.User, 'getByEmail').resolves(user);
|
|
|
|
sinon.stub(models.User, 'isPasswordCorrect').resolves();
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
sinon.stub(user, 'updateLastSeen').resolves();
|
|
|
|
sinon.stub(user, 'save').resolves();
|
|
|
|
|
|
|
|
return models.User.check({email: user.get('email'), password: 'test'});
|
2018-02-16 02:49:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('user status is active', function () {
|
2019-01-22 15:36:30 +03:00
|
|
|
const user = models.User.forge(testUtils.DataGenerator.forKnex.createUser({
|
|
|
|
status: 'active',
|
|
|
|
email: 'test@ghost.de'
|
|
|
|
}));
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
sinon.stub(models.User, 'getByEmail').resolves(user);
|
|
|
|
sinon.stub(models.User, 'isPasswordCorrect').resolves();
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
sinon.stub(user, 'updateLastSeen').resolves();
|
|
|
|
sinon.stub(user, 'save').resolves();
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
return models.User.check({email: user.get('email'), password: 'test'});
|
2018-02-16 02:49:15 +03:00
|
|
|
});
|
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
it('password is incorrect', function () {
|
|
|
|
const user = models.User.forge(testUtils.DataGenerator.forKnex.createUser({
|
|
|
|
status: 'active',
|
|
|
|
email: 'test@ghost.de'
|
|
|
|
}));
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
sinon.stub(models.User, 'getByEmail').resolves(user);
|
2020-05-22 21:22:20 +03:00
|
|
|
sinon.stub(models.User, 'isPasswordCorrect').rejects(new errors.ValidationError());
|
2018-02-16 02:49:15 +03:00
|
|
|
|
2019-01-22 15:36:30 +03:00
|
|
|
return models.User.check({email: user.get('email'), password: 'test'})
|
2018-02-16 02:49:15 +03:00
|
|
|
.catch(function (err) {
|
2020-05-22 21:22:20 +03:00
|
|
|
(err instanceof errors.ValidationError).should.eql(true);
|
2018-02-16 02:49:15 +03:00
|
|
|
});
|
|
|
|
});
|
2020-05-05 21:37:53 +03:00
|
|
|
|
|
|
|
it('status is locked', function () {
|
|
|
|
const user = models.User.forge(testUtils.DataGenerator.forKnex.createUser({
|
|
|
|
status: 'locked',
|
|
|
|
email: 'test@ghost.de'
|
|
|
|
}));
|
|
|
|
|
|
|
|
sinon.stub(models.User, 'getByEmail').resolves(user);
|
|
|
|
|
|
|
|
return models.User.check({email: user.get('email'), password: 'test'})
|
|
|
|
.catch(function (err) {
|
2020-05-26 21:10:29 +03:00
|
|
|
(err instanceof errors.PasswordResetRequiredError).should.eql(true);
|
2020-05-05 21:37:53 +03:00
|
|
|
});
|
|
|
|
});
|
2018-02-15 22:15:43 +03:00
|
|
|
});
|
|
|
|
|
2018-10-06 23:13:52 +03:00
|
|
|
describe('permissible', function () {
|
|
|
|
function getUserModel(id, role, roleId) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const hasRole = sinon.stub();
|
2018-02-07 12:46:22 +03:00
|
|
|
|
|
|
|
hasRole.withArgs(role).returns(true);
|
|
|
|
|
|
|
|
return {
|
2018-10-06 23:13:52 +03:00
|
|
|
id: id,
|
2018-02-07 12:46:22 +03:00
|
|
|
hasRole: hasRole,
|
2019-01-21 19:53:44 +03:00
|
|
|
related: sinon.stub().returns([{name: role, id: roleId}]),
|
|
|
|
get: sinon.stub().returns(id)
|
2018-02-07 12:46:22 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('cannot delete owner', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(1, 'Owner');
|
|
|
|
const context = {user: 1};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
models.User.permissible(mockUser, 'destroy', context, {}, testUtils.permissions.owner, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
done(new Error('Permissible function should have errored'));
|
|
|
|
}).catch((error) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
error.should.be.an.instanceof(errors.NoPermissionError);
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.calledOnce).be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can always edit self', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Contributor');
|
|
|
|
const context = {user: 3};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.contributor, false, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-06 23:13:52 +03:00
|
|
|
it('cannot edit my status to inactive', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Editor');
|
|
|
|
const context = {user: 3};
|
2018-10-06 23:13:52 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, {status: 'inactive'}, testUtils.permissions.editor, false, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.NoPermissionError);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('without related roles', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.User, 'findOne').withArgs({
|
2018-10-06 23:13:52 +03:00
|
|
|
id: 3,
|
|
|
|
status: 'all'
|
|
|
|
}, {withRelated: ['roles']}).resolves(getUserModel(3, 'Contributor'));
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
const mockUser = {id: 3, related: sinon.stub().returns()};
|
2018-10-06 23:13:52 +03:00
|
|
|
const context = {user: 3};
|
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.contributor, false, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(() => {
|
|
|
|
models.User.findOne.calledOnce.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('change role', function () {
|
|
|
|
function getUserToEdit(id, role) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const hasRole = sinon.stub();
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
hasRole.withArgs(role).returns(true);
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: id,
|
|
|
|
hasRole: hasRole,
|
2019-01-21 19:53:44 +03:00
|
|
|
related: sinon.stub().returns([role]),
|
|
|
|
get: sinon.stub().returns(id)
|
2018-10-06 23:13:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.User, 'getOwnerUser');
|
|
|
|
sinon.stub(permissions, 'canThis');
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
models.User.getOwnerUser.resolves({
|
|
|
|
id: testUtils.context.owner.context.user,
|
|
|
|
related: () => {
|
|
|
|
return {
|
|
|
|
at: () => {
|
|
|
|
return testUtils.permissions.owner.user.roles[0].id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cannot change own role', function () {
|
|
|
|
const mockUser = getUserToEdit(testUtils.context.admin.context.user, testUtils.permissions.editor.user.roles[0]);
|
|
|
|
const context = testUtils.context.admin.context;
|
|
|
|
const unsafeAttrs = testUtils.permissions.editor.user;
|
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, unsafeAttrs, testUtils.permissions.admin, false, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.NoPermissionError);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('is owner and does not change the role', function () {
|
|
|
|
const mockUser = getUserToEdit(testUtils.context.owner.context.user, testUtils.permissions.owner.user.roles[0]);
|
|
|
|
const context = testUtils.context.owner.context;
|
|
|
|
const unsafeAttrs = testUtils.permissions.owner.user;
|
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, unsafeAttrs, testUtils.permissions.owner, false, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(() => {
|
|
|
|
models.User.getOwnerUser.calledOnce.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('cannot change owner\'s role', function () {
|
|
|
|
const mockUser = getUserToEdit(testUtils.context.owner.context.user, testUtils.permissions.owner.user.roles[0]);
|
|
|
|
const context = testUtils.context.admin.context;
|
|
|
|
const unsafeAttrs = testUtils.permissions.editor.user;
|
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, unsafeAttrs, testUtils.permissions.admin, false, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.NoPermissionError);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('admin can change author role', function () {
|
|
|
|
const mockUser = getUserToEdit(testUtils.context.author.context.user, testUtils.permissions.author.user.roles[0]);
|
|
|
|
const context = testUtils.context.admin.context;
|
|
|
|
const unsafeAttrs = testUtils.permissions.editor.user;
|
|
|
|
|
|
|
|
permissions.canThis.returns({
|
|
|
|
assign: {
|
2019-01-21 19:53:44 +03:00
|
|
|
role: sinon.stub().resolves()
|
2018-10-06 23:13:52 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, unsafeAttrs, testUtils.permissions.admin, true, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(() => {
|
|
|
|
models.User.getOwnerUser.calledOnce.should.be.true();
|
|
|
|
permissions.canThis.calledOnce.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('author can\'t change admin role', function () {
|
|
|
|
const mockUser = getUserToEdit(testUtils.context.admin.context.user, testUtils.permissions.admin.user.roles[0]);
|
|
|
|
const context = testUtils.context.author.context;
|
|
|
|
const unsafeAttrs = testUtils.permissions.editor.user;
|
|
|
|
|
|
|
|
permissions.canThis.returns({
|
|
|
|
assign: {
|
2019-01-21 19:53:44 +03:00
|
|
|
role: sinon.stub().resolves()
|
2018-10-06 23:13:52 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, unsafeAttrs, testUtils.permissions.author, false, true, true)
|
2018-10-06 23:13:52 +03:00
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.NoPermissionError);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-07 12:46:22 +03:00
|
|
|
describe('as editor', function () {
|
|
|
|
it('can\'t edit another editor', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Editor');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
done(new Error('Permissible function should have errored'));
|
|
|
|
}).catch((error) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
error.should.be.an.instanceof(errors.NoPermissionError);
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-06 23:13:52 +03:00
|
|
|
it('can\'t edit owner', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Owner');
|
|
|
|
const context = {user: 2};
|
2018-10-06 23:13:52 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-10-06 23:13:52 +03:00
|
|
|
done(new Error('Permissible function should have errored'));
|
|
|
|
}).catch((error) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
error.should.be.an.instanceof(errors.NoPermissionError);
|
2018-10-06 23:13:52 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-02-07 12:46:22 +03:00
|
|
|
it('can\'t edit an admin', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Administrator');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
done(new Error('Permissible function should have errored'));
|
|
|
|
}).catch((error) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
error.should.be.an.instanceof(errors.NoPermissionError);
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can edit author', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Author');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can edit contributor', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Contributor');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'edit', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can destroy self', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Editor');
|
|
|
|
const context = {user: 3};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'destroy', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can\'t destroy another editor', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Editor');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
models.User.permissible(mockUser, 'destroy', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
done(new Error('Permissible function should have errored'));
|
|
|
|
}).catch((error) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
error.should.be.an.instanceof(errors.NoPermissionError);
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can\'t destroy an admin', function (done) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Administrator');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
models.User.permissible(mockUser, 'destroy', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
done(new Error('Permissible function should have errored'));
|
|
|
|
}).catch((error) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
error.should.be.an.instanceof(errors.NoPermissionError);
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can destroy an author', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Author');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'destroy', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can destroy a contributor', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
const mockUser = getUserModel(3, 'Contributor');
|
|
|
|
const context = {user: 2};
|
2018-02-07 12:46:22 +03:00
|
|
|
|
2019-01-18 15:39:53 +03:00
|
|
|
return models.User.permissible(mockUser, 'destroy', context, {}, testUtils.permissions.editor, true, true, true).then(() => {
|
2018-02-07 12:46:22 +03:00
|
|
|
should(mockUser.hasRole.called).be.true();
|
|
|
|
should(mockUser.get.calledOnce).be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-03-05 11:10:27 +03:00
|
|
|
|
2018-10-06 23:13:52 +03:00
|
|
|
describe('transferOwnership', function () {
|
|
|
|
beforeEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.Role, 'findOne');
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
models.Role.findOne
|
|
|
|
.withArgs({name: 'Owner'})
|
|
|
|
.resolves(testUtils.permissions.owner.user.roles[0]);
|
|
|
|
|
|
|
|
models.Role.findOne
|
|
|
|
.withArgs({name: 'Administrator'})
|
|
|
|
.resolves(testUtils.permissions.admin.user.roles[0]);
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.User, 'findOne');
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Cannot transfer ownership if not owner', function () {
|
|
|
|
const loggedInUser = testUtils.context.admin;
|
|
|
|
const userToChange = loggedInUser;
|
2019-01-21 19:53:44 +03:00
|
|
|
const contextUser = sinon.stub();
|
2018-10-06 23:13:52 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
contextUser.toJSON = sinon.stub().returns(testUtils.permissions.admin.user);
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
models.User
|
|
|
|
.findOne
|
|
|
|
.withArgs({id: loggedInUser.context.user}, {withRelated: ['roles']})
|
|
|
|
.resolves(contextUser);
|
|
|
|
|
|
|
|
return models.User.transferOwnership({id: loggedInUser.context.user}, loggedInUser)
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.NoPermissionError);
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-04-16 12:30:29 +03:00
|
|
|
it('Owner tries to transfer ownership to editor', function () {
|
2018-10-06 23:13:52 +03:00
|
|
|
const loggedInUser = testUtils.context.owner;
|
|
|
|
const userToChange = testUtils.context.editor;
|
|
|
|
|
2019-04-16 12:30:29 +03:00
|
|
|
const loggedInContext = {
|
|
|
|
toJSON: sinon.stub().returns(testUtils.permissions.owner.user)
|
|
|
|
};
|
|
|
|
const userToChangeContext = {
|
|
|
|
toJSON: sinon.stub().returns(
|
|
|
|
// Test utils don't contain `status` which is required
|
|
|
|
Object.assign({status: 'active'}, testUtils.permissions.editor.user)
|
|
|
|
)
|
|
|
|
};
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
models.User
|
|
|
|
.findOne
|
|
|
|
.withArgs({id: loggedInUser.context.user}, {withRelated: ['roles']})
|
2019-04-16 12:30:29 +03:00
|
|
|
.resolves(loggedInContext);
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
models.User
|
|
|
|
.findOne
|
|
|
|
.withArgs({id: userToChange.context.user}, {withRelated: ['roles']})
|
2019-04-16 12:30:29 +03:00
|
|
|
.resolves(userToChangeContext);
|
|
|
|
|
|
|
|
return models.User.transferOwnership({id: userToChange.context.user}, loggedInUser)
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.ValidationError);
|
2019-04-16 12:30:29 +03:00
|
|
|
err.message.indexOf('Only administrators can')
|
|
|
|
.should.be.aboveOrEqual(0, 'contains correct error message');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Owner tries to transfer ownership to suspended user', function () {
|
|
|
|
const loggedInUser = testUtils.context.owner;
|
|
|
|
const userToChange = testUtils.context.admin;
|
|
|
|
|
|
|
|
const userToChangeJSON = Object.assign({status: 'inactive'}, testUtils.permissions.admin.user);
|
|
|
|
const loggedInContext = {
|
|
|
|
toJSON: sinon.stub().returns(testUtils.permissions.owner.user)
|
|
|
|
};
|
|
|
|
const userToChangeContext = {
|
|
|
|
toJSON: sinon.stub().returns(userToChangeJSON)
|
|
|
|
};
|
|
|
|
|
|
|
|
models.User
|
|
|
|
.findOne
|
|
|
|
.withArgs({id: loggedInUser.context.user}, {withRelated: ['roles']})
|
|
|
|
.resolves(loggedInContext);
|
|
|
|
|
|
|
|
models.User
|
|
|
|
.findOne
|
|
|
|
.withArgs({id: userToChange.context.user}, {withRelated: ['roles']})
|
|
|
|
.resolves(userToChangeContext);
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
return models.User.transferOwnership({id: userToChange.context.user}, loggedInUser)
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-22 21:22:20 +03:00
|
|
|
err.should.be.an.instanceof(errors.ValidationError);
|
2019-04-16 12:30:29 +03:00
|
|
|
err.message.indexOf('Only active administrators can')
|
|
|
|
.should.be.aboveOrEqual(0, 'contains correct error message');
|
2018-10-06 23:13:52 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('isSetup', function () {
|
|
|
|
it('active', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.User, 'getOwnerUser').resolves({get: sinon.stub().returns('active')});
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
return models.User.isSetup()
|
|
|
|
.then((result) => {
|
|
|
|
result.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('inactive', function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(models.User, 'getOwnerUser').resolves({get: sinon.stub().returns('inactive')});
|
2018-10-06 23:13:52 +03:00
|
|
|
|
|
|
|
return models.User.isSetup()
|
|
|
|
.then((result) => {
|
|
|
|
result.should.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-02-07 12:46:22 +03:00
|
|
|
});
|