LimitationsManagerTests.coffee 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../../app/js/Features/Subscription/LimitationsManager'
  5. Settings = require("settings-sharelatex")
  6. describe "LimitationsManager", ->
  7. beforeEach ->
  8. @project = { _id: "project-id" }
  9. @user = { _id: "user-id", features:{} }
  10. @Project =
  11. findById: (project_id, fields, callback) =>
  12. if project_id == @project_id
  13. callback null, @project
  14. else
  15. callback null, null
  16. @User =
  17. findById: (user_id, callback) =>
  18. if user_id == @user.id
  19. callback null, @user
  20. else
  21. callback null, null
  22. @SubscriptionLocator =
  23. getUsersSubscription: sinon.stub()
  24. @LimitationsManager = SandboxedModule.require modulePath, requires:
  25. '../../models/Project' : Project: @Project
  26. '../../models/User' : User: @User
  27. './SubscriptionLocator':@SubscriptionLocator
  28. 'settings-sharelatex' : @Settings = {}
  29. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
  30. 'logger-sharelatex':log:->
  31. describe "allowedNumberOfCollaboratorsInProject", ->
  32. describe "when the project is owned by a user without a subscription", ->
  33. beforeEach ->
  34. @Settings.defaultPlanCode = collaborators: 23
  35. delete @user.features
  36. @callback = sinon.stub()
  37. @LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
  38. it "should return the default number", ->
  39. @callback.calledWith(null, @Settings.defaultPlanCode.collaborators).should.equal true
  40. describe "when the project is owned by a user with a subscription", ->
  41. beforeEach ->
  42. @user.features =
  43. collaborators: 21
  44. @callback = sinon.stub()
  45. @LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
  46. it "should return the number of collaborators the user is allowed", ->
  47. @callback.calledWith(null, @user.features.collaborators).should.equal true
  48. describe "canAddXCollaborators", ->
  49. beforeEach ->
  50. @CollaboratorsHandler.getCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  51. sinon.stub @LimitationsManager,
  52. "allowedNumberOfCollaboratorsInProject",
  53. (project_id, callback) => callback(null, @allowed_number)
  54. @callback = sinon.stub()
  55. describe "when the project has fewer collaborators than allowed", ->
  56. beforeEach ->
  57. @current_number = 1
  58. @allowed_number = 2
  59. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  60. it "should return true", ->
  61. @callback.calledWith(null, true).should.equal true
  62. describe "when the project has fewer collaborators than allowed but I want to add more than allowed", ->
  63. beforeEach ->
  64. @current_number = 1
  65. @allowed_number = 2
  66. @LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
  67. it "should return false", ->
  68. @callback.calledWith(null, false).should.equal true
  69. describe "when the project has more collaborators than allowed", ->
  70. beforeEach ->
  71. @current_number = 3
  72. @allowed_number = 2
  73. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  74. it "should return false", ->
  75. @callback.calledWith(null, false).should.equal true
  76. describe "when the project has infinite collaborators", ->
  77. beforeEach ->
  78. @current_number = 100
  79. @allowed_number = -1
  80. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  81. it "should return true", ->
  82. @callback.calledWith(null, true).should.equal true
  83. describe "userHasSubscription", ->
  84. beforeEach ->
  85. @SubscriptionLocator.getUsersSubscription = sinon.stub()
  86. it "should return true if the recurly token is set", (done)->
  87. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, recurlySubscription_id : "1234")
  88. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  89. hasSubscription.should.equal true
  90. done()
  91. it "should return false if the recurly token is not set", (done)->
  92. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  93. @subscription = {}
  94. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  95. hasSubscription.should.equal false
  96. done()
  97. it "should return false if the subscription is undefined", (done)->
  98. @SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  99. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  100. hasSubscription.should.equal false
  101. done()
  102. it "should return the subscription", (done)->
  103. stubbedSubscription = {freeTrial:{}, token:""}
  104. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
  105. @LimitationsManager.userHasSubscription @user, (err, hasSubOrIsGroupMember, subscription)->
  106. subscription.should.deep.equal stubbedSubscription
  107. done()
  108. describe "userIsMemberOfGroupSubscription", ->
  109. beforeEach ->
  110. @SubscriptionLocator.getMemberSubscriptions = sinon.stub()
  111. it "should return false if there are no groups subcriptions", (done)->
  112. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
  113. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember)->
  114. isMember.should.equal false
  115. done()
  116. it "should return true if there are no groups subcriptions", (done)->
  117. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, subscriptions = ["mock-subscription"])
  118. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember, retSubscriptions)->
  119. isMember.should.equal true
  120. retSubscriptions.should.equal subscriptions
  121. done()
  122. describe "userHasSubscriptionOrIsGroupMember", ->
  123. beforeEach ->
  124. @LimitationsManager.userIsMemberOfGroupSubscription = sinon.stub()
  125. @LimitationsManager.userHasSubscription = sinon.stub()
  126. it "should return true if both are true", (done)->
  127. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
  128. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  129. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  130. hasSubOrIsGroupMember.should.equal true
  131. done()
  132. it "should return true if one is true", (done)->
  133. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
  134. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  135. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  136. hasSubOrIsGroupMember.should.equal true
  137. done()
  138. it "should return true if other is true", (done)->
  139. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
  140. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  141. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  142. hasSubOrIsGroupMember.should.equal true
  143. done()
  144. it "should return false if both are false", (done)->
  145. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
  146. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  147. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  148. hasSubOrIsGroupMember.should.equal false
  149. done()
  150. describe "hasGroupMembersLimitReached", ->
  151. beforeEach ->
  152. @user_id = "12312"
  153. @subscription =
  154. membersLimit: 2
  155. member_ids: ["", ""]
  156. it "should return true if the limit is hit", (done)->
  157. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  158. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  159. limitReached.should.equal true
  160. done()
  161. it "should return false if the limit is not hit", (done)->
  162. @subscription.membersLimit = 3
  163. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  164. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  165. limitReached.should.equal false
  166. done()
  167. it "should return true if the limit has been exceded", (done)->
  168. @subscription.membersLimit = 0
  169. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  170. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  171. limitReached.should.equal true
  172. done()