2022-08-23 07:04:43 +03:00
|
|
|
const assert = require('assert');
|
2022-09-01 17:30:37 +03:00
|
|
|
const sinon = require('sinon');
|
2022-09-09 17:30:05 +03:00
|
|
|
const DomainEvents = require('@tryghost/domain-events');
|
2022-02-09 12:34:09 +03:00
|
|
|
const MemberRepository = require('../../../../lib/repositories/member');
|
2022-09-09 17:30:05 +03:00
|
|
|
const {SubscriptionCreatedEvent} = require('@tryghost/member-events');
|
2022-02-09 12:34:09 +03:00
|
|
|
|
|
|
|
describe('MemberRepository', function () {
|
2022-03-18 13:47:24 +03:00
|
|
|
describe('#isComplimentarySubscription', function () {
|
2022-02-09 12:34:09 +03:00
|
|
|
it('Does not error when subscription.plan is null', function () {
|
|
|
|
const repo = new MemberRepository({});
|
|
|
|
repo.isComplimentarySubscription({});
|
|
|
|
});
|
|
|
|
});
|
2022-08-23 07:04:43 +03:00
|
|
|
|
|
|
|
describe('#resolveContextSource', function (){
|
|
|
|
it('Maps context to source', function (){
|
|
|
|
const repo = new MemberRepository({});
|
|
|
|
|
|
|
|
let source = repo._resolveContextSource({
|
|
|
|
import: true
|
|
|
|
});
|
|
|
|
assert.equal(source, 'import');
|
|
|
|
|
|
|
|
source = repo._resolveContextSource({
|
|
|
|
importer: true
|
|
|
|
});
|
|
|
|
assert.equal(source, 'import');
|
|
|
|
|
|
|
|
source = repo._resolveContextSource({
|
|
|
|
user: true
|
|
|
|
});
|
|
|
|
assert.equal(source, 'admin');
|
|
|
|
|
|
|
|
source = repo._resolveContextSource({
|
|
|
|
user: true,
|
|
|
|
api_key: true
|
|
|
|
});
|
2022-08-23 09:38:25 +03:00
|
|
|
assert.equal(source, 'api');
|
2022-09-01 17:30:37 +03:00
|
|
|
|
2022-08-23 07:04:43 +03:00
|
|
|
source = repo._resolveContextSource({
|
|
|
|
api_key: true
|
|
|
|
});
|
|
|
|
assert.equal(source, 'api');
|
|
|
|
|
|
|
|
source = repo._resolveContextSource({
|
|
|
|
});
|
|
|
|
assert.equal(source, 'member');
|
|
|
|
|
|
|
|
source = repo._resolveContextSource({
|
|
|
|
generic_context: true
|
|
|
|
});
|
|
|
|
assert.equal(source, 'member');
|
|
|
|
});
|
|
|
|
});
|
2022-09-01 17:30:37 +03:00
|
|
|
|
|
|
|
describe('linkSubscription', function (){
|
|
|
|
let Member;
|
|
|
|
let notifySpy;
|
|
|
|
let MemberPaidSubscriptionEvent;
|
|
|
|
let StripeCustomerSubscription;
|
|
|
|
let MemberProductEvent;
|
|
|
|
let stripeAPIService;
|
|
|
|
let productRepository;
|
|
|
|
let labsService;
|
|
|
|
let subscriptionData;
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
notifySpy = sinon.spy();
|
|
|
|
subscriptionData = {
|
|
|
|
id: 'sub_123',
|
|
|
|
customer: 'cus_123',
|
|
|
|
status: 'active',
|
|
|
|
items: {
|
|
|
|
type: 'list',
|
|
|
|
data: [{
|
|
|
|
id: 'item_123',
|
|
|
|
price: {
|
|
|
|
id: 'price_123',
|
|
|
|
product: 'product_123',
|
|
|
|
active: true,
|
|
|
|
nickname: 'Monthly',
|
|
|
|
currency: 'usd',
|
|
|
|
recurring: {
|
|
|
|
interval: 'month'
|
|
|
|
},
|
|
|
|
unit_amount: 500,
|
|
|
|
type: 'recurring'
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
start_date: Date.now() / 1000,
|
|
|
|
current_period_end: Date.now() / 1000 + (60 * 60 * 24 * 31),
|
|
|
|
cancel_at_period_end: false
|
|
|
|
};
|
|
|
|
|
|
|
|
Member = {
|
|
|
|
findOne: sinon.stub().resolves({
|
|
|
|
related: () => {
|
|
|
|
return {
|
|
|
|
query: sinon.stub().returns({
|
|
|
|
fetchOne: sinon.stub().resolves({})
|
|
|
|
}),
|
|
|
|
toJSON: sinon.stub().returns([]),
|
|
|
|
fetch: sinon.stub().resolves({
|
|
|
|
toJSON: sinon.stub().returns({})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
},
|
|
|
|
toJSON: sinon.stub().returns({})
|
|
|
|
}),
|
|
|
|
edit: sinon.stub().resolves({
|
|
|
|
attributes: {},
|
|
|
|
_previousAttributes: {}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
MemberPaidSubscriptionEvent = {
|
|
|
|
add: sinon.stub().resolves()
|
|
|
|
};
|
|
|
|
StripeCustomerSubscription = {
|
|
|
|
add: sinon.stub().resolves({
|
|
|
|
get: sinon.stub().returns()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
MemberProductEvent = {
|
|
|
|
add: sinon.stub().resolves({})
|
|
|
|
};
|
|
|
|
|
|
|
|
stripeAPIService = {
|
|
|
|
configured: true,
|
|
|
|
getSubscription: sinon.stub().resolves(subscriptionData)
|
|
|
|
};
|
|
|
|
|
|
|
|
productRepository = {
|
|
|
|
get: sinon.stub().resolves({
|
|
|
|
get: sinon.stub().returns(),
|
|
|
|
toJSON: sinon.stub().returns({})
|
|
|
|
}),
|
|
|
|
update: sinon.stub().resolves({})
|
|
|
|
};
|
|
|
|
|
|
|
|
labsService = {
|
|
|
|
isSet: sinon.stub().returns(true)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-09-09 17:30:05 +03:00
|
|
|
it('dispatches paid subscription event', async function (){
|
2022-09-01 17:30:37 +03:00
|
|
|
const repo = new MemberRepository({
|
|
|
|
stripeAPIService,
|
|
|
|
StripeCustomerSubscription,
|
|
|
|
MemberPaidSubscriptionEvent,
|
|
|
|
MemberProductEvent,
|
|
|
|
productRepository,
|
|
|
|
labsService,
|
|
|
|
Member
|
|
|
|
});
|
|
|
|
|
|
|
|
sinon.stub(repo, 'getSubscriptionByStripeID').resolves(null);
|
|
|
|
|
2022-09-09 17:30:05 +03:00
|
|
|
DomainEvents.subscribe(SubscriptionCreatedEvent, notifySpy);
|
|
|
|
|
2022-09-01 17:30:37 +03:00
|
|
|
await repo.linkSubscription({
|
|
|
|
subscription: subscriptionData
|
|
|
|
}, {
|
2022-09-09 17:30:05 +03:00
|
|
|
transacting: {
|
|
|
|
executionPromise: Promise.resolve()
|
|
|
|
},
|
2022-09-01 17:30:37 +03:00
|
|
|
context: {}
|
|
|
|
});
|
|
|
|
|
|
|
|
notifySpy.calledOnce.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
});
|
2022-02-09 12:34:09 +03:00
|
|
|
});
|