|
|
@@ -1,5 +1,6 @@
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
should = require('chai').should()
|
|
|
+expect = require('chai').expect
|
|
|
sinon = require 'sinon'
|
|
|
modulePath = "../../../../app/js/Features/Subscription/SubscriptionUpdater"
|
|
|
assert = require("chai").assert
|
|
|
@@ -22,6 +23,7 @@ describe "SubscriptionUpdater", ->
|
|
|
save: sinon.stub().callsArgWith(0)
|
|
|
freeTrial:{}
|
|
|
planCode:"student_or_something"
|
|
|
+ @user_id = @adminuser_id
|
|
|
|
|
|
@groupSubscription =
|
|
|
admin_id: @adminUser._id
|
|
|
@@ -48,15 +50,15 @@ describe "SubscriptionUpdater", ->
|
|
|
@Settings =
|
|
|
freeTrialPlanCode: "collaborator"
|
|
|
defaultPlanCode: "personal"
|
|
|
+ defaultFeatures: { "default": "features" }
|
|
|
|
|
|
@UserFeaturesUpdater =
|
|
|
- updateFeatures : sinon.stub().callsArgWith(2)
|
|
|
+ updateFeatures : sinon.stub().yields()
|
|
|
|
|
|
@PlansLocator =
|
|
|
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)}}
|
|
|
@SubscriptionUpdater = SandboxedModule.require modulePath, requires:
|
|
|
'../../models/Subscription': Subscription:@SubscriptionModel
|
|
|
@@ -65,8 +67,9 @@ describe "SubscriptionUpdater", ->
|
|
|
'./PlansLocator': @PlansLocator
|
|
|
"logger-sharelatex": log:->
|
|
|
'settings-sharelatex': @Settings
|
|
|
- "../Referal/ReferalAllocator" : @ReferalAllocator
|
|
|
+ "../Referal/ReferalFeatures" : @ReferalFeatures
|
|
|
'../../infrastructure/Modules': @Modules
|
|
|
+ "./V1SubscriptionManager": @V1SubscriptionManager = {}
|
|
|
|
|
|
|
|
|
describe "syncSubscription", ->
|
|
|
@@ -97,7 +100,7 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
describe "_updateSubscriptionFromRecurly", ->
|
|
|
beforeEach ->
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub().callsArgWith(1)
|
|
|
+ @SubscriptionUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
it "should update the subscription with token etc when not expired", (done)->
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
|
@@ -108,7 +111,7 @@ describe "SubscriptionUpdater", ->
|
|
|
assert.equal(@subscription.freeTrial.expiresAt, undefined)
|
|
|
assert.equal(@subscription.freeTrial.planCode, undefined)
|
|
|
@subscription.save.called.should.equal true
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
done()
|
|
|
|
|
|
it "should remove the recurlySubscription_id when expired", (done)->
|
|
|
@@ -117,15 +120,15 @@ describe "SubscriptionUpdater", ->
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly @recurlySubscription, @subscription, (err)=>
|
|
|
assert.equal(@subscription.recurlySubscription_id, undefined)
|
|
|
@subscription.save.called.should.equal true
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
done()
|
|
|
|
|
|
it "should update all the users features", (done)->
|
|
|
@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
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@adminUser._id).should.equal true
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@allUserIds[0]).should.equal true
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@allUserIds[1]).should.equal true
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@allUserIds[2]).should.equal true
|
|
|
done()
|
|
|
|
|
|
it "should set group to true and save how many members can be added to group", (done)->
|
|
|
@@ -168,7 +171,7 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
describe "removeUserFromGroup", ->
|
|
|
beforeEach ->
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub().callsArgWith(1)
|
|
|
+ @SubscriptionUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
it "should pull the users id from the group", (done)->
|
|
|
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
|
|
|
@@ -181,70 +184,159 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
it "should update the users features", (done)->
|
|
|
@SubscriptionUpdater.removeUserFromGroup @adminUser._id, @otherUserId, =>
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures.calledWith(@otherUserId).should.equal true
|
|
|
+ @SubscriptionUpdater.refreshFeatures.calledWith(@otherUserId).should.equal true
|
|
|
done()
|
|
|
|
|
|
- describe "_setUsersMinimumFeatures", ->
|
|
|
+ describe "refreshFeatures", ->
|
|
|
+ beforeEach ->
|
|
|
+ @SubscriptionUpdater._getIndividualFeatures = sinon.stub().yields(null, { 'individual': 'features' })
|
|
|
+ @SubscriptionUpdater._getGroupFeatureSets = sinon.stub().yields(null, [{ 'group': 'features' }, { 'group': 'features2' }])
|
|
|
+ @SubscriptionUpdater._getV1Features = sinon.stub().yields(null, { 'v1': 'features' })
|
|
|
+ @ReferalFeatures.getBonusFeatures = sinon.stub().yields(null, { 'bonus': 'features' })
|
|
|
+ @SubscriptionUpdater._mergeFeatures = sinon.stub().returns({'merged': 'features'})
|
|
|
+ @callback = sinon.stub()
|
|
|
+ @SubscriptionUpdater.refreshFeatures @user_id, @callback
|
|
|
+
|
|
|
+ it "should get the individual features", ->
|
|
|
+ @SubscriptionUpdater._getIndividualFeatures
|
|
|
+ .calledWith(@user_id)
|
|
|
+ .should.equal true
|
|
|
|
|
|
- it "should call updateFeatures with the subscription if set", (done)->
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null)
|
|
|
+ it "should get the group features", ->
|
|
|
+ @SubscriptionUpdater._getGroupFeatureSets
|
|
|
+ .calledWith(@user_id)
|
|
|
+ .should.equal true
|
|
|
|
|
|
- @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 get the v1 features", ->
|
|
|
+ @SubscriptionUpdater._getV1Features
|
|
|
+ .calledWith(@user_id)
|
|
|
+ .should.equal true
|
|
|
|
|
|
- it "should call updateFeatures with the group subscription if set", (done)->
|
|
|
- @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null)
|
|
|
- @SubscriptionLocator.getGroupSubscriptionMemberOf.callsArgWith(1, null, @groupSubscription)
|
|
|
+ it "should get the bonus features", ->
|
|
|
+ @ReferalFeatures.getBonusFeatures
|
|
|
+ .calledWith(@user_id)
|
|
|
+ .should.equal true
|
|
|
|
|
|
- @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 merge from the default features", ->
|
|
|
+ @SubscriptionUpdater._mergeFeatures.calledWith(@Settings.defaultFeatures).should.equal true
|
|
|
|
|
|
- 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'])
|
|
|
+ it "should merge the individual features", ->
|
|
|
+ @SubscriptionUpdater._mergeFeatures.calledWith(sinon.match.any, { 'individual': 'features' }).should.equal true
|
|
|
|
|
|
- @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 merge the group features", ->
|
|
|
+ @SubscriptionUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features' }).should.equal true
|
|
|
+ @SubscriptionUpdater._mergeFeatures.calledWith(sinon.match.any, { 'group': 'features2' }).should.equal true
|
|
|
|
|
|
- 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 merge the v1 features", ->
|
|
|
+ @SubscriptionUpdater._mergeFeatures.calledWith(sinon.match.any, { 'v1': 'features' }).should.equal true
|
|
|
|
|
|
+ it "should merge the bonus features", ->
|
|
|
+ @SubscriptionUpdater._mergeFeatures.calledWith(sinon.match.any, { 'bonus': 'features' }).should.equal true
|
|
|
|
|
|
- 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 update the user with the merged features", ->
|
|
|
+ @UserFeaturesUpdater.updateFeatures
|
|
|
+ .calledWith(@user_id, {'merged': 'features'})
|
|
|
+ .should.equal true
|
|
|
|
|
|
- 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
|
|
|
- done()
|
|
|
+ describe "_mergeFeatures", ->
|
|
|
+ it "should prefer priority over standard for compileGroup", ->
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ compileGroup: 'priority'
|
|
|
+ }, {
|
|
|
+ compileGroup: 'standard'
|
|
|
+ })).to.deep.equal({
|
|
|
+ compileGroup: 'priority'
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ compileGroup: 'standard'
|
|
|
+ }, {
|
|
|
+ compileGroup: 'priority'
|
|
|
+ })).to.deep.equal({
|
|
|
+ compileGroup: 'priority'
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ compileGroup: 'priority'
|
|
|
+ }, {
|
|
|
+ compileGroup: 'priority'
|
|
|
+ })).to.deep.equal({
|
|
|
+ compileGroup: 'priority'
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ compileGroup: 'standard'
|
|
|
+ }, {
|
|
|
+ compileGroup: 'standard'
|
|
|
+ })).to.deep.equal({
|
|
|
+ compileGroup: 'standard'
|
|
|
+ })
|
|
|
+
|
|
|
+ it "should prefer -1 over any other for collaborators", ->
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ collaborators: -1
|
|
|
+ }, {
|
|
|
+ collaborators: 10
|
|
|
+ })).to.deep.equal({
|
|
|
+ collaborators: -1
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ collaborators: 10
|
|
|
+ }, {
|
|
|
+ collaborators: -1
|
|
|
+ })).to.deep.equal({
|
|
|
+ collaborators: -1
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ collaborators: 4
|
|
|
+ }, {
|
|
|
+ collaborators: 10
|
|
|
+ })).to.deep.equal({
|
|
|
+ collaborators: 10
|
|
|
+ })
|
|
|
+
|
|
|
+ it "should prefer the higher of compileTimeout", ->
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ compileTimeout: 20
|
|
|
+ }, {
|
|
|
+ compileTimeout: 10
|
|
|
+ })).to.deep.equal({
|
|
|
+ compileTimeout: 20
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ compileTimeout: 10
|
|
|
+ }, {
|
|
|
+ compileTimeout: 20
|
|
|
+ })).to.deep.equal({
|
|
|
+ compileTimeout: 20
|
|
|
+ })
|
|
|
+
|
|
|
+ it "should prefer the true over false for other keys", ->
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ github: true
|
|
|
+ }, {
|
|
|
+ github: false
|
|
|
+ })).to.deep.equal({
|
|
|
+ github: true
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ github: false
|
|
|
+ }, {
|
|
|
+ github: true
|
|
|
+ })).to.deep.equal({
|
|
|
+ github: true
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ github: true
|
|
|
+ }, {
|
|
|
+ github: true
|
|
|
+ })).to.deep.equal({
|
|
|
+ github: true
|
|
|
+ })
|
|
|
+ expect(@SubscriptionUpdater._mergeFeatures({
|
|
|
+ github: false
|
|
|
+ }, {
|
|
|
+ github: false
|
|
|
+ })).to.deep.equal({
|
|
|
+ github: false
|
|
|
+ })
|
|
|
|
|
|
describe "deleteSubscription", ->
|
|
|
beforeEach (done) ->
|
|
|
@@ -255,7 +347,7 @@ describe "SubscriptionUpdater", ->
|
|
|
member_ids: [ ObjectId(), ObjectId(), ObjectId() ]
|
|
|
}
|
|
|
@SubscriptionLocator.getSubscription = sinon.stub().yields(null, @subscription)
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures = sinon.stub().yields()
|
|
|
+ @SubscriptionUpdater.refreshFeatures = sinon.stub().yields()
|
|
|
@SubscriptionUpdater.deleteSubscription @subscription_id, done
|
|
|
|
|
|
it "should look up the subscription", ->
|
|
|
@@ -269,22 +361,12 @@ describe "SubscriptionUpdater", ->
|
|
|
.should.equal true
|
|
|
|
|
|
it "should downgrade the admin_id", ->
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures
|
|
|
+ @SubscriptionUpdater.refreshFeatures
|
|
|
.calledWith(@subscription.admin_id)
|
|
|
.should.equal true
|
|
|
|
|
|
it "should downgrade all of the members", ->
|
|
|
for user_id in @subscription.member_ids
|
|
|
- @SubscriptionUpdater._setUsersMinimumFeatures
|
|
|
+ @SubscriptionUpdater.refreshFeatures
|
|
|
.calledWith(user_id)
|
|
|
.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
|