Ghost/ghost/core/test/integration/url_service.test.js
Simon Backx 8f8ca481a6
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 14:06:20 +01:00

310 lines
12 KiB
JavaScript

const _ = require('lodash');
const should = require('should');
const sinon = require('sinon');
const rewire = require('rewire');
const testUtils = require('../utils');
const configUtils = require('../utils/configUtils');
const models = require('../../core/server/models');
const UrlService = require('../../core/server/services/url/UrlService');
describe('Integration: services/url/UrlService', function () {
let urlService;
before(function () {
models.init();
});
before(testUtils.teardownDb);
before(testUtils.setup('users:roles', 'posts'));
after(testUtils.teardownDb);
after(function () {
sinon.restore();
});
describe('functional: default routing set', function () {
before(function (done) {
urlService = new UrlService();
urlService.onRouterAddedType('unique-id-1', 'featured:false', 'posts', '/:slug/');
urlService.onRouterAddedType('unique-id-2', null, 'authors', '/author/:slug/');
urlService.onRouterAddedType('unique-id-3', null, 'tags', '/tag/:slug/');
urlService.onRouterAddedType('unique-id-4', null, 'pages', '/:slug/');
// We can't use our url service utils here, because this is a local copy of the urlService, not the singletone
urlService.init();
let timeout;
(function retry() {
clearTimeout(timeout);
if (urlService.hasFinished()) {
return done();
}
setTimeout(retry, 50);
})();
});
after(function () {
urlService.reset();
});
it('getUrl', function () {
urlService.urlGenerators.forEach(function (generator) {
if (generator.resourceType === 'posts') {
generator.getUrls().length.should.eql(2);
}
if (generator.resourceType === 'pages') {
generator.getUrls().length.should.eql(1);
}
if (generator.resourceType === 'tags') {
generator.getUrls().length.should.eql(3);
}
if (generator.resourceType === 'authors') {
generator.getUrls().length.should.eql(2);
}
});
let url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[0].id);
url.should.eql('/html-ipsum/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[1].id);
url.should.eql('/ghostly-kitchen-sink/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[2].id);
url.should.eql('/404/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[0].id);
url.should.eql('/tag/kitchen-sink/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[1].id);
url.should.eql('/tag/bacon/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[2].id);
url.should.eql('/tag/chorizo/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[3].id);
url.should.eql('/404/'); // tags with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[0].id);
url.should.eql('/author/joe-bloggs/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[1].id);
url.should.eql('/404/'); // users with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[2].id);
url.should.eql('/404/'); // users with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[3].id);
url.should.eql('/author/slimer-mcectoplasm/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[4].id);
url.should.eql('/404/'); // users with no posts should not be public
});
it('getResource', function () {
let resource = urlService.getResource('/html-ipsum/');
resource.data.id.should.eql(testUtils.DataGenerator.forKnex.posts[0].id);
resource = urlService.getResource('/does-not-exist/');
should.not.exist(resource);
});
});
describe('functional: extended/modified routing set', function () {
before(testUtils.teardownDb);
before(testUtils.setup('users:roles', 'posts'));
before(function () {
urlService.resetGenerators();
});
before(function (done) {
urlService = new UrlService();
urlService.onRouterAddedType('unique-id-1', 'featured:true', 'posts', '/podcast/:slug/');
urlService.onRouterAddedType('unique-id-2', 'type:post', 'posts', '/collection/:year/:slug/');
urlService.onRouterAddedType('unique-id-3', null, 'authors', '/persons/:slug/');
urlService.onRouterAddedType('unique-id-4', null, 'tags', '/category/:slug/');
urlService.onRouterAddedType('unique-id-5', null, 'pages', '/:slug/');
// We can't use our url service utils here, because this is a local copy of the urlService, not the singletone
urlService.init();
let timeout;
(function retry() {
clearTimeout(timeout);
if (urlService.hasFinished()) {
return done();
}
setTimeout(retry, 50);
})();
});
after(function () {
urlService.resetGenerators();
});
it('getUrl', function () {
urlService.urlGenerators.forEach(function (generator) {
if (generator.resourceType === 'posts' && generator.filter === 'type:post') {
generator.getUrls().length.should.eql(2);
}
if (generator.resourceType === 'posts' && generator.filter === 'featured:true') {
generator.getUrls().length.should.eql(2);
}
if (generator.resourceType === 'pages') {
generator.getUrls().length.should.eql(1);
}
if (generator.resourceType === 'tags') {
generator.getUrls().length.should.eql(3);
}
if (generator.resourceType === 'authors') {
generator.getUrls().length.should.eql(2);
}
});
let url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[0].id);
url.should.eql('/collection/2015/html-ipsum/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[1].id);
url.should.eql('/collection/2015/ghostly-kitchen-sink/');
// featured
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[2].id);
url.should.eql('/podcast/short-and-sweet/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[0].id);
url.should.eql('/category/kitchen-sink/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[1].id);
url.should.eql('/category/bacon/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[2].id);
url.should.eql('/category/chorizo/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[3].id);
url.should.eql('/404/'); // tags with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[0].id);
url.should.eql('/persons/joe-bloggs/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[1].id);
url.should.eql('/404/'); // users with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[2].id);
url.should.eql('/404/'); // users with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[3].id);
url.should.eql('/persons/slimer-mcectoplasm/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[4].id);
url.should.eql('/404/'); // users with no posts should not be public
});
});
describe('functional: subdirectory', function () {
beforeEach(function (done) {
configUtils.set('url', 'http://localhost:2388/blog/');
urlService = new UrlService();
urlService.onRouterAddedType('unique-id-1', 'featured:false', 'posts', '/collection/:year/:slug/');
urlService.onRouterAddedType('unique-id-2', 'featured:true', 'posts', '/podcast/:slug/');
urlService.onRouterAddedType('unique-id-3', null, 'authors', '/persons/:slug/');
urlService.onRouterAddedType('unique-id-4', null, 'tags', '/category/:slug/');
urlService.onRouterAddedType('unique-id-5', null, 'pages', '/:slug/');
// We can't use our url service utils here, because this is a local copy of the urlService, not the singletone
urlService.init();
let timeout;
(function retry() {
clearTimeout(timeout);
if (urlService.hasFinished()) {
return done();
}
setTimeout(retry, 50);
})();
});
afterEach(async function () {
urlService.resetGenerators();
await configUtils.restore();
});
it('getUrl', function () {
urlService.urlGenerators.forEach(function (generator) {
if (generator.resourceType === 'posts' && generator.filter === 'featured:false') {
generator.getUrls().length.should.eql(2);
}
if (generator.resourceType === 'posts' && generator.filter === 'featured:true') {
generator.getUrls().length.should.eql(2);
}
if (generator.resourceType === 'pages') {
generator.getUrls().length.should.eql(1);
}
if (generator.resourceType === 'tags') {
generator.getUrls().length.should.eql(3);
}
if (generator.resourceType === 'authors') {
generator.getUrls().length.should.eql(2);
}
});
let url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[0].id);
url.should.eql('/collection/2015/html-ipsum/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[1].id);
url.should.eql('/collection/2015/ghostly-kitchen-sink/');
// featured
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.posts[2].id);
url.should.eql('/podcast/short-and-sweet/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[0].id);
url.should.eql('/category/kitchen-sink/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[1].id);
url.should.eql('/category/bacon/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[2].id);
url.should.eql('/category/chorizo/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.tags[3].id);
url.should.eql('/404/'); // tags with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[0].id);
url.should.eql('/persons/joe-bloggs/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[1].id);
url.should.eql('/404/'); // users with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[2].id);
url.should.eql('/404/'); // users with no posts should not be public
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[3].id);
url.should.eql('/persons/slimer-mcectoplasm/');
url = urlService.getUrlByResourceId(testUtils.DataGenerator.forKnex.users[4].id);
url.should.eql('/404/'); // users with no posts should not be public
});
});
});