SubscriptionControllerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 = {email:"tom@yahoo.com"}
  21. @activeRecurlySubscription = mockSubscriptions["subscription-123-active"]
  22. @SecurityManager =
  23. getCurrentUser: sinon.stub().callsArgWith(1, null, @user)
  24. @SubscriptionHandler =
  25. createSubscription: sinon.stub().callsArgWith(3)
  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.ip = "1234.3123.3131.333 313.133.445.666 653.5345.5345.534"
  72. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  73. @res.callback = done
  74. @SubscriptionController.plansPage(@req, @res)
  75. it "should set the recommended currency from the geoiplookup", (done)->
  76. @res.renderedVariables.recomendedCurrency.should.equal(@stubbedCurrencyCode)
  77. @GeoIpLookup.getCurrencyCode.calledWith(@req.ip).should.equal true
  78. done()
  79. describe "editBillingDetailsPage", ->
  80. describe "with a user with a subscription", ->
  81. beforeEach (done) ->
  82. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  83. @user.id = @activeRecurlySubscription.account.account_code
  84. @res.callback = done
  85. @SubscriptionController.editBillingDetailsPage(@req, @res)
  86. it "should render the edit billing details page", ->
  87. @res.rendered.should.equal true
  88. @res.renderedTemplate.should.equal "subscriptions/edit-billing-details"
  89. it "should set the correct variables for the template", ->
  90. should.exist @res.renderedVariables.signature
  91. @res.renderedVariables.successURL.should.equal "#{@settings.siteUrl}/user/subscription/update"
  92. @res.renderedVariables.user.id.should.equal @user.id
  93. describe "with a user without subscription", ->
  94. beforeEach (done) ->
  95. @res.callback = done
  96. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  97. @SubscriptionController.reactivateSubscription @req, @res
  98. it "should redirect to the subscription dashboard", ->
  99. @res.redirected.should.equal true
  100. @res.redirectedTo.should.equal "/user/subscription"
  101. describe "paymentPage", ->
  102. beforeEach ->
  103. @req.headers = {}
  104. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  105. describe "with a user without a subscription", ->
  106. beforeEach ->
  107. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  108. @PlansLocator.findLocalPlanInSettings.returns({})
  109. describe "with a valid plan code", ->
  110. it "should render the new subscription page", (done)->
  111. @res.render = (page, opts)=>
  112. page.should.equal "subscriptions/new"
  113. done()
  114. @SubscriptionController.paymentPage @req, @res
  115. it "should set the successURL", (done)->
  116. @req.session._csrf = @csrfToken = "mock-csrf-token"
  117. @res.render = (page, opts)=>
  118. url = JSON.parse(opts.subscriptionFormOptions).successURL
  119. url.should.equal("#{@settings.siteUrl}/user/subscription/create?_csrf=#{@csrfToken}")
  120. done()
  121. @SubscriptionController.paymentPage @req, @res
  122. describe "with a user with subscription", ->
  123. it "should redirect to the subscription dashboard", (done)->
  124. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  125. @res.redirect = (url)=>
  126. url.should.equal "/user/subscription"
  127. done()
  128. @SubscriptionController.paymentPage(@req, @res)
  129. describe "with an invalid plan code", ->
  130. it "should redirect to the subscription dashboard", (done)->
  131. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  132. @PlansLocator.findLocalPlanInSettings.returns(null)
  133. @res.redirect = (url)=>
  134. url.should.equal "/user/subscription"
  135. done()
  136. @SubscriptionController.paymentPage(@req, @res)
  137. describe "which currency to use", ->
  138. beforeEach ->
  139. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  140. @PlansLocator.findLocalPlanInSettings.returns({})
  141. it "should use the set currency from the query string", (done)->
  142. @req.query.currency = "EUR"
  143. @res.render = (page, opts)=>
  144. opts.currency.should.equal "EUR"
  145. opts.currency.should.not.equal @stubbedCurrencyCode
  146. done()
  147. @SubscriptionController.paymentPage @req, @res
  148. it "should upercase the currency code", (done)->
  149. @req.query.currency = "eur"
  150. @res.render = (page, opts)=>
  151. opts.currency.should.equal "EUR"
  152. done()
  153. @SubscriptionController.paymentPage @req, @res
  154. it "should use the geo ip currency if non is provided", (done)->
  155. @req.query.currency = null
  156. @res.render = (page, opts)=>
  157. opts.currency.should.equal @stubbedCurrencyCode
  158. done()
  159. @SubscriptionController.paymentPage @req, @res
  160. describe "successful_subscription", ->
  161. beforeEach (done) ->
  162. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, {})
  163. @res.callback = done
  164. @SubscriptionController.successful_subscription @req, @res
  165. describe "userSubscriptionPage", ->
  166. describe "with a user without a subscription", ->
  167. beforeEach (done) ->
  168. @res.callback = done
  169. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
  170. @SubscriptionController.userSubscriptionPage @req, @res
  171. it "should redirect to the plans page", ->
  172. @res.redirected.should.equal true
  173. @res.redirectedTo.should.equal "/user/subscription/plans"
  174. describe "with a user with a paid subscription", ->
  175. beforeEach (done) ->
  176. @res.callback = done
  177. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  178. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  179. @SubscriptionController.userSubscriptionPage @req, @res
  180. it "should render the dashboard", (done)->
  181. @res.rendered.should.equal true
  182. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  183. done()
  184. it "should set the correct subscription details", ->
  185. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  186. describe "with a user with a free trial", ->
  187. beforeEach (done) ->
  188. @res.callback = done
  189. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  190. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  191. @SubscriptionController.userSubscriptionPage @req, @res
  192. it "should render the dashboard", ->
  193. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  194. it "should set the correct subscription details", ->
  195. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  196. describe "when its a custom subscription which is non recurly", ->
  197. beforeEach ()->
  198. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true, {customAccount:true})
  199. @SubscriptionController.userSubscriptionPage @req, @res
  200. it "should redirect to /user/subscription/custom_account", ->
  201. @res.redirectedTo.should.equal("/user/subscription/custom_account")
  202. describe "userCustomSubscriptionPage", ->
  203. beforeEach (done) ->
  204. @res.callback = done
  205. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  206. @SubscriptionController.userCustomSubscriptionPage @req, @res
  207. it "should render the page", (done)->
  208. @res.rendered.should.equal true
  209. @res.renderedTemplate.should.equal "subscriptions/custom_account"
  210. done()
  211. describe "createSubscription", ->
  212. beforeEach (done)->
  213. @res =
  214. send:->
  215. done()
  216. sinon.spy @res, "send"
  217. @subscriptionDetails =
  218. card:"1234"
  219. cvv:"123"
  220. @req.body.recurly_token_id = "1234"
  221. @req.body.subscriptionDetails = @subscriptionDetails
  222. @SubscriptionController.createSubscription @req, @res
  223. it "should send the user and subscriptionId to the handler", (done)->
  224. @SubscriptionHandler.createSubscription.calledWith(@user, @subscriptionDetails, @req.body.recurly_token_id).should.equal true
  225. done()
  226. it "should redurect to the subscription page", (done)->
  227. @res.send.calledWith(201).should.equal true
  228. done()
  229. describe "updateSubscription via post", ->
  230. beforeEach (done)->
  231. @res =
  232. redirect:->
  233. done()
  234. sinon.spy @res, "redirect"
  235. @plan_code = "1234"
  236. @req.body.plan_code = @plan_code
  237. @SubscriptionController.updateSubscription @req, @res
  238. it "should send the user and subscriptionId to the handler", (done)->
  239. @SubscriptionHandler.updateSubscription.calledWith(@user, @plan_code).should.equal true
  240. done()
  241. it "should redurect to the subscription page", (done)->
  242. @res.redirect.calledWith("/user/subscription").should.equal true
  243. done()
  244. describe "reactivateSubscription", ->
  245. beforeEach (done)->
  246. @res =
  247. redirect:->
  248. done()
  249. sinon.spy @res, "redirect"
  250. @SubscriptionController.reactivateSubscription @req, @res
  251. it "should tell the handler to reactivate this user", (done)->
  252. @SubscriptionHandler.reactivateSubscription.calledWith(@user).should.equal true
  253. done()
  254. it "should redurect to the subscription page", (done)->
  255. @res.redirect.calledWith("/user/subscription").should.equal true
  256. done()
  257. describe "cancelSubscription", ->
  258. beforeEach (done)->
  259. @res =
  260. redirect:->
  261. done()
  262. sinon.spy @res, "redirect"
  263. @SubscriptionController.cancelSubscription @req, @res
  264. it "should tell the handler to cancel this user", (done)->
  265. @SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
  266. done()
  267. it "should redurect to the subscription page", (done)->
  268. @res.redirect.calledWith("/user/subscription").should.equal true
  269. done()
  270. describe "recurly callback", ->
  271. describe "with a actionable request", ->
  272. beforeEach (done)->
  273. @req =
  274. body:
  275. expired_subscription_notification:
  276. subscription:
  277. uuid: @activeRecurlySubscription.uuid
  278. @res = send:->
  279. done()
  280. sinon.spy @res, "send"
  281. @SubscriptionController.recurlyCallback @req, @res
  282. it "should tell the SubscriptionHandler to process the recurly callback", (done)->
  283. @SubscriptionHandler.recurlyCallback.called.should.equal true
  284. done()
  285. it "should send a 200", (done)->
  286. @res.send.calledWith(200)
  287. done()
  288. describe "with a non-actionable request", ->
  289. beforeEach (done) ->
  290. @user.id = @activeRecurlySubscription.account.account_code
  291. @req =
  292. body:
  293. new_subscription_notification:
  294. subscription:
  295. uuid: @activeRecurlySubscription.uuid
  296. @res = send:->
  297. done()
  298. sinon.spy @res, "send"
  299. @SubscriptionController.recurlyCallback @req, @res
  300. it "should not call the subscriptionshandler", ->
  301. @SubscriptionHandler.recurlyCallback.called.should.equal false
  302. it "should respond with a 200 status", ->
  303. @res.send.calledWith(200)
  304. describe "renderUpgradeToAnnualPlanPage", ->
  305. it "should redirect to the plans page if the user does not have a subscription", (done)->
  306. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  307. @res.redirect = (url)->
  308. url.should.equal "/user/subscription/plans"
  309. done()
  310. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  311. it "should pass the plan code to the view - student", (done)->
  312. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"Student free trial 14 days"})
  313. @res.render = (view, opts)->
  314. view.should.equal "subscriptions/upgradeToAnnual"
  315. opts.planName.should.equal "student"
  316. done()
  317. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  318. it "should pass the plan code to the view - collaborator", (done)->
  319. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"free trial for Collaborator free trial 14 days"})
  320. @res.render = (view, opts)->
  321. opts.planName.should.equal "collaborator"
  322. done()
  323. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  324. it "should pass annual as the plan name if the user is already on an annual plan", (done)->
  325. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"student annual with free trial"})
  326. @res.render = (view, opts)->
  327. opts.planName.should.equal "annual"
  328. done()
  329. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  330. describe "processUpgradeToAnnualPlan", ->
  331. beforeEach ->
  332. it "should tell the subscription handler to update the subscription with the annual plan and apply a coupon code", (done)->
  333. @req.body =
  334. planName:"student"
  335. @res.send = ()=>
  336. @SubscriptionHandler.updateSubscription.calledWith(@user, "student-annual", "STUDENTCODEHERE").should.equal true
  337. done()
  338. @SubscriptionController.processUpgradeToAnnualPlan @req, @res
  339. it "should get the collaborator coupon code", (done)->
  340. @req.body =
  341. planName:"collaborator"
  342. @res.send = (url)=>
  343. @SubscriptionHandler.updateSubscription.calledWith(@user, "collaborator-annual", "COLLABORATORCODEHERE").should.equal true
  344. done()
  345. @SubscriptionController.processUpgradeToAnnualPlan @req, @res