LimitationsManagerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. getSubscription: sinon.stub()
  25. @LimitationsManager = SandboxedModule.require modulePath, requires:
  26. '../Project/ProjectGetter': @ProjectGetter
  27. '../User/UserGetter' : @UserGetter
  28. './SubscriptionLocator':@SubscriptionLocator
  29. 'settings-sharelatex' : @Settings = {}
  30. "../Collaborators/CollaboratorsHandler": @CollaboratorsHandler = {}
  31. "../Collaborators/CollaboratorsInviteHandler": @CollaboratorsInviteHandler = {}
  32. "./V1SubscriptionManager": @V1SubscriptionManager = {}
  33. 'logger-sharelatex':log:->
  34. describe "allowedNumberOfCollaboratorsInProject", ->
  35. describe "when the project is owned by a user without a subscription", ->
  36. beforeEach ->
  37. @Settings.defaultFeatures = collaborators: 23
  38. @project.owner_ref = @user_id
  39. delete @user.features
  40. @callback = sinon.stub()
  41. @LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
  42. it "should return the default number", ->
  43. @callback.calledWith(null, @Settings.defaultFeatures.collaborators).should.equal true
  44. describe "when the project is owned by a user with a subscription", ->
  45. beforeEach ->
  46. @project.owner_ref = @user_id
  47. @user.features =
  48. collaborators: 21
  49. @callback = sinon.stub()
  50. @LimitationsManager.allowedNumberOfCollaboratorsInProject(@project_id, @callback)
  51. it "should return the number of collaborators the user is allowed", ->
  52. @callback.calledWith(null, @user.features.collaborators).should.equal true
  53. describe "allowedNumberOfCollaboratorsForUser", ->
  54. describe "when the user has no features", ->
  55. beforeEach ->
  56. @Settings.defaultFeatures = collaborators: 23
  57. delete @user.features
  58. @callback = sinon.stub()
  59. @LimitationsManager.allowedNumberOfCollaboratorsForUser(@user_id, @callback)
  60. it "should return the default number", ->
  61. @callback.calledWith(null, @Settings.defaultFeatures.collaborators).should.equal true
  62. describe "when the user has features", ->
  63. beforeEach ->
  64. @user.features =
  65. collaborators: 21
  66. @callback = sinon.stub()
  67. @LimitationsManager.allowedNumberOfCollaboratorsForUser(@user_id, @callback)
  68. it "should return the number of collaborators the user is allowed", ->
  69. @callback.calledWith(null, @user.features.collaborators).should.equal true
  70. describe "canAddXCollaborators", ->
  71. describe "when the project has fewer collaborators than allowed", ->
  72. beforeEach ->
  73. @current_number = 1
  74. @allowed_number = 2
  75. @invite_count = 0
  76. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  77. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  78. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  79. callback(null, @allowed_number)
  80. @callback = sinon.stub()
  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. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  90. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  91. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  92. callback(null, @allowed_number)
  93. @callback = sinon.stub()
  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 fewer collaborators than allowed but I want to add more than allowed", ->
  98. beforeEach ->
  99. @current_number = 1
  100. @allowed_number = 2
  101. @invite_count = 0
  102. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  103. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  104. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  105. callback(null, @allowed_number)
  106. @callback = sinon.stub()
  107. @LimitationsManager.canAddXCollaborators(@project_id, 2, @callback)
  108. it "should return false", ->
  109. @callback.calledWith(null, false).should.equal true
  110. describe "when the project has more collaborators than allowed", ->
  111. beforeEach ->
  112. @current_number = 3
  113. @allowed_number = 2
  114. @invite_count = 0
  115. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  116. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  117. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  118. callback(null, @allowed_number)
  119. @callback = sinon.stub()
  120. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  121. it "should return false", ->
  122. @callback.calledWith(null, false).should.equal true
  123. describe "when the project has infinite collaborators", ->
  124. beforeEach ->
  125. @current_number = 100
  126. @allowed_number = -1
  127. @invite_count = 0
  128. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  129. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  130. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  131. callback(null, @allowed_number)
  132. @callback = sinon.stub()
  133. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  134. it "should return true", ->
  135. @callback.calledWith(null, true).should.equal true
  136. describe 'when the project has more invites than allowed', ->
  137. beforeEach ->
  138. @current_number = 0
  139. @allowed_number = 2
  140. @invite_count = 2
  141. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  142. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  143. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  144. callback(null, @allowed_number)
  145. @callback = sinon.stub()
  146. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  147. it "should return false", ->
  148. @callback.calledWith(null, false).should.equal true
  149. describe 'when the project has more invites and collaborators than allowed', ->
  150. beforeEach ->
  151. @current_number = 1
  152. @allowed_number = 2
  153. @invite_count = 1
  154. @CollaboratorsHandler.getInvitedCollaboratorCount = (project_id, callback) => callback(null, @current_number)
  155. @CollaboratorsInviteHandler.getInviteCount = (project_id, callback) => callback(null, @invite_count)
  156. sinon.stub @LimitationsManager, "allowedNumberOfCollaboratorsInProject", (project_id, callback) =>
  157. callback(null, @allowed_number)
  158. @callback = sinon.stub()
  159. @LimitationsManager.canAddXCollaborators(@project_id, 1, @callback)
  160. it "should return false", ->
  161. @callback.calledWith(null, false).should.equal true
  162. describe "userHasV2Subscription", ->
  163. beforeEach ->
  164. @SubscriptionLocator.getUsersSubscription = sinon.stub()
  165. it "should return true if the recurly token is set", (done)->
  166. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, recurlySubscription_id : "1234")
  167. @LimitationsManager.userHasV2Subscription @user, (err, hasSubscription)->
  168. hasSubscription.should.equal true
  169. done()
  170. it "should return false if the recurly token is not set", (done)->
  171. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, {})
  172. @subscription = {}
  173. @LimitationsManager.userHasV2Subscription @user, (err, hasSubscription)->
  174. hasSubscription.should.equal false
  175. done()
  176. it "should return false if the subscription is undefined", (done)->
  177. @SubscriptionLocator.getUsersSubscription.callsArgWith(1)
  178. @LimitationsManager.userHasV2Subscription @user, (err, hasSubscription)->
  179. hasSubscription.should.equal false
  180. done()
  181. it "should return the subscription", (done)->
  182. stubbedSubscription = {freeTrial:{}, token:""}
  183. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, stubbedSubscription)
  184. @LimitationsManager.userHasV2Subscription @user, (err, hasSubOrIsGroupMember, subscription)->
  185. subscription.should.deep.equal stubbedSubscription
  186. done()
  187. describe "when user has a custom account", ->
  188. beforeEach ->
  189. @fakeSubscription = {customAccount: true}
  190. @SubscriptionLocator.getUsersSubscription.callsArgWith(1, null, @fakeSubscription)
  191. it 'should return true', (done) ->
  192. @LimitationsManager.userHasV2Subscription @user, (err, hasSubscription, subscription)->
  193. hasSubscription.should.equal true
  194. done()
  195. it 'should return the subscription', (done) ->
  196. @LimitationsManager.userHasV2Subscription @user, (err, hasSubscription, subscription)=>
  197. subscription.should.deep.equal @fakeSubscription
  198. done()
  199. describe "userIsMemberOfGroupSubscription", ->
  200. beforeEach ->
  201. @SubscriptionLocator.getMemberSubscriptions = sinon.stub()
  202. it "should return false if there are no groups subcriptions", (done)->
  203. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, [])
  204. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember)->
  205. isMember.should.equal false
  206. done()
  207. it "should return true if there are no groups subcriptions", (done)->
  208. @SubscriptionLocator.getMemberSubscriptions.callsArgWith(1, null, subscriptions = ["mock-subscription"])
  209. @LimitationsManager.userIsMemberOfGroupSubscription @user, (err, isMember, retSubscriptions)->
  210. isMember.should.equal true
  211. retSubscriptions.should.equal subscriptions
  212. done()
  213. describe "hasPaidSubscription", ->
  214. beforeEach ->
  215. @LimitationsManager.userIsMemberOfGroupSubscription = sinon.stub().yields(null, false)
  216. @LimitationsManager.userHasV2Subscription = sinon.stub().yields(null, false)
  217. @LimitationsManager.userHasV1Subscription = sinon.stub().yields(null, false)
  218. it "should return true if userIsMemberOfGroupSubscription", (done)->
  219. @LimitationsManager.userIsMemberOfGroupSubscription = sinon.stub().yields(null, true)
  220. @LimitationsManager.hasPaidSubscription @user, (err, hasSubOrIsGroupMember)->
  221. hasSubOrIsGroupMember.should.equal true
  222. done()
  223. it "should return true if userHasV2Subscription", (done)->
  224. @LimitationsManager.userHasV2Subscription = sinon.stub().yields(null, true)
  225. @LimitationsManager.hasPaidSubscription @user, (err, hasSubOrIsGroupMember)->
  226. hasSubOrIsGroupMember.should.equal true
  227. done()
  228. it "should return true if userHasV1Subscription", (done)->
  229. @LimitationsManager.userHasV1Subscription= sinon.stub().yields(null, true)
  230. @LimitationsManager.hasPaidSubscription @user, (err, hasSubOrIsGroupMember)->
  231. hasSubOrIsGroupMember.should.equal true
  232. done()
  233. it "should return false if none are true", (done)->
  234. @LimitationsManager.hasPaidSubscription @user, (err, hasSubOrIsGroupMember)->
  235. hasSubOrIsGroupMember.should.equal false
  236. done()
  237. it "should have userHasSubscriptionOrIsGroupMember alias", (done)->
  238. @LimitationsManager.userHasSubscriptionOrIsGroupMember @user, (err, hasSubOrIsGroupMember)->
  239. hasSubOrIsGroupMember.should.equal false
  240. done()
  241. describe "userHasV1OrV2Subscription", ->
  242. beforeEach ->
  243. @LimitationsManager.userHasV2Subscription = sinon.stub().yields(null, false)
  244. @LimitationsManager.userHasV1Subscription = sinon.stub().yields(null, false)
  245. it "should return true if userHasV2Subscription", (done)->
  246. @LimitationsManager.userHasV2Subscription = sinon.stub().yields(null, true)
  247. @LimitationsManager.userHasV1OrV2Subscription @user, (err, hasSub)->
  248. hasSub.should.equal true
  249. done()
  250. it "should return true if userHasV1Subscription", (done)->
  251. @LimitationsManager.userHasV1Subscription = sinon.stub().yields(null, true)
  252. @LimitationsManager.userHasV1OrV2Subscription @user, (err, hasSub)->
  253. hasSub.should.equal true
  254. done()
  255. it "should return false if none are true", (done)->
  256. @LimitationsManager.userHasV1OrV2Subscription @user, (err, hasSub)->
  257. hasSub.should.equal false
  258. done()
  259. describe "hasGroupMembersLimitReached", ->
  260. beforeEach ->
  261. @subscriptionId = "12312"
  262. @subscription =
  263. membersLimit: 3
  264. member_ids: ["", ""]
  265. teamInvites: [
  266. { email: "bob@example.com", sentAt: new Date(), token: "hey" }
  267. ]
  268. it "should return true if the limit is hit (including members and invites)", (done)->
  269. @SubscriptionLocator.getSubscription.callsArgWith(1, null, @subscription)
  270. @LimitationsManager.hasGroupMembersLimitReached @subscriptionId, (err, limitReached)->
  271. limitReached.should.equal true
  272. done()
  273. it "should return false if the limit is not hit (including members and invites)", (done)->
  274. @subscription.membersLimit = 4
  275. @SubscriptionLocator.getSubscription.callsArgWith(1, null, @subscription)
  276. @LimitationsManager.hasGroupMembersLimitReached @subscriptionId, (err, limitReached)->
  277. limitReached.should.equal false
  278. done()
  279. it "should return true if the limit has been exceded (including members and invites)", (done)->
  280. @subscription.membersLimit = 2
  281. @SubscriptionLocator.getSubscription.callsArgWith(1, null, @subscription)
  282. @LimitationsManager.hasGroupMembersLimitReached @subscriptionId, (err, limitReached)->
  283. limitReached.should.equal true
  284. done()
  285. describe 'userHasV1Subscription', ->
  286. it 'should return true if v1 returns has_subscription = true', (done) ->
  287. @V1SubscriptionManager.getSubscriptionsFromV1 = sinon.stub().yields(null, { has_subscription: true })
  288. @LimitationsManager.userHasV1Subscription @user, (error, result) =>
  289. @V1SubscriptionManager.getSubscriptionsFromV1.calledWith(@user_id).should.equal true
  290. result.should.equal true
  291. done()
  292. it 'should return false if v1 returns has_subscription = false', (done) ->
  293. @V1SubscriptionManager.getSubscriptionsFromV1 = sinon.stub().yields(null, { has_subscription: false })
  294. @LimitationsManager.userHasV1Subscription @user, (error, result) =>
  295. @V1SubscriptionManager.getSubscriptionsFromV1.calledWith(@user_id).should.equal true
  296. result.should.equal false
  297. done()
  298. it 'should return false if v1 returns nothing', (done) ->
  299. @V1SubscriptionManager.getSubscriptionsFromV1 = sinon.stub().yields(null, null)
  300. @LimitationsManager.userHasV1Subscription @user, (error, result) =>
  301. @V1SubscriptionManager.getSubscriptionsFromV1.calledWith(@user_id).should.equal true
  302. result.should.equal false
  303. done()