2017-03-21 11:24:11 +03:00
|
|
|
var should = require('should'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
path = require('path'),
|
2014-02-02 10:07:39 +04:00
|
|
|
EventEmitter = require('events').EventEmitter,
|
2017-03-21 11:24:11 +03:00
|
|
|
_ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
2017-04-04 19:07:35 +03:00
|
|
|
helpers = require('../../server/helpers/register'),
|
2017-03-21 11:24:11 +03:00
|
|
|
filters = require('../../server/filters'),
|
|
|
|
i18n = require('../../server/i18n'),
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2017-03-21 11:24:11 +03:00
|
|
|
AppProxy = require('../../server/apps/proxy'),
|
|
|
|
AppSandbox = require('../../server/apps/sandbox'),
|
2014-02-12 05:04:13 +04:00
|
|
|
AppDependencies = require('../../server/apps/dependencies'),
|
2017-03-21 11:24:11 +03:00
|
|
|
AppPermissions = require('../../server/apps/permissions'),
|
|
|
|
|
|
|
|
sandbox = sinon.sandbox.create();
|
|
|
|
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.init();
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
describe('Apps', function () {
|
2017-03-21 11:24:11 +03:00
|
|
|
var fakeApi;
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fakeApi = {
|
|
|
|
posts: {
|
|
|
|
browse: sandbox.stub(),
|
|
|
|
read: sandbox.stub(),
|
|
|
|
edit: sandbox.stub(),
|
|
|
|
add: sandbox.stub(),
|
|
|
|
destroy: sandbox.stub()
|
|
|
|
},
|
|
|
|
users: {
|
|
|
|
browse: sandbox.stub(),
|
|
|
|
read: sandbox.stub(),
|
|
|
|
edit: sandbox.stub()
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
all: sandbox.stub()
|
|
|
|
},
|
|
|
|
notifications: {
|
|
|
|
destroy: sandbox.stub(),
|
|
|
|
add: sandbox.stub()
|
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
browse: sandbox.stub(),
|
|
|
|
read: sandbox.stub(),
|
|
|
|
add: sandbox.stub()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Proxy', function () {
|
2014-04-22 04:22:13 +04:00
|
|
|
it('requires a name to be passed', function () {
|
|
|
|
function makeWithoutName() {
|
|
|
|
return new AppProxy({});
|
|
|
|
}
|
|
|
|
|
|
|
|
makeWithoutName.should.throw('Must provide an app name for api context');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('requires permissions to be passed', function () {
|
|
|
|
function makeWithoutPerms() {
|
|
|
|
return new AppProxy({
|
|
|
|
name: 'NoPerms'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
makeWithoutPerms.should.throw('Must provide app permissions');
|
|
|
|
});
|
|
|
|
|
2014-02-01 02:53:27 +04:00
|
|
|
it('creates a ghost proxy', function () {
|
2014-04-22 04:22:13 +04:00
|
|
|
var appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['prePostRender'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-01 02:53:27 +04:00
|
|
|
should.exist(appProxy.filters);
|
2014-02-10 03:17:28 +04:00
|
|
|
should.exist(appProxy.filters.register);
|
2014-02-10 16:44:07 +04:00
|
|
|
should.exist(appProxy.filters.deregister);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
should.exist(appProxy.helpers);
|
2014-02-10 03:17:28 +04:00
|
|
|
should.exist(appProxy.helpers.register);
|
|
|
|
should.exist(appProxy.helpers.registerAsync);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
should.exist(appProxy.api);
|
|
|
|
|
|
|
|
should.exist(appProxy.api.posts);
|
2014-04-22 04:22:13 +04:00
|
|
|
should.exist(appProxy.api.posts.browse);
|
|
|
|
should.exist(appProxy.api.posts.read);
|
|
|
|
should.exist(appProxy.api.posts.edit);
|
|
|
|
should.exist(appProxy.api.posts.add);
|
|
|
|
should.exist(appProxy.api.posts.destroy);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
should.not.exist(appProxy.api.users);
|
|
|
|
|
|
|
|
should.exist(appProxy.api.tags);
|
2014-04-22 04:22:13 +04:00
|
|
|
should.exist(appProxy.api.tags.browse);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
should.exist(appProxy.api.notifications);
|
2014-04-22 04:22:13 +04:00
|
|
|
should.exist(appProxy.api.notifications.browse);
|
|
|
|
should.exist(appProxy.api.notifications.add);
|
|
|
|
should.exist(appProxy.api.notifications.destroy);
|
2014-02-01 02:53:27 +04:00
|
|
|
|
|
|
|
should.exist(appProxy.api.settings);
|
2014-04-22 04:22:13 +04:00
|
|
|
should.exist(appProxy.api.settings.browse);
|
|
|
|
should.exist(appProxy.api.settings.read);
|
|
|
|
should.exist(appProxy.api.settings.edit);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows filter registration with permission', function (done) {
|
2014-09-03 19:42:55 +04:00
|
|
|
var registerSpy = sandbox.spy(filters, 'registerFilter'),
|
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['testFilter'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
}),
|
2014-09-10 08:06:24 +04:00
|
|
|
fakePosts = [{id: 0}, {id: 1}],
|
2014-09-03 19:42:55 +04:00
|
|
|
filterStub = sandbox.spy(function (val) {
|
|
|
|
return val;
|
|
|
|
});
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
appProxy.filters.register('testFilter', 5, filterStub);
|
|
|
|
|
|
|
|
registerSpy.called.should.equal(true);
|
|
|
|
|
|
|
|
filterStub.called.should.equal(false);
|
|
|
|
|
|
|
|
filters.doFilter('testFilter', fakePosts)
|
|
|
|
.then(function () {
|
|
|
|
filterStub.called.should.equal(true);
|
|
|
|
appProxy.filters.deregister('testFilter', 5, filterStub);
|
|
|
|
done();
|
|
|
|
})
|
2014-05-06 00:58:58 +04:00
|
|
|
.catch(done);
|
2014-04-22 04:22:13 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow filter registration without permission', function () {
|
2014-09-03 19:42:55 +04:00
|
|
|
var registerSpy = sandbox.spy(filters, 'registerFilter'),
|
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['prePostRender'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
filterStub = sandbox.stub().returns('test result');
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
function registerFilterWithoutPermission() {
|
|
|
|
appProxy.filters.register('superSecretFilter', 5, filterStub);
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
registerFilterWithoutPermission.should.throw('The App "TestApp" attempted to perform an action or access' +
|
|
|
|
' a resource (filters.superSecretFilter) without permission.');
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
registerSpy.called.should.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows filter deregistration with permission', function (done) {
|
2014-09-03 19:42:55 +04:00
|
|
|
var registerSpy = sandbox.spy(filters, 'deregisterFilter'),
|
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['prePostsRender'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
}),
|
2014-09-10 08:06:24 +04:00
|
|
|
fakePosts = [{id: 0}, {id: 1}],
|
2014-09-03 19:42:55 +04:00
|
|
|
filterStub = sandbox.stub().returns(fakePosts);
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
appProxy.filters.deregister('prePostsRender', 5, filterStub);
|
|
|
|
|
|
|
|
registerSpy.called.should.equal(true);
|
|
|
|
|
|
|
|
filterStub.called.should.equal(false);
|
|
|
|
|
|
|
|
filters.doFilter('prePostsRender', fakePosts)
|
|
|
|
.then(function () {
|
|
|
|
filterStub.called.should.equal(false);
|
|
|
|
done();
|
|
|
|
})
|
2014-05-06 00:58:58 +04:00
|
|
|
.catch(done);
|
2014-04-22 04:22:13 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow filter deregistration without permission', function () {
|
2014-09-03 19:42:55 +04:00
|
|
|
var registerSpy = sandbox.spy(filters, 'deregisterFilter'),
|
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['prePostRender'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
filterStub = sandbox.stub().returns('test result');
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
function deregisterFilterWithoutPermission() {
|
|
|
|
appProxy.filters.deregister('superSecretFilter', 5, filterStub);
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
deregisterFilterWithoutPermission.should.throw('The App "TestApp" attempted to perform an action or ' +
|
|
|
|
'access a resource (filters.superSecretFilter) without permission.');
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
registerSpy.called.should.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows helper registration with permission', function () {
|
2017-04-04 19:07:35 +03:00
|
|
|
var registerSpy = sandbox.stub(helpers, 'registerThemeHelper'),
|
2014-09-03 19:42:55 +04:00
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['prePostRender'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
});
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
appProxy.helpers.register('myTestHelper', sandbox.stub().returns('test result'));
|
|
|
|
|
|
|
|
registerSpy.called.should.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow helper registration without permission', function () {
|
2017-04-04 19:07:35 +03:00
|
|
|
var registerSpy = sandbox.stub(helpers, 'registerThemeHelper'),
|
2014-09-03 19:42:55 +04:00
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {
|
|
|
|
filters: ['prePostRender'],
|
|
|
|
helpers: ['myTestHelper'],
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete']
|
|
|
|
}
|
|
|
|
});
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
function registerWithoutPermissions() {
|
|
|
|
appProxy.helpers.register('otherHelper', sandbox.stub().returns('test result'));
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:26:03 +04:00
|
|
|
registerWithoutPermissions.should.throw('The App "TestApp" attempted to perform an action or access a ' +
|
|
|
|
'resource (helpers.otherHelper) without permission.');
|
2014-04-22 04:22:13 +04:00
|
|
|
|
|
|
|
registerSpy.called.should.equal(false);
|
2014-02-01 02:53:27 +04:00
|
|
|
});
|
2016-04-14 20:32:43 +03:00
|
|
|
|
|
|
|
it('does allow INTERNAL app to register helper without permission', function () {
|
2017-04-04 19:07:35 +03:00
|
|
|
var registerSpy = sandbox.stub(helpers, 'registerThemeHelper'),
|
2016-04-14 20:32:43 +03:00
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {},
|
|
|
|
internal: true
|
|
|
|
});
|
|
|
|
|
|
|
|
function registerWithoutPermissions() {
|
|
|
|
appProxy.helpers.register('otherHelper', sandbox.stub().returns('test result'));
|
|
|
|
}
|
|
|
|
|
|
|
|
registerWithoutPermissions.should.not.throw('The App "TestApp" attempted to perform an action or access a ' +
|
|
|
|
'resource (helpers.otherHelper) without permission.');
|
|
|
|
|
|
|
|
registerSpy.called.should.equal(true);
|
|
|
|
});
|
2014-02-01 02:53:27 +04:00
|
|
|
});
|
2014-02-06 15:58:58 +04:00
|
|
|
|
2014-02-01 02:53:27 +04:00
|
|
|
describe('Sandbox', function () {
|
|
|
|
it('loads apps in a sandbox', function () {
|
|
|
|
var appBox = new AppSandbox(),
|
|
|
|
appPath = path.resolve(__dirname, '..', 'utils', 'fixtures', 'app', 'good.js'),
|
|
|
|
GoodApp,
|
2014-04-22 04:22:13 +04:00
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {}
|
|
|
|
}),
|
2014-02-01 02:53:27 +04:00
|
|
|
app;
|
|
|
|
|
|
|
|
GoodApp = appBox.loadApp(appPath);
|
|
|
|
|
|
|
|
should.exist(GoodApp);
|
|
|
|
|
|
|
|
app = new GoodApp(appProxy);
|
|
|
|
|
|
|
|
app.install(appProxy);
|
|
|
|
|
|
|
|
app.app.something.should.equal(42);
|
|
|
|
app.app.util.util().should.equal(42);
|
|
|
|
app.app.nested.other.should.equal(42);
|
|
|
|
app.app.path.should.equal(appPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow apps to require blacklisted modules at top level', function () {
|
|
|
|
var appBox = new AppSandbox(),
|
|
|
|
badAppPath = path.join(__dirname, '..', 'utils', 'fixtures', 'app', 'badtop.js'),
|
|
|
|
loadApp = function () {
|
|
|
|
appBox.loadApp(badAppPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
loadApp.should.throw('Unsafe App require: knex');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow apps to require blacklisted modules at install', function () {
|
|
|
|
var appBox = new AppSandbox(),
|
|
|
|
badAppPath = path.join(__dirname, '..', 'utils', 'fixtures', 'app', 'badinstall.js'),
|
|
|
|
BadApp,
|
2014-04-22 04:22:13 +04:00
|
|
|
appProxy = new AppProxy({
|
|
|
|
name: 'TestApp',
|
|
|
|
permissions: {}
|
|
|
|
}),
|
2014-02-01 02:53:27 +04:00
|
|
|
app,
|
|
|
|
installApp = function () {
|
|
|
|
app.install(appProxy);
|
|
|
|
};
|
|
|
|
|
|
|
|
BadApp = appBox.loadApp(badAppPath);
|
|
|
|
|
|
|
|
app = new BadApp(appProxy);
|
|
|
|
|
|
|
|
installApp.should.throw('Unsafe App require: knex');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow apps to require blacklisted modules from other requires', function () {
|
|
|
|
var appBox = new AppSandbox(),
|
|
|
|
badAppPath = path.join(__dirname, '..', 'utils', 'fixtures', 'app', 'badrequire.js'),
|
|
|
|
BadApp,
|
|
|
|
loadApp = function () {
|
|
|
|
BadApp = appBox.loadApp(badAppPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
loadApp.should.throw('Unsafe App require: knex');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not allow apps to require modules relatively outside their directory', function () {
|
|
|
|
var appBox = new AppSandbox(),
|
|
|
|
badAppPath = path.join(__dirname, '..', 'utils', 'fixtures', 'app', 'badoutside.js'),
|
|
|
|
BadApp,
|
|
|
|
loadApp = function () {
|
|
|
|
BadApp = appBox.loadApp(badAppPath);
|
|
|
|
};
|
|
|
|
|
2014-02-06 15:58:58 +04:00
|
|
|
loadApp.should.throw(/^Unsafe App require[\w\W]*example$/);
|
2014-02-01 02:53:27 +04:00
|
|
|
});
|
2016-04-14 20:32:43 +03:00
|
|
|
|
|
|
|
it('does allow INTERNAL apps to require modules relatively outside their directory', function () {
|
|
|
|
var appBox = new AppSandbox({internal: true}),
|
|
|
|
badAppPath = path.join(__dirname, '..', 'utils', 'fixtures', 'app', 'badoutside.js'),
|
|
|
|
InternalApp,
|
|
|
|
loadApp = function () {
|
|
|
|
InternalApp = appBox.loadApp(badAppPath);
|
|
|
|
};
|
|
|
|
|
|
|
|
InternalApp = appBox.loadApp(badAppPath);
|
|
|
|
|
|
|
|
loadApp.should.not.throw(/^Unsafe App require[\w\W]*example$/);
|
|
|
|
|
|
|
|
InternalApp.should.be.a.Function();
|
|
|
|
});
|
2014-02-01 02:53:27 +04:00
|
|
|
});
|
2014-02-02 10:07:39 +04:00
|
|
|
|
|
|
|
describe('Dependencies', function () {
|
|
|
|
it('can install by package.json', function (done) {
|
|
|
|
var deps = new AppDependencies(process.cwd()),
|
|
|
|
fakeEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
deps.spawnCommand = sandbox.stub().returns(fakeEmitter);
|
|
|
|
|
|
|
|
deps.install().then(function () {
|
|
|
|
deps.spawnCommand.calledWith('npm').should.equal(true);
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-02-02 10:07:39 +04:00
|
|
|
|
|
|
|
_.delay(function () {
|
|
|
|
fakeEmitter.emit('exit');
|
|
|
|
}, 30);
|
|
|
|
});
|
|
|
|
it('does not install when no package.json', function (done) {
|
|
|
|
var deps = new AppDependencies(__dirname),
|
|
|
|
fakeEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
deps.spawnCommand = sandbox.stub().returns(fakeEmitter);
|
|
|
|
|
|
|
|
deps.install().then(function () {
|
|
|
|
deps.spawnCommand.called.should.equal(false);
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-02-02 10:07:39 +04:00
|
|
|
|
|
|
|
_.defer(function () {
|
|
|
|
fakeEmitter.emit('exit');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
describe('Permissions', function () {
|
|
|
|
var noGhostPackageJson = {
|
2014-09-10 08:06:24 +04:00
|
|
|
name: 'myapp',
|
|
|
|
version: '0.0.1',
|
|
|
|
description: 'My example app',
|
|
|
|
main: 'index.js',
|
|
|
|
scripts: {
|
|
|
|
test: 'echo \'Error: no test specified\' && exit 1'
|
2014-02-12 05:04:13 +04:00
|
|
|
},
|
2014-09-10 08:06:24 +04:00
|
|
|
author: 'Ghost',
|
|
|
|
license: 'MIT',
|
|
|
|
dependencies: {
|
|
|
|
'ghost-app': '0.0.1'
|
2014-02-12 05:04:13 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
validGhostPackageJson = {
|
2014-09-10 08:06:24 +04:00
|
|
|
name: 'myapp',
|
|
|
|
version: '0.0.1',
|
|
|
|
description: 'My example app',
|
|
|
|
main: 'index.js',
|
|
|
|
scripts: {
|
|
|
|
test: 'echo \'Error: no test specified\' && exit 1'
|
2014-02-12 05:04:13 +04:00
|
|
|
},
|
2014-09-10 08:06:24 +04:00
|
|
|
author: 'Ghost',
|
|
|
|
license: 'MIT',
|
|
|
|
dependencies: {
|
|
|
|
'ghost-app': '0.0.1'
|
2014-02-12 05:04:13 +04:00
|
|
|
},
|
2014-09-10 08:06:24 +04:00
|
|
|
ghost: {
|
|
|
|
permissions: {
|
|
|
|
posts: ['browse', 'read', 'edit', 'add', 'delete'],
|
|
|
|
users: ['browse', 'read', 'edit', 'add', 'delete'],
|
|
|
|
settings: ['browse', 'read', 'edit', 'add', 'delete']
|
2014-02-12 05:04:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
it('has default permissions to read and browse posts', function () {
|
|
|
|
should.exist(AppPermissions.DefaultPermissions);
|
|
|
|
|
|
|
|
should.exist(AppPermissions.DefaultPermissions.posts);
|
|
|
|
|
2014-09-17 19:44:30 +04:00
|
|
|
AppPermissions.DefaultPermissions.posts.should.containEql('browse');
|
|
|
|
AppPermissions.DefaultPermissions.posts.should.containEql('read');
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
// Make it hurt to add more so additional checks are added here
|
|
|
|
_.keys(AppPermissions.DefaultPermissions).length.should.equal(1);
|
|
|
|
});
|
|
|
|
it('uses default permissions if no package.json', function (done) {
|
2014-09-10 08:06:24 +04:00
|
|
|
var perms = new AppPermissions('test');
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
// No package.json in this directory
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'checkPackageContentsExists').returns(Promise.resolve(false));
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
perms.read().then(function (readPerms) {
|
|
|
|
should.exist(readPerms);
|
|
|
|
|
|
|
|
readPerms.should.equal(AppPermissions.DefaultPermissions);
|
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-02-12 05:04:13 +04:00
|
|
|
});
|
|
|
|
it('uses default permissions if no ghost object in package.json', function (done) {
|
2014-09-10 08:06:24 +04:00
|
|
|
var perms = new AppPermissions('test'),
|
2014-02-12 05:04:13 +04:00
|
|
|
noGhostPackageJsonContents = JSON.stringify(noGhostPackageJson, null, 2);
|
|
|
|
|
|
|
|
// package.json IS in this directory
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'checkPackageContentsExists').returns(Promise.resolve(true));
|
2014-02-12 05:04:13 +04:00
|
|
|
// no ghost property on package
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'getPackageContents').returns(Promise.resolve(noGhostPackageJsonContents));
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
perms.read().then(function (readPerms) {
|
|
|
|
should.exist(readPerms);
|
|
|
|
|
|
|
|
readPerms.should.equal(AppPermissions.DefaultPermissions);
|
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-02-12 05:04:13 +04:00
|
|
|
});
|
|
|
|
it('rejects when reading malformed package.json', function (done) {
|
2014-09-10 08:06:24 +04:00
|
|
|
var perms = new AppPermissions('test');
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
// package.json IS in this directory
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'checkPackageContentsExists').returns(Promise.resolve(true));
|
2014-02-12 05:04:13 +04:00
|
|
|
// malformed JSON on package
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'getPackageContents').returns(Promise.reject(new Error('package.json file is malformed')));
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
perms.read().then(function (readPerms) {
|
2014-06-05 01:26:03 +04:00
|
|
|
/*jshint unused:false*/
|
2014-02-12 05:04:13 +04:00
|
|
|
done(new Error('should not resolve'));
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(function (err) {
|
|
|
|
err.message.should.equal('package.json file is malformed');
|
2014-02-12 05:04:13 +04:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('reads from package.json in root of app directory', function (done) {
|
2014-09-10 08:06:24 +04:00
|
|
|
var perms = new AppPermissions('test'),
|
2014-02-12 05:04:13 +04:00
|
|
|
validGhostPackageJsonContents = validGhostPackageJson;
|
|
|
|
|
|
|
|
// package.json IS in this directory
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'checkPackageContentsExists').returns(Promise.resolve(true));
|
2014-02-12 05:04:13 +04:00
|
|
|
// valid ghost property on package
|
2014-09-10 08:06:24 +04:00
|
|
|
sandbox.stub(perms, 'getPackageContents').returns(Promise.resolve(validGhostPackageJsonContents));
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
perms.read().then(function (readPerms) {
|
|
|
|
should.exist(readPerms);
|
|
|
|
|
|
|
|
readPerms.should.not.equal(AppPermissions.DefaultPermissions);
|
|
|
|
|
|
|
|
should.exist(readPerms.posts);
|
|
|
|
readPerms.posts.length.should.equal(5);
|
|
|
|
|
|
|
|
should.exist(readPerms.users);
|
|
|
|
readPerms.users.length.should.equal(5);
|
|
|
|
|
|
|
|
should.exist(readPerms.settings);
|
|
|
|
readPerms.settings.length.should.equal(5);
|
|
|
|
|
|
|
|
_.keys(readPerms).length.should.equal(3);
|
|
|
|
|
|
|
|
done();
|
2014-05-06 00:58:58 +04:00
|
|
|
}).catch(done);
|
2014-02-12 05:04:13 +04:00
|
|
|
});
|
|
|
|
});
|
2014-02-10 03:17:28 +04:00
|
|
|
});
|