Ghost/core/test/ghost/api_settings_spec.js

184 lines
4.8 KiB
JavaScript
Raw Normal View History

/*globals describe, beforeEach, it*/
(function () {
"use strict";
var _ = require("underscore"),
should = require('should'),
helpers = require('./helpers'),
2013-06-01 18:47:41 +04:00
Models = require('../../shared/models');
2013-06-01 18:47:41 +04:00
describe('Bookshelf Setting Model', function () {
2013-06-01 18:47:41 +04:00
var SettingModel = Models.Setting;
beforeEach(function (done) {
helpers.resetData().then(function () {
done();
2013-06-01 18:47:41 +04:00
}, done);
});
it('can browse', function (done) {
2013-06-01 18:47:41 +04:00
SettingModel.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
done();
}).then(null, done);
});
it('can read', function (done) {
var firstSetting;
2013-06-01 18:47:41 +04:00
SettingModel.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstSetting = results.models[0];
2013-06-01 18:47:41 +04:00
return SettingModel.read(firstSetting.attributes.key);
2013-05-27 03:53:44 +04:00
}).then(function (found) {
2013-05-27 03:53:44 +04:00
should.exist(found);
2013-05-27 03:53:44 +04:00
found.attributes.value.should.equal(firstSetting.attributes.value);
2013-05-27 03:53:44 +04:00
done();
}).then(null, done);
});
it('can edit single', function (done) {
var firstPost,
toEdit = {};
2013-06-01 18:47:41 +04:00
SettingModel.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstPost = results.models[0];
2013-05-27 03:53:44 +04:00
// The edit method has been modified to take an object of
// key/value pairs
toEdit[firstPost.attributes.key] = "new value";
2013-06-01 18:47:41 +04:00
return SettingModel.edit(toEdit);
2013-05-27 03:53:44 +04:00
}).then(function (edited) {
2013-05-27 03:53:44 +04:00
should.exist(edited);
2013-05-27 03:53:44 +04:00
edited.length.should.equal(1);
2013-05-27 03:53:44 +04:00
edited = edited[0];
2013-05-27 03:53:44 +04:00
edited.attributes.key.should.equal(firstPost.attributes.key);
edited.attributes.value.should.equal('new value');
2013-05-27 03:53:44 +04:00
done();
}).then(null, done);
});
it('can edit multiple', function (done) {
var firstPost,
secondPost,
editedPost,
toEdit = {};
2013-06-01 18:47:41 +04:00
SettingModel.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstPost = results.models[0];
secondPost = results.models[1];
2013-05-27 03:53:44 +04:00
// The edit method has been modified to take an object of
// key/value pairs
toEdit[firstPost.attributes.key] = "new value1";
toEdit[secondPost.attributes.key] = "new value2";
2013-06-01 18:47:41 +04:00
return SettingModel.edit(toEdit);
2013-05-27 03:53:44 +04:00
}).then(function (edited) {
2013-05-27 03:53:44 +04:00
should.exist(edited);
2013-05-27 03:53:44 +04:00
edited.length.should.equal(2);
2013-05-27 03:53:44 +04:00
editedPost = edited[0];
2013-05-27 03:53:44 +04:00
editedPost.attributes.key.should.equal(firstPost.attributes.key);
editedPost.attributes.value.should.equal('new value1');
2013-05-27 03:53:44 +04:00
editedPost = edited[1];
2013-05-27 03:53:44 +04:00
editedPost.attributes.key.should.equal(secondPost.attributes.key);
editedPost.attributes.value.should.equal('new value2');
done();
}).then(null, done);
});
it('can add', function (done) {
var newSetting = {
key: 'TestSetting1',
value: 'Test Content 1'
};
2013-06-01 18:47:41 +04:00
SettingModel.add(newSetting).then(function (createdSetting) {
should.exist(createdSetting);
createdSetting.attributes.key.should.equal(newSetting.key, "key is correct");
createdSetting.attributes.value.should.equal(newSetting.value, "value is correct");
done();
}).then(null, done);
});
it('can delete', function (done) {
2013-05-27 03:53:44 +04:00
var firstSettingId;
2013-06-01 18:47:41 +04:00
SettingModel.browse().then(function (results) {
should.exist(results);
results.length.should.be.above(0);
firstSettingId = results.models[0].id;
2013-06-01 18:47:41 +04:00
return SettingModel.destroy(firstSettingId);
2013-05-27 03:53:44 +04:00
}).then(function () {
2013-06-01 18:47:41 +04:00
return SettingModel.browse();
2013-05-27 03:53:44 +04:00
}).then(function (newResults) {
2013-05-27 03:53:44 +04:00
var ids, hasDeletedId;
2013-05-27 03:53:44 +04:00
ids = _.pluck(newResults.models, "id");
2013-05-27 03:53:44 +04:00
hasDeletedId = _.any(ids, function (id) {
return id === firstSettingId;
});
2013-05-27 03:53:44 +04:00
hasDeletedId.should.equal(false);
done();
}).then(null, done);
});
});
}());