🐛 Fixed setting Tier prices after changing Stripe accounts

refs https://github.com/TryGhost/Team/issues/1212

This now emits the event when the service is reconfigured, rather than
when we issue the reconfigure command, which causes the event and the
action to be run in the wrong order. This would then cause knock on effects
of having the database in an undefined state - with stripe data in not linked
to the current Stripe account.
This commit is contained in:
Fabien 'egg' O'Carroll 2021-11-10 14:03:03 +02:00 committed by GitHub
parent edff6e0ef1
commit 7fae5b8341
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View File

@ -27,7 +27,10 @@ function configureApi() {
}
}
const debouncedConfigureApi = _.debounce(configureApi, 600);
const debouncedConfigureApi = _.debounce(() => {
configureApi();
events.emit('services.stripe.reconfigured');
}, 600);
module.exports = {
async init() {
@ -37,7 +40,6 @@ module.exports = {
return;
}
debouncedConfigureApi();
events.emit('services.stripe.reconfigured');
});
},

View File

@ -0,0 +1,38 @@
const sinon = require('sinon');
const rewire = require('rewire');
const events = require('events');
const rewiredStripeService = rewire('../../../core/server/services/stripe');
describe('Stripe Service', function () {
beforeEach(function () {
this.clock = sinon.useFakeTimers();
});
afterEach(function () {
this.clock.restore();
});
it('Emits a "services.stripe.reconfigured" event when it is reconfigured', async function () {
const eventsStub = new events.EventEmitter();
const configureApiStub = sinon.spy();
const emitReconfiguredEventSpy = sinon.spy(eventsStub, 'emit').withArgs('services.stripe.reconfigured');
rewiredStripeService.__set__('events', eventsStub);
await rewiredStripeService.init();
// This is _after_ init, because init calls configureApi, and we DGAF about that call.
rewiredStripeService.__set__('configureApi', configureApiStub);
eventsStub.emit('settings.edited', {
get: sinon.stub().withArgs('key').returns('stripe_connect_secret_key')
});
this.clock.tick(600);
sinon.assert.callOrder(configureApiStub, emitReconfiguredEventSpy);
});
});