overleaf/services/web/test/unit/coffee/Subscription/SubscriptionUpdaterTests.coffee

234 lines
9 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
SandboxedModule = require('sandboxed-module')
should = require('chai').should()
expect = require('chai').expect
2014-02-12 05:23:40 -05:00
sinon = require 'sinon'
modulePath = "../../../../app/js/Features/Subscription/SubscriptionUpdater"
assert = require("chai").assert
2018-06-28 06:08:20 -04:00
ObjectId = require('mongoose').Types.ObjectId
2014-02-12 05:23:40 -05:00
describe "SubscriptionUpdater", ->
2014-02-12 05:23:40 -05:00
beforeEach ->
2018-06-28 06:08:20 -04:00
@recurlySubscription =
2014-02-12 05:23:40 -05:00
uuid: "1238uoijdasjhd"
plan:
plan_code: "kjhsakjds"
2018-06-28 06:08:20 -04:00
@adminUser =
_id: @adminuser_id = "5208dd34438843e2db000007"
2014-02-12 05:23:40 -05:00
@otherUserId = "5208dd34438842e2db000005"
@allUserIds = ["13213", "dsadas", "djsaiud89"]
@subscription = subscription =
admin_id: @adminUser._id
2016-03-31 12:36:06 -04:00
member_ids: @allUserIds
2014-02-12 05:23:40 -05:00
save: sinon.stub().callsArgWith(0)
freeTrial:{}
planCode:"student_or_something"
@user_id = @adminuser_id
2014-02-12 05:23:40 -05:00
@groupSubscription =
admin_id: @adminUser._id
2016-03-31 12:36:06 -04:00
member_ids: @allUserIds
save: sinon.stub().callsArgWith(0)
freeTrial:{}
planCode:"group_subscription"
2014-02-12 05:23:40 -05:00
@updateStub = sinon.stub().callsArgWith(2, null)
@findAndModifyStub = sinon.stub().callsArgWith(2, null, @subscription)
@SubscriptionModel = class
2018-06-28 06:08:20 -04:00
constructor: (opts)->
2014-02-12 05:23:40 -05:00
subscription.admin_id = opts.admin_id
return subscription
@remove: sinon.stub().yields()
2014-02-12 05:23:40 -05:00
@SubscriptionModel.update = @updateStub
@SubscriptionModel.findAndModify = @findAndModifyStub
2018-06-28 06:08:20 -04:00
@SubscriptionLocator =
2014-02-12 05:23:40 -05:00
getUsersSubscription: sinon.stub()
getGroupSubscriptionMemberOf:sinon.stub()
2018-06-28 06:08:20 -04:00
@Settings =
2014-02-12 05:23:40 -05:00
freeTrialPlanCode: "collaborator"
defaultPlanCode: "personal"
defaultFeatures: { "default": "features" }
2014-02-12 05:23:40 -05:00
@UserFeaturesUpdater =
updateFeatures : sinon.stub().yields()
2014-02-12 05:23:40 -05:00
@PlansLocator =
findLocalPlanInSettings: sinon.stub().returns({})
2018-06-28 10:19:41 -04:00
@UserGetter =
getUsers: (memberIds, projection, callback) ->
users = memberIds.map (id) -> { _id: id }
callback(null, users)
@ReferalFeatures = getBonusFeatures: sinon.stub().callsArgWith(1)
2017-12-04 10:34:46 -05:00
@Modules = {hooks: {fire: sinon.stub().callsArgWith(2, null, null)}}
2014-02-12 05:23:40 -05:00
@SubscriptionUpdater = SandboxedModule.require modulePath, requires:
'../../models/Subscription': Subscription:@SubscriptionModel
'./UserFeaturesUpdater': @UserFeaturesUpdater
'./SubscriptionLocator': @SubscriptionLocator
'../User/UserGetter': @UserGetter
2014-02-12 05:23:40 -05:00
'./PlansLocator': @PlansLocator
"logger-sharelatex": log:->
'settings-sharelatex': @Settings
"./FeaturesUpdater": @FeaturesUpdater = {}
2014-02-12 05:23:40 -05:00
describe "syncSubscription", ->
beforeEach ->
2014-02-12 05:23:40 -05:00
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
@SubscriptionUpdater._updateSubscriptionFromRecurly = sinon.stub().callsArgWith(2)
it "should update the subscription if the user already is admin of one", (done)->
2014-02-12 05:23:40 -05:00
@SubscriptionUpdater._createNewSubscription = sinon.stub()
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
@SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal true
@SubscriptionUpdater._updateSubscriptionFromRecurly.calledWith(@recurlySubscription, @subscription).should.equal true
done()
it "should not call updateFeatures with group subscription if recurly subscription is not expired", (done)->
2014-02-12 05:23:40 -05:00
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
@SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal true
@SubscriptionUpdater._updateSubscriptionFromRecurly.calledWith(@recurlySubscription, @subscription).should.equal true
@UserFeaturesUpdater.updateFeatures.called.should.equal false
2014-02-12 05:23:40 -05:00
done()
describe "_updateSubscriptionFromRecurly", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
@SubscriptionUpdater.deleteSubscription = sinon.stub().yields()
2018-06-28 06:08:20 -04:00
2014-02-12 05:23:40 -05:00
it "should update the subscription with token etc when not expired", (done)->
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
2014-02-12 05:23:40 -05:00
@subscription.recurlySubscription_id.should.equal @recurlySubscription.uuid
@subscription.planCode.should.equal @recurlySubscription.plan.plan_code
@subscription.freeTrial.allowed.should.equal true
assert.equal(@subscription.freeTrial.expiresAt, undefined)
assert.equal(@subscription.freeTrial.planCode, undefined)
@subscription.save.called.should.equal true
@FeaturesUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
2014-02-12 05:23:40 -05:00
done()
it "should remove the subscription when expired", (done)->
2014-02-12 05:23:40 -05:00
@recurlySubscription.state = "expired"
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
@SubscriptionUpdater.deleteSubscription.calledWith(@subscription._id).should.equal true
2014-02-12 05:23:40 -05:00
done()
it "should update all the users features", (done)->
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
@FeaturesUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
@FeaturesUpdater.refreshFeatures.calledWith(@allUserIds[0]).should.equal true
@FeaturesUpdater.refreshFeatures.calledWith(@allUserIds[1]).should.equal true
@FeaturesUpdater.refreshFeatures.calledWith(@allUserIds[2]).should.equal true
2014-02-12 05:23:40 -05:00
done()
it "should set group to true and save how many members can be added to group", (done)->
@PlansLocator.findLocalPlanInSettings.withArgs(@recurlySubscription.plan.plan_code).returns({groupPlan:true, membersLimit:5})
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
2014-02-12 05:23:40 -05:00
@subscription.membersLimit.should.equal 5
@subscription.groupPlan.should.equal true
done()
it "should not set group to true or set groupPlan", (done)->
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
2014-02-12 05:23:40 -05:00
assert.notEqual @subscription.membersLimit, 5
assert.notEqual @subscription.groupPlan, true
done()
2014-02-12 05:23:40 -05:00
describe "_createNewSubscription", ->
it "should create a new subscription then update the subscription", (done)->
@SubscriptionUpdater._createNewSubscription @adminUser._id, =>
@subscription.admin_id.should.equal @adminUser._id
@subscription.freeTrial.allowed.should.equal false
@subscription.save.called.should.equal true
done()
describe "addUserToGroup", ->
beforeEach ->
2018-06-28 10:19:41 -04:00
@SubscriptionUpdater.addUsersToGroup = sinon.stub().yields(null)
2018-06-28 10:19:41 -04:00
it "delegates to addUsersToGroup", (done)->
2014-02-12 05:23:40 -05:00
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
2018-06-28 10:19:41 -04:00
@SubscriptionUpdater.addUsersToGroup
.calledWith(@adminUser._id, [@otherUserId]).should.equal true
done()
describe "addUsersToGroup", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
it "should add the user ids to the group as a set", (done)->
@SubscriptionUpdater.addUsersToGroup @adminUser._id, [@otherUserId], =>
2018-06-28 06:08:20 -04:00
searchOps =
2014-02-12 05:23:40 -05:00
admin_id: @adminUser._id
2018-06-28 06:08:20 -04:00
insertOperation =
2018-06-28 10:19:41 -04:00
{ $push: { member_ids: { $each: [@otherUserId] } } }
2014-02-12 05:23:40 -05:00
@findAndModifyStub.calledWith(searchOps, insertOperation).should.equal true
done()
it "should update the users features", (done)->
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
@FeaturesUpdater.refreshFeatures.calledWith(@otherUserId).should.equal true
2014-02-12 05:23:40 -05:00
done()
describe "removeUserFromGroup", ->
beforeEach ->
@FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
2014-02-12 05:23:40 -05:00
it "should pull the users id from the group", (done)->
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
2018-06-28 06:08:20 -04:00
searchOps =
2014-02-12 05:23:40 -05:00
admin_id:@adminUser._id
2018-06-28 06:08:20 -04:00
removeOperation =
2014-02-12 05:23:40 -05:00
"$pull": {member_ids:@otherUserId}
@updateStub.calledWith(searchOps, removeOperation).should.equal true
done()
it "should update the users features", (done)->
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
@FeaturesUpdater.refreshFeatures.calledWith(@otherUserId).should.equal true
2014-02-12 05:23:40 -05:00
done()
describe "deleteSubscription", ->
beforeEach (done) ->
@subscription_id = ObjectId().toString()
@subscription = {
"mock": "subscription",
admin_id: ObjectId(),
member_ids: [ ObjectId(), ObjectId(), ObjectId() ]
}
@SubscriptionLocator.getSubscription = sinon.stub().yields(null, @subscription)
@FeaturesUpdater.refreshFeatures = sinon.stub().yields()
@SubscriptionUpdater.deleteSubscription @subscription_id, done
2018-06-28 06:08:20 -04:00
it "should look up the subscription", ->
@SubscriptionLocator.getSubscription
.calledWith(@subscription_id)
.should.equal true
2018-06-28 06:08:20 -04:00
it "should remove the subscription", ->
@SubscriptionModel.remove
.calledWith({_id: ObjectId(@subscription_id)})
.should.equal true
2018-06-28 06:08:20 -04:00
it "should downgrade the admin_id", ->
@FeaturesUpdater.refreshFeatures
.calledWith(@subscription.admin_id)
.should.equal true
2018-06-28 06:08:20 -04:00
it "should downgrade all of the members", ->
for user_id in @subscription.member_ids
@FeaturesUpdater.refreshFeatures
.calledWith(user_id)
.should.equal true