2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-02-23 18:00:18 +03:00
|
|
|
const {
|
|
|
|
agentProvider,
|
|
|
|
fixtureManager,
|
|
|
|
mockManager,
|
|
|
|
matchers
|
|
|
|
} = require('../../utils/e2e-framework');
|
2023-10-06 05:04:11 +03:00
|
|
|
const models = require('../../../core/server/models/index');
|
2023-01-17 14:56:29 +03:00
|
|
|
const {anyContentVersion, anyEtag} = matchers;
|
2022-02-23 18:00:18 +03:00
|
|
|
|
|
|
|
describe('Tiers API', function () {
|
|
|
|
let agent;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
agent = await agentProvider.getAdminAPIAgent();
|
|
|
|
await fixtureManager.init('members');
|
|
|
|
await agent.loginAsOwner();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can browse Tiers', async function () {
|
|
|
|
await agent
|
|
|
|
.get('/tiers/')
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
tiers: Array(2).fill({
|
|
|
|
id: matchers.anyObjectId,
|
2022-03-01 14:17:13 +03:00
|
|
|
created_at: matchers.anyISODateTime,
|
|
|
|
updated_at: matchers.anyISODateTime
|
2022-02-23 18:00:18 +03:00
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Errors when price is non-integer', async function () {
|
|
|
|
const tier = {
|
|
|
|
name: 'Blah',
|
2022-05-16 21:47:18 +03:00
|
|
|
monthly_price: 99.99,
|
|
|
|
currency: 'usd'
|
2022-02-23 18:00:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post('/tiers/')
|
|
|
|
.body({tiers: [tier]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: matchers.anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Errors when price is negative', async function () {
|
|
|
|
const tier = {
|
|
|
|
name: 'Blah',
|
2022-05-16 21:47:18 +03:00
|
|
|
monthly_price: -100,
|
|
|
|
currency: 'usd'
|
2022-02-23 18:00:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post('/tiers/')
|
|
|
|
.body({tiers: [tier]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: matchers.anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Errors when price is too large', async function () {
|
|
|
|
const tier = {
|
|
|
|
name: 'Blah',
|
2022-05-16 21:47:18 +03:00
|
|
|
monthly_price: Number.MAX_SAFE_INTEGER,
|
|
|
|
currency: 'usd'
|
2022-02-23 18:00:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post('/tiers/')
|
|
|
|
.body({tiers: [tier]})
|
|
|
|
.expectStatus(422)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
errors: [{
|
|
|
|
id: matchers.anyUuid
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
});
|
2022-03-07 17:46:42 +03:00
|
|
|
|
|
|
|
it('Can read Tiers', async function () {
|
|
|
|
const {body: {tiers: [tier]}} = await agent.get('/tiers/');
|
|
|
|
|
|
|
|
await agent.get(`/tiers/${tier.id}/`)
|
|
|
|
.expectStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can edit visibility', async function () {
|
2022-11-30 09:31:06 +03:00
|
|
|
const {body: {tiers: [tier]}} = await agent.get('/tiers/?filter=type:paid&limit=1');
|
2022-03-07 17:46:42 +03:00
|
|
|
|
|
|
|
const visibility = tier.visibility === 'none' ? 'public' : 'none';
|
|
|
|
|
|
|
|
await agent.put(`/tiers/${tier.id}/`)
|
|
|
|
.body({
|
|
|
|
tiers: [{
|
|
|
|
visibility
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const {body: {tiers: [updatedTier]}} = await agent.get(`/tiers/${tier.id}/`);
|
|
|
|
|
|
|
|
assert(updatedTier.visibility === visibility, `The visibility of the Tier should have been updated to ${visibility}`);
|
|
|
|
});
|
2022-08-09 06:10:57 +03:00
|
|
|
|
|
|
|
it('Can save with trial_days as null', async function () {
|
|
|
|
const {body: {tiers: [tier]}} = await agent.get('/tiers/?limit=1');
|
|
|
|
|
|
|
|
await agent.put(`/tiers/${tier.id}/`)
|
|
|
|
.body({
|
|
|
|
tiers: [{
|
|
|
|
trial_days: null
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
const {body: {tiers: [updatedTier]}} = await agent.get(`/tiers/${tier.id}/`);
|
|
|
|
|
|
|
|
assert(updatedTier.trial_days === 0, `The trial_days should have been set to 0`);
|
|
|
|
});
|
2022-11-01 12:25:55 +03:00
|
|
|
|
2022-11-10 13:29:28 +03:00
|
|
|
it('Can edit tier properties and relations', async function () {
|
2022-11-30 09:31:06 +03:00
|
|
|
let {body: {tiers: [tier]}} = await agent.get('/tiers/?filter=type:paid&limit=1')
|
2022-11-10 13:29:28 +03:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-11-10 13:29:28 +03:00
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
2022-11-30 09:31:06 +03:00
|
|
|
tiers: Array(1).fill({
|
2022-11-10 13:29:28 +03:00
|
|
|
id: matchers.anyObjectId,
|
|
|
|
created_at: matchers.anyISODateTime,
|
|
|
|
updated_at: matchers.anyISODateTime
|
|
|
|
})
|
|
|
|
});
|
2022-11-01 12:25:55 +03:00
|
|
|
|
|
|
|
await agent.put(`/tiers/${tier.id}/`)
|
|
|
|
.body({
|
|
|
|
tiers: [{
|
2022-11-10 13:29:28 +03:00
|
|
|
description: 'Updated description',
|
|
|
|
benefits: ['daily cat pictures', 'delicious avo toast']
|
2022-11-01 12:25:55 +03:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200);
|
|
|
|
|
2023-03-10 14:53:35 +03:00
|
|
|
await agent.get(`/tiers/${tier.id}/`)
|
2022-11-10 13:29:28 +03:00
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
2023-01-17 14:56:29 +03:00
|
|
|
'content-version': anyContentVersion,
|
2022-11-10 13:29:28 +03:00
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
tiers: Array(1).fill({
|
|
|
|
id: matchers.anyObjectId,
|
|
|
|
created_at: matchers.anyISODateTime,
|
|
|
|
updated_at: matchers.anyISODateTime
|
|
|
|
})
|
|
|
|
});
|
2022-11-01 12:25:55 +03:00
|
|
|
});
|
2023-03-10 06:34:05 +03:00
|
|
|
|
|
|
|
it('Can create a new tier', async function () {
|
|
|
|
const tier = {
|
|
|
|
name: 'new premium tier',
|
|
|
|
monthly_price: 100,
|
|
|
|
currency: 'usd'
|
|
|
|
};
|
|
|
|
|
|
|
|
await agent
|
|
|
|
.post('/tiers/')
|
|
|
|
.body({tiers: [tier]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
tiers: Array(1).fill({
|
|
|
|
id: matchers.anyObjectId,
|
|
|
|
created_at: matchers.anyISODate,
|
|
|
|
name: 'new premium tier',
|
|
|
|
slug: 'new-premium-tier',
|
|
|
|
monthly_price: 100,
|
|
|
|
currency: 'USD'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
2023-10-06 05:04:11 +03:00
|
|
|
|
|
|
|
it('Can update a benefit\'s capitalization', async function () {
|
|
|
|
const tierData = {
|
|
|
|
name: 'benefit test tier',
|
|
|
|
monthly_price: 100,
|
|
|
|
currency: 'usd',
|
|
|
|
benefits: ['TEST BENEFIT']
|
|
|
|
};
|
|
|
|
|
|
|
|
let {body: {tiers: [tier]}} = await agent.post('/tiers/')
|
|
|
|
.body({tiers: [tierData]})
|
|
|
|
.expectStatus(201)
|
|
|
|
.matchBodySnapshot({
|
|
|
|
tiers: Array(1).fill({
|
|
|
|
id: matchers.anyObjectId,
|
|
|
|
created_at: matchers.anyISODate,
|
|
|
|
name: 'benefit test tier',
|
|
|
|
slug: 'benefit-test-tier',
|
|
|
|
monthly_price: 100,
|
|
|
|
currency: 'USD'
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
await agent.put(`/tiers/${tier.id}/`)
|
|
|
|
.body({
|
|
|
|
tiers: [{
|
|
|
|
benefits: ['Test benefit']
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
.expectStatus(200);
|
|
|
|
|
|
|
|
await agent.get(`/tiers/${tier.id}/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
'content-version': anyContentVersion,
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot({
|
|
|
|
tiers: Array(1).fill({
|
|
|
|
id: matchers.anyObjectId,
|
|
|
|
created_at: matchers.anyISODate,
|
|
|
|
benefits: ['Test benefit']
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const benefit = await models.Benefit.findOne({slug: 'test-benefit'});
|
|
|
|
assert(benefit.attributes.name === 'Test benefit', 'The benefit should have been updated.');
|
|
|
|
const previousBenefit = await models.Benefit.findOne({slug: 'test-benefit-2'});
|
|
|
|
assert(!previousBenefit, 'The previous benefit should have been overwritten');
|
|
|
|
});
|
2022-02-23 18:00:18 +03:00
|
|
|
});
|