LimitationsManagerTests.coffee 9.8 KB

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