Ghost/ghost/members-api/test/unit/lib/controllers/router.test.js
Rishabh Garg 8bdad78377
🐛 Fixed broken redemption count for offers (#15954)
refs https://github.com/TryGhost/Team/issues/2369

- offer id was not getting attached to stripe checkout metadata, causing the checkout event to not store any offer information for a subscription. This got changed in a prev refactor [here](25d8d694a0 (diff-b7dfcd660902a2a20dff7da5e886d8e10234bda4ba78228255afc8d4a8e78cf6L206))
- cleans up offer id handling for checkout session event
2022-12-07 14:30:11 +05:30

94 lines
2.8 KiB
JavaScript

const sinon = require('sinon');
const RouterController = require('../../../../lib/controllers/router');
describe('RouterController', function () {
describe('createCheckoutSession', function (){
let offersAPI;
let paymentsService;
let tiersService;
let stripeAPIService;
let labsService;
let getPaymentLinkSpy;
beforeEach(async function () {
getPaymentLinkSpy = sinon.spy();
tiersService = {
api: {
read: sinon.stub().resolves({
id: 'tier_123'
})
}
};
paymentsService = {
getPaymentLink: getPaymentLinkSpy
};
offersAPI = {
getOffer: sinon.stub().resolves({
id: 'offer_123',
tier: {
id: 'tier_123'
}
}),
findOne: sinon.stub().resolves({
related: () => {
return {
query: sinon.stub().returns({
fetchOne: sinon.stub().resolves({})
}),
toJSON: sinon.stub().returns([]),
fetch: sinon.stub().resolves({
toJSON: sinon.stub().returns({})
})
};
},
toJSON: sinon.stub().returns({})
}),
edit: sinon.stub().resolves({
attributes: {},
_previousAttributes: {}
})
};
stripeAPIService = {
configured: true
};
labsService = {
isSet: sinon.stub().returns(true)
};
});
it('passes offer metadata to payment link method', async function (){
const routerController = new RouterController({
tiersService,
paymentsService,
offersAPI,
stripeAPIService,
labsService
});
await routerController.createCheckoutSession({
body: {
offerId: 'offer_123'
}
}, {
writeHead: () => {},
end: () => {}
});
getPaymentLinkSpy.calledOnce.should.be.true();
// Payment link is called with the offer id in metadata
getPaymentLinkSpy.calledWith(sinon.match({
metadata: {offer: 'offer_123'}
})).should.be.true();
});
afterEach(function () {
sinon.restore();
});
});
});