0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/test/unit/services/members/config_spec.js
Fabien O'Carroll 44f1c0a7b6 Updated members config to respect stripeDirect
no-issue

If the stripeDirect config value is NOT set / false, then connect token
should be used over the API keys if both are present, or else use
whichever is present.  If the stripeDirect config value is set /
true,then API keys should be used and stripe connect token should be
completely ignored.

The reason for this is that we want to use Stripe Connect as the primary
auth method going forward, but we want to keep the direct version
availiable should the Ghost Foundation ever dismantle. This will allow
people to use all the same features with no dependency on an external
service.
2020-06-02 15:28:42 +02:00

136 lines
4.6 KiB
JavaScript

const should = require('should');
const MembersConfigProvider = require('../../../../core/server/services/members/config');
const urlUtils = require('../../../../core/shared/url-utils');
const sinon = require('sinon');
/**
* @param {object} options
* @param {boolean} options.stripeDirectValue - The value the stripeDirect config property should have
*/
function createConfigMock({stripeDirectValue}) {
return {
get: sinon.stub()
.withArgs('stripeDirect').returns(stripeDirectValue)
};
}
/**
* @param {object} options
* @param {boolean} options.setDirect - Whether the "direct" keys should be set
* @param {boolean} options.setConnect - Whether the connect_integration keys should be set
*/
function createSettingsMock({setDirect, setConnect}) {
const membersSubscriptionSettings = {
fromAddress: 'noreply',
allowSelfSignup: true,
paymentProcessors: [{
adapter: 'stripe',
config: {
secret_token: setDirect ? 'direct_secret' : null,
public_token: setDirect ? 'direct_public' : null,
product: {
name: 'Test'
},
plans: [{
name: 'Monthly',
currency: 'usd',
interval: 'month',
amount: 1000
}, {
name: 'Yearly',
currency: 'usd',
interval: 'year',
amount: 10000
}]
}
}]
};
const stripeConnectIntegration = {
secret_key: setConnect ? 'connect_secret' : null,
public_key: setConnect ? 'connect_public' : null,
livemode: true
};
const getStub = sinon.stub();
getStub.withArgs('members_subscription_settings').returns(membersSubscriptionSettings);
getStub.withArgs('stripe_connect_integration').returns(stripeConnectIntegration);
return {
get: getStub
};
}
describe('Members - config', function () {
it('Uses direct keys when stripeDirect is true, regardles of which keys exist', function () {
const config = createConfigMock({stripeDirectValue: true});
const settingsCache = createSettingsMock({setDirect: true, setConnect: Math.random() < 0.5});
const membersConfig = new MembersConfigProvider({
config,
settingsCache,
urlUtils,
ghostVersion: {original: 'v7357'},
logging: console
});
const paymentConfig = membersConfig.getStripePaymentConfig();
should.equal(paymentConfig.publicKey, 'direct_public');
should.equal(paymentConfig.secretKey, 'direct_secret');
});
it('Does not use connect keys if stripeDirect is true, and the direct keys do not exist', function () {
const config = createConfigMock({stripeDirectValue: true});
const settingsCache = createSettingsMock({setDirect: false, setConnect: true});
const membersConfig = new MembersConfigProvider({
config,
settingsCache,
urlUtils,
ghostVersion: {original: 'v7357'},
logging: console
});
const paymentConfig = membersConfig.getStripePaymentConfig();
should.equal(paymentConfig, null);
});
it('Uses connect keys when stripeDirect is false, and the connect keys exist', function () {
const config = createConfigMock({stripeDirectValue: false});
const settingsCache = createSettingsMock({setDirect: true, setConnect: true});
const membersConfig = new MembersConfigProvider({
config,
settingsCache,
urlUtils,
ghostVersion: {original: 'v7357'},
logging: console
});
const paymentConfig = membersConfig.getStripePaymentConfig();
should.equal(paymentConfig.publicKey, 'connect_public');
should.equal(paymentConfig.secretKey, 'connect_secret');
});
it('Uses direct keys when stripeDirect is false, but the connect keys do not exist', function () {
const config = createConfigMock({stripeDirectValue: false});
const settingsCache = createSettingsMock({setDirect: true, setConnect: false});
const membersConfig = new MembersConfigProvider({
config,
settingsCache,
urlUtils,
ghostVersion: {original: 'v7357'},
logging: console
});
const paymentConfig = membersConfig.getStripePaymentConfig();
should.equal(paymentConfig.publicKey, 'direct_public');
should.equal(paymentConfig.secretKey, 'direct_secret');
});
});