SubscriptionControllerTests.coffee 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require 'sinon'
  3. should = require("chai").should()
  4. MockRequest = require "../helpers/MockRequest"
  5. MockResponse = require "../helpers/MockResponse"
  6. modulePath = '../../../../app/js/Features/Subscription/SubscriptionController'
  7. mockSubscriptions =
  8. "subscription-123-active":
  9. uuid: "subscription-123-active"
  10. plan:
  11. name: "Gold"
  12. plan_code: "gold"
  13. current_period_ends_at: new Date()
  14. state: "active"
  15. unit_amount_in_cents: 999
  16. account:
  17. account_code: "user-123"
  18. describe "SubscriptionController sanboxed", ->
  19. beforeEach ->
  20. @user = {}
  21. @activeRecurlySubscription = mockSubscriptions["subscription-123-active"]
  22. @SecurityManager =
  23. getCurrentUser: sinon.stub().callsArgWith(1, null, @user)
  24. @SubscriptionHandler =
  25. createSubscription: sinon.stub().callsArgWith(2)
  26. updateSubscription: sinon.stub().callsArgWith(3)
  27. reactivateSubscription: sinon.stub().callsArgWith(1)
  28. cancelSubscription: sinon.stub().callsArgWith(1)
  29. recurlyCallback: sinon.stub().callsArgWith(1)
  30. startFreeTrial: sinon.stub()
  31. @PlansLocator =
  32. findLocalPlanInSettings: sinon.stub()
  33. @LimitationsManager =
  34. userHasSubscriptionOrIsGroupMember: sinon.stub()
  35. userHasSubscription : sinon.stub()
  36. @RecurlyWrapper =
  37. sign: sinon.stub().callsArgWith(1, null, "somthing")
  38. @SubscriptionViewModelBuilder =
  39. buildUsersSubscriptionViewModel:sinon.stub().callsArgWith(1, null, @activeRecurlySubscription)
  40. buildViewModel: sinon.stub()
  41. @settings =
  42. coupon_codes:
  43. upgradeToAnnualPromo:
  44. student:"STUDENTCODEHERE"
  45. collaborator:"COLLABORATORCODEHERE"
  46. apis:
  47. recurly:
  48. subdomain:"sl.recurly.com"
  49. siteUrl: "http://de.sharelatex.dev:3000"
  50. gaExperiments:{}
  51. @GeoIpLookup =
  52. getCurrencyCode:sinon.stub()
  53. @SubscriptionController = SandboxedModule.require modulePath, requires:
  54. '../../managers/SecurityManager': @SecurityManager
  55. './SubscriptionHandler': @SubscriptionHandler
  56. "./PlansLocator": @PlansLocator
  57. './SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder
  58. "./LimitationsManager": @LimitationsManager
  59. "../../infrastructure/GeoIpLookup":@GeoIpLookup
  60. './RecurlyWrapper': @RecurlyWrapper
  61. "logger-sharelatex": log:->
  62. "settings-sharelatex": @settings
  63. @res = new MockResponse()
  64. @req = new MockRequest()
  65. @req.body = {}
  66. @req.query =
  67. planCode:"123123"
  68. @stubbedCurrencyCode = "GBP"
  69. describe "plansPage", ->
  70. beforeEach (done) ->
  71. @req.headers =
  72. "x-forwarded-for" : "1234.3123.3131.333 313.133.445.666 653.5345.5345.534"
  73. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  74. @res.callback = done
  75. @SubscriptionController.plansPage(@req, @res)
  76. it "should set the recommended currency from the geoiplookup", (done)->
  77. @res.renderedVariables.recomendedCurrency.should.equal(@stubbedCurrencyCode)
  78. @GeoIpLookup.getCurrencyCode.calledWith(@req.headers["x-forwarded-for"]).should.equal true
  79. done()
  80. describe "editBillingDetailsPage", ->
  81. describe "with a user with a subscription", ->
  82. beforeEach (done) ->
  83. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  84. @user.id = @activeRecurlySubscription.account.account_code
  85. @res.callback = done
  86. @SubscriptionController.editBillingDetailsPage(@req, @res)
  87. it "should render the edit billing details page", ->
  88. @res.rendered.should.equal true
  89. @res.renderedTemplate.should.equal "subscriptions/edit-billing-details"
  90. it "should set the correct variables for the template", ->
  91. should.exist @res.renderedVariables.signature
  92. @res.renderedVariables.successURL.should.equal "#{@settings.siteUrl}/user/subscription/update"
  93. @res.renderedVariables.user.id.should.equal @user.id
  94. describe "with a user without subscription", ->
  95. beforeEach (done) ->
  96. @res.callback = done
  97. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  98. @SubscriptionController.reactivateSubscription @req, @res
  99. it "should redirect to the subscription dashboard", ->
  100. @res.redirected.should.equal true
  101. @res.redirectedTo.should.equal "/user/subscription"
  102. describe "paymentPage", ->
  103. describe "with a user without a subscription", ->
  104. beforeEach ->
  105. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  106. @PlansLocator.findLocalPlanInSettings.returns({})
  107. describe "with a valid plan code", ->
  108. it "should render the new subscription page", (done)->
  109. @res.render = (page, opts)=>
  110. page.should.equal "subscriptions/new"
  111. done()
  112. @SubscriptionController.paymentPage @req, @res
  113. it "should set the successURL", (done)->
  114. @req.session._csrf = @csrfToken = "mock-csrf-token"
  115. @res.render = (page, opts)=>
  116. url = JSON.parse(opts.subscriptionFormOptions).successURL
  117. url.should.equal("#{@settings.siteUrl}/user/subscription/create?_csrf=#{@csrfToken}")
  118. done()
  119. @SubscriptionController.paymentPage @req, @res
  120. describe "with a user with subscription", ->
  121. it "should redirect to the subscription dashboard", (done)->
  122. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  123. @res.redirect = (url)=>
  124. url.should.equal "/user/subscription"
  125. done()
  126. @SubscriptionController.paymentPage(@req, @res)
  127. describe "with an invalid plan code", ->
  128. it "should redirect to the subscription dashboard", (done)->
  129. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  130. @PlansLocator.findLocalPlanInSettings.returns(null)
  131. @res.redirect = (url)=>
  132. url.should.equal "/user/subscription"
  133. done()
  134. @SubscriptionController.paymentPage(@req, @res)
  135. describe "successful_subscription", ->
  136. beforeEach (done) ->
  137. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, {})
  138. @res.callback = done
  139. @SubscriptionController.successful_subscription @req, @res
  140. describe "userSubscriptionPage", ->
  141. describe "with a user without a subscription", ->
  142. beforeEach (done) ->
  143. @res.callback = done
  144. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
  145. @SubscriptionController.userSubscriptionPage @req, @res
  146. it "should redirect to the plans page", ->
  147. @res.redirected.should.equal true
  148. @res.redirectedTo.should.equal "/user/subscription/plans"
  149. describe "with a user with a paid subscription", ->
  150. beforeEach (done) ->
  151. @res.callback = done
  152. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  153. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  154. @SubscriptionController.userSubscriptionPage @req, @res
  155. it "should render the dashboard", (done)->
  156. @res.rendered.should.equal true
  157. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  158. done()
  159. it "should set the correct subscription details", ->
  160. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  161. describe "with a user with a free trial", ->
  162. beforeEach (done) ->
  163. @res.callback = done
  164. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  165. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  166. @SubscriptionController.userSubscriptionPage @req, @res
  167. it "should render the dashboard", ->
  168. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  169. it "should set the correct subscription details", ->
  170. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  171. describe "when its a custom subscription which is non recurly", ->
  172. beforeEach ()->
  173. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true, {customAccount:true})
  174. @SubscriptionController.userSubscriptionPage @req, @res
  175. it "should redirect to /user/subscription/custom_account", ->
  176. @res.redirectedTo.should.equal("/user/subscription/custom_account")
  177. describe "userCustomSubscriptionPage", ->
  178. beforeEach (done) ->
  179. @res.callback = done
  180. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  181. @SubscriptionController.userCustomSubscriptionPage @req, @res
  182. it "should render the page", (done)->
  183. @res.rendered.should.equal true
  184. @res.renderedTemplate.should.equal "subscriptions/custom_account"
  185. done()
  186. describe "createSubscription", ->
  187. beforeEach (done)->
  188. @res =
  189. redirect:->
  190. done()
  191. sinon.spy @res, "redirect"
  192. @req.body.recurly_token = "1234"
  193. @SubscriptionController.createSubscription @req, @res
  194. it "should send the user and subscriptionId to the handler", (done)->
  195. @SubscriptionHandler.createSubscription.calledWith(@user, @req.body.recurly_token).should.equal true
  196. done()
  197. it "should redurect to the subscription page", (done)->
  198. @res.redirect.calledWith("/user/subscription/thank-you").should.equal true
  199. done()
  200. describe "updateSubscription via post", ->
  201. beforeEach (done)->
  202. @res =
  203. redirect:->
  204. done()
  205. sinon.spy @res, "redirect"
  206. @plan_code = "1234"
  207. @req.body.plan_code = @plan_code
  208. @SubscriptionController.updateSubscription @req, @res
  209. it "should send the user and subscriptionId to the handler", (done)->
  210. @SubscriptionHandler.updateSubscription.calledWith(@user, @plan_code).should.equal true
  211. done()
  212. it "should redurect to the subscription page", (done)->
  213. @res.redirect.calledWith("/user/subscription").should.equal true
  214. done()
  215. describe "reactivateSubscription", ->
  216. beforeEach (done)->
  217. @res =
  218. redirect:->
  219. done()
  220. sinon.spy @res, "redirect"
  221. @SubscriptionController.reactivateSubscription @req, @res
  222. it "should tell the handler to reactivate this user", (done)->
  223. @SubscriptionHandler.reactivateSubscription.calledWith(@user).should.equal true
  224. done()
  225. it "should redurect to the subscription page", (done)->
  226. @res.redirect.calledWith("/user/subscription").should.equal true
  227. done()
  228. describe "cancelSubscription", ->
  229. beforeEach (done)->
  230. @res =
  231. redirect:->
  232. done()
  233. sinon.spy @res, "redirect"
  234. @SubscriptionController.cancelSubscription @req, @res
  235. it "should tell the handler to cancel this user", (done)->
  236. @SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
  237. done()
  238. it "should redurect to the subscription page", (done)->
  239. @res.redirect.calledWith("/user/subscription").should.equal true
  240. done()
  241. describe "recurly callback", ->
  242. describe "with a actionable request", ->
  243. beforeEach (done)->
  244. @req =
  245. body:
  246. expired_subscription_notification:
  247. subscription:
  248. uuid: @activeRecurlySubscription.uuid
  249. @res = send:->
  250. done()
  251. sinon.spy @res, "send"
  252. @SubscriptionController.recurlyCallback @req, @res
  253. it "should tell the SubscriptionHandler to process the recurly callback", (done)->
  254. @SubscriptionHandler.recurlyCallback.called.should.equal true
  255. done()
  256. it "should send a 200", (done)->
  257. @res.send.calledWith(200)
  258. done()
  259. describe "with a non-actionable request", ->
  260. beforeEach (done) ->
  261. @user.id = @activeRecurlySubscription.account.account_code
  262. @req =
  263. body:
  264. new_subscription_notification:
  265. subscription:
  266. uuid: @activeRecurlySubscription.uuid
  267. @res = send:->
  268. done()
  269. sinon.spy @res, "send"
  270. @SubscriptionController.recurlyCallback @req, @res
  271. it "should not call the subscriptionshandler", ->
  272. @SubscriptionHandler.recurlyCallback.called.should.equal false
  273. it "should respond with a 200 status", ->
  274. @res.send.calledWith(200)
  275. describe "renderUpgradeToAnnualPlanPage", ->
  276. it "should redirect to the plans page if the user does not have a subscription", (done)->
  277. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  278. @res.redirect = (url)->
  279. url.should.equal "/user/subscription/plans"
  280. done()
  281. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  282. it "should pass the plan code to the view - student", (done)->
  283. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"Student free trial 14 days"})
  284. @res.render = (view, opts)->
  285. view.should.equal "subscriptions/upgradeToAnnual"
  286. opts.planName.should.equal "student"
  287. done()
  288. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  289. it "should pass the plan code to the view - collaborator", (done)->
  290. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"free trial for Collaborator free trial 14 days"})
  291. @res.render = (view, opts)->
  292. opts.planName.should.equal "collaborator"
  293. done()
  294. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  295. it "should pass annual as the plan name if the user is already on an annual plan", (done)->
  296. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"student annual with free trial"})
  297. @res.render = (view, opts)->
  298. opts.planName.should.equal "annual"
  299. done()
  300. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  301. describe "processUpgradeToAnnualPlan", ->
  302. beforeEach ->
  303. it "should tell the subscription handler to update the subscription with the annual plan and apply a coupon code", (done)->
  304. @req.body =
  305. planName:"student"
  306. @res.send = ()=>
  307. @SubscriptionHandler.updateSubscription.calledWith(@user, "student-annual", "STUDENTCODEHERE").should.equal true
  308. done()
  309. @SubscriptionController.processUpgradeToAnnualPlan @req, @res
  310. it "should get the collaborator coupon code", (done)->
  311. @req.body =
  312. planName:"collaborator"
  313. @res.send = (url)=>
  314. @SubscriptionHandler.updateSubscription.calledWith(@user, "collaborator-annual", "COLLABORATORCODEHERE").should.equal true
  315. done()
  316. @SubscriptionController.processUpgradeToAnnualPlan @req, @res