🚨 change logic for test/utils/configUtils

refs #6982
- adaption because of using nconf
- change tests which changed config directly

[ci skip]
This commit is contained in:
kirrg001 2016-09-13 18:35:20 +02:00 committed by Hannah Wolfe
parent bdad235f6a
commit b158a3a944
10 changed files with 123 additions and 99 deletions

View File

@ -1,6 +1,6 @@
var testUtils = require('../../utils'),
configUtils = require('../../utils/configUtils'),
should = require('should'),
config = require('../../../server/config'),
i18n = require('../../../../core/server/i18n'),
// test data
@ -14,6 +14,7 @@ var testUtils = require('../../utils'),
options: {}
}]
};
i18n.init();
describe('Mail API', function () {
@ -21,8 +22,12 @@ describe('Mail API', function () {
afterEach(testUtils.teardown);
beforeEach(testUtils.setup('perms:mail', 'perms:init'));
afterEach(function () {
configUtils.restore();
});
it('returns a success', function (done) {
config.set({mail: {transport: 'stub'}});
configUtils.set({mail: {transport: 'stub'}});
var MailAPI = require('../../../server/api/mail');
@ -37,7 +42,7 @@ describe('Mail API', function () {
});
it('returns a boo boo', function (done) {
config.set({mail: {transport: 'stub', options: {error: 'Stub made a boo boo :('}}});
configUtils.set({mail: {transport: 'stub', options: {error: 'Stub made a boo boo :('}}});
var MailAPI = require('../../../server/api/mail');

View File

@ -47,9 +47,7 @@ describe('Post API', function () {
describe('Browse', function () {
beforeEach(function () {
configUtils.set({theme: {
permalinks: '/:slug/'
}});
configUtils.set('theme:permalinks', '/:slug/');
});
afterEach(function () {

View File

@ -14,9 +14,7 @@ var testUtils = require('../../utils'),
errors = require('../../../server/errors'),
DataGenerator = testUtils.DataGenerator,
context = testUtils.context.owner,
configUtils = require('../../utils/configUtils'),
sandbox = sinon.sandbox.create();
describe('Post Model', function () {
@ -68,9 +66,7 @@ describe('Post Model', function () {
describe('findAll', function () {
beforeEach(function () {
configUtils.set({theme: {
permalinks: '/:slug/'
}});
configUtils.set('theme:permalinks', '/:slug/');
});
it('can findAll', function (done) {
@ -293,9 +289,7 @@ describe('Post Model', function () {
describe('findOne', function () {
beforeEach(function () {
configUtils.set({theme: {
permalinks: '/:slug/'
}});
configUtils.set('theme:permalinks', '/:slug/');
});
it('can findOne', function (done) {
@ -346,9 +340,7 @@ describe('Post Model', function () {
it('can findOne, returning a dated permalink', function (done) {
var firstPost = 1;
configUtils.set({theme: {
permalinks: '/:year/:month/:day/:slug/'
}});
configUtils.set('theme:permalinks', '/:year/:month/:day/:slug/');
PostModel.findOne({id: firstPost})
.then(function (result) {

View File

@ -3,12 +3,10 @@ var should = require('should'),
Promise = require('bluebird'),
_ = require('lodash'),
// Stuff we are testing
// Stuff we are testing
channels = require('../../../../server/controllers/frontend/channels'),
api = require('../../../../server/api'),
configUtils = require('../../../utils/configUtils'),
sandbox = sinon.sandbox.create();
describe('Channels', function () {
@ -122,9 +120,9 @@ describe('Channels', function () {
// Return basic paths for the activeTheme
function setupActiveTheme() {
configUtils.set({paths: {availableThemes: {casper: {
configUtils.set('paths', {availableThemes: {casper: {
'index.hbs': '/content/themes/casper/index.hbs'
}}}});
}}});
}
beforeEach(function () {
@ -257,9 +255,9 @@ describe('Channels', function () {
// Return basic paths for the activeTheme
function setupActiveTheme() {
configUtils.set({paths: {availableThemes: {casper: {
configUtils.set('paths', {availableThemes: {casper: {
'index.hbs': '/content/themes/casper/index.hbs'
}}}});
}}});
}
beforeEach(function () {
@ -279,10 +277,10 @@ describe('Channels', function () {
});
it('should render the first page of the tag channel using tag.hbs by default', function (done) {
configUtils.set({paths: {availableThemes: {casper: {
configUtils.set('paths',{availableThemes: {casper: {
'index.hbs': '/content/themes/casper/index.hbs',
'tag.hbs': '/content/themes/casper/tag.hbs'
}}}});
}}});
testChannelRender({url: '/tag/my-tag/'}, function (view) {
should.exist(view);
@ -293,11 +291,11 @@ describe('Channels', function () {
});
it('should render the first page of the tag channel using tag-:slug.hbs if available', function (done) {
configUtils.set({paths: {availableThemes: {casper: {
configUtils.set('paths', {availableThemes: {casper: {
'index.hbs': '/content/themes/casper/index.hbs',
'tag.hbs': '/content/themes/casper/tag.hbs',
'tag-my-tag.hbs': '/content/themes/casper/tag-my-tag.hbs'
}}}});
}}});
testChannelRender({url: '/tag/my-tag/'}, function (view) {
should.exist(view);
@ -317,10 +315,10 @@ describe('Channels', function () {
});
it('should use tag.hbs to render the tag channel if available', function (done) {
configUtils.set({paths: {availableThemes: {casper: {
configUtils.set('paths', {availableThemes: {casper: {
'index.hbs': '/content/themes/casper/index.hbs',
'tag.hbs': '/content/themes/casper/tag.hbs'
}}}});
}}});
testChannelRender({url: '/tag/my-tag/page/2/'}, function (view) {
should.exist(view);
@ -330,11 +328,11 @@ describe('Channels', function () {
});
it('should use tag-:slug.hbs to render the tag channel if available', function (done) {
configUtils.set({paths: {availableThemes: {casper: {
configUtils.set('paths', {availableThemes: {casper: {
'index.hbs': '/content/themes/casper/index.hbs',
'tag.hbs': '/content/themes/casper/tag.hbs',
'tag-my-tag.hbs': '/content/themes/casper/tag-my-tag.hbs'
}}}});
}}});
testChannelRender({url: '/tag/my-tag/page/2/'}, function (view) {
should.exist(view);

View File

@ -1,15 +1,10 @@
var should = require('should'),
sinon = require('sinon'),
Promise = require('bluebird'),
_ = require('lodash'),
// Stuff we are testing
api = require('../../../../server/api'),
fetchData = require('../../../../server/controllers/frontend/fetch-data'),
config = require('../../../../server/config'),
origConfig = _.cloneDeep(config),
configUtils = require('../../../utils/configUtils'),
sandbox = sinon.sandbox.create();
describe('fetchData', function () {
@ -25,13 +20,13 @@ describe('fetchData', function () {
});
afterEach(function () {
config.set(origConfig);
configUtils.restore();
sandbox.restore();
});
describe('channel config', function () {
beforeEach(function () {
config.set({theme: {postsPerPage: 10}});
configUtils.set({theme: {postsPerPage: 10}});
});
it('should handle no post options', function (done) {
@ -75,6 +70,7 @@ describe('fetchData', function () {
}
}
};
fetchData(channelOpts).then(function (result) {
should.exist(result);
result.should.be.an.Object().with.properties('posts', 'meta', 'data');
@ -102,6 +98,7 @@ describe('fetchData', function () {
}
}
};
fetchData(channelOpts).then(function (result) {
should.exist(result);
@ -151,7 +148,7 @@ describe('fetchData', function () {
describe('valid postsPerPage', function () {
beforeEach(function () {
config.set({theme: {postsPerPage: 10}});
configUtils.set({theme: {postsPerPage: 10}});
});
it('Adds limit & includes to options by default', function (done) {
@ -167,7 +164,7 @@ describe('fetchData', function () {
describe('invalid postsPerPage', function () {
beforeEach(function () {
config.set({theme: {postsPerPage: '-1'}});
configUtils.set({theme: {postsPerPage: '-1'}});
});
it('Will not add limit if postsPerPage is not valid', function (done) {

View File

@ -1,10 +1,13 @@
var getAuthorImage = require('../../../server/data/meta/author_image'),
should = require('should'),
config = require('../../../server/config');
configUtils = require('../../utils/configUtils');
describe('getAuthorImage', function () {
it('should return author image url if post and has url',
function () {
afterEach(function () {
configUtils.restore();
});
it('should return author image url if post and has url', function () {
var imageUrl = getAuthorImage({
context: ['post'],
post: {
@ -16,8 +19,7 @@ describe('getAuthorImage', function () {
imageUrl.should.equal('/content/images/2016/01/myimage.jpg');
});
it('should return absolute author image url if post and has url',
function () {
it('should return absolute author image url if post and has url', function () {
var imageUrl = getAuthorImage({
context: ['post'],
post: {
@ -30,8 +32,7 @@ describe('getAuthorImage', function () {
imageUrl.should.match(/\/content\/images\/2016\/01\/myimage\.jpg$/);
});
it('should return author image url if AMP post and has url',
function () {
it('should return author image url if AMP post and has url', function () {
var imageUrl = getAuthorImage({
context: ['amp', 'post'],
post: {
@ -43,8 +44,7 @@ describe('getAuthorImage', function () {
imageUrl.should.equal('/content/images/2016/01/myimage.jpg');
});
it('should return absolute author image url if AMP post and has url',
function () {
it('should return absolute author image url if AMP post and has url', function () {
var imageUrl = getAuthorImage({
context: ['amp', 'post'],
post: {
@ -57,8 +57,7 @@ describe('getAuthorImage', function () {
imageUrl.should.match(/\/content\/images\/2016\/01\/myimage\.jpg$/);
});
it('should return null if context does not contain author image url and is a post',
function () {
it('should return null if context does not contain author image url and is a post', function () {
var imageUrl = getAuthorImage({
context: ['post'],
post: {
@ -86,17 +85,17 @@ describe('getAuthorImage', function () {
});
it('should return config theme author image if context is a post and no post',
function () {
config.set({
theme: {
author: {
image: '/content/images/2016/01/myimage.jpg'
function () {
configUtils.set({
theme: {
author: {
image: '/content/images/2016/01/myimage.jpg'
}
}
}
});
var imageUrl = getAuthorImage({
context: ['post']
});
imageUrl.should.match(/\/content\/images\/2016\/01\/myimage\.jpg$/);
});
var imageUrl = getAuthorImage({
context: ['post']
});
imageUrl.should.match(/\/content\/images\/2016\/01\/myimage\.jpg$/);
});
});

View File

@ -1,26 +1,32 @@
var getTitle = require('../../../server/data/meta/title'),
config = require('../../../server/config');
configUtils = require('../../utils/configUtils');
describe('getTitle', function () {
afterEach(function () {
configUtils.restore();
});
it('should return meta_title if on data root', function () {
var title = getTitle({
meta_title: 'My test title'
});
title.should.equal('My test title');
});
it('should return blog title if on home', function () {
config.set({
configUtils.set({
theme: {
title: 'My blog title'
}
});
var title = getTitle({}, {context: 'home'});
title.should.equal('My blog title');
});
it('should return author name - blog title if on data author page', function () {
config.set({
configUtils.set({
theme: {
title: 'My blog title 2'
}
@ -34,11 +40,12 @@ describe('getTitle', function () {
});
it('should return author page title if on data author page with more then one page', function () {
config.set({
configUtils.set({
theme: {
title: 'My blog title 2'
}
});
var title = getTitle({
author: {
name: 'Author Name'
@ -50,29 +57,33 @@ describe('getTitle', function () {
page: 3
}
});
title.should.equal('Author Name - Page 3 - My blog title 2');
});
it('should return tag name - blog title if on data tag page no meta_title', function () {
config.set({
configUtils.set({
theme: {
title: 'My blog title 3'
}
});
var title = getTitle({
tag: {
name: 'Tag Name'
}
}, {context: ['tag']});
title.should.equal('Tag Name - My blog title 3');
});
it('should return tag name - page - blog title if on data tag page no meta_title', function () {
config.set({
configUtils.set({
theme: {
title: 'My blog title 3'
}
});
var title = getTitle({
tag: {
name: 'Tag Name'
@ -84,6 +95,7 @@ describe('getTitle', function () {
page: 39
}
});
title.should.equal('Tag Name - Page 39 - My blog title 3');
});
@ -94,6 +106,7 @@ describe('getTitle', function () {
meta_title: 'My Tag Meta Title!'
}
}, {context: ['tag']});
title.should.equal('My Tag Meta Title!');
});
@ -103,6 +116,7 @@ describe('getTitle', function () {
title: 'My awesome post!'
}
}, {context: ['post']});
title.should.equal('My awesome post!');
});
@ -154,11 +168,12 @@ describe('getTitle', function () {
});
it('should return blog title with page if unknown type', function () {
config.set({
configUtils.set({
theme: {
title: 'My blog title 4'
}
});
var title = getTitle({}, {
context: ['paged'],
pagination: {
@ -166,6 +181,7 @@ describe('getTitle', function () {
page: 35
}
});
title.should.equal('My blog title 4 - Page 35');
});
});

View File

@ -1125,7 +1125,12 @@ describe('Fixtures', function () {
it('sqlite: no UTC update, only format', function (done) {
createdAt = moment(1464798678537).toDate();
configUtils.config.database.client = 'sqlite3';
configUtils.set({
database: {
client: 'sqlite3'
}
});
moment(createdAt).format('YYYY-MM-DD HH:mm:ss').should.eql('2016-06-01 16:31:18');
@ -1149,7 +1154,12 @@ describe('Fixtures', function () {
* we expect 2016-06-01 05:00:00
*/
createdAt = moment('2016-06-01 06:00:00').toDate();
configUtils.config.database.client = 'mysql';
configUtils.set({
database: {
client: 'mysql'
}
});
moment(createdAt).format('YYYY-MM-DD HH:mm:ss').should.eql('2016-06-01 06:00:00');

View File

@ -290,7 +290,7 @@ describe('Url', function () {
describe('urlPathForPost', function () {
it('permalink is /:slug/, timezone is default', function () {
config.theme.permalinks = '/:slug/';
configUtils.set('theme:permalinks', '/:slug/');
var testData = testUtils.DataGenerator.Content.posts[2],
postLink = '/short-and-sweet/';
@ -299,8 +299,8 @@ describe('Url', function () {
});
it('permalink is /:year/:month/:day/:slug, blog timezone is Los Angeles', function () {
config.theme.timezone = 'America/Los_Angeles';
config.theme.permalinks = '/:year/:month/:day/:slug/';
configUtils.set('theme:timezone', 'America/Los_Angeles');
configUtils.set('theme:permalinks', '/:year/:month/:day/:slug/');
var testData = testUtils.DataGenerator.Content.posts[2],
postLink = '/2016/05/17/short-and-sweet/';
@ -310,8 +310,8 @@ describe('Url', function () {
});
it('permalink is /:year/:month/:day/:slug, blog timezone is Asia Tokyo', function () {
config.theme.timezone = 'Asia/Tokyo';
config.theme.permalinks = '/:year/:month/:day/:slug/';
configUtils.set('theme:timezone', 'Asia/Tokyo');
configUtils.set('theme:permalinks', '/:year/:month/:day/:slug/');
var testData = testUtils.DataGenerator.Content.posts[2],
postLink = '/2016/05/18/short-and-sweet/';
@ -321,8 +321,8 @@ describe('Url', function () {
});
it('post is page, no permalink usage allowed at all', function () {
config.theme.timezone = 'America/Los_Angeles';
config.theme.permalinks = '/:year/:month/:day/:slug/';
configUtils.set('theme:timezone', 'America/Los_Angeles');
configUtils.set('theme:permalinks', '/:year/:month/:day/:slug/');
var testData = testUtils.DataGenerator.Content.posts[5],
postLink = '/static-page-test/';
@ -331,8 +331,8 @@ describe('Url', function () {
});
it('permalink is /:year/:id:/:author', function () {
config.theme.timezone = 'America/Los_Angeles';
config.theme.permalinks = '/:year/:id/:author/';
configUtils.set('theme:timezone', 'America/Los_Angeles');
configUtils.set('theme:permalinks', '/:year/:id/:author/');
var testData = _.merge(testUtils.DataGenerator.Content.posts[2], {id: 3}, {author: {slug: 'joe-blog'}}),
postLink = '/2015/3/joe-blog/';
@ -342,8 +342,8 @@ describe('Url', function () {
});
it('permalink is /:year/:id:/:author', function () {
config.theme.timezone = 'Europe/Berlin';
config.theme.permalinks = '/:year/:id/:author/';
configUtils.set('theme:timezone', 'Europe/Berlin');
configUtils.set('theme:permalinks', '/:year/:id/:author/');
var testData = _.merge(testUtils.DataGenerator.Content.posts[2], {id: 3}, {author: {slug: 'joe-blog'}}),
postLink = '/2016/3/joe-blog/';
@ -353,7 +353,7 @@ describe('Url', function () {
});
it('post is not published yet', function () {
config.theme.permalinks = '/:year/:month/:day/:slug/';
configUtils.set('theme:permalinks', '/:year/:month/:day/:slug/');
var testData = _.merge(testUtils.DataGenerator.Content.posts[2], {id: 3, published_at: null}),
nowMoment = moment(),

View File

@ -1,25 +1,34 @@
var _ = require('lodash'),
config = require('../../server/config'),
origConfig = _.cloneDeep(config),
var _ = require('lodash'),
config = require('../../server/config'),
configUtils = {};
configUtils.config = config;
configUtils.defaultConfig = _.cloneDeep(config.get());
configUtils.set = function (newConfig) {
config.set(newConfig);
/**
* configUtils.set({});
* configUtils.set('key', 'value');
*/
configUtils.set = function () {
var key = arguments[0],
value = arguments[1];
if (_.isObject(key)) {
_.each(key, function (value, key) {
config.set(key, value);
});
} else {
config.set(key, value);
}
};
/**
* important: do not delete cloneDeep for value
* nconf keeps this as a reference and then it can happen that the defaultConfig get's overridden by new values
*/
configUtils.restore = function () {
var topLevelOptional = ['mail', 'updateCheck', 'storage', 'forceAdminSSL', 'urlSSL', 'compress', 'privacy'];
config.set(_.merge({}, origConfig, configUtils.defaultConfig));
// @TODO make this horror go away
_.each(topLevelOptional, function (option) {
if (origConfig[option] === undefined) {
delete config[option];
}
_.each(configUtils.defaultConfig, function (value, key) {
config.set(key, _.cloneDeep(value));
});
};