Split urlServiceUtils from testUtils & unified

- There is now just one way to wait until the UrlService is finished across all of our tests!
- db.ready is only called in the one utility + the actual UrlService tests
This commit is contained in:
Hannah Wolfe 2021-02-17 17:36:27 +00:00
parent d3b50c8af3
commit 1b4ebce3b0
6 changed files with 143 additions and 164 deletions

View File

@ -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});
});
});

View File

@ -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();

View File

@ -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']);

View File

@ -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(...)

View File

@ -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
};

View File

@ -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});
};