Added an e2e test for creating a member subscription

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

- Used nock to mock the Stripe API.
- Re-used the price fixture for the Stripe API mocks.
This commit is contained in:
Thibaut Patel 2022-02-16 14:23:59 +01:00 committed by GitHub
parent 43960b7de6
commit bed0115ddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 147 additions and 1 deletions

View File

@ -856,6 +856,80 @@ Object {
}
`;
exports[`Members API Can add a subcription 1: [body] 1`] = `
Object {
"members": Array [
Object {
"avatar_image": null,
"comped": false,
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"email": "member1@test.com",
"email_count": 0,
"email_open_rate": null,
"email_opened_count": 0,
"geolocation": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"labels": Any<Array>,
"name": "Mr Egg",
"note": null,
"products": Array [],
"status": "paid",
"subscribed": true,
"subscriptions": Array [
Object {
"cancel_at_period_end": false,
"cancellation_reason": null,
"current_period_end": Any<String>,
"customer": Object {
"email": "member1@test.com",
"id": "cus_123",
"name": null,
},
"default_payment_card_last4": null,
"id": "sub_123",
"plan": Object {
"amount": 5000,
"currency": "USD",
"id": "173e16a1fffa7d232b398e4a9b08d266a456ae8f3d23e5f11cc608ced6730b12",
"interval": "month",
"nickname": "month",
},
"price": Object {
"amount": 5000,
"currency": "USD",
"id": "173e16a1fffa7d232b398e4a9b08d266a456ae8f3d23e5f11cc608ced6730b12",
"interval": "month",
"nickname": "Monthly",
"price_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"product": Object {
"id": "109c85c734fb9992e7bc30a26af66c22f5c94d8dc62e0a33cb797be902c06b2d",
"product_id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
},
"type": "recurring",
},
"start_date": Any<String>,
"status": "active",
},
],
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
"uuid": StringMatching /\\[a-f0-9\\]\\{8\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{4\\}-\\[a-f0-9\\]\\{12\\}/,
},
],
}
`;
exports[`Members API Can add a subcription 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "1333",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Members API Can add complimentary subscription 1: [body] 1`] = `
Object {
"members": Array [

View File

@ -1,6 +1,7 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyUuid, anyDate, anyString, anyArray} = matchers;
const nock = require('nock');
const should = require('should');
const sinon = require('sinon');
const testUtils = require('../../utils');
@ -9,8 +10,8 @@ const Papa = require('papaparse');
let agent;
describe('Members API', function () {
let request;
before(async function () {
mockManager.setupStripe();
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init('members');
await agent.loginAsOwner();
@ -18,6 +19,7 @@ describe('Members API', function () {
beforeEach(function () {
mockManager.mockLabsEnabled('multipleProducts');
mockManager.mockStripe();
});
afterEach(function () {
@ -412,4 +414,74 @@ describe('Members API', function () {
should.not.exist(csv.data.find(row => row.name === 'Ray Stantz'));
should.not.exist(csv.data.find(row => row.email === 'member2@test.com'));
});
it('Can add a subcription', async function () {
const memberId = testUtils.DataGenerator.Content.members[0].id;
const price = testUtils.DataGenerator.Content.stripe_prices[0];
function nockCallback(method, uri, body) {
const [match, resource, id] = uri.match(/\/?v1\/(\w+)(?:\/(\w+))?/) || [null];
if (!match) {
return [500];
}
if (resource === 'customers') {
return [200, {id: 'cus_123', email: 'member1@test.com'}];
}
if (resource === 'subscriptions') {
const now = Math.floor(Date.now() / 1000);
return [200, {id: 'sub_123', customer: 'cus_123', cancel_at_period_end: false, items: {
data: [{price: {
id: price.stripe_price_id,
recurring: {
interval: price.interval
},
unit_amount: price.amount,
currency: price.currency.toLowerCase()
}}]
}, status: 'active', current_period_end: now + 24 * 3600, start_date: now}];
}
}
nock('https://api.stripe.com:443')
.persist()
.post(/v1\/.*/)
.reply((uri, body) => nockCallback('POST', uri, body));
nock('https://api.stripe.com:443')
.persist()
.get(/v1\/.*/)
.reply((uri, body) => nockCallback('GET', uri, body));
await agent
.post(`/members/${memberId}/subscriptions/`)
.body({
stripe_price_id: price.id
})
.expectStatus(200)
.matchBodySnapshot({
members: new Array(1).fill({
id: anyObjectId,
uuid: anyUuid,
created_at: anyDate,
updated_at: anyDate,
labels: anyArray,
subscriptions: [{
start_date: anyString,
current_period_end: anyString,
price: {
price_id: anyObjectId,
product: {
product_id: anyObjectId
}
}
}]
})
})
.matchHeaderSnapshot({
etag: anyEtag
});
});
});