2013-09-06 20:07:25 +04:00
|
|
|
/*globals describe, before, beforeEach, afterEach, it*/
|
2013-10-30 01:34:47 +04:00
|
|
|
var testUtils = require('../unit/testUtils'),
|
2013-06-25 15:43:15 +04:00
|
|
|
should = require('should'),
|
2013-09-06 20:07:25 +04:00
|
|
|
_ = require("underscore"),
|
|
|
|
|
|
|
|
// Stuff we are testing
|
2013-09-02 05:49:08 +04:00
|
|
|
Models = require('../../server/models'),
|
2013-09-23 02:20:08 +04:00
|
|
|
knex = require('../../server/models/base').knex;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
describe('Settings Model', function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
var SettingsModel = Models.Settings;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
before(function (done) {
|
2013-09-06 20:07:25 +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().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().then(function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
describe('API', function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
it('can browse', function (done) {
|
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
it('can read', function (done) {
|
|
|
|
var firstSetting;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
firstSetting = results.models[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
return SettingsModel.read(firstSetting.attributes.key);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(function (found) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(found);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
found.attributes.value.should.equal(firstSetting.attributes.value);
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
done();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
it('can edit single', function (done) {
|
|
|
|
var firstSetting;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-27 15:52:31 +04:00
|
|
|
return SettingsModel.edit({key: "description", value: "new value"});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(edited);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
edited.length.should.equal(1);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
edited = edited[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-27 15:52:31 +04:00
|
|
|
edited.attributes.key.should.equal('description');
|
2013-09-02 05:49:08 +04:00
|
|
|
edited.attributes.value.should.equal('new value');
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
done();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
it('can edit multiple', function (done) {
|
|
|
|
var model1,
|
|
|
|
model2,
|
|
|
|
editedModel;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-27 15:52:31 +04:00
|
|
|
model1 = {key: "description", value: "another new value"};
|
|
|
|
model2 = {key: "title", value: "new title"};
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
return SettingsModel.edit([model1, model2]);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(edited);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
edited.length.should.equal(2);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
editedModel = edited[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-27 15:52:31 +04:00
|
|
|
editedModel.attributes.key.should.equal(model1.key);
|
|
|
|
editedModel.attributes.value.should.equal(model1.value);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
editedModel = edited[1];
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-27 15:52:31 +04:00
|
|
|
editedModel.attributes.key.should.equal(model2.key);
|
|
|
|
editedModel.attributes.value.should.equal(model2.value);
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
done();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
it('can add', function (done) {
|
|
|
|
var newSetting = {
|
|
|
|
key: 'TestSetting1',
|
|
|
|
value: 'Test Content 1'
|
|
|
|
};
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
SettingsModel.add(newSetting).then(function (createdSetting) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(createdSetting);
|
|
|
|
createdSetting.has('uuid').should.equal(true);
|
|
|
|
createdSetting.attributes.key.should.equal(newSetting.key, "key is correct");
|
|
|
|
createdSetting.attributes.value.should.equal(newSetting.value, "value is correct");
|
2013-09-14 22:04:41 +04:00
|
|
|
createdSetting.attributes.type.should.equal("core");
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
it('can delete', function (done) {
|
|
|
|
var firstSettingId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
firstSettingId = results.models[0].id;
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
return SettingsModel.destroy(firstSettingId);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
return SettingsModel.browse();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
}).then(function (newResults) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
var ids, hasDeletedId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
ids = _.pluck(newResults.models, "id");
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
hasDeletedId = _.any(ids, function (id) {
|
|
|
|
return id === firstSettingId;
|
|
|
|
});
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
hasDeletedId.should.equal(false);
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
done();
|
|
|
|
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
2013-09-02 05:49:08 +04:00
|
|
|
|
|
|
|
describe('populating defaults from settings.json', function (done) {
|
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
knex('settings').truncate().then(function () {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('populates any unset settings from the JSON defaults', function (done) {
|
|
|
|
SettingsModel.findAll().then(function (allSettings) {
|
|
|
|
allSettings.length.should.equal(0);
|
|
|
|
return SettingsModel.populateDefaults();
|
|
|
|
}).then(function () {
|
|
|
|
return SettingsModel.findAll();
|
|
|
|
}).then(function (allSettings) {
|
|
|
|
allSettings.length.should.be.above(0);
|
|
|
|
return SettingsModel.read('description');
|
|
|
|
}).then(function (descriptionSetting) {
|
|
|
|
// Testing against the actual value in default-settings.json feels icky,
|
|
|
|
// but it's easier to fix the test if that ever changes than to mock out that behaviour
|
|
|
|
descriptionSetting.get('value').should.equal('Just a blogging platform.');
|
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('doesn\'t overwrite any existing settings', function (done) {
|
|
|
|
SettingsModel.edit({key: 'description', value: 'Adam\'s Blog'}).then(function () {
|
|
|
|
return SettingsModel.populateDefaults();
|
|
|
|
}).then(function () {
|
|
|
|
return SettingsModel.read('description');
|
|
|
|
}).then(function (descriptionSetting) {
|
|
|
|
descriptionSetting.get('value').should.equal('Adam\'s Blog');
|
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|