2019-05-29 05:21:06 -04:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath =
|
|
|
|
'../../../../app/src/Features/Subscription/SubscriptionUpdater'
|
2021-02-18 06:47:17 -05:00
|
|
|
const { assert, expect } = require('chai')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('SubscriptionUpdater', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription = {
|
|
|
|
uuid: '1238uoijdasjhd',
|
|
|
|
plan: {
|
2021-04-27 03:52:58 -04:00
|
|
|
plan_code: 'kjhsakjds',
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.adminUser = { _id: (this.adminuser_id = '5208dd34438843e2db000007') }
|
|
|
|
this.otherUserId = '5208dd34438842e2db000005'
|
|
|
|
this.allUserIds = ['13213', 'dsadas', 'djsaiud89']
|
2019-06-05 11:30:13 -04:00
|
|
|
this.subscription = {
|
2019-05-29 05:21:06 -04:00
|
|
|
_id: '111111111111111111111111',
|
|
|
|
admin_id: this.adminUser._id,
|
|
|
|
manager_ids: [this.adminUser._id],
|
2020-05-06 10:58:29 -04:00
|
|
|
member_ids: [],
|
2019-05-29 05:21:06 -04:00
|
|
|
save: sinon.stub().callsArgWith(0),
|
2021-04-27 03:52:58 -04:00
|
|
|
planCode: 'student_or_something',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.user_id = this.adminuser_id
|
|
|
|
|
|
|
|
this.groupSubscription = {
|
|
|
|
_id: '222222222222222222222222',
|
|
|
|
admin_id: this.adminUser._id,
|
|
|
|
manager_ids: [this.adminUser._id],
|
|
|
|
member_ids: this.allUserIds,
|
|
|
|
save: sinon.stub().callsArgWith(0),
|
2021-04-27 03:52:58 -04:00
|
|
|
planCode: 'group_subscription',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.updateStub = sinon.stub().callsArgWith(2, null)
|
|
|
|
this.updateManyStub = sinon.stub().callsArgWith(2, null)
|
2019-06-05 11:30:13 -04:00
|
|
|
this.findOneAndUpdateStub = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.subscription)
|
|
|
|
|
|
|
|
let subscription = this.subscription
|
|
|
|
this.SubscriptionModel = class {
|
|
|
|
constructor(opts) {
|
|
|
|
// Always return our mock subscription when creating a new one
|
|
|
|
subscription.admin_id = opts.admin_id
|
|
|
|
subscription.manager_ids = [opts.admin_id]
|
|
|
|
return subscription
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-05 11:30:13 -04:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
this.SubscriptionModel.deleteOne = sinon.stub().yields()
|
|
|
|
this.SubscriptionModel.updateOne = this.updateStub
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionModel.updateMany = this.updateManyStub
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionModel.findOneAndUpdate = this.findOneAndUpdateStub
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
this.SubscriptionLocator = {
|
|
|
|
getUsersSubscription: sinon.stub(),
|
|
|
|
getGroupSubscriptionMemberOf: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
getMemberSubscriptions: sinon.stub().yields(null, []),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.Settings = {
|
|
|
|
defaultPlanCode: 'personal',
|
2021-04-27 03:52:58 -04:00
|
|
|
defaultFeatures: { default: 'features' },
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.UserFeaturesUpdater = { updateFeatures: sinon.stub().yields() }
|
|
|
|
|
|
|
|
this.PlansLocator = { findLocalPlanInSettings: sinon.stub().returns({}) }
|
|
|
|
|
|
|
|
this.UserGetter = {
|
|
|
|
getUsers(memberIds, projection, callback) {
|
|
|
|
const users = memberIds.map(id => ({ _id: id }))
|
2019-06-05 11:30:13 -04:00
|
|
|
callback(null, users)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
2021-04-27 03:52:58 -04:00
|
|
|
getUser: sinon.stub(),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.ReferalFeatures = { getBonusFeatures: sinon.stub().callsArgWith(1) }
|
|
|
|
this.Modules = { hooks: { fire: sinon.stub().callsArgWith(2, null, null) } }
|
2019-06-05 11:30:13 -04:00
|
|
|
this.FeaturesUpdater = {
|
2021-04-27 03:52:58 -04:00
|
|
|
refreshFeatures: sinon.stub().yields(),
|
2019-06-05 11:30:13 -04:00
|
|
|
}
|
2019-09-09 07:51:34 -04:00
|
|
|
this.DeletedSubscription = {
|
2021-04-27 03:52:58 -04:00
|
|
|
findOneAndUpdate: sinon.stub().yields(),
|
2019-09-09 07:51:34 -04:00
|
|
|
}
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater = SandboxedModule.require(modulePath, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
2020-10-05 04:23:21 -04:00
|
|
|
mongodb: { ObjectId },
|
2019-05-29 05:21:06 -04:00
|
|
|
'../../models/Subscription': {
|
2021-04-27 03:52:58 -04:00
|
|
|
Subscription: this.SubscriptionModel,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
'./UserFeaturesUpdater': this.UserFeaturesUpdater,
|
|
|
|
'./SubscriptionLocator': this.SubscriptionLocator,
|
|
|
|
'../User/UserGetter': this.UserGetter,
|
|
|
|
'./PlansLocator': this.PlansLocator,
|
|
|
|
'settings-sharelatex': this.Settings,
|
2020-10-01 04:30:26 -04:00
|
|
|
'../../infrastructure/mongodb': { db: {}, ObjectId },
|
2019-09-09 07:51:34 -04:00
|
|
|
'./FeaturesUpdater': this.FeaturesUpdater,
|
|
|
|
'../../models/DeletedSubscription': {
|
2021-04-27 03:52:58 -04:00
|
|
|
DeletedSubscription: this.DeletedSubscription,
|
|
|
|
},
|
|
|
|
},
|
2019-06-05 11:30:13 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('updateAdmin', function () {
|
|
|
|
it('should update the subscription admin', function (done) {
|
2020-02-28 10:08:20 -05:00
|
|
|
this.subscription.groupPlan = true
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.updateAdmin(
|
2020-02-28 10:08:20 -05:00
|
|
|
this.subscription,
|
2019-06-05 11:30:13 -04:00
|
|
|
this.otherUserId,
|
|
|
|
err => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
const query = {
|
|
|
|
_id: ObjectId(this.subscription._id),
|
2021-04-27 03:52:58 -04:00
|
|
|
customAccount: true,
|
2019-06-05 11:30:13 -04:00
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
$set: { admin_id: ObjectId(this.otherUserId) },
|
2021-04-27 03:52:58 -04:00
|
|
|
$addToSet: { manager_ids: ObjectId(this.otherUserId) },
|
2019-06-05 11:30:13 -04:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
this.SubscriptionModel.updateOne.should.have.been.calledOnce
|
|
|
|
this.SubscriptionModel.updateOne.should.have.been.calledWith(
|
2019-06-05 11:30:13 -04:00
|
|
|
query,
|
|
|
|
update
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2020-02-28 10:08:20 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should remove the manager for non-group subscriptions', function (done) {
|
2020-02-28 10:08:20 -05:00
|
|
|
this.SubscriptionUpdater.updateAdmin(
|
|
|
|
this.subscription,
|
|
|
|
this.otherUserId,
|
|
|
|
err => {
|
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
|
|
|
const query = {
|
|
|
|
_id: ObjectId(this.subscription._id),
|
2021-04-27 03:52:58 -04:00
|
|
|
customAccount: true,
|
2020-02-28 10:08:20 -05:00
|
|
|
}
|
|
|
|
const update = {
|
|
|
|
$set: {
|
|
|
|
admin_id: ObjectId(this.otherUserId),
|
2021-04-27 03:52:58 -04:00
|
|
|
manager_ids: [ObjectId(this.otherUserId)],
|
|
|
|
},
|
2020-02-28 10:08:20 -05:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
this.SubscriptionModel.updateOne.should.have.been.calledOnce
|
|
|
|
this.SubscriptionModel.updateOne.should.have.been.calledWith(
|
2020-02-28 10:08:20 -05:00
|
|
|
query,
|
|
|
|
update
|
|
|
|
)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('syncSubscription', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionLocator.getUsersSubscription.callsArgWith(
|
|
|
|
1,
|
|
|
|
null,
|
|
|
|
this.subscription
|
|
|
|
)
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2019-09-09 07:51:34 -04:00
|
|
|
.yields()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should update the subscription if the user already is admin of one', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionUpdater._createNewSubscription = sinon.stub()
|
|
|
|
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.syncSubscription(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.adminUser._id,
|
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionLocator.getUsersSubscription
|
|
|
|
.calledWith(this.adminUser._id)
|
|
|
|
.should.equal(true)
|
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly
|
|
|
|
.calledWith(this.recurlySubscription, this.subscription)
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not call updateFeatures with group subscription if recurly subscription is not expired', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.syncSubscription(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.adminUser._id,
|
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionLocator.getUsersSubscription
|
|
|
|
.calledWith(this.adminUser._id)
|
|
|
|
.should.equal(true)
|
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal(
|
|
|
|
true
|
|
|
|
)
|
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly
|
|
|
|
.calledWith(this.recurlySubscription, this.subscription)
|
|
|
|
.should.equal(true)
|
|
|
|
this.UserFeaturesUpdater.updateFeatures.called.should.equal(false)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('_updateSubscriptionFromRecurly', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.deleteSubscription = sinon.stub().yields()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
afterEach(function () {
|
2020-05-06 10:58:29 -04:00
|
|
|
this.subscription.member_ids = []
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should update the subscription with token etc when not expired', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.subscription,
|
2019-09-09 07:51:34 -04:00
|
|
|
{},
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription.recurlySubscription_id.should.equal(
|
|
|
|
this.recurlySubscription.uuid
|
|
|
|
)
|
|
|
|
this.subscription.planCode.should.equal(
|
|
|
|
this.recurlySubscription.plan.plan_code
|
|
|
|
)
|
|
|
|
this.subscription.save.called.should.equal(true)
|
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.adminUser._id)
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should remove the subscription when expired', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription.state = 'expired'
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.subscription,
|
2019-09-09 07:51:34 -04:00
|
|
|
{},
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.SubscriptionUpdater.deleteSubscription
|
2019-09-09 07:51:34 -04:00
|
|
|
.calledWithMatch(this.subscription)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should update all the users features', function (done) {
|
2020-05-06 10:58:29 -04:00
|
|
|
this.subscription.member_ids = this.allUserIds
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.subscription,
|
2019-09-09 07:51:34 -04:00
|
|
|
{},
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.adminUser._id)
|
|
|
|
.should.equal(true)
|
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.allUserIds[0])
|
|
|
|
.should.equal(true)
|
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.allUserIds[1])
|
|
|
|
.should.equal(true)
|
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.allUserIds[2])
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set group to true and save how many members can be added to group', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.PlansLocator.findLocalPlanInSettings
|
|
|
|
.withArgs(this.recurlySubscription.plan.plan_code)
|
|
|
|
.returns({ groupPlan: true, membersLimit: 5 })
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.subscription,
|
2019-09-09 07:51:34 -04:00
|
|
|
{},
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription.membersLimit.should.equal(5)
|
|
|
|
this.subscription.groupPlan.should.equal(true)
|
2020-05-06 10:58:29 -04:00
|
|
|
this.subscription.member_ids.should.deep.equal([
|
2021-04-27 03:52:58 -04:00
|
|
|
this.subscription.admin_id,
|
2020-05-06 10:58:29 -04:00
|
|
|
])
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not set group to true or set groupPlan', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.recurlySubscription,
|
|
|
|
this.subscription,
|
2019-09-09 07:51:34 -04:00
|
|
|
{},
|
2019-05-29 05:21:06 -04:00
|
|
|
err => {
|
2019-06-05 11:30:13 -04:00
|
|
|
if (err != null) {
|
|
|
|
return done(err)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
assert.notEqual(this.subscription.membersLimit, 5)
|
|
|
|
assert.notEqual(this.subscription.groupPlan, true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2021-02-18 06:47:17 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the plan allows adding more seats', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-18 06:47:17 -05:00
|
|
|
this.membersLimitAddOn = 'add_on1'
|
|
|
|
this.PlansLocator.findLocalPlanInSettings
|
|
|
|
.withArgs(this.recurlySubscription.plan.plan_code)
|
|
|
|
.returns({
|
|
|
|
groupPlan: true,
|
|
|
|
membersLimit: 5,
|
2021-04-27 03:52:58 -04:00
|
|
|
membersLimitAddOn: this.membersLimitAddOn,
|
2021-02-18 06:47:17 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function expectMembersLimit(limit) {
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the membersLimit accordingly', function (done) {
|
2021-02-18 06:47:17 -05:00
|
|
|
this.SubscriptionUpdater._updateSubscriptionFromRecurly(
|
|
|
|
this.recurlySubscription,
|
|
|
|
this.subscription,
|
|
|
|
{},
|
|
|
|
error => {
|
|
|
|
if (error) return done(error)
|
|
|
|
|
|
|
|
expect(this.subscription.membersLimit).to.equal(limit)
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the recurlySubscription does not have add ons', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-18 06:47:17 -05:00
|
|
|
delete this.recurlySubscription.subscription_add_ons
|
|
|
|
})
|
|
|
|
expectMembersLimit(5)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the recurlySubscription has non-matching add ons', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-18 06:47:17 -05:00
|
|
|
this.recurlySubscription.subscription_add_ons = [
|
2021-04-27 03:52:58 -04:00
|
|
|
{ add_on_code: 'add_on_99', quantity: 3 },
|
2021-02-18 06:47:17 -05:00
|
|
|
]
|
|
|
|
})
|
|
|
|
expectMembersLimit(5)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the recurlySubscription has a matching add on', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-18 06:47:17 -05:00
|
|
|
this.recurlySubscription.subscription_add_ons = [
|
2021-04-27 03:52:58 -04:00
|
|
|
{ add_on_code: this.membersLimitAddOn, quantity: 10 },
|
2021-02-18 06:47:17 -05:00
|
|
|
]
|
|
|
|
})
|
|
|
|
expectMembersLimit(15)
|
|
|
|
})
|
|
|
|
|
|
|
|
// NOTE: This is unexpected, but we are going to support it anyways.
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the recurlySubscription has multiple matching add ons', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-18 06:47:17 -05:00
|
|
|
this.recurlySubscription.subscription_add_ons = [
|
|
|
|
{ add_on_code: this.membersLimitAddOn, quantity: 10 },
|
2021-04-27 03:52:58 -04:00
|
|
|
{ add_on_code: this.membersLimitAddOn, quantity: 3 },
|
2021-02-18 06:47:17 -05:00
|
|
|
]
|
|
|
|
})
|
|
|
|
expectMembersLimit(18)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('_createNewSubscription', function () {
|
|
|
|
it('should create a new subscription then update the subscription', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater._createNewSubscription(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.adminUser._id,
|
|
|
|
() => {
|
|
|
|
this.subscription.admin_id.should.equal(this.adminUser._id)
|
|
|
|
this.subscription.manager_ids.should.deep.equal([this.adminUser._id])
|
|
|
|
this.subscription.save.called.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2019-08-07 10:04:04 -04:00
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('addUserToGroup', function () {
|
|
|
|
beforeEach(function () {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.addUsersToGroup = sinon.stub().yields(null)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('delegates to addUsersToGroup', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.addUserToGroup(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription._id,
|
|
|
|
this.otherUserId,
|
|
|
|
() => {
|
|
|
|
this.SubscriptionUpdater.addUsersToGroup
|
|
|
|
.calledWith(this.subscription._id, [this.otherUserId])
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('addUsersToGroup', function () {
|
|
|
|
beforeEach(function () {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should add the user ids to the group as a set', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.addUsersToGroup(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription._id,
|
|
|
|
[this.otherUserId],
|
|
|
|
() => {
|
|
|
|
const searchOps = { _id: this.subscription._id }
|
|
|
|
const insertOperation = {
|
2021-04-27 03:52:58 -04:00
|
|
|
$addToSet: { member_ids: { $each: [this.otherUserId] } },
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
this.updateStub
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(searchOps, insertOperation)
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should update the users features', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.addUserToGroup(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription._id,
|
|
|
|
this.otherUserId,
|
|
|
|
() => {
|
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.otherUserId)
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('removeUserFromGroups', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
2019-10-01 07:27:15 -04:00
|
|
|
this.UserGetter.getUser.yields(null, {})
|
2019-05-29 05:21:06 -04:00
|
|
|
this.fakeSubscriptions = [{ _id: 'fake-id-1' }, { _id: 'fake-id-2' }]
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionLocator.getMemberSubscriptions.yields(
|
2019-05-29 05:21:06 -04:00
|
|
|
null,
|
|
|
|
this.fakeSubscriptions
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should pull the users id from the group', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.removeUserFromGroup(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription._id,
|
|
|
|
this.otherUserId,
|
|
|
|
() => {
|
|
|
|
const searchOps = { _id: this.subscription._id }
|
|
|
|
const removeOperation = { $pull: { member_ids: this.otherUserId } }
|
|
|
|
this.updateManyStub
|
|
|
|
.calledWith(searchOps, removeOperation)
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should pull the users id from all groups', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.removeUserFromAllGroups(this.otherUserId, () => {
|
|
|
|
const filter = { _id: ['fake-id-1', 'fake-id-2'] }
|
|
|
|
const removeOperation = { $pull: { member_ids: this.otherUserId } }
|
|
|
|
sinon.assert.calledWith(this.updateManyStub, filter, removeOperation)
|
|
|
|
done()
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should update the users features', function (done) {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.SubscriptionUpdater.removeUserFromGroup(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription._id,
|
|
|
|
this.otherUserId,
|
|
|
|
() => {
|
|
|
|
this.FeaturesUpdater.refreshFeatures
|
|
|
|
.calledWith(this.otherUserId)
|
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('deleteSubscription', function () {
|
|
|
|
beforeEach(function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.subscription = {
|
2019-09-09 07:51:34 -04:00
|
|
|
_id: ObjectId().toString(),
|
2019-05-29 05:21:06 -04:00
|
|
|
mock: 'subscription',
|
|
|
|
admin_id: ObjectId(),
|
2021-04-27 03:52:58 -04:00
|
|
|
member_ids: [ObjectId(), ObjectId(), ObjectId()],
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.SubscriptionLocator.getSubscription = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, this.subscription)
|
|
|
|
this.FeaturesUpdater.refreshFeatures = sinon.stub().yields()
|
2019-09-09 07:51:34 -04:00
|
|
|
this.SubscriptionUpdater.deleteSubscription(this.subscription, {}, done)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should remove the subscription', function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
this.SubscriptionModel.deleteOne
|
2019-09-09 07:51:34 -04:00
|
|
|
.calledWith({ _id: this.subscription._id })
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should downgrade the admin_id', function () {
|
2019-06-05 11:30:13 -04:00
|
|
|
this.FeaturesUpdater.refreshFeatures
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(this.subscription.admin_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should downgrade all of the members', function () {
|
2019-06-05 11:30:13 -04:00
|
|
|
for (const userId of this.subscription.member_ids) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FeaturesUpdater.refreshFeatures
|
2019-06-05 11:30:13 -04:00
|
|
|
.calledWith(userId)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
2019-06-05 11:30:13 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|