diff --git a/test/regression/site/dynamic_routing_spec.js b/test/regression/site/dynamic_routing_spec.js index 3bd3ab1325..e3b9e7a91a 100644 --- a/test/regression/site/dynamic_routing_spec.js +++ b/test/regression/site/dynamic_routing_spec.js @@ -731,7 +731,7 @@ describe('Dynamic Routing', function () { path: path.join(config.get('paths:appRoot'), 'test', 'utils', 'fixtures', 'settings', 'newroutes.yaml') } }).then(() => { - return testUtils.integrationTesting.urlService.waitTillFinished({dbIsReady: true}); + return testUtils.integrationTesting.urlService.isFinished({disableDbReadyEvent: true}); }); }); diff --git a/test/regression/site/site_spec.js b/test/regression/site/site_spec.js index af06831867..bfd7c07d74 100644 --- a/test/regression/site/site_spec.js +++ b/test/regression/site/site_spec.js @@ -33,7 +33,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); @@ -402,7 +402,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -519,7 +519,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -580,7 +580,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -682,7 +682,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -768,7 +768,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -926,7 +926,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -1043,7 +1043,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -1113,7 +1113,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -1172,7 +1172,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -1358,7 +1358,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(10); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -1608,7 +1608,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -1731,7 +1731,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); @@ -2100,7 +2100,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2219,7 +2219,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2280,7 +2280,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2382,7 +2382,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2468,7 +2468,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2626,7 +2626,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2743,7 +2743,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2813,7 +2813,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -2872,7 +2872,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -3058,7 +3058,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(10); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -3308,7 +3308,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -3431,7 +3431,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); @@ -3802,7 +3802,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -3919,7 +3919,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -3980,7 +3980,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4082,7 +4082,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4168,7 +4168,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4326,7 +4326,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4443,7 +4443,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4513,7 +4513,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4572,7 +4572,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -4758,7 +4758,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(10); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -5009,7 +5009,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }); }); @@ -5133,7 +5133,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); @@ -5252,7 +5252,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); @@ -5414,7 +5414,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); @@ -5461,7 +5461,7 @@ describe('Integration - Web - Site', function () { sinon.stub(themeService.getActive(), 'config').withArgs('posts_per_page').returns(2); app = siteApp({start: true}); - return testUtils.integrationTesting.urlService.waitTillFinished(); + return testUtils.integrationTesting.urlService.isFinished(); }) .then(() => { return appService.init(); diff --git a/test/utils/db-utils.js b/test/utils/db-utils.js index 72121ab54c..6e980aa8ab 100644 --- a/test/utils/db-utils.js +++ b/test/utils/db-utils.js @@ -12,26 +12,13 @@ const db = require('../../core/server/data/db'); const schema = require('../../core/server/data/schema').tables; const schemaTables = Object.keys(schema); -const urlService = require('../../core/frontend/services/url'); +// Other Test Utilities +const urlServiceUtils = require('./url-service-utils'); module.exports.initData = () => { return knexMigrator.init() .then(function () { - events.emit('db.ready'); - - let timeout; - - return new Promise(function (resolve) { - (function retry() { - clearTimeout(timeout); - - if (urlService.hasFinished()) { - return resolve(); - } - - timeout = setTimeout(retry, 50); - })(); - }); + return urlServiceUtils.isFinished(); }); }; @@ -58,7 +45,7 @@ module.exports.clearData = () => { debug('Database reset'); return knexMigrator.reset({force: true}) .then(function () { - urlService.softReset(); + urlServiceUtils.reset(); }); }; @@ -68,7 +55,7 @@ module.exports.clearData = () => { */ module.exports.teardown = () => { debug('Database teardown'); - urlService.softReset(); + urlServiceUtils.reset(); const tables = schemaTables.concat(['migrations']); diff --git a/test/utils/index.js b/test/utils/index.js index 4ea834ca01..214fd16464 100644 --- a/test/utils/index.js +++ b/test/utils/index.js @@ -21,11 +21,9 @@ const db = require('../../core/server/data/db'); const models = require('../../core/server/models'); const urlUtils = require('../../core/shared/url-utils'); const urlService = require('../../core/frontend/services/url'); -const routingService = require('../../core/frontend/services/routing'); const settingsService = require('../../core/server/services/settings'); const frontendSettingsService = require('../../core/frontend/services/settings'); const settingsCache = require('../../core/server/services/settings/cache'); -const imageLib = require('../../core/server/lib/image'); const web = require('../../core/server/web'); const themes = require('../../core/frontend/services/themes'); @@ -34,6 +32,8 @@ const APIUtils = require('./api'); const configUtils = require('./configUtils'); const dbUtils = require('./db-utils'); const fixtureUtils = require('./fixture-utils'); +const urlServiceUtils = require('./url-service-utils'); +const oldIntegrationUtils = require('./old-integration-utils'); const redirects = require('./redirects'); const context = require('./fixtures/context'); const DataGenerator = require('./fixtures/data-generator'); @@ -180,22 +180,8 @@ const startGhost = function startGhost(options) { return themes.init(); }) .then(function () { - urlService.softReset(); - events.emit('db.ready'); - - let timeout; - - return new Promise(function (resolve) { - (function retry() { - clearTimeout(timeout); - - if (urlService.hasFinished()) { - return resolve(); - } - - timeout = setTimeout(retry, 50); - })(); - }); + urlServiceUtils.reset(); + return urlServiceUtils.isFinished(); }) .then(function () { web.shared.middlewares.customRedirects.reload(); @@ -324,93 +310,6 @@ module.exports = { }); } }, - - integrationTesting: { - overrideGhostConfig: function overrideGhostConfig(utils) { - utils.set('paths:contentPath', path.join(__dirname, 'fixtures')); - utils.set('times:getImageSizeTimeoutInMS', 1); - }, - - defaultMocks: function defaultMocks(sandbox, options) { - options = options || {}; - - configUtils.set('paths:contentPath', path.join(__dirname, 'fixtures')); - - const cacheStub = sandbox.stub(settingsCache, 'get'); - - cacheStub.withArgs('active_theme').returns(options.theme || 'casper'); - cacheStub.withArgs('timezone').returns('Etc/UTC'); - cacheStub.withArgs('permalinks').returns('/:slug/'); - cacheStub.withArgs('ghost_private_key').returns('-----BEGIN RSA PRIVATE KEY-----\nMB8CAQACAgPBAgMBAAECAgMFAgEfAgEfAgEXAgEXAgEA\n-----END RSA PRIVATE KEY-----\n'); - cacheStub.withArgs('ghost_public_key').returns('-----BEGIN RSA PUBLIC KEY-----\nMAkCAgPBAgMBAAE=\n-----END RSA PUBLIC KEY-----\n'); - - if (options.amp) { - cacheStub.withArgs('amp').returns(true); - } - - sandbox.stub(imageLib.imageSize, 'getImageSizeFromUrl').resolves(); - }, - - initGhost: function () { - models.init(); - - settingsCache.shutdown(); - - return settingsService.init() - .then(() => { - return themes.init(); - }); - }, - - routing: { - reset: function () { - routingService.registry.resetAll(); - } - }, - - urlService: { - waitTillFinished: function (options = {dbIsReady: false}) { - let timeout; - - if (!options.dbIsReady) { - events.emit('db.ready'); - } - - return new Promise(function (resolve) { - (function retry() { - clearTimeout(timeout); - - if (urlService.hasFinished()) { - return resolve(); - } - - timeout = setTimeout(retry, 50); - })(); - }); - }, - - init: function () { - const routes = frontendSettingsService.get('routes'); - - const collectionRouter = new routingService.CollectionRouter('/', routes.collections['/']); - const tagRouter = new routingService.TaxonomyRouter('tag', routes.taxonomies.tag); - const authorRouter = new routingService.TaxonomyRouter('author', routes.taxonomies.author); - - events.emit('db.ready'); - - return this.waitTillFinished(); - }, - - reset: function () { - urlService.softReset(); - }, - - resetGenerators: function () { - urlService.resetGenerators(); - urlService.resources.reset({ignoreDBReady: true}); - } - } - }, teardownDb: dbUtils.teardown, truncate: dbUtils.truncate, setup: setup, @@ -418,6 +317,8 @@ module.exports = { createPost: createPost, createEmailedPost, + integrationTesting: oldIntegrationUtils, + /** * renderObject: res.render(view, dbResponse) * templateOptions: hbs.updateTemplateOptions(...) diff --git a/test/utils/old-integration-utils.js b/test/utils/old-integration-utils.js new file mode 100644 index 0000000000..768f4b76c4 --- /dev/null +++ b/test/utils/old-integration-utils.js @@ -0,0 +1,60 @@ +// Utility Packages +const path = require('path'); + +// Ghost Internals +const models = require('../../core/server/models'); +const routingService = require('../../core/frontend/services/routing'); +const settingsService = require('../../core/server/services/settings'); +const settingsCache = require('../../core/server/services/settings/cache'); +const imageLib = require('../../core/server/lib/image'); +const themes = require('../../core/frontend/services/themes'); + +// Other Test Utilities +const configUtils = require('./configUtils'); +const urlServiceUtils = require('./url-service-utils'); + +module.exports = { + overrideGhostConfig: (utils) => { + utils.set('paths:contentPath', path.join(__dirname, 'fixtures')); + utils.set('times:getImageSizeTimeoutInMS', 1); + }, + + defaultMocks: (sandbox, options) => { + options = options || {}; + + configUtils.set('paths:contentPath', path.join(__dirname, 'fixtures')); + + const cacheStub = sandbox.stub(settingsCache, 'get'); + + cacheStub.withArgs('active_theme').returns(options.theme || 'casper'); + cacheStub.withArgs('timezone').returns('Etc/UTC'); + cacheStub.withArgs('permalinks').returns('/:slug/'); + cacheStub.withArgs('ghost_private_key').returns('-----BEGIN RSA PRIVATE KEY-----\nMB8CAQACAgPBAgMBAAECAgMFAgEfAgEfAgEXAgEXAgEA\n-----END RSA PRIVATE KEY-----\n'); + cacheStub.withArgs('ghost_public_key').returns('-----BEGIN RSA PUBLIC KEY-----\nMAkCAgPBAgMBAAE=\n-----END RSA PUBLIC KEY-----\n'); + + if (options.amp) { + cacheStub.withArgs('amp').returns(true); + } + + sandbox.stub(imageLib.imageSize, 'getImageSizeFromUrl').resolves(); + }, + + initGhost: () => { + models.init(); + + settingsCache.shutdown(); + + return settingsService.init() + .then(() => { + return themes.init(); + }); + }, + + routing: { + reset: function () { + routingService.registry.resetAll(); + } + }, + + urlService: urlServiceUtils +}; diff --git a/test/utils/url-service-utils.js b/test/utils/url-service-utils.js new file mode 100644 index 0000000000..5ca01f36d7 --- /dev/null +++ b/test/utils/url-service-utils.js @@ -0,0 +1,31 @@ +const urlService = require('../../core/frontend/services/url'); +const events = require('../../core/server/lib/common/events'); + +module.exports.isFinished = async (options = {disableDbReadyEvent: false}) => { + let timeout; + + if (options.disableDbReadyEvent === false) { + events.emit('db.ready'); + } + + return new Promise(function (resolve) { + (function retry() { + clearTimeout(timeout); + + if (urlService.hasFinished()) { + return resolve(); + } + + timeout = setTimeout(retry, 50); + })(); + }); +}; + +module.exports.reset = () => { + urlService.softReset(); +}, + +module.exports.resetGenerators = () => { + urlService.resetGenerators(); + urlService.resources.reset({ignoreDBReady: true}); +};