|
@@ -4,16 +4,16 @@ 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
|
|
|
-ObjectId = require('mongoose').Types.ObjectId
|
|
|
|
|
|
|
+ObjectId = require('mongoose').Types.ObjectId
|
|
|
|
|
|
|
|
describe "SubscriptionUpdater", ->
|
|
describe "SubscriptionUpdater", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
- @recurlySubscription =
|
|
|
|
|
|
|
+ @recurlySubscription =
|
|
|
uuid: "1238uoijdasjhd"
|
|
uuid: "1238uoijdasjhd"
|
|
|
plan:
|
|
plan:
|
|
|
plan_code: "kjhsakjds"
|
|
plan_code: "kjhsakjds"
|
|
|
- @adminUser =
|
|
|
|
|
|
|
+ @adminUser =
|
|
|
_id: @adminuser_id = "5208dd34438843e2db000007"
|
|
_id: @adminuser_id = "5208dd34438843e2db000007"
|
|
|
@otherUserId = "5208dd34438842e2db000005"
|
|
@otherUserId = "5208dd34438842e2db000005"
|
|
|
@allUserIds = ["13213", "dsadas", "djsaiud89"]
|
|
@allUserIds = ["13213", "dsadas", "djsaiud89"]
|
|
@@ -36,18 +36,18 @@ describe "SubscriptionUpdater", ->
|
|
|
@updateStub = sinon.stub().callsArgWith(2, null)
|
|
@updateStub = sinon.stub().callsArgWith(2, null)
|
|
|
@findAndModifyStub = sinon.stub().callsArgWith(2, null, @subscription)
|
|
@findAndModifyStub = sinon.stub().callsArgWith(2, null, @subscription)
|
|
|
@SubscriptionModel = class
|
|
@SubscriptionModel = class
|
|
|
- constructor: (opts)->
|
|
|
|
|
|
|
+ constructor: (opts)->
|
|
|
subscription.admin_id = opts.admin_id
|
|
subscription.admin_id = opts.admin_id
|
|
|
return subscription
|
|
return subscription
|
|
|
@remove: sinon.stub().yields()
|
|
@remove: sinon.stub().yields()
|
|
|
@SubscriptionModel.update = @updateStub
|
|
@SubscriptionModel.update = @updateStub
|
|
|
@SubscriptionModel.findAndModify = @findAndModifyStub
|
|
@SubscriptionModel.findAndModify = @findAndModifyStub
|
|
|
|
|
|
|
|
- @SubscriptionLocator =
|
|
|
|
|
|
|
+ @SubscriptionLocator =
|
|
|
getUsersSubscription: sinon.stub()
|
|
getUsersSubscription: sinon.stub()
|
|
|
getGroupSubscriptionMemberOf:sinon.stub()
|
|
getGroupSubscriptionMemberOf:sinon.stub()
|
|
|
-
|
|
|
|
|
- @Settings =
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @Settings =
|
|
|
freeTrialPlanCode: "collaborator"
|
|
freeTrialPlanCode: "collaborator"
|
|
|
defaultPlanCode: "personal"
|
|
defaultPlanCode: "personal"
|
|
|
defaultFeatures: { "default": "features" }
|
|
defaultFeatures: { "default": "features" }
|
|
@@ -58,12 +58,18 @@ describe "SubscriptionUpdater", ->
|
|
|
@PlansLocator =
|
|
@PlansLocator =
|
|
|
findLocalPlanInSettings: sinon.stub().returns({})
|
|
findLocalPlanInSettings: sinon.stub().returns({})
|
|
|
|
|
|
|
|
|
|
+ @UserGetter =
|
|
|
|
|
+ getUsers: (memberIds, projection, callback) ->
|
|
|
|
|
+ users = memberIds.map (id) -> { _id: id }
|
|
|
|
|
+ callback(null, users)
|
|
|
|
|
+
|
|
|
@ReferalFeatures = getBonusFeatures: sinon.stub().callsArgWith(1)
|
|
@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
|
|
|
'./UserFeaturesUpdater': @UserFeaturesUpdater
|
|
'./UserFeaturesUpdater': @UserFeaturesUpdater
|
|
|
'./SubscriptionLocator': @SubscriptionLocator
|
|
'./SubscriptionLocator': @SubscriptionLocator
|
|
|
|
|
+ '../User/UserGetter': @UserGetter
|
|
|
'./PlansLocator': @PlansLocator
|
|
'./PlansLocator': @PlansLocator
|
|
|
"logger-sharelatex": log:->
|
|
"logger-sharelatex": log:->
|
|
|
'settings-sharelatex': @Settings
|
|
'settings-sharelatex': @Settings
|
|
@@ -73,7 +79,6 @@ describe "SubscriptionUpdater", ->
|
|
|
describe "syncSubscription", ->
|
|
describe "syncSubscription", ->
|
|
|
|
|
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
-
|
|
|
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
@SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly = sinon.stub().callsArgWith(2)
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly = sinon.stub().callsArgWith(2)
|
|
|
|
|
|
|
@@ -87,7 +92,6 @@ describe "SubscriptionUpdater", ->
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
it "should not call updateFeatures with group subscription if recurly subscription is not expired", (done)->
|
|
it "should not call updateFeatures with group subscription if recurly subscription is not expired", (done)->
|
|
|
-
|
|
|
|
|
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
|
|
@SubscriptionUpdater.syncSubscription @recurlySubscription, @adminUser._id, (err)=>
|
|
|
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
|
|
@SubscriptionLocator.getUsersSubscription.calledWith(@adminUser._id).should.equal true
|
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal true
|
|
@SubscriptionUpdater._updateSubscriptionFromRecurly.called.should.equal true
|
|
@@ -99,7 +103,7 @@ describe "SubscriptionUpdater", ->
|
|
|
describe "_updateSubscriptionFromRecurly", ->
|
|
describe "_updateSubscriptionFromRecurly", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
@FeaturesUpdater.refreshFeatures = 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)=>
|
|
|
@subscription.recurlySubscription_id.should.equal @recurlySubscription.uuid
|
|
@subscription.recurlySubscription_id.should.equal @recurlySubscription.uuid
|
|
@@ -143,7 +147,6 @@ describe "SubscriptionUpdater", ->
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
describe "_createNewSubscription", ->
|
|
describe "_createNewSubscription", ->
|
|
|
it "should create a new subscription then update the subscription", (done)->
|
|
it "should create a new subscription then update the subscription", (done)->
|
|
|
@SubscriptionUpdater._createNewSubscription @adminUser._id, =>
|
|
@SubscriptionUpdater._createNewSubscription @adminUser._id, =>
|
|
@@ -154,14 +157,24 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
|
|
describe "addUserToGroup", ->
|
|
describe "addUserToGroup", ->
|
|
|
beforeEach ->
|
|
beforeEach ->
|
|
|
- @FeaturesUpdater.refreshFeatures = sinon.stub().callsArgWith(1)
|
|
|
|
|
|
|
+ @SubscriptionUpdater.addUsersToGroup = sinon.stub().yields(null)
|
|
|
|
|
|
|
|
- it "should add the users id to the group as a set", (done)->
|
|
|
|
|
|
|
+ it "delegates to addUsersToGroup", (done)->
|
|
|
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
|
|
@SubscriptionUpdater.addUserToGroup @adminUser._id, @otherUserId, =>
|
|
|
- searchOps =
|
|
|
|
|
|
|
+ @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], =>
|
|
|
|
|
+ searchOps =
|
|
|
admin_id: @adminUser._id
|
|
admin_id: @adminUser._id
|
|
|
- insertOperation =
|
|
|
|
|
- "$addToSet": {member_ids:@otherUserId}
|
|
|
|
|
|
|
+ insertOperation =
|
|
|
|
|
+ { $push: { member_ids: { $each: [@otherUserId] } } }
|
|
|
@findAndModifyStub.calledWith(searchOps, insertOperation).should.equal true
|
|
@findAndModifyStub.calledWith(searchOps, insertOperation).should.equal true
|
|
|
done()
|
|
done()
|
|
|
|
|
|
|
@@ -176,9 +189,9 @@ describe "SubscriptionUpdater", ->
|
|
|
|
|
|
|
|
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, =>
|
|
|
- searchOps =
|
|
|
|
|
|
|
+ searchOps =
|
|
|
admin_id:@adminUser._id
|
|
admin_id:@adminUser._id
|
|
|
- removeOperation =
|
|
|
|
|
|
|
+ removeOperation =
|
|
|
"$pull": {member_ids:@otherUserId}
|
|
"$pull": {member_ids:@otherUserId}
|
|
|
@updateStub.calledWith(searchOps, removeOperation).should.equal true
|
|
@updateStub.calledWith(searchOps, removeOperation).should.equal true
|
|
|
done()
|
|
done()
|
|
@@ -199,22 +212,22 @@ describe "SubscriptionUpdater", ->
|
|
|
@SubscriptionLocator.getSubscription = sinon.stub().yields(null, @subscription)
|
|
@SubscriptionLocator.getSubscription = sinon.stub().yields(null, @subscription)
|
|
|
@FeaturesUpdater.refreshFeatures = 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", ->
|
|
|
@SubscriptionLocator.getSubscription
|
|
@SubscriptionLocator.getSubscription
|
|
|
.calledWith(@subscription_id)
|
|
.calledWith(@subscription_id)
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
it "should remove the subscription", ->
|
|
it "should remove the subscription", ->
|
|
|
@SubscriptionModel.remove
|
|
@SubscriptionModel.remove
|
|
|
.calledWith({_id: ObjectId(@subscription_id)})
|
|
.calledWith({_id: ObjectId(@subscription_id)})
|
|
|
.should.equal true
|
|
.should.equal true
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
it "should downgrade the admin_id", ->
|
|
it "should downgrade the admin_id", ->
|
|
|
@FeaturesUpdater.refreshFeatures
|
|
@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
|
|
|
@FeaturesUpdater.refreshFeatures
|
|
@FeaturesUpdater.refreshFeatures
|