2019-06-18 16:13:55 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const UrlUtils = require('@tryghost/url-utils');
|
2021-06-17 16:05:30 +03:00
|
|
|
const configUtils = require('./configUtils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../core/shared/config');
|
2020-05-28 13:57:02 +03:00
|
|
|
const urlUtils = require('../../core/shared/url-utils');
|
2019-06-18 16:13:55 +03:00
|
|
|
|
|
|
|
const defaultSandbox = sinon.createSandbox();
|
|
|
|
|
|
|
|
const getInstance = (options) => {
|
|
|
|
const opts = {
|
2021-06-17 16:05:30 +03:00
|
|
|
getSubdir: config.getSubdir,
|
|
|
|
getSiteUrl: config.getSiteUrl,
|
|
|
|
getAdminUrl: config.getAdminUrl,
|
2019-06-18 16:13:55 +03:00
|
|
|
apiVersions: options.apiVersions,
|
2022-01-21 17:50:56 +03:00
|
|
|
defaultApiVersion: 'canary',
|
2019-06-18 16:13:55 +03:00
|
|
|
slugs: options.slugs,
|
|
|
|
redirectCacheMaxAge: options.redirectCacheMaxAge,
|
|
|
|
baseApiPath: '/ghost/api'
|
|
|
|
};
|
|
|
|
|
|
|
|
return new UrlUtils(opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
const stubUrlUtils = (options, sandbox) => {
|
|
|
|
const stubInstance = getInstance(options);
|
2019-08-12 11:31:42 +03:00
|
|
|
const classPropNames = Object.getOwnPropertyNames(Object.getPrototypeOf(urlUtils))
|
|
|
|
.filter(name => name !== 'constructor');
|
|
|
|
|
|
|
|
classPropNames.forEach((key) => {
|
|
|
|
if (typeof urlUtils[key] === 'function') {
|
|
|
|
sandbox.stub(urlUtils, key).callsFake(function () {
|
|
|
|
return stubInstance[key](...arguments);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sandbox.stub(urlUtils, key).get(function () {
|
|
|
|
return stubInstance[key];
|
|
|
|
});
|
|
|
|
}
|
2019-06-18 16:13:55 +03:00
|
|
|
});
|
2021-05-31 19:58:54 +03:00
|
|
|
|
|
|
|
return stubInstance;
|
2019-06-18 16:13:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Method for regressions tests must be used with restore method
|
|
|
|
const stubUrlUtilsFromConfig = () => {
|
|
|
|
const options = {
|
|
|
|
apiVersions: config.get('api:versions'),
|
2022-01-21 17:50:56 +03:00
|
|
|
defaultApiVersion: 'canary',
|
2019-06-18 16:13:55 +03:00
|
|
|
slugs: config.get('slugs').protected,
|
|
|
|
redirectCacheMaxAge: config.get('caching:301:maxAge'),
|
|
|
|
baseApiPath: '/ghost/api'
|
|
|
|
};
|
2021-05-31 19:58:54 +03:00
|
|
|
|
|
|
|
return stubUrlUtils(options, defaultSandbox);
|
2019-06-18 16:13:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const restore = () => {
|
|
|
|
defaultSandbox.restore();
|
2021-06-17 16:05:30 +03:00
|
|
|
configUtils.restore();
|
2019-06-18 16:13:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.stubUrlUtilsFromConfig = stubUrlUtilsFromConfig;
|
|
|
|
module.exports.restore = restore;
|
|
|
|
module.exports.getInstance = getInstance;
|