2019-06-18 16:13:55 +03:00
|
|
|
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
|
|
|
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 = {
|
|
|
|
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();
|
Fixed configUtils and adapter cache issues in E2E tests (#16167)
no issue
There are a couple of issues with resetting the Ghost instance between
E2E test files:
These issues came to the surface because of new tests written in
https://github.com/TryGhost/Ghost/pull/16117
**1. configUtils.restore does not work correctly**
`config.reset()` is a callback based method. On top of that, it doesn't
really work reliably (https://github.com/indexzero/nconf/issues/93)
What kinda happens, is that you first call `config.reset` but
immediately after you correcty reset the config using the `config.set`
calls afterwards. But since `config.reset` is async, that reset will
happen after all those sets, and the end result is that it isn't reset
correctly.
This mainly caused issues in the new updated images tests, which were
updating the config `imageOptimization.contentImageSizes`, which is a
deeply nested config value. Maybe some references to objects are reused
in nconf that cause this issue?
Wrapping `config.reset()` in a promise does fix the issue.
**2. Adapters cache not reset between tests**
At the start of each test, we set `paths:contentPath` to a nice new
temporary directory. But if a previous test already requests a
localStorage adapter, that adapter would have been created and in the
constructor `paths:contentPath` would have been passed. That same
instance will be reused in the next test run. So it won't read the new
config again. To fix this, we need to reset the adapter instances
between E2E tests.
How was this visible? Test uploads were stored in the actual git
repository, and not in a temporary directory. When writing the new image
upload tests, this also resulted in unreliable test runs because some
image names were already taken (from previous test runs).
**3. Old 2E2 test Ghost server not stopped**
Sometimes we still need access to the frontend test server using
`getAgentsWithFrontend`. But that does start a new Ghost server which is
actually listening for HTTP traffic. This could result in a fatal error
in tests because the port is already in use. The issue is that old E2E
tests also start a HTTP server, but they don't stop the server. When you
used the old `startGhost` util, it would check if a server was already
running and stop it first. The new `getAgentsWithFrontend` now also has
the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
configUtils.restore().catch(console.error);
|
2019-06-18 16:13:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports.stubUrlUtilsFromConfig = stubUrlUtilsFromConfig;
|
|
|
|
module.exports.restore = restore;
|
|
|
|
module.exports.getInstance = getInstance;
|