2023-03-10 20:43:35 +03:00
|
|
|
const {agentProvider, fixtureManager, matchers, mockManager} = require('../../utils/e2e-framework');
|
|
|
|
const {mockStripe, stripeMocker} = require('../../utils/e2e-framework-mock-manager');
|
2023-01-17 14:56:29 +03:00
|
|
|
const {anyContentVersion, anyEtag, anyISODate, anyObjectId} = matchers;
|
2023-03-10 20:43:35 +03:00
|
|
|
const assert = require('assert');
|
2022-03-31 17:01:11 +03:00
|
|
|
|
|
|
|
let agent;
|
|
|
|
|
2023-03-10 20:43:35 +03:00
|
|
|
const matchSubscriptionStats = {
|
|
|
|
stats: [{
|
|
|
|
date: anyISODate,
|
|
|
|
tier: anyObjectId
|
|
|
|
}, {
|
|
|
|
date: anyISODate,
|
|
|
|
tier: anyObjectId
|
|
|
|
}],
|
|
|
|
meta: {
|
|
|
|
tiers: [anyObjectId],
|
|
|
|
totals: [{
|
|
|
|
tier: anyObjectId
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-31 17:01:11 +03:00
|
|
|
describe('Stats API', function () {
|
|
|
|
before(async function () {
|
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
2022-09-21 19:16:56 +03:00
|
|
|
await fixtureManager.init('posts', 'members');
|
2022-03-31 17:01:11 +03:00
|
|
|
await agent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
2023-03-10 20:43:35 +03:00
|
|
|
beforeEach(async function () {
|
|
|
|
mockStripe();
|
2023-03-22 19:25:10 +03:00
|
|
|
mockManager.mockMail();
|
2023-03-10 20:43:35 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
await mockManager.restore();
|
|
|
|
});
|
|
|
|
|
2022-03-31 17:01:11 +03:00
|
|
|
it('Can fetch member count history', async function () {
|
|
|
|
await agent
|
2022-03-31 18:09:28 +03:00
|
|
|
.get(`/stats/member_count`)
|
2022-03-31 17:01:11 +03:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
stats: [{
|
|
|
|
date: anyISODate
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-03-31 17:01:11 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
2022-04-08 10:18:04 +03:00
|
|
|
|
|
|
|
it('Can fetch MRR history', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`/stats/mrr`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
stats: [{
|
|
|
|
date: anyISODate
|
2022-04-27 16:53:32 +03:00
|
|
|
}, {
|
|
|
|
date: anyISODate
|
2022-04-08 10:18:04 +03:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-04-08 10:18:04 +03:00
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
2022-04-27 16:53:32 +03:00
|
|
|
|
2023-03-10 20:43:35 +03:00
|
|
|
describe('Subscriptions history', function () {
|
|
|
|
it('Can fetch subscriptions history', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot(matchSubscriptionStats)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can fetch history for free trials', async function () {
|
|
|
|
// Get stats before tests
|
|
|
|
const {body: before} = await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const customer = stripeMocker.createCustomer();
|
|
|
|
const price = await stripeMocker.getPriceForTier('default-product', 'month');
|
|
|
|
const subscription = await stripeMocker.createTrialSubscription({
|
|
|
|
customer,
|
|
|
|
price
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check the stats have not changed
|
|
|
|
await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot(matchSubscriptionStats)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
assert.deepStrictEqual(body, before, 'A free trial should not be counted as a paid subscriber');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Activate the subscription
|
|
|
|
await stripeMocker.updateSubscription({
|
|
|
|
id: subscription.id,
|
|
|
|
status: 'active',
|
|
|
|
trial_end_at: null
|
2022-04-27 16:53:32 +03:00
|
|
|
});
|
2023-03-10 20:43:35 +03:00
|
|
|
|
|
|
|
// Check the stats have changed
|
|
|
|
await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot(matchSubscriptionStats)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
assert.notDeepStrictEqual(body, before, 'The stats should change after a free trial is activated');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can fetch history for 3D secure payments', async function () {
|
|
|
|
// Get stats before tests
|
|
|
|
const {body: before} = await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const customer = stripeMocker.createCustomer();
|
|
|
|
const price = await stripeMocker.getPriceForTier('default-product', 'month');
|
|
|
|
const subscription = await stripeMocker.createIncompleteSubscription({
|
|
|
|
customer,
|
|
|
|
price
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check the stats have not changed
|
|
|
|
await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot(matchSubscriptionStats)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
assert.deepStrictEqual(body, before, 'An incomplete subscription should not be counted as a paid subscriber');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Activate the subscription
|
|
|
|
await stripeMocker.updateSubscription({
|
|
|
|
id: subscription.id,
|
|
|
|
status: 'active'
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check the stats have changed
|
|
|
|
await agent
|
|
|
|
.get(`/stats/subscriptions`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot(matchSubscriptionStats)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.expect(({body}) => {
|
|
|
|
assert.notDeepStrictEqual(body, before, 'The stats should change after an incomplete subscription is activated');
|
|
|
|
});
|
|
|
|
});
|
2022-04-27 16:53:32 +03:00
|
|
|
});
|
2022-09-21 19:16:56 +03:00
|
|
|
|
|
|
|
describe('Post attribution stats', function () {
|
|
|
|
it('Can fetch attribution stats', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`/stats/referrers/posts/${fixtureManager.get('posts', 1).id}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
stats: [
|
|
|
|
{
|
|
|
|
source: 'Direct',
|
|
|
|
signups: 2,
|
|
|
|
paid_conversions: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
source: 'Twitter',
|
|
|
|
signups: 1,
|
|
|
|
paid_conversions: 0
|
|
|
|
}
|
|
|
|
],
|
|
|
|
meta: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-09-22 14:04:26 +03:00
|
|
|
|
|
|
|
describe('Referrer source history stats', function () {
|
|
|
|
it('Can fetch attribution stats', async function () {
|
|
|
|
await agent
|
|
|
|
.get(`/stats/referrers/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
stats: [
|
2023-03-10 20:43:35 +03:00
|
|
|
{
|
|
|
|
date: anyISODate,
|
|
|
|
source: null,
|
|
|
|
signups: 2,
|
|
|
|
paid_conversions: 2
|
|
|
|
},
|
2022-09-22 14:04:26 +03:00
|
|
|
{
|
|
|
|
date: anyISODate,
|
|
|
|
source: 'Direct',
|
|
|
|
signups: 4,
|
|
|
|
paid_conversions: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
date: anyISODate,
|
|
|
|
source: 'Twitter',
|
|
|
|
signups: 4,
|
|
|
|
paid_conversions: 2
|
|
|
|
}
|
|
|
|
],
|
|
|
|
meta: {}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-03-31 17:01:11 +03:00
|
|
|
});
|