LimitationsManagerTests.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. @ProjectGetter =
  11. getProject: (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. '../Project/ProjectGetter': @ProjectGetter
  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. describe "when the project has fewer collaborators than allowed", ->
  70. beforeEach ->
  71. @current_number = 1
  72. @allowed_number = 2
  73. @invite_count = 0
  74. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  75. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  76. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  77. callback(null, @allowed_number)
  78. @callback = sinon.stub()
  79. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  80. it "should return true", ->
  81. @callback.calledWith(null, true).should.equal true
  82. describe "when the project has fewer collaborators and invites than allowed", ->
  83. beforeEach ->
  84. @current_number = 1
  85. @allowed_number = 4
  86. @invite_count = 1
  87. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  88. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  89. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  90. callback(null, @allowed_number)
  91. @callback = sinon.stub()
  92. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  93. it "should return true", ->
  94. @callback.calledWith(null, true).should.equal true
  95. describe "when the project has fewer collaborators than allowed but I want to add more than allowed", ->
  96. beforeEach ->
  97. @current_number = 1
  98. @allowed_number = 2
  99. @invite_count = 0
  100. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  101. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  102. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  103. callback(null, @allowed_number)
  104. @callback = sinon.stub()
  105. @LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
  106. it "should return false", ->
  107. @callback.calledWith(null, false).should.equal true
  108. describe "when the project has more collaborators than allowed", ->
  109. beforeEach ->
  110. @current_number = 3
  111. @allowed_number = 2
  112. @invite_count = 0
  113. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  114. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  115. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  116. callback(null, @allowed_number)
  117. @callback = sinon.stub()
  118. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  119. it "should return false", ->
  120. @callback.calledWith(null, false).should.equal true
  121. describe "when the project has infinite collaborators", ->
  122. beforeEach ->
  123. @current_number = 100
  124. @allowed_number = -1
  125. @invite_count = 0
  126. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  127. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  128. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  129. callback(null, @allowed_number)
  130. @callback = sinon.stub()
  131. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  132. it "should return true", ->
  133. @callback.calledWith(null, true).should.equal true
  134. describe 'when the project has more invites than allowed', ->
  135. beforeEach ->
  136. @current_number = 0
  137. @allowed_number = 2
  138. @invite_count = 2
  139. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  140. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  141. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  142. callback(null, @allowed_number)
  143. @callback = sinon.stub()
  144. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  145. it "should return false", ->
  146. @callback.calledWith(null, false).should.equal true
  147. describe 'when the project has more invites and collaborators than allowed', ->
  148. beforeEach ->
  149. @current_number = 1
  150. @allowed_number = 2
  151. @invite_count = 1
  152. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  153. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  154. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  155. callback(null, @allowed_number)
  156. @callback = sinon.stub()
  157. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  158. it "should return false", ->
  159. @callback.calledWith(null, false).should.equal true
  160. describe "userHasSubscription", ->
  161. beforeEach ->
  162. @SubscriptionLocator.getUsersSubscription = sinon.stub()
  163. it "should return true if the recurly token is set", (done)->
  164. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, recurlySubscription_id : "1234")
  165. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  166. hasSubscription.should.equal true
  167. done()
  168. it "should return false if the recurly token is not set", (done)->
  169. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  170. @subscription = {}
  171. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  172. hasSubscription.should.equal false
  173. done()
  174. it "should return false if the subscription is undefined", (done)->
  175. @SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  176. @LimitationsManager.userHasSubscription @user, (err, hasSubscription)->
  177. hasSubscription.should.equal false
  178. done()
  179. it "should return the subscription", (done)->
  180. stubbedSubscription = {freeTrial:{}, token:""}
  181. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
  182. @LimitationsManager.userHasSubscription @user, (err, hasSubOrIsGroupMember, subscription)->
  183. subscription.should.deep.equal stubbedSubscription
  184. done()
  185. describe "when user has a custom account", ->
  186. beforeEach ->
  187. @fakeSubscription = {customAccount: true}
  188. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @fakeSubscription)
  189. it 'should return true', (done) ->
  190. @LimitationsManager.userHasSubscription @user, (err, hasSubscription, subscription)->
  191. hasSubscription.should.equal true
  192. done()
  193. it 'should return the subscription', (done) ->
  194. @LimitationsManager.userHasSubscription @user, (err, hasSubscription, subscription)=>
  195. subscription.should.deep.equal @fakeSubscription
  196. done()
  197. describe "userIsMemberOfGroupSubscription", ->
  198. beforeEach ->
  199. @SubscriptionLocator.getMemberSubscriptions = sinon.stub()
  200. it "should return false if there are no groups subcriptions", (done)->
  201. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
  202. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember)->
  203. isMember.should.equal false
  204. done()
  205. it "should return true if there are no groups subcriptions", (done)->
  206. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, subscriptions = ["mock-subscription"])
  207. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember, retSubscriptions)->
  208. isMember.should.equal true
  209. retSubscriptions.should.equal subscriptions
  210. done()
  211. describe "userHasSubscriptionOrIsGroupMember", ->
  212. beforeEach ->
  213. @LimitationsManager.userIsMemberOfGroupSubscription = sinon.stub()
  214. @LimitationsManager.userHasSubscription = sinon.stub()
  215. it "should return true if both are true", (done)->
  216. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
  217. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  218. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  219. hasSubOrIsGroupMember.should.equal true
  220. done()
  221. it "should return true if one is true", (done)->
  222. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, true)
  223. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  224. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  225. hasSubOrIsGroupMember.should.equal true
  226. done()
  227. it "should return true if other is true", (done)->
  228. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
  229. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  230. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  231. hasSubOrIsGroupMember.should.equal true
  232. done()
  233. it "should return false if both are false", (done)->
  234. @LimitationsManager.userIsMemberOfGroupSubscription.callsArgWith(1, null, false)
  235. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  236. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  237. hasSubOrIsGroupMember.should.equal false
  238. done()
  239. describe "hasGroupMembersLimitReached", ->
  240. beforeEach ->
  241. @user_id = "12312"
  242. @subscription =
  243. membersLimit: 3
  244. member_ids: ["", ""]
  245. invited_emails: ["bob@example.com"]
  246. it "should return true if the limit is hit (including members and invites)", (done)->
  247. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  248. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  249. limitReached.should.equal true
  250. done()
  251. it "should return false if the limit is not hit (including members and invites)", (done)->
  252. @subscription.membersLimit = 4
  253. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  254. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  255. limitReached.should.equal false
  256. done()
  257. it "should return true if the limit has been exceded (including members and invites)", (done)->
  258. @subscription.membersLimit = 2
  259. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @subscription)
  260. @LimitationsManager.hasGroupMembersLimitReached @user_id, (err, limitReached)->
  261. limitReached.should.equal true
  262. done()