SubscriptionHandlerTests.coffee 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. SandboxedModule = require('sandboxed-module')
  2. should = require('chai').should()
  3. sinon = require 'sinon'
  4. querystring = require 'querystring'
  5. modulePath = "../../../../app/js/Features/Subscription/SubscriptionHandler"
  6. mockRecurlySubscriptions =
  7. "subscription-123-active":
  8. uuid: "subscription-123-active"
  9. plan:
  10. name: "Gold"
  11. plan_code: "gold"
  12. current_period_ends_at: new Date()
  13. state: "active"
  14. unit_amount_in_cents: 999
  15. account:
  16. account_code: "user-123"
  17. describe "SubscriptionHandler", ->
  18. beforeEach ->
  19. @Settings =
  20. plans : [{
  21. planCode: "collaborator"
  22. name: "Collaborator"
  23. features:
  24. collaborators: -1
  25. versioning: true
  26. }]
  27. defaultPlanCode :
  28. collaborators: 0
  29. versioning: false
  30. @activeRecurlySubscription = mockRecurlySubscriptions["subscription-123-active"]
  31. @User = {}
  32. @user =
  33. _id: @user_id = "user_id_here_"
  34. @subscription =
  35. recurlySubscription_id: @activeRecurlySubscription.uuid
  36. @RecurlyWrapper =
  37. getSubscription: sinon.stub().callsArgWith(2, null, @activeRecurlySubscription)
  38. updateSubscription: sinon.stub().callsArgWith(2, null, @activeRecurlySubscription)
  39. cancelSubscription: sinon.stub().callsArgWith(1)
  40. reactivateSubscription: sinon.stub().callsArgWith(1)
  41. redeemCoupon:sinon.stub().callsArgWith(2)
  42. createSubscription: sinon.stub().callsArgWith(3, null, @activeRecurlySubscription)
  43. @DropboxHandler =
  44. unlinkAccount:sinon.stub().callsArgWith(1)
  45. @SubscriptionUpdater =
  46. syncSubscription: sinon.stub().callsArgWith(2)
  47. startFreeTrial: sinon.stub().callsArgWith(1)
  48. @LimitationsManager =
  49. userHasV2Subscription: sinon.stub()
  50. @EmailHandler =
  51. sendEmail:sinon.stub()
  52. @AnalyticsManager =
  53. recordEvent:sinon.stub()
  54. @SubscriptionHandler = SandboxedModule.require modulePath, requires:
  55. "./RecurlyWrapper": @RecurlyWrapper
  56. "settings-sharelatex": @Settings
  57. '../../models/User': User:@User
  58. './SubscriptionUpdater': @SubscriptionUpdater
  59. "logger-sharelatex":{log:->}
  60. './LimitationsManager':@LimitationsManager
  61. "../Email/EmailHandler":@EmailHandler
  62. "../Dropbox/DropboxHandler":@DropboxHandler
  63. "../../infrastructure/Events": @Events = {emit: sinon.stub()}
  64. "../Analytics/AnalyticsManager": @AnalyticsManager
  65. @SubscriptionHandler.syncSubscriptionToUser = sinon.stub().callsArgWith(2)
  66. describe "createSubscription", ->
  67. beforeEach ->
  68. @callback = sinon.stub()
  69. @subscriptionDetails =
  70. cvv:"123"
  71. number:"12345"
  72. @recurly_token_id = "45555666"
  73. @SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, true)
  74. describe "successfully", ->
  75. beforeEach ->
  76. @SubscriptionHandler.createSubscription(@user, @subscriptionDetails, @recurly_token_id, @callback)
  77. it "should create the subscription with the wrapper", ->
  78. @RecurlyWrapper.createSubscription.calledWith(@user, @subscriptionDetails, @recurly_token_id).should.equal true
  79. it "should sync the subscription to the user", ->
  80. @SubscriptionUpdater.syncSubscription.calledOnce.should.equal true
  81. @SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal @activeRecurlySubscription
  82. @SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal @user._id
  83. describe "when there is already a subscription in Recurly", ->
  84. beforeEach ->
  85. @SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, false)
  86. @SubscriptionHandler.createSubscription(@user, @subscriptionDetails, @recurly_token_id, @callback)
  87. it "should return an error", ->
  88. @callback.calledWith(new Error("user already has subscription in recurly"))
  89. describe "updateSubscription", ->
  90. describe "with a user with a subscription", ->
  91. describe "with a valid plan code", ->
  92. beforeEach (done) ->
  93. @plan_code = "collaborator"
  94. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, @subscription)
  95. @SubscriptionHandler.updateSubscription(@user, @plan_code, null, done)
  96. it "should update the subscription", ->
  97. @RecurlyWrapper.updateSubscription.calledWith(@subscription.recurlySubscription_id).should.equal true
  98. updateOptions = @RecurlyWrapper.updateSubscription.args[0][1]
  99. updateOptions.plan_code.should.equal @plan_code
  100. it "should update immediately", ->
  101. updateOptions = @RecurlyWrapper.updateSubscription.args[0][1]
  102. updateOptions.timeframe.should.equal "now"
  103. it "should sync the new subscription to the user", ->
  104. @SubscriptionUpdater.syncSubscription.calledOnce.should.equal true
  105. @SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal @activeRecurlySubscription
  106. @SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal @user._id
  107. describe "with a user without a subscription", ->
  108. beforeEach (done) ->
  109. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false)
  110. @SubscriptionHandler.updateSubscription(@user, @plan_code, null, done)
  111. it "should redirect to the subscription dashboard", ->
  112. @RecurlyWrapper.updateSubscription.called.should.equal false
  113. @SubscriptionHandler.syncSubscriptionToUser.called.should.equal false
  114. describe "with a coupon code", ->
  115. beforeEach (done) ->
  116. @plan_code = "collaborator"
  117. @coupon_code = "1231312"
  118. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, @subscription)
  119. @SubscriptionHandler.updateSubscription(@user, @plan_code, @coupon_code, done)
  120. it "should get the users account", ->
  121. @RecurlyWrapper.getSubscription.calledWith(@activeRecurlySubscription.uuid).should.equal true
  122. it "should redeme the coupon", (done)->
  123. @RecurlyWrapper.redeemCoupon.calledWith(@activeRecurlySubscription.account.account_code, @coupon_code).should.equal true
  124. done()
  125. it "should update the subscription", ->
  126. @RecurlyWrapper.updateSubscription.calledWith(@subscription.recurlySubscription_id).should.equal true
  127. updateOptions = @RecurlyWrapper.updateSubscription.args[0][1]
  128. updateOptions.plan_code.should.equal @plan_code
  129. describe "cancelSubscription", ->
  130. describe "with a user without a subscription", ->
  131. beforeEach (done) ->
  132. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false, @subscription)
  133. @SubscriptionHandler.cancelSubscription @user, done
  134. it "should redirect to the subscription dashboard", ->
  135. @RecurlyWrapper.cancelSubscription.called.should.equal false
  136. describe "with a user with a subscription", ->
  137. beforeEach (done) ->
  138. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, @subscription)
  139. @SubscriptionHandler.cancelSubscription @user, done
  140. it "should cancel the subscription", ->
  141. @RecurlyWrapper.cancelSubscription.called.should.equal true
  142. @RecurlyWrapper.cancelSubscription.calledWith(@subscription.recurlySubscription_id).should.equal true
  143. it "should trigger the cancel subscription event", ->
  144. @Events.emit.calledWith("cancelSubscription", @user._id).should.equal true
  145. describe "reactiveRecurlySubscription", ->
  146. describe "with a user without a subscription", ->
  147. beforeEach (done) ->
  148. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false, @subscription)
  149. @SubscriptionHandler.reactivateSubscription @user, done
  150. it "should redirect to the subscription dashboard", ->
  151. @RecurlyWrapper.reactivateSubscription.called.should.equal false
  152. describe "with a user with a subscription", ->
  153. beforeEach (done) ->
  154. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, @subscription)
  155. @SubscriptionHandler.reactivateSubscription @user, done
  156. it "should reactivate the subscription", ->
  157. @RecurlyWrapper.reactivateSubscription.called.should.equal true
  158. @RecurlyWrapper.reactivateSubscription.calledWith(@subscription.recurlySubscription_id).should.equal true
  159. describe "recurlyCallback", ->
  160. describe "with an actionable request", ->
  161. beforeEach (done) ->
  162. @user.id = @activeRecurlySubscription.account.account_code
  163. @User.findById = (userId, callback) =>
  164. userId.should.equal @user.id
  165. callback null, @user
  166. @SubscriptionHandler.recurlyCallback(@activeRecurlySubscription, done)
  167. it "should request the affected subscription from the API", ->
  168. @RecurlyWrapper.getSubscription.calledWith(@activeRecurlySubscription.uuid).should.equal true
  169. it "should request the account details of the subscription", ->
  170. options = @RecurlyWrapper.getSubscription.args[0][1]
  171. options.includeAccount.should.equal true
  172. it "should sync the subscription to the user", ->
  173. @SubscriptionUpdater.syncSubscription.calledOnce.should.equal true
  174. @SubscriptionUpdater.syncSubscription.args[0][0].should.deep.equal @activeRecurlySubscription
  175. @SubscriptionUpdater.syncSubscription.args[0][1].should.deep.equal @user._id
  176. describe "validateNoSubscriptionInRecurly", ->
  177. beforeEach ->
  178. @subscriptions = []
  179. @RecurlyWrapper.listAccountActiveSubscriptions = sinon.stub().yields(null, @subscriptions)
  180. @SubscriptionUpdater.syncSubscription = sinon.stub().yields()
  181. @callback = sinon.stub()
  182. describe "with no subscription in recurly", ->
  183. beforeEach ->
  184. @subscriptions.push @subscription = { "mock": "subscription" }
  185. @SubscriptionHandler.validateNoSubscriptionInRecurly @user_id, @callback
  186. it "should call RecurlyWrapper.listAccountActiveSubscriptions with the user id", ->
  187. @RecurlyWrapper.listAccountActiveSubscriptions
  188. .calledWith(@user_id)
  189. .should.equal true
  190. it "should sync the subscription", ->
  191. @SubscriptionUpdater.syncSubscription
  192. .calledWith(@subscription, @user_id)
  193. .should.equal true
  194. it "should call the callback with valid == false", ->
  195. @callback.calledWith(null, false).should.equal true
  196. describe "with a subscription in recurly", ->
  197. beforeEach ->
  198. @SubscriptionHandler.validateNoSubscriptionInRecurly @user_id, @callback
  199. it "should not sync the subscription", ->
  200. @SubscriptionUpdater.syncSubscription
  201. .called
  202. .should.equal false
  203. it "should call the callback with valid == true", ->
  204. @callback.calledWith(null, true).should.equal true