From b158a3a9443f7d425bc1734261c8a56af312b34c Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Tue, 13 Sep 2016 18:35:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20=20change=20logic=20for=20test/u?= =?UTF-8?q?tils/configUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #6982 - adaption because of using nconf - change tests which changed config directly [ci skip] --- core/test/integration/api/api_mail_spec.js | 11 +++-- core/test/integration/api/api_posts_spec.js | 4 +- .../integration/model/model_posts_spec.js | 14 ++---- .../controllers/frontend/channels_spec.js | 28 ++++++------ .../controllers/frontend/fetch-data_spec.js | 17 +++----- core/test/unit/metadata/author_image_spec.js | 43 +++++++++---------- core/test/unit/metadata/title_spec.js | 30 ++++++++++--- core/test/unit/migration_fixture_spec.js | 14 +++++- core/test/unit/utils/url_spec.js | 24 +++++------ core/test/utils/configUtils.js | 37 ++++++++++------ 10 files changed, 123 insertions(+), 99 deletions(-) diff --git a/core/test/integration/api/api_mail_spec.js b/core/test/integration/api/api_mail_spec.js index 3253d9fc22..f07942711e 100644 --- a/core/test/integration/api/api_mail_spec.js +++ b/core/test/integration/api/api_mail_spec.js @@ -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'); diff --git a/core/test/integration/api/api_posts_spec.js b/core/test/integration/api/api_posts_spec.js index 001011d020..85ce628753 100644 --- a/core/test/integration/api/api_posts_spec.js +++ b/core/test/integration/api/api_posts_spec.js @@ -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 () { diff --git a/core/test/integration/model/model_posts_spec.js b/core/test/integration/model/model_posts_spec.js index c18e3ea214..948caac0eb 100644 --- a/core/test/integration/model/model_posts_spec.js +++ b/core/test/integration/model/model_posts_spec.js @@ -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) { diff --git a/core/test/unit/controllers/frontend/channels_spec.js b/core/test/unit/controllers/frontend/channels_spec.js index 0d4e0e4756..868fe6a95e 100644 --- a/core/test/unit/controllers/frontend/channels_spec.js +++ b/core/test/unit/controllers/frontend/channels_spec.js @@ -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); diff --git a/core/test/unit/controllers/frontend/fetch-data_spec.js b/core/test/unit/controllers/frontend/fetch-data_spec.js index 015f89eb61..10ee405ae0 100644 --- a/core/test/unit/controllers/frontend/fetch-data_spec.js +++ b/core/test/unit/controllers/frontend/fetch-data_spec.js @@ -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) { diff --git a/core/test/unit/metadata/author_image_spec.js b/core/test/unit/metadata/author_image_spec.js index 1136031251..dc882e5f7b 100644 --- a/core/test/unit/metadata/author_image_spec.js +++ b/core/test/unit/metadata/author_image_spec.js @@ -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$/); - }); }); diff --git a/core/test/unit/metadata/title_spec.js b/core/test/unit/metadata/title_spec.js index 742f43167f..27ab17c76a 100644 --- a/core/test/unit/metadata/title_spec.js +++ b/core/test/unit/metadata/title_spec.js @@ -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'); }); }); diff --git a/core/test/unit/migration_fixture_spec.js b/core/test/unit/migration_fixture_spec.js index 94bf2b2b35..b14bdd5ed4 100644 --- a/core/test/unit/migration_fixture_spec.js +++ b/core/test/unit/migration_fixture_spec.js @@ -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'); diff --git a/core/test/unit/utils/url_spec.js b/core/test/unit/utils/url_spec.js index aa4d22e999..8d2e3083ee 100644 --- a/core/test/unit/utils/url_spec.js +++ b/core/test/unit/utils/url_spec.js @@ -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(), diff --git a/core/test/utils/configUtils.js b/core/test/utils/configUtils.js index f2ea9b90ea..7a2114f29e 100644 --- a/core/test/utils/configUtils.js +++ b/core/test/utils/configUtils.js @@ -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)); }); };