mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 05:50:35 +03:00
Added scaffolding for testing Members with Stripe (#14142)
refs https://github.com/TryGhost/Team/issues/1338 This adds some initial scaffolding to make it easier to test Members with Stripe - `mockStripe` method to disable the network, so we can use nock to intercept Stripe API calls - `setupStripe` method to be called _before_ getting an agent so that Stripe is configured - `getMembersAPIAgent` to get an agent for the Members frontend API
This commit is contained in:
parent
b2e04cce4b
commit
133a41c256
61
test/e2e-api/members/webhooks.test.js
Normal file
61
test/e2e-api/members/webhooks.test.js
Normal file
@ -0,0 +1,61 @@
|
||||
const stripe = require('stripe');
|
||||
const {agentProvider, mockManager, fixtureManager} = require('../../utils/e2e-framework');
|
||||
|
||||
let agent;
|
||||
|
||||
describe('Members API', function () {
|
||||
before(async function () {
|
||||
process.env.WEBHOOK_SECRET = 'pissoff';
|
||||
// Weird - most of the mocks happen after getting the agent
|
||||
// but to mock stripe we want to fake the stripe keys in the settings.
|
||||
// And it's initialised at boot - so mocking it before
|
||||
// Probably wanna replace this with a settinfs fixture mock or smth??
|
||||
mockManager.setupStripe();
|
||||
agent = await agentProvider.getMembersAPIAgent();
|
||||
await fixtureManager.init('members');
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
mockManager.mockLabsEnabled('multipleProducts');
|
||||
mockManager.mockMail();
|
||||
mockManager.mockStripe();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
mockManager.restore();
|
||||
});
|
||||
|
||||
it('Can communicate with the frontend Members API', async function () {
|
||||
await agent.get('/api/site/')
|
||||
.expectStatus(200);
|
||||
});
|
||||
|
||||
describe('/webhooks/stripe/', function () {
|
||||
it('Responds with a 401 when the signature is invalid', async function () {
|
||||
await agent.post('/webhooks/stripe/')
|
||||
.body({
|
||||
fake: 'data'
|
||||
})
|
||||
.header('stripe-signature', 'dodgy')
|
||||
.expectStatus(401);
|
||||
});
|
||||
|
||||
it('Responds with a 200 to unknown events with valid signature', async function () {
|
||||
const webhookPayload = JSON.stringify({
|
||||
type: 'unknown',
|
||||
data: {
|
||||
id: 'id_123'
|
||||
}
|
||||
});
|
||||
const webhookSignature = stripe.webhooks.generateTestHeaderString({
|
||||
payload: webhookPayload,
|
||||
secret: process.env.WEBHOOK_SECRET
|
||||
});
|
||||
|
||||
await agent.post('/webhooks/stripe/')
|
||||
.body(webhookPayload)
|
||||
.header('stripe-signature', webhookSignature)
|
||||
.expectStatus(200);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,6 +1,7 @@
|
||||
const errors = require('@tryghost/errors');
|
||||
const sinon = require('sinon');
|
||||
const assert = require('assert');
|
||||
const nock = require('nock');
|
||||
|
||||
// Helper services
|
||||
const configUtils = require('./configUtils');
|
||||
@ -11,6 +12,7 @@ let emailCount = 0;
|
||||
// Mockable services
|
||||
const mailService = require('../../core/server/services/mail/index');
|
||||
const labs = require('../../core/shared/labs');
|
||||
const settingsCache = require('../../core/shared/settings-cache');
|
||||
|
||||
const mockMail = () => {
|
||||
mocks.mail = sinon
|
||||
@ -20,6 +22,25 @@ const mockMail = () => {
|
||||
return mocks.mail;
|
||||
};
|
||||
|
||||
const mockStripe = () => {
|
||||
nock.disableNetConnect();
|
||||
};
|
||||
|
||||
const setupStripe = () => {
|
||||
mocks.settings = sinon.stub(settingsCache, 'get').callsFake(function (key, ...args) {
|
||||
if (key === 'stripe_connect_secret_key') {
|
||||
return 'sk_test_blah';
|
||||
}
|
||||
if (key === 'stripe_connect_publishable_key') {
|
||||
return 'pk_test_blah';
|
||||
}
|
||||
if (key === 'stripe_connect_account_name') {
|
||||
return 'Test Account';
|
||||
}
|
||||
return settingsCache.get.wrappedMethod.call(settingsCache, key, ...args);
|
||||
});
|
||||
};
|
||||
|
||||
const mockLabsEnabled = (flag, alpha = true) => {
|
||||
mocks.labs = mocks.labs || {};
|
||||
|
||||
@ -88,10 +109,14 @@ const restore = () => {
|
||||
sinon.restore();
|
||||
mocks = {};
|
||||
emailCount = 0;
|
||||
nock.cleanAll();
|
||||
nock.enableNetConnect();
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
mockMail,
|
||||
setupStripe,
|
||||
mockStripe,
|
||||
mockLabsEnabled,
|
||||
mockLabsDisabled,
|
||||
restore,
|
||||
|
Loading…
Reference in New Issue
Block a user