2013-05-25 20:48:15 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
2013-06-25 15:43:15 +04:00
|
|
|
var _ = require("underscore"),
|
|
|
|
should = require('should'),
|
|
|
|
helpers = require('./helpers'),
|
2013-07-11 23:02:18 +04:00
|
|
|
Models = require('../../server/models');
|
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) {
|
|
|
|
helpers.clearData().then(function () {
|
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
beforeEach(function (done) {
|
2013-08-25 00:51:58 +04:00
|
|
|
this.timeout(5000);
|
|
|
|
helpers.initData().then(function () {
|
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function (done) {
|
|
|
|
helpers.clearData().then(function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can browse', function (done) {
|
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +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-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can read', function (done) {
|
|
|
|
var firstSetting;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
firstSetting = results.models[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return SettingsModel.read(firstSetting.attributes.key);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (found) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(found);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
found.attributes.value.should.equal(firstSetting.attributes.value);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can edit single', function (done) {
|
|
|
|
var firstSetting;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
firstSetting = results.models[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// The edit method has been modified to take an object of
|
|
|
|
// key/value pairs
|
|
|
|
firstSetting.set('value', 'new value');
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return SettingsModel.edit(firstSetting);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(edited);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
edited.length.should.equal(1);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
edited = edited[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
edited.attributes.key.should.equal(firstSetting.attributes.key);
|
|
|
|
edited.attributes.value.should.equal('new value');
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can edit multiple', function (done) {
|
|
|
|
var model1,
|
|
|
|
model2,
|
|
|
|
editedModel;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
model1 = results.models[0];
|
|
|
|
model2 = results.models[1];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// The edit method has been modified to take an object of
|
|
|
|
// key/value pairs
|
|
|
|
model1.set('value', 'new value1');
|
|
|
|
model2.set('value', 'new value2');
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return SettingsModel.edit([model1, model2]);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(edited);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
edited.length.should.equal(2);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
editedModel = edited[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
editedModel.attributes.key.should.equal(model1.attributes.key);
|
|
|
|
editedModel.attributes.value.should.equal('new value1');
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
editedModel = edited[1];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
editedModel.attributes.key.should.equal(model2.attributes.key);
|
|
|
|
editedModel.attributes.value.should.equal('new value2');
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can add', function (done) {
|
|
|
|
var newSetting = {
|
|
|
|
key: 'TestSetting1',
|
|
|
|
value: 'Test Content 1'
|
|
|
|
};
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
SettingsModel.add(newSetting).then(function (createdSetting) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +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");
|
|
|
|
createdSetting.attributes.type.should.equal("general");
|
2013-05-25 20:48:15 +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-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can delete', function (done) {
|
|
|
|
var firstSettingId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
SettingsModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
firstSettingId = results.models[0].id;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return SettingsModel.destroy(firstSettingId);
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return SettingsModel.browse();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (newResults) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
var ids, hasDeletedId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ids = _.pluck(newResults.models, "id");
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
hasDeletedId = _.any(ids, function (id) {
|
|
|
|
return id === firstSettingId;
|
|
|
|
});
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
hasDeletedId.should.equal(false);
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|