|
@@ -1,5 +1,6 @@
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
should = require('chai').should()
|
|
should = require('chai').should()
|
|
|
|
|
+expect = require('chai').expect
|
|
|
sinon = require 'sinon'
|
|
sinon = require 'sinon'
|
|
|
modulePath = "../../../../app/js/Features/Subscription/SubscriptionUpdater"
|
|
modulePath = "../../../../app/js/Features/Subscription/SubscriptionUpdater"
|
|
|
assert = require("chai").assert
|
|
assert = require("chai").assert
|
|
@@ -22,6 +23,7 @@ describe "SubscriptionUpdater", ->
|
|
|
save: sinon.stub().callsArgWith(0)
|
|
save: sinon.stub().callsArgWith(0)
|
|
|
freeTrial:{}
|
|
freeTrial:{}
|
|
|
planCode:"student_or_something"
|
|
planCode:"student_or_something"
|
|
|
|
|
+ @user_id = @adminuser_id
|
|
|
|
|
|
|
|
@groupSubscription =
|
|
@groupSubscription =
|
|
|
admin_id: @adminUser._id
|
|
admin_id: @adminUser._id
|
|
@@ -48,15 +50,15 @@ describe "SubscriptionUpdater", ->
|
|
|
@Settings =
|
|
@Settings =
|
|
|
freeTrialPlanCode: "collaborator"
|
|
freeTrialPlanCode: "collaborator"
|
|
|
defaultPlanCode: "personal"
|
|
defaultPlanCode: "personal"
|
|
|
|
|
+ defaultFeatures: { "default": "features" }
|
|
|
|
|
|
|
|
@UserFeaturesUpdater =
|
|
@UserFeaturesUpdater =
|
|
|
- updateFeatures : sinon.stub().callsArgWith(2)
|
|
|
|
|
|
|
+ updateFeatures : sinon.stub().yields()
|
|
|
|
|
|
|
|
@PlansLocator =
|
|
@PlansLocator =
|
|
|
findLocalPlanInSettings: sinon.stub().returns({})
|
|
findLocalPlanInSettings: sinon.stub().returns({})
|
|
|
|
|
|
|
|
- @ReferalAllocator = assignBonus:sinon.stub().callsArgWith(1)
|
|
|
|
|
- @ReferalAllocator.cock = true
|
|
|
|
|
|
|
+ @ReferalFeatures = getBonusFeatures: sinon.stub().callsArgWith(1)
|
|
|
@Modules = {hooks: {fire: sinon.stub().callsArgWith(2, null, null)}}
|
|
@Modules = {hooks: {fire: sinon.stub().callsArgWith(2, null, null)}}
|
|
|
@SubscriptionUpdater = SandboxedModule.require modulePath, requires:
|
|
@SubscriptionUpdater = SandboxedModule.require modulePath, requires:
|
|
|
'../../models/Subscription': Subscription:@SubscriptionModel
|
|
'../../models/Subscription': Subscription:@SubscriptionModel
|
|
@@ -65,8 +67,7 @@ describe "SubscriptionUpdater", ->
|
|
|
'./PlansLocator': @PlansLocator
|
|
'./PlansLocator': @PlansLocator
|
|
|
"logger-sharelatex": log:->
|
|
"logger-sharelatex": log:->
|
|
|
'settings-sharelatex': @Settings
|
|
'settings-sharelatex': @Settings
|
|
|
- "../Referal/ReferalAllocator" : @ReferalAllocator
|
|
|
|
|
- '../../infrastructure/Modules': @Modules
|
|
|
|
|
|
|
+ "./FeaturesUpdater": @FeaturesUpdater = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
describe "syncSubscription", ->
|
|
describe "syncSubscription", ->
|
|
@@ -97,7 +98,7 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
|
|
describe "_updateSubscriptionFromRecurly", ->
|
|
describe "_updateSubscriptionFromRecurly", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
|
|
it "should update the subscription with token etc when not expired", (done)->
|
|
it "should update the subscription with token etc when not expired", (done)->
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
@@ -108,7 +109,7 @@ describe "SubscriptionUpdater", ->
|
|
|
assert.equal(@subscription.freeTrial.expiresAt, undefined)
|
|
assert.equal(@subscription.freeTrial.expiresAt, undefined)
|
|
|
assert.equal(@subscription.freeTrial.planCode, undefined)
|
|
assert.equal(@subscription.freeTrial.planCode, undefined)
|
|
|
@subscription.save.called.should.equal true
|
|
@subscription.save.called.should.equal true
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
it "should remove the recurlySubscription_id when expired", (done)->
|
|
it "should remove the recurlySubscription_id when expired", (done)->
|
|
@@ -117,15 +118,15 @@ describe "SubscriptionUpdater", ->
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
|
assert.equal(@subscription.recurlySubscription_id, undefined)
|
|
assert.equal(@subscription.recurlySubscription_id, undefined)
|
|
|
@subscription.save.called.should.equal true
|
|
@subscription.save.called.should.equal true
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
it "should update all the users features", (done)->
|
|
it "should update all the users features", (done)->
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@allUserIds[0]).should.equal true
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@allUserIds[1]).should.equal true
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@allUserIds[2]).should.equal true
|
|
|
|
|
|
|
+ @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
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
it "should set group to true and save how many members can be added to group", (done)->
|
|
it "should set group to true and save how many members can be added to group", (done)->
|
|
@@ -152,6 +153,9 @@ describe "SubscriptionUpdater", ->
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
describe "addUserToGroup", ->
|
|
describe "addUserToGroup", ->
|
|
|
|
|
+ beforeEach ->
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
+
|
|
|
it "should add the users id to the group as a set", (done)->
|
|
it "should add the users id to the group as a set", (done)->
|
|
|
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
|
|
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
|
|
|
searchOps =
|
|
searchOps =
|
|
@@ -163,12 +167,12 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
|
|
it "should update the users features", (done)->
|
|
it "should update the users features", (done)->
|
|
|
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
|
|
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
|
|
|
- @UserFeaturesUpdater.updateFeatures.calledWith(@otherUserId, @subscription.planCode).should.equal true
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures.calledWith(@otherUserId).should.equal true
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
describe "removeUserFromGroup", ->
|
|
describe "removeUserFromGroup", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
|
|
it "should pull the users id from the group", (done)->
|
|
it "should pull the users id from the group", (done)->
|
|
|
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
|
|
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
|
|
@@ -181,69 +185,7 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
|
|
it "should update the users features", (done)->
|
|
it "should update the users features", (done)->
|
|
|
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
|
|
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@otherUserId).should.equal true
|
|
|
|
|
- done()
|
|
|
|
|
-
|
|
|
|
|
- describe "_setUsersMinimumFeatures", ->
|
|
|
|
|
-
|
|
|
|
|
- it "should call updateFeatures with the subscription if set", (done)->
|
|
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null)
|
|
|
|
|
-
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures @adminUser._id, (err)=>
|
|
|
|
|
- args = @UserFeaturesUpdater.updateFeatures.args[0]
|
|
|
|
|
- assert.equal args[0], @adminUser._id
|
|
|
|
|
- assert.equal args[1], @subscription.planCode
|
|
|
|
|
- done()
|
|
|
|
|
-
|
|
|
|
|
- it "should call updateFeatures with the group subscription if set", (done)->
|
|
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null)
|
|
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null, @groupSubscription)
|
|
|
|
|
-
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures @adminUser._id, (err)=>
|
|
|
|
|
- args = @UserFeaturesUpdater.updateFeatures.args[0]
|
|
|
|
|
- assert.equal args[0], @adminUser._id
|
|
|
|
|
- assert.equal args[1], @groupSubscription.planCode
|
|
|
|
|
- done()
|
|
|
|
|
-
|
|
|
|
|
- it "should call updateFeatures with the overleaf subscription if set", (done)->
|
|
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null)
|
|
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null, null)
|
|
|
|
|
- @Modules.hooks.fire = sinon.stub().callsArgWith(2, null, ['ol_pro'])
|
|
|
|
|
-
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures @adminUser._id, (err)=>
|
|
|
|
|
- args = @UserFeaturesUpdater.updateFeatures.args[0]
|
|
|
|
|
- assert.equal args[0], @adminUser._id
|
|
|
|
|
- assert.equal args[1], 'ol_pro'
|
|
|
|
|
- done()
|
|
|
|
|
-
|
|
|
|
|
- it "should call not call updateFeatures with users subscription if the subscription plan code is the default one (downgraded)", (done)->
|
|
|
|
|
- @subscription.planCode = @Settings.defaultPlanCode
|
|
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null, @groupSubscription)
|
|
|
|
|
- @Modules.hooks.fire = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures @adminuser_id, (err)=>
|
|
|
|
|
- args = @UserFeaturesUpdater.updateFeatures.args[0]
|
|
|
|
|
- assert.equal args[0], @adminUser._id
|
|
|
|
|
- assert.equal args[1], @groupSubscription.planCode
|
|
|
|
|
- done()
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- it "should call updateFeatures with default if there are no subscriptions for user", (done)->
|
|
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null)
|
|
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null)
|
|
|
|
|
- @Modules.hooks.fire = sinon.stub().callsArgWith(2, null, null)
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures @adminuser_id, (err)=>
|
|
|
|
|
- args = @UserFeaturesUpdater.updateFeatures.args[0]
|
|
|
|
|
- assert.equal args[0], @adminUser._id
|
|
|
|
|
- assert.equal args[1], @Settings.defaultPlanCode
|
|
|
|
|
- done()
|
|
|
|
|
-
|
|
|
|
|
- it "should call assignBonus", (done)->
|
|
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null)
|
|
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null)
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures @adminuser_id, (err)=>
|
|
|
|
|
- @ReferalAllocator.assignBonus.calledWith(@adminuser_id).should.equal true
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures.calledWith(@otherUserId).should.equal true
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
describe "deleteSubscription", ->
|
|
describe "deleteSubscription", ->
|
|
@@ -255,7 +197,7 @@ describe "SubscriptionUpdater", ->
|
|
|
member_ids: [ ObjectId(), ObjectId(), ObjectId() ]
|
|
member_ids: [ ObjectId(), ObjectId(), ObjectId() ]
|
|
|
}
|
|
}
|
|
|
@SubscriptionLocator.getSubscription = sinon.stub().yields(null, @subscription)
|
|
@SubscriptionLocator.getSubscription = sinon.stub().yields(null, @subscription)
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub().yields()
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures = sinon.stub().yields()
|
|
|
@SubscriptionUpdater.deleteSubscription @subscription_id, done
|
|
@SubscriptionUpdater.deleteSubscription @subscription_id, done
|
|
|
|
|
|
|
|
it "should look up the subscription", ->
|
|
it "should look up the subscription", ->
|
|
@@ -269,22 +211,12 @@ describe "SubscriptionUpdater", ->
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should downgrade the admin_id", ->
|
|
it "should downgrade the admin_id", ->
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures
|
|
|
.calledWith(@subscription.admin_id)
|
|
.calledWith(@subscription.admin_id)
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should downgrade all of the members", ->
|
|
it "should downgrade all of the members", ->
|
|
|
for user_id in @subscription.member_ids
|
|
for user_id in @subscription.member_ids
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures
|
|
|
|
|
|
|
+ @FeaturesUpdater.refreshFeatures
|
|
|
.calledWith(user_id)
|
|
.calledWith(user_id)
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
-
|
|
|
|
|
- describe 'refreshSubscription', ->
|
|
|
|
|
- beforeEach ->
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub()
|
|
|
|
|
- .callsArgWith(1, null)
|
|
|
|
|
-
|
|
|
|
|
- it 'should call to _setUsersMinimumFeatures', ->
|
|
|
|
|
- @SubscriptionUpdater.refreshSubscription(@adminUser._id, ()->)
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.callCount.should.equal 1
|
|
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@adminUser._id).should.equal true
|
|
|