LimitationsManagerTests.coffee 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. 'logger-sharelatex':log:->
  30. describe "allowedNumberOfCollaboratorsInProject", ->
  31. describe "when the project is owned by a user without a subscription", ->
  32. beforeEach ->
  33. @Settings.defaultPlanCode = collaborators: 23
  34. delete @user.features
  35. @callback = sinon.stub()
  36. @LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
  37. it "should return the default number", ->
  38. @callback.calledWith(null, @Settings.defaultPlanCode.collaborators).should.equal true
  39. describe "when the project is owned by a user with a subscription", ->
  40. beforeEach ->
  41. @user.features =
  42. collaborators: 21
  43. @callback = sinon.stub()
  44. @LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
  45. it "should return the number of collaborators the user is allowed", ->
  46. @callback.calledWith(null, @user.features.collaborators).should.equal true
  47. describe "currentNumberOfCollaboratorsInProject", ->
  48. beforeEach ->
  49. @project.collaberator_refs = ["one", "two"]
  50. @project.readOnly_refs = ["three"]
  51. @callback = sinon.stub()
  52. @LimitationsManager.currentNumberOfCollaboratorsInProject(@project_id, @callback)
  53. it "should return the total number of collaborators", ->
  54. @callback.calledWith(null, 3).should.equal true
  55. describe "canAddXCollaborators", ->
  56. beforeEach ->
  57. sinon.stub @LimitationsManager,
  58. "currentNumberOfCollaboratorsInProject",
  59. (project_id, callback) => callback(null, @current_number)
  60. sinon.stub @LimitationsManager,
  61. "allowedNumberOfCollaboratorsInProject",
  62. (project_id, callback) => callback(null, @allowed_number)
  63. @callback = sinon.stub()
  64. describe "when the project has fewer collaborators than allowed", ->
  65. beforeEach ->
  66. @current_number = 1
  67. @allowed_number = 2
  68. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  69. it "should return true", ->
  70. @callback.calledWith(null, true).should.equal true
  71. describe "when the project has fewer collaborators than allowed but I want to add more than allowed", ->
  72. beforeEach ->
  73. @current_number = 1
  74. @allowed_number = 2
  75. @LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
  76. it "should return false", ->
  77. @callback.calledWith(null, false).should.equal true
  78. describe "when the project has more collaborators than allowed", ->
  79. beforeEach ->
  80. @current_number = 3
  81. @allowed_number = 2
  82. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  83. it "should return false", ->
  84. @callback.calledWith(null, false).should.equal true
  85. describe "when the project has infinite collaborators", ->
  86. beforeEach ->
  87. @current_number = 100
  88. @allowed_number = -1
  89. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  90. it "should return true", ->
  91. @callback.calledWith(null, true).should.equal true
  92. describe "userHasSubscription", ->
  93. beforeEach ->
  94. @SubscriptionLocator.getUsersSubscription = sinon.stub()
  95. it "should return true if the recurly token is set", (done)->
  96. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, recurlySubscription_id : "1234")
  97. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  98. hasSubscription.should.equal true
  99. done()
  100. it "should return false if the recurly token is not set", (done)->
  101. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  102. @subscription = {}
  103. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  104. hasSubscription.should.equal false
  105. done()
  106. it "should return false if the subscription is undefined", (done)->
  107. @SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  108. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  109. hasSubscription.should.equal false
  110. done()
  111. it "should return the subscription", (done)->
  112. stubbedSubscription = {freeTrial:{}, token:""}
  113. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
  114. @LimitationsManager.userHasSubscription @user, (err, hasSubOrIsGroupMember, subscription)->
  115. subscription.should.deep.equal stubbedSubscription
  116. done()
  117. describe "userIsMemberOfGroupSubscription", ->
  118. beforeEach ->
  119. @SubscriptionLocator.getMemberSubscriptions = sinon.stub()
  120. it "should return false if there are no groups subcriptions", (done)->
  121. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
  122. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember)->
  123. isMember.should.equal false
  124. done()
  125. it "should return true if there are no groups subcriptions", (done)->
  126. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, subscriptions = ["mock-subscription"])
  127. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember, retSubscriptions)->
  128. isMember.should.equal true
  129. retSubscriptions.should.equal subscriptions
  130. done()
  131. describe "userHasSubscriptionOrIsGroupMember", ->
  132. beforeEach ->
  133. @LimitationsManager.userIsMemberOfGroupSubscription = sinon.stub()
  134. @LimitationsManager.userHasSubscription = sinon.stub()
  135. it "should return true if both are true", (done)->
  136. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
  137. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  138. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  139. hasSubOrIsGroupMember.should.equal true
  140. done()
  141. it "should return true if one is true", (done)->
  142. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
  143. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  144. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  145. hasSubOrIsGroupMember.should.equal true
  146. done()
  147. it "should return true if other is true", (done)->
  148. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
  149. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  150. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  151. hasSubOrIsGroupMember.should.equal true
  152. done()
  153. it "should return false if both are false", (done)->
  154. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
  155. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  156. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  157. hasSubOrIsGroupMember.should.equal false
  158. done()
  159. describe "hasGroupMembersLimitReached", ->
  160. beforeEach ->
  161. @user_id = "12312"
  162. @subscription =
  163. membersLimit: 2
  164. member_ids: ["", ""]
  165. it "should return true if the limit is hit", (done)->
  166. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  167. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  168. limitReached.should.equal true
  169. done()
  170. it "should return false if the limit is not hit", (done)->
  171. @subscription.membersLimit = 3
  172. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  173. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  174. limitReached.should.equal false
  175. done()
  176. it "should return true if the limit has been exceded", (done)->
  177. @subscription.membersLimit = 0
  178. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  179. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  180. limitReached.should.equal true
  181. done()