0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Fixed same redirectManager used in offer service between tests (#14340)

refs https://ghost.slack.com/archives/C02G9E68C/p1647599592576139
refs https://ghost.slack.com/archives/C02G9E68C/p1647620250625909

Issue: `Cannot destructure property 'fromRegex' of 'this.redirectsredirectId]' as it is undefined.` is being thrown, only when running all tests.
Cause: duplicate redirects are added to a redirectManager, and not cleared correctly in the redirectManager, which throws an error when removing one of the duplicate redirects.

Because the same redirectManager is used, multiple event listeners are connected to the same redirectManager. So when the offer service has been initialised multiple times, multiple listeners are added, which create a redirect for every newly created offer... to the same redirectManager.
So there are three possible fixes for the same problem (would be best to fix them all):
- Create a new redirectManager every time the offer service is initialised
- Figure out a way to remove DomainEvents subscribers between tests
- Don't add the same redirect id multiple times to redirectIds in addRedirect from the express-dynamic-redirects package

This commit contains a fix for the first solution.

It also moved the offers service initialising before the frontend (to `initServicesForFrontend`)
This commit is contained in:
Simon Backx 2022-03-24 10:18:52 +01:00 committed by GitHub
parent d5942943a5
commit 6bd1806650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 10 deletions

View file

@ -148,6 +148,11 @@ async function initServicesForFrontend() {
await themeService.init();
debug('End: Themes');
debug('Begin: Offers');
const offers = require('./server/services/offers');
await offers.init();
debug('End: Offers');
debug('End: initServicesForFrontend');
}
@ -239,7 +244,6 @@ async function initServices({config}) {
debug('Begin: Services');
const stripe = require('./server/services/stripe');
const members = require('./server/services/members');
const offers = require('./server/services/offers');
const permissions = require('./server/services/permissions');
const xmlrpc = require('./server/services/xmlrpc');
const slack = require('./server/services/slack');
@ -258,7 +262,6 @@ async function initServices({config}) {
// NOTE: Members service depends on these
// so they are initialized before it.
await stripe.init();
await offers.init();
await Promise.all([
members.init(),

View file

@ -5,19 +5,20 @@ const config = require('../../../shared/config');
const urlUtils = require('../../../shared/url-utils');
const models = require('../../models');
const redirectManager = new DynamicRedirectManager({
permanentMaxAge: config.get('caching:customRedirects:maxAge'),
getSubdirectoryURL: (pathname) => {
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
}
});
let redirectManager;
module.exports = {
async init() {
redirectManager = new DynamicRedirectManager({
permanentMaxAge: config.get('caching:customRedirects:maxAge'),
getSubdirectoryURL: (pathname) => {
return urlUtils.urlJoin(urlUtils.getSubdir(), pathname);
}
});
const offersModule = OffersModule.create({
OfferModel: models.Offer,
OfferRedemptionModel: models.OfferRedemption,
redirectManager: redirectManager
redirectManager
});
this.api = offersModule.api;
@ -27,5 +28,7 @@ module.exports = {
api: null,
middleware: redirectManager.handleRequest
get middleware() {
return redirectManager.handleRequest;
}
};