mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Added stripe test assertions (#14150)
We've split the tests into two describes, one for when Stripe is enabled and one without, because we setup Stripe in the before method. We use nock to mock the Stripe server and assert that there is no call to delete the subscription.
This commit is contained in:
parent
2b241b4e6a
commit
dc78d273c3
2 changed files with 1693 additions and 216 deletions
File diff suppressed because it is too large
Load diff
|
@ -1,4 +1,5 @@
|
|||
const assert = require('assert');
|
||||
const nock = require('nock');
|
||||
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../../utils/e2e-framework');
|
||||
const {anyString, anyArray, anyObjectId, anyEtag, anyUuid, anyErrorId, anyDate} = matchers;
|
||||
|
||||
|
@ -21,242 +22,263 @@ const memberMatcherShallowIncludes = {
|
|||
};
|
||||
|
||||
describe('Members API', function () {
|
||||
before(async function () {
|
||||
agent = await agentProvider.getAdminAPIAgent();
|
||||
await fixtureManager.init('members');
|
||||
await agent.loginAsOwner();
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
mockManager.mockLabsEnabled('members');
|
||||
mockManager.mockMail();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
mockManager.restore();
|
||||
});
|
||||
|
||||
it('Can add and send a signup confirmation email', async function () {
|
||||
const member = {
|
||||
name: 'Send Me Confirmation',
|
||||
email: 'member_getting_confirmation@test.com',
|
||||
subscribed: true
|
||||
};
|
||||
|
||||
const queryParams = {
|
||||
send_email: true,
|
||||
email_type: 'signup'
|
||||
};
|
||||
|
||||
const {body} = await agent
|
||||
.post('/members/?send_email=true&email_type=signup')
|
||||
.body({members: [member]})
|
||||
.expectStatus(201)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherNoIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag,
|
||||
location: anyString
|
||||
});
|
||||
|
||||
mockManager.assert.sentEmail({
|
||||
subject: '🙌 Complete your sign up to Ghost!',
|
||||
to: 'member_getting_confirmation@test.com'
|
||||
describe('without Stripe', function () {
|
||||
before(async function () {
|
||||
agent = await agentProvider.getAdminAPIAgent();
|
||||
await fixtureManager.init('members');
|
||||
await agent.loginAsOwner();
|
||||
});
|
||||
|
||||
// @TODO: do we really need to delete this member here?
|
||||
await agent
|
||||
.delete(`members/${body.members[0].id}/`)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.expectStatus(204);
|
||||
beforeEach(function () {
|
||||
mockManager.mockLabsEnabled('members');
|
||||
mockManager.mockMail();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
mockManager.restore();
|
||||
});
|
||||
|
||||
it('Add should fail when comped flag is passed in but Stripe is not enabled', async function () {
|
||||
const newMember = {
|
||||
email: 'memberTestAdd@test.com',
|
||||
comped: true
|
||||
};
|
||||
|
||||
await agent
|
||||
.post(`members/`)
|
||||
.body({members: [newMember]})
|
||||
.expectStatus(422)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
errors: [{
|
||||
id: anyErrorId
|
||||
}]
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Can order by email_open_rate', async function () {
|
||||
await agent
|
||||
.get('members/?order=email_open_rate%20desc')
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: new Array(8).fill(memberMatcherShallowIncludes)
|
||||
})
|
||||
.expect(({body}) => {
|
||||
const {members} = body;
|
||||
assert.equal(members[0].email_open_rate > members[1].email_open_rate, true, 'Expected the first member to have a greater open rate than the second.');
|
||||
describe('Members API with Stripe', function () {
|
||||
before(async function () {
|
||||
mockManager.setupStripe();
|
||||
agent = await agentProvider.getAdminAPIAgent();
|
||||
await fixtureManager.init('members');
|
||||
await agent.loginAsOwner();
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
mockManager.mockLabsEnabled('members');
|
||||
mockManager.mockMail();
|
||||
mockManager.mockStripe();
|
||||
});
|
||||
|
||||
afterEach(function () {
|
||||
mockManager.restore();
|
||||
});
|
||||
|
||||
it('Can add and send a signup confirmation email', async function () {
|
||||
const member = {
|
||||
name: 'Send Me Confirmation',
|
||||
email: 'member_getting_confirmation@test.com',
|
||||
subscribed: true
|
||||
};
|
||||
|
||||
const queryParams = {
|
||||
send_email: true,
|
||||
email_type: 'signup'
|
||||
};
|
||||
|
||||
const {body} = await agent
|
||||
.post('/members/?send_email=true&email_type=signup')
|
||||
.body({members: [member]})
|
||||
.expectStatus(201)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherNoIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag,
|
||||
location: anyString
|
||||
});
|
||||
|
||||
mockManager.assert.sentEmail({
|
||||
subject: '🙌 Complete your sign up to Ghost!',
|
||||
to: 'member_getting_confirmation@test.com'
|
||||
});
|
||||
|
||||
await agent
|
||||
.get('members/?order=email_open_rate%20asc')
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: new Array(8).fill(memberMatcherShallowIncludes)
|
||||
})
|
||||
.expect(({body}) => {
|
||||
const {members} = body;
|
||||
assert.equal(members[0].email_open_rate < members[1].email_open_rate, true, 'Expected the first member to have a smaller open rate than the second.');
|
||||
});
|
||||
});
|
||||
// @TODO: do we really need to delete this member here?
|
||||
await agent
|
||||
.delete(`members/${body.members[0].id}/`)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.expectStatus(204);
|
||||
});
|
||||
|
||||
it('Sarch by case-insensitive name egg receives member with name Mr Egg', async function () {
|
||||
await agent
|
||||
.get('members/?search=egg')
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
});
|
||||
});
|
||||
it('Can order by email_open_rate', async function () {
|
||||
await agent
|
||||
.get('members/?order=email_open_rate%20desc')
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: new Array(8).fill(memberMatcherShallowIncludes)
|
||||
})
|
||||
.expect(({body}) => {
|
||||
const {members} = body;
|
||||
assert.equal(members[0].email_open_rate > members[1].email_open_rate, true, 'Expected the first member to have a greater open rate than the second.');
|
||||
});
|
||||
|
||||
it('Search by case-insensitive email MEMBER2 receives member with email member2@test.com', async function () {
|
||||
await agent
|
||||
.get('members/?search=MEMBER2')
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
});
|
||||
});
|
||||
await agent
|
||||
.get('members/?order=email_open_rate%20asc')
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: new Array(8).fill(memberMatcherShallowIncludes)
|
||||
})
|
||||
.expect(({body}) => {
|
||||
const {members} = body;
|
||||
assert.equal(members[0].email_open_rate < members[1].email_open_rate, true, 'Expected the first member to have a smaller open rate than the second.');
|
||||
});
|
||||
});
|
||||
|
||||
it('Sarch for paid members retrieves member with email paid@test.com', async function () {
|
||||
await agent
|
||||
.get('members/?search=egon&paid=true')
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
});
|
||||
});
|
||||
it('Sarch by case-insensitive name egg receives member with name Mr Egg', async function () {
|
||||
await agent
|
||||
.get('members/?search=egg')
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
});
|
||||
});
|
||||
|
||||
it('Search for non existing member returns empty result set', async function () {
|
||||
await agent
|
||||
.get('members/?search=do_not_exist')
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: []
|
||||
});
|
||||
});
|
||||
it('Search by case-insensitive email MEMBER2 receives member with email member2@test.com', async function () {
|
||||
await agent
|
||||
.get('members/?search=MEMBER2')
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
});
|
||||
});
|
||||
|
||||
it('Can update a member with subscription included, change name to "Updated name"', async function () {
|
||||
const memberChanged = {
|
||||
name: 'Updated name'
|
||||
};
|
||||
it('Sarch for paid members retrieves member with email paid@test.com', async function () {
|
||||
await agent
|
||||
.get('members/?search=egon&paid=true')
|
||||
.expectStatus(200)
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
})
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
});
|
||||
});
|
||||
|
||||
const paidMember = fixtureManager.get('members', 2);
|
||||
it('Search for non existing member returns empty result set', async function () {
|
||||
await agent
|
||||
.get('members/?search=do_not_exist')
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: []
|
||||
});
|
||||
});
|
||||
|
||||
await agent
|
||||
.put(`members/${paidMember.id}/`)
|
||||
.body({members: [memberChanged]})
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
});
|
||||
});
|
||||
it('Can update a member with subscription included, change name to "Updated name"', async function () {
|
||||
const memberChanged = {
|
||||
name: 'Updated name'
|
||||
};
|
||||
|
||||
it('Add should fail when passing incorrect email_type query parameter', async function () {
|
||||
const newMember = {
|
||||
name: 'test',
|
||||
email: 'memberTestAdd@test.com'
|
||||
};
|
||||
const paidMember = fixtureManager.get('members', 2);
|
||||
|
||||
await agent
|
||||
.post(`members/?send_email=true&email_type=lel`)
|
||||
.body({members: [newMember]})
|
||||
.expectStatus(422)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
errors: [{
|
||||
id: anyErrorId
|
||||
}]
|
||||
});
|
||||
});
|
||||
await agent
|
||||
.put(`members/${paidMember.id}/`)
|
||||
.body({members: [memberChanged]})
|
||||
.expectStatus(200)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: [memberMatcherShallowIncludes]
|
||||
});
|
||||
});
|
||||
|
||||
it('Add should fail when comped flag is passed in but Stripe is not enabled', async function () {
|
||||
const newMember = {
|
||||
email: 'memberTestAdd@test.com',
|
||||
comped: true
|
||||
};
|
||||
it('Add should fail when passing incorrect email_type query parameter', async function () {
|
||||
const newMember = {
|
||||
name: 'test',
|
||||
email: 'memberTestAdd@test.com'
|
||||
};
|
||||
|
||||
await agent
|
||||
.post(`members/`)
|
||||
.body({members: [newMember]})
|
||||
.expectStatus(422)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
errors: [{
|
||||
id: anyErrorId
|
||||
}]
|
||||
});
|
||||
});
|
||||
await agent
|
||||
.post(`members/?send_email=true&email_type=lel`)
|
||||
.body({members: [newMember]})
|
||||
.expectStatus(422)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
errors: [{
|
||||
id: anyErrorId
|
||||
}]
|
||||
});
|
||||
});
|
||||
|
||||
it('Can delete a member without cancelling Stripe Subscription', async function () {
|
||||
const newMember = {
|
||||
name: 'Member 2 Delete',
|
||||
email: 'Member2Delete@test.com'
|
||||
};
|
||||
it('Can delete a member without cancelling Stripe Subscription', async function () {
|
||||
let subscriptionCanceled = false;
|
||||
nock('https://api.stripe.com')
|
||||
.persist()
|
||||
.delete(/v1\/.*/)
|
||||
.reply((uri) => {
|
||||
const [match, resource, id] = uri.match(/\/?v1\/(\w+)\/?(\w+)/) || [null];
|
||||
|
||||
// @TODO: use a fixture member in the right state using fixtureManager.get('members', x)
|
||||
// @TODO: instead of creating a new member as this wastes a request
|
||||
const createdMember = await agent
|
||||
.post(`members/`)
|
||||
.body({members: [newMember]})
|
||||
.expectStatus(201)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag,
|
||||
location: anyString
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
members: [
|
||||
memberMatcherNoIncludes
|
||||
]
|
||||
})
|
||||
.then(({body}) => {
|
||||
return body.members[0];
|
||||
});
|
||||
if (match && resource === 'subscriptions') {
|
||||
subscriptionCanceled = true;
|
||||
return [200, {
|
||||
id,
|
||||
status: 'canceled'
|
||||
}];
|
||||
}
|
||||
|
||||
await agent
|
||||
.delete(`members/${createdMember.id}/`)
|
||||
.expectStatus(204)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot();
|
||||
// @TODO: assert side effect - stripe subscription is not cancelled
|
||||
});
|
||||
return [500];
|
||||
});
|
||||
|
||||
it('Errors when fetching stats with unknown days param value', async function () {
|
||||
await agent
|
||||
.get('members/stats/?days=nope')
|
||||
.expectStatus(422)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
errors: [{
|
||||
id: anyErrorId
|
||||
}]
|
||||
});
|
||||
// TODO This is wrong because it changes the state for teh rest of the tests
|
||||
// We need to add a member via a fixture and then remove them OR work out how
|
||||
// to reapply fixtures before each test
|
||||
const memberToDelete = fixtureManager.get('members', 2);
|
||||
|
||||
await agent
|
||||
.delete(`members/${memberToDelete.id}/`)
|
||||
.expectStatus(204)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot();
|
||||
|
||||
assert.equal(subscriptionCanceled, false, 'expected subscription not to be canceled');
|
||||
});
|
||||
|
||||
it('Errors when fetching stats with unknown days param value', async function () {
|
||||
await agent
|
||||
.get('members/stats/?days=nope')
|
||||
.expectStatus(422)
|
||||
.matchHeaderSnapshot({
|
||||
etag: anyEtag
|
||||
})
|
||||
.matchBodySnapshot({
|
||||
errors: [{
|
||||
id: anyErrorId
|
||||
}]
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue