2020-07-23 19:21:10 +03:00
|
|
|
const should = require('should');
|
|
|
|
const BaseModel = require('../../../core/server/models/base');
|
|
|
|
const {Member} = require('../../../core/server/models/member');
|
|
|
|
const {MemberStripeCustomer} = require('../../../core/server/models/member-stripe-customer');
|
2021-04-20 14:07:59 +03:00
|
|
|
const {Product} = require('../../../core/server/models/product');
|
2020-07-23 19:21:10 +03:00
|
|
|
const {StripeCustomerSubscription} = require('../../../core/server/models/stripe-customer-subscription');
|
2021-04-20 14:07:59 +03:00
|
|
|
const {StripePrice} = require('../../../core/server/models/stripe-price');
|
|
|
|
const {StripeProduct} = require('../../../core/server/models/stripe-product');
|
2020-07-23 19:21:10 +03:00
|
|
|
|
|
|
|
const testUtils = require('../../utils');
|
|
|
|
|
|
|
|
describe('MemberStripeCustomer Model', function run() {
|
|
|
|
before(testUtils.teardownDb);
|
|
|
|
beforeEach(testUtils.setup('roles'));
|
|
|
|
afterEach(testUtils.teardownDb);
|
|
|
|
|
|
|
|
describe('subscriptions', function () {
|
|
|
|
// For some reason the initial .add of MemberStripeCustomer is **not** adding a StripeCustomerSubscription :(
|
|
|
|
it.skip('Is correctly mapped to the stripe subscriptions', async function () {
|
|
|
|
const context = testUtils.context.admin;
|
2020-08-05 14:20:30 +03:00
|
|
|
|
|
|
|
const member = await Member.add({
|
|
|
|
email: 'test@test.test'
|
|
|
|
});
|
|
|
|
|
2021-04-20 14:07:59 +03:00
|
|
|
const product = await Product.add({
|
|
|
|
name: 'Ghost Product',
|
|
|
|
slug: 'ghost-product'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
await StripeProduct.add({
|
|
|
|
product_id: product.get('id'),
|
|
|
|
stripe_product_id: 'fake_product_id'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
await StripePrice.add({
|
|
|
|
stripe_price_id: 'fake_plan_id',
|
|
|
|
stripe_product_id: 'fake_product_id',
|
|
|
|
amount: 5000,
|
|
|
|
interval: 'monthly',
|
|
|
|
currency: 'USD',
|
|
|
|
active: 1,
|
|
|
|
nickname: 'Monthly',
|
|
|
|
type: 'recurring'
|
|
|
|
}, context);
|
|
|
|
|
2020-07-23 19:21:10 +03:00
|
|
|
await MemberStripeCustomer.add({
|
2020-08-05 14:20:30 +03:00
|
|
|
member_id: member.get('id'),
|
2020-07-23 19:21:10 +03:00
|
|
|
customer_id: 'fake_customer_id',
|
|
|
|
subscriptions: [{
|
|
|
|
subscription_id: 'fake_subscription_id1',
|
|
|
|
plan_id: 'fake_plan_id',
|
2021-04-20 14:07:59 +03:00
|
|
|
stripe_price_id: 'fake_plan_id',
|
2020-07-23 19:21:10 +03:00
|
|
|
plan_amount: 1337,
|
|
|
|
plan_nickname: 'e-LEET',
|
|
|
|
plan_interval: 'year',
|
|
|
|
plan_currency: 'btc',
|
|
|
|
status: 'active',
|
|
|
|
start_date: new Date(),
|
|
|
|
current_period_end: new Date(),
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}]
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
const subscription1 = await StripeCustomerSubscription.findOne({
|
|
|
|
subscription_id: 'fake_subscription_id1'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
should.exist(subscription1, 'StripeCustomerSubscription should have been created');
|
|
|
|
|
|
|
|
await StripeCustomerSubscription.add({
|
|
|
|
customer_id: 'fake_customer_id',
|
|
|
|
subscription_id: 'fake_subscription_id2',
|
|
|
|
plan_id: 'fake_plan_id',
|
2021-04-20 14:07:59 +03:00
|
|
|
stripe_price_id: 'fake_plan_id',
|
2020-07-23 19:21:10 +03:00
|
|
|
plan_amount: 1337,
|
|
|
|
plan_nickname: 'e-LEET',
|
|
|
|
plan_interval: 'year',
|
|
|
|
plan_currency: 'btc',
|
|
|
|
status: 'active',
|
|
|
|
start_date: new Date(),
|
|
|
|
current_period_end: new Date(),
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
const customer = await MemberStripeCustomer.findOne({
|
|
|
|
customer_id: 'fake_customer_id'
|
|
|
|
}, Object.assign({}, context, {
|
|
|
|
withRelated: ['subscriptions']
|
|
|
|
}));
|
|
|
|
|
|
|
|
should.exist(customer.related('subscriptions'), 'MemberStripeCustomer should have been fetched with subscriptions');
|
|
|
|
|
|
|
|
const subscriptions = customer.related('subscriptions');
|
|
|
|
|
|
|
|
should.equal(subscriptions.length, 2, 'Should be two subscriptions');
|
|
|
|
|
|
|
|
should.equal(subscriptions.models[0].get('subscription_id'), 'fake_subscription_id1');
|
|
|
|
should.equal(subscriptions.models[1].get('subscription_id'), 'fake_subscription_id2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('member', function () {
|
|
|
|
it('Is correctly mapped to the member', async function () {
|
|
|
|
const context = testUtils.context.admin;
|
2020-08-05 14:20:30 +03:00
|
|
|
const member = await Member.add({
|
2020-07-23 19:21:10 +03:00
|
|
|
email: 'test@test.member'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
await MemberStripeCustomer.add({
|
2020-08-05 14:20:30 +03:00
|
|
|
member_id: member.get('id'),
|
2020-07-23 19:21:10 +03:00
|
|
|
customer_id: 'fake_customer_id'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
const customer = await MemberStripeCustomer.findOne({
|
|
|
|
customer_id: 'fake_customer_id'
|
|
|
|
}, Object.assign({}, context, {
|
|
|
|
withRelated: ['member']
|
|
|
|
}));
|
|
|
|
|
2020-08-05 14:20:30 +03:00
|
|
|
const memberFromRelation = customer.related('member');
|
2020-07-23 19:21:10 +03:00
|
|
|
|
2020-08-05 14:20:30 +03:00
|
|
|
should.exist(memberFromRelation, 'MemberStripeCustomer should have been fetched with member');
|
2020-07-23 19:21:10 +03:00
|
|
|
|
2020-08-05 14:20:30 +03:00
|
|
|
should.equal(memberFromRelation.get('id'), member.get('id'));
|
|
|
|
should.equal(memberFromRelation.get('email'), 'test@test.member');
|
2020-07-23 19:21:10 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('destroy', function () {
|
|
|
|
it('Cascades to members_stripe_customers_subscriptions', async function () {
|
|
|
|
const context = testUtils.context.admin;
|
2020-08-05 14:20:30 +03:00
|
|
|
const member = await Member.add({
|
|
|
|
email: 'test@test.member'
|
|
|
|
}, context);
|
|
|
|
|
2020-07-23 19:21:10 +03:00
|
|
|
await MemberStripeCustomer.add({
|
2020-08-05 14:20:30 +03:00
|
|
|
member_id: member.get('id'),
|
2020-07-23 19:21:10 +03:00
|
|
|
customer_id: 'fake_customer_id'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
const customer = await MemberStripeCustomer.findOne({
|
2020-08-05 14:20:30 +03:00
|
|
|
customer_id: 'fake_customer_id'
|
2020-07-23 19:21:10 +03:00
|
|
|
}, context);
|
|
|
|
|
|
|
|
should.exist(customer, 'Customer should have been created');
|
|
|
|
|
2021-04-20 14:07:59 +03:00
|
|
|
const product = await Product.add({
|
|
|
|
name: 'Ghost Product',
|
|
|
|
slug: 'ghost-product'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
await StripeProduct.add({
|
|
|
|
product_id: product.get('id'),
|
|
|
|
stripe_product_id: 'fake_product_id'
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
await StripePrice.add({
|
|
|
|
stripe_price_id: 'fake_plan_id',
|
|
|
|
stripe_product_id: 'fake_product_id',
|
|
|
|
amount: 5000,
|
|
|
|
interval: 'monthly',
|
|
|
|
active: 1,
|
|
|
|
nickname: 'Monthly',
|
|
|
|
currency: 'USD',
|
|
|
|
type: 'recurring'
|
|
|
|
}, context);
|
|
|
|
|
2020-07-23 19:21:10 +03:00
|
|
|
await StripeCustomerSubscription.add({
|
|
|
|
customer_id: 'fake_customer_id',
|
|
|
|
subscription_id: 'fake_subscription_id',
|
|
|
|
plan_id: 'fake_plan_id',
|
2021-04-20 14:07:59 +03:00
|
|
|
stripe_price_id: 'fake_plan_id',
|
2020-07-23 19:21:10 +03:00
|
|
|
plan_amount: 1337,
|
|
|
|
plan_nickname: 'e-LEET',
|
|
|
|
plan_interval: 'year',
|
|
|
|
plan_currency: 'btc',
|
|
|
|
status: 'active',
|
|
|
|
start_date: new Date(),
|
|
|
|
current_period_end: new Date(),
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
const subscription = await StripeCustomerSubscription.findOne({
|
|
|
|
customer_id: customer.get('customer_id')
|
|
|
|
}, context);
|
|
|
|
|
|
|
|
should.exist(subscription, 'Subscription should have been created');
|
|
|
|
|
|
|
|
await MemberStripeCustomer.destroy(Object.assign({
|
|
|
|
id: customer.get('id')
|
|
|
|
}, context));
|
|
|
|
|
|
|
|
const customerAfterDestroy = await MemberStripeCustomer.findOne({
|
2020-08-05 14:20:30 +03:00
|
|
|
customer_id: 'fake_customer_id'
|
2020-07-23 19:21:10 +03:00
|
|
|
});
|
|
|
|
should.not.exist(customerAfterDestroy, 'MemberStripeCustomer should have been destroyed');
|
|
|
|
|
|
|
|
const subscriptionAfterDestroy = await StripeCustomerSubscription.findOne({
|
2020-08-05 14:20:30 +03:00
|
|
|
customer_id: 'fake_customer_id'
|
2020-07-23 19:21:10 +03:00
|
|
|
});
|
|
|
|
should.not.exist(subscriptionAfterDestroy, 'StripeCustomerSubscription should have been destroyed');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|