Added donations checkout session unit tests (#20827)

ref PLG-196

- Added comprehensive unit tests to cover various scenarios for the
createDonationCheckoutSession function.
- Verified correct handling of customer object, customerEmail and
metadata.
- Ensured accurate parameter passing to Stripe API, including
success_url and cancel_url.
This commit is contained in:
Ronald Langeveld 2024-08-27 12:36:43 +09:00 committed by GitHub
parent 8fc8dc72e6
commit 9449e0a048
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -413,5 +413,117 @@ describe('StripeAPI', function () {
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_update);
});
});
describe('createDonationCheckoutSession', function () {
beforeEach(function () {
mockStripe = {
checkout: {
sessions: {
create: sinon.stub().resolves()
}
}
};
sinon.stub(mockLabs, 'isSet');
const mockStripeConstructor = sinon.stub().returns(mockStripe);
StripeAPI.__set__('Stripe', mockStripeConstructor);
api.configure({
checkoutSessionSuccessUrl: '/success',
checkoutSessionCancelUrl: '/cancel',
checkoutSetupSessionSuccessUrl: '/setup-success',
checkoutSetupSessionCancelUrl: '/setup-cancel',
secretKey: ''
});
});
afterEach(function () {
sinon.restore();
});
it('createDonationCheckoutSession sends success_url and cancel_url', async function () {
await api.createDonationCheckoutSession('priceId', {});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.success_url);
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.cancel_url);
});
it('createDonationCheckoutSession does not send currency if additionalPaymentMethods flag is off', async function () {
mockLabs.isSet.withArgs('additionalPaymentMethods').returns(false);
await api.createDonationCheckoutSession('priceId', {currency: 'usd'});
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.currency);
});
it('passes customer ID when a valid customer object is provided', async function () {
const mockCustomer = {
id: mockCustomerId,
email: mockCustomerEmail,
name: mockCustomerName
};
await api.createDonationCheckoutSession({
priceId: 'priceId',
successUrl: '/success',
cancelUrl: '/cancel',
metadata: {},
customer: mockCustomer
});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer);
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.customer, mockCustomerId);
});
it('passes customer_email when no customer object is provided', async function () {
await api.createDonationCheckoutSession({
priceId: 'priceId',
successUrl: '/success',
cancelUrl: '/cancel',
metadata: {},
customerEmail: mockCustomerEmail
});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_email);
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_email, mockCustomerEmail);
});
it('uses only customer when both customer and customerEmail are provided', async function () {
const mockCustomer = {
id: mockCustomerId,
email: mockCustomerEmail,
name: mockCustomerName
};
await api.createDonationCheckoutSession({
priceId: 'priceId',
successUrl: '/success',
cancelUrl: '/cancel',
metadata: {},
customer: mockCustomer,
customerEmail: 'another_email@example.com'
});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer);
should.equal(mockStripe.checkout.sessions.create.firstCall.firstArg.customer, mockCustomerId);
should.not.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.customer_email);
});
it('passes metadata correctly', async function () {
const metadata = {
key1: 'value1',
key2: 'value2'
};
await api.createDonationCheckoutSession({
priceId: 'priceId',
successUrl: '/success',
cancelUrl: '/cancel',
metadata,
customer: null,
customerEmail: mockCustomerEmail
});
should.exist(mockStripe.checkout.sessions.create.firstCall.firstArg.metadata);
should.deepEqual(mockStripe.checkout.sessions.create.firstCall.firstArg.metadata, metadata);
});
});
});
});