LimitationsManagerTests.coffee 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "isCollaboratorLimitReached", ->
  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.isCollaboratorLimitReached(@project_id, @callback)
  69. it "should return false", ->
  70. @callback.calledWith(null, false).should.equal true
  71. describe "when the project has more collaborators than allowed", ->
  72. beforeEach ->
  73. @current_number = 3
  74. @allowed_number = 2
  75. @LimitationsManager.isCollaboratorLimitReached(@project_id, @callback)
  76. it "should return true", ->
  77. @callback.calledWith(null, true).should.equal true
  78. describe "when the project has infinite collaborators", ->
  79. beforeEach ->
  80. @current_number = 100
  81. @allowed_number = -1
  82. @LimitationsManager.isCollaboratorLimitReached(@project_id, @callback)
  83. it "should return false", ->
  84. @callback.calledWith(null, false).should.equal true
  85. describe "userHasSubscription", ->
  86. beforeEach ->
  87. @SubscriptionLocator.getUsersSubscription = sinon.stub()
  88. it "should return true if the recurly token is set", (done)->
  89. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, recurlySubscription_id : "1234")
  90. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  91. hasSubscription.should.equal true
  92. done()
  93. it "should return false if the recurly token is not set", (done)->
  94. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  95. @subscription = {}
  96. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  97. hasSubscription.should.equal false
  98. done()
  99. it "should return false if the subscription is undefined", (done)->
  100. @SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  101. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  102. hasSubscription.should.equal false
  103. done()
  104. it "should return the subscription", (done)->
  105. stubbedSubscription = {freeTrial:{}, token:""}
  106. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
  107. @LimitationsManager.userHasSubscription @user, (err, hasSubOrFreeTrial, subscription)->
  108. subscription.should.deep.equal stubbedSubscription
  109. done()
  110. describe "userHasFreeTrial", ->
  111. beforeEach ->
  112. @SubscriptionLocator.getUsersSubscription = sinon.stub()
  113. it "should return true if the free trial is set", (done)->
  114. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, freeTrial:{expiresAt : "1234"})
  115. @LimitationsManager.userHasFreeTrial @user, (err, hasFreeTrial)->
  116. hasFreeTrial.should.equal true
  117. done()
  118. it "should return false if the free trial is not set", (done)->
  119. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {freeTrial:{}})
  120. @LimitationsManager.userHasFreeTrial @user, (err, hasFreeTrial)->
  121. hasFreeTrial.should.equal false
  122. done()
  123. it "should return false if the free trial is not there", (done)->
  124. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  125. @LimitationsManager.userHasFreeTrial @user, (err, hasFreeTrial)->
  126. hasFreeTrial.should.equal false
  127. done()
  128. it "should return false if the free trial is undefined", (done)->
  129. @SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  130. @LimitationsManager.userHasFreeTrial @user, (err, hasFreeTrial)->
  131. hasFreeTrial.should.equal false
  132. done()
  133. it "should return the subscription", (done)->
  134. stubbedSubscription = {freeTrial:{}, token:""}
  135. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
  136. @LimitationsManager.userHasFreeTrial @user, (err, hasSubOrFreeTrial, subscription)->
  137. subscription.should.deep.equal stubbedSubscription
  138. done()
  139. describe "userHasSubscriptionOrFreeTrial", ->
  140. beforeEach ->
  141. @hasFreeTrial = true
  142. @hasSubscription = true
  143. @LimitationsManager.userHasFreeTrial = sinon.stub().callsArgWith(1, null, @hasFreeTrial)
  144. @LimitationsManager.userHasSubscription = sinon.stub().callsArgWith(1, null, @hasSubscription)
  145. it "should return true if both are true", (done)->
  146. @hasFreeTrial = true
  147. @hasSubscription = true
  148. @LimitationsManager.userHasSubscriptionOrFreeTrial @user, (err, hasSubOrFreeTrial)->
  149. hasSubOrFreeTrial.should.equal true
  150. done()
  151. it "should return true if one is true", (done)->
  152. @hasFreeTrial = false
  153. @hasSubscription = true
  154. @LimitationsManager.userHasSubscriptionOrFreeTrial @user, (err, hasSubOrFreeTrial)->
  155. hasSubOrFreeTrial.should.equal true
  156. done()
  157. it "should return true if other is true", (done)->
  158. @hasFreeTrial = true
  159. @hasSubscription = false
  160. @LimitationsManager.userHasSubscriptionOrFreeTrial @user, (err, hasSubOrFreeTrial)->
  161. hasSubOrFreeTrial.should.equal true
  162. done()
  163. it "should return false if both are false", (done)->
  164. @hasSubscription = false
  165. @hasFreeTrial = false
  166. @LimitationsManager.userHasSubscriptionOrFreeTrial @user, (err, hasSubOrFreeTrial)->
  167. hasSubOrFreeTrial.should.equal true
  168. done()
  169. it "should return the subscription", (done)->
  170. stubbedSubscription = {freeTrial:{}, token:""}
  171. @LimitationsManager.userHasSubscription = sinon.stub().callsArgWith(1, null, null, stubbedSubscription)
  172. @LimitationsManager.userHasSubscriptionOrFreeTrial @user, (err, hasSubOrFreeTrial, subscription)->
  173. subscription.should.deep.equal stubbedSubscription
  174. done()
  175. describe "hasGroupMembersLimitReached", ->
  176. beforeEach ->
  177. @user_id = "12312"
  178. @subscription =
  179. membersLimit: 2
  180. member_ids: ["", ""]
  181. it "should return true if the limit is hit", (done)->
  182. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  183. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  184. limitReached.should.equal true
  185. done()
  186. it "should return false if the limit is not hit", (done)->
  187. @subscription.membersLimit = 3
  188. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  189. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  190. limitReached.should.equal false
  191. done()
  192. it "should return true if the limit has been exceded", (done)->
  193. @subscription.membersLimit = 0
  194. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  195. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  196. limitReached.should.equal true
  197. done()