LimitationsManagerTests.coffee 11 KB

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