2019-03-06 14:56:26 +03:00
|
|
|
const should = require('should');
|
2019-03-26 06:37:32 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const mockDb = require('mock-knex');
|
2020-03-30 18:26:47 +03:00
|
|
|
const models = require('../../../core/server/models');
|
|
|
|
const {knex} = require('../../../core/server/data/db');
|
2020-05-28 19:54:18 +03:00
|
|
|
const {events} = require('../../../core/server/lib/common');
|
2020-05-28 13:40:49 +03:00
|
|
|
const defaultSettings = require('../../../core/server/data/schema/default-settings');
|
2019-03-06 14:56:26 +03:00
|
|
|
|
|
|
|
describe('Unit: models/settings', function () {
|
|
|
|
before(function () {
|
|
|
|
models.init();
|
|
|
|
});
|
|
|
|
|
2019-03-26 06:37:32 +03:00
|
|
|
describe('events', function () {
|
|
|
|
let tracker;
|
|
|
|
let eventSpy;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockDb.mock(knex);
|
|
|
|
tracker = mockDb.getTracker();
|
|
|
|
tracker.install();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockDb.unmock(knex);
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2020-05-28 19:54:18 +03:00
|
|
|
eventSpy = sinon.spy(events, 'emit');
|
2019-03-26 06:37:32 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits add events', function () {
|
|
|
|
tracker.on('query', (query, step) => {
|
|
|
|
return [
|
|
|
|
function fetchAddQuery() {
|
|
|
|
query.response([{}]);
|
|
|
|
},
|
|
|
|
function addQuery() {
|
|
|
|
query.response([{
|
|
|
|
key: 'description',
|
|
|
|
value: 'added value'
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
][step - 1]();
|
|
|
|
});
|
|
|
|
|
|
|
|
return models.Settings.edit({
|
2019-08-19 14:41:09 +03:00
|
|
|
key: 'description',
|
|
|
|
value: 'added value'
|
|
|
|
})
|
2019-03-26 06:37:32 +03:00
|
|
|
.then(() => {
|
|
|
|
eventSpy.calledTwice.should.be.true();
|
|
|
|
eventSpy.firstCall.calledWith('settings.added').should.be.true();
|
|
|
|
eventSpy.secondCall.calledWith('settings.description.added').should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('emits edit events', function () {
|
|
|
|
tracker.on('query', (query, step) => {
|
|
|
|
return [
|
|
|
|
function fetchEditQuery() {
|
|
|
|
query.response([{
|
2019-08-19 14:41:09 +03:00
|
|
|
id: 1, // NOTE: `id` imitates existing value for 'edit' event
|
2019-03-26 06:37:32 +03:00
|
|
|
key: 'description',
|
|
|
|
value: 'db value'
|
|
|
|
}]);
|
|
|
|
}
|
|
|
|
][step - 1]();
|
|
|
|
});
|
|
|
|
|
|
|
|
return models.Settings.edit({
|
2019-08-19 14:41:09 +03:00
|
|
|
key: 'description',
|
|
|
|
value: 'edited value'
|
|
|
|
})
|
2019-03-26 06:37:32 +03:00
|
|
|
.then(() => {
|
|
|
|
eventSpy.calledTwice.should.be.true();
|
|
|
|
eventSpy.firstCall.calledWith('settings.edited').should.be.true();
|
|
|
|
eventSpy.secondCall.calledWith('settings.description.edited').should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('defaults', function () {
|
|
|
|
let tracker;
|
|
|
|
let eventSpy;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockDb.mock(knex);
|
|
|
|
tracker = mockDb.getTracker();
|
|
|
|
tracker.install();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockDb.unmock(knex);
|
|
|
|
tracker.uninstall();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
2020-05-28 19:54:18 +03:00
|
|
|
eventSpy = sinon.spy(events, 'emit');
|
2019-03-26 06:37:32 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('populates unset defaults', function () {
|
2020-06-25 16:22:15 +03:00
|
|
|
let insertQueries = [];
|
|
|
|
|
2019-03-26 06:37:32 +03:00
|
|
|
tracker.on('query', (query) => {
|
2020-06-25 16:22:15 +03:00
|
|
|
// skip group and flags columns so we can test the insertion column skip
|
|
|
|
if (query.method === 'columnInfo') {
|
|
|
|
return query.response([
|
|
|
|
{name: 'id', type: 'varchar'},
|
|
|
|
// {name: 'group', type: 'varchar'},
|
|
|
|
{name: 'key', type: 'varchar'},
|
|
|
|
{name: 'value', type: 'varchar'},
|
|
|
|
{name: 'type', type: 'varchar'},
|
|
|
|
// {name: 'flags', type: 'varchar'},
|
|
|
|
{name: 'created_at', type: 'datetime'},
|
|
|
|
{name: 'created_by', type: 'varchar'},
|
|
|
|
{name: 'updated_at', type: 'varchar'},
|
|
|
|
{name: 'updated_by', type: 'datetime'}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.method === 'insert') {
|
|
|
|
insertQueries.push(query);
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:37:32 +03:00
|
|
|
return query.response([{}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return models.Settings.populateDefaults()
|
|
|
|
.then(() => {
|
2020-05-28 13:40:49 +03:00
|
|
|
const numberOfSettings = Object.keys(defaultSettings).reduce((settings, settingGroup) => {
|
|
|
|
return settings.concat(Object.keys(defaultSettings[settingGroup]));
|
|
|
|
}, []).length;
|
|
|
|
|
2020-06-25 16:22:15 +03:00
|
|
|
insertQueries.length.should.equal(numberOfSettings);
|
2019-03-26 06:37:32 +03:00
|
|
|
|
2020-06-25 16:22:15 +03:00
|
|
|
// non-existent columns should not be populated
|
|
|
|
insertQueries[0].sql.should.not.match(/group/);
|
|
|
|
insertQueries[0].sql.should.not.match(/flags/);
|
2019-03-26 06:37:32 +03:00
|
|
|
|
2020-06-25 16:22:15 +03:00
|
|
|
// no events are emitted because we're not using the model layer
|
|
|
|
eventSpy.callCount.should.equal(0);
|
2019-03-26 06:37:32 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('doesn\'t overwrite any existing settings', function () {
|
2020-06-25 16:22:15 +03:00
|
|
|
let insertQueries = [];
|
|
|
|
|
2019-03-26 06:37:32 +03:00
|
|
|
tracker.on('query', (query) => {
|
2020-06-25 16:22:15 +03:00
|
|
|
if (query.method === 'columnInfo') {
|
|
|
|
return query.response([
|
|
|
|
{name: 'id', type: 'varchar'},
|
|
|
|
{name: 'key', type: 'varchar'},
|
|
|
|
{name: 'value', type: 'varchar'}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.method === 'insert') {
|
|
|
|
insertQueries.push(query);
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:37:32 +03:00
|
|
|
return query.response([{
|
|
|
|
key: 'description',
|
|
|
|
value: 'Adam\'s Blog'
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return models.Settings.populateDefaults()
|
|
|
|
.then(() => {
|
2020-06-25 16:22:15 +03:00
|
|
|
const numberOfSettings = Object.keys(defaultSettings).reduce((settings, settingGroup) => {
|
|
|
|
return settings.concat(Object.keys(defaultSettings[settingGroup]));
|
|
|
|
}, []).length;
|
|
|
|
|
|
|
|
insertQueries.length.should.equal(numberOfSettings - 1);
|
2019-03-26 06:37:32 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-06 14:56:26 +03:00
|
|
|
describe('parse', function () {
|
|
|
|
it('ensure correct parsing when fetching from db', function () {
|
|
|
|
const setting = models.Settings.forge();
|
|
|
|
|
|
|
|
let returns = setting.parse({key: 'is_private', value: 'false'});
|
|
|
|
should.equal(returns.value, false);
|
|
|
|
|
|
|
|
returns = setting.parse({key: 'is_private', value: false});
|
|
|
|
should.equal(returns.value, false);
|
|
|
|
|
|
|
|
returns = setting.parse({key: 'is_private', value: true});
|
|
|
|
should.equal(returns.value, true);
|
|
|
|
|
|
|
|
returns = setting.parse({key: 'is_private', value: 'true'});
|
|
|
|
should.equal(returns.value, true);
|
|
|
|
|
|
|
|
returns = setting.parse({key: 'is_private', value: '0'});
|
|
|
|
should.equal(returns.value, false);
|
|
|
|
|
|
|
|
returns = setting.parse({key: 'is_private', value: '1'});
|
|
|
|
should.equal(returns.value, true);
|
|
|
|
|
|
|
|
returns = setting.parse({key: 'something', value: 'null'});
|
2019-03-11 19:25:45 +03:00
|
|
|
should.equal(returns.value, 'null');
|
2019-03-06 14:56:26 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|