LimitationsManagerTests.coffee 17 KB

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