SubscriptionControllerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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.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. redirect:->
  215. done()
  216. sinon.spy @res, "redirect"
  217. @req.body.recurly_token = "1234"
  218. @SubscriptionController.createSubscription @req, @res
  219. it "should send the user and subscriptionId to the handler", (done)->
  220. @SubscriptionHandler.createSubscription.calledWith(@user, @req.body.recurly_token).should.equal true
  221. done()
  222. it "should redurect to the subscription page", (done)->
  223. @res.redirect.calledWith("/user/subscription/thank-you").should.equal true
  224. done()
  225. describe "updateSubscription via post", ->
  226. beforeEach (done)->
  227. @res =
  228. redirect:->
  229. done()
  230. sinon.spy @res, "redirect"
  231. @plan_code = "1234"
  232. @req.body.plan_code = @plan_code
  233. @SubscriptionController.updateSubscription @req, @res
  234. it "should send the user and subscriptionId to the handler", (done)->
  235. @SubscriptionHandler.updateSubscription.calledWith(@user, @plan_code).should.equal true
  236. done()
  237. it "should redurect to the subscription page", (done)->
  238. @res.redirect.calledWith("/user/subscription").should.equal true
  239. done()
  240. describe "reactivateSubscription", ->
  241. beforeEach (done)->
  242. @res =
  243. redirect:->
  244. done()
  245. sinon.spy @res, "redirect"
  246. @SubscriptionController.reactivateSubscription @req, @res
  247. it "should tell the handler to reactivate this user", (done)->
  248. @SubscriptionHandler.reactivateSubscription.calledWith(@user).should.equal true
  249. done()
  250. it "should redurect to the subscription page", (done)->
  251. @res.redirect.calledWith("/user/subscription").should.equal true
  252. done()
  253. describe "cancelSubscription", ->
  254. beforeEach (done)->
  255. @res =
  256. redirect:->
  257. done()
  258. sinon.spy @res, "redirect"
  259. @SubscriptionController.cancelSubscription @req, @res
  260. it "should tell the handler to cancel this user", (done)->
  261. @SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
  262. done()
  263. it "should redurect to the subscription page", (done)->
  264. @res.redirect.calledWith("/user/subscription").should.equal true
  265. done()
  266. describe "recurly callback", ->
  267. describe "with a actionable request", ->
  268. beforeEach (done)->
  269. @req =
  270. body:
  271. expired_subscription_notification:
  272. subscription:
  273. uuid: @activeRecurlySubscription.uuid
  274. @res = send:->
  275. done()
  276. sinon.spy @res, "send"
  277. @SubscriptionController.recurlyCallback @req, @res
  278. it "should tell the SubscriptionHandler to process the recurly callback", (done)->
  279. @SubscriptionHandler.recurlyCallback.called.should.equal true
  280. done()
  281. it "should send a 200", (done)->
  282. @res.send.calledWith(200)
  283. done()
  284. describe "with a non-actionable request", ->
  285. beforeEach (done) ->
  286. @user.id = @activeRecurlySubscription.account.account_code
  287. @req =
  288. body:
  289. new_subscription_notification:
  290. subscription:
  291. uuid: @activeRecurlySubscription.uuid
  292. @res = send:->
  293. done()
  294. sinon.spy @res, "send"
  295. @SubscriptionController.recurlyCallback @req, @res
  296. it "should not call the subscriptionshandler", ->
  297. @SubscriptionHandler.recurlyCallback.called.should.equal false
  298. it "should respond with a 200 status", ->
  299. @res.send.calledWith(200)
  300. describe "renderUpgradeToAnnualPlanPage", ->
  301. it "should redirect to the plans page if the user does not have a subscription", (done)->
  302. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  303. @res.redirect = (url)->
  304. url.should.equal "/user/subscription/plans"
  305. done()
  306. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  307. it "should pass the plan code to the view - student", (done)->
  308. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"Student free trial 14 days"})
  309. @res.render = (view, opts)->
  310. view.should.equal "subscriptions/upgradeToAnnual"
  311. opts.planName.should.equal "student"
  312. done()
  313. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  314. it "should pass the plan code to the view - collaborator", (done)->
  315. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"free trial for Collaborator free trial 14 days"})
  316. @res.render = (view, opts)->
  317. opts.planName.should.equal "collaborator"
  318. done()
  319. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  320. it "should pass annual as the plan name if the user is already on an annual plan", (done)->
  321. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"student annual with free trial"})
  322. @res.render = (view, opts)->
  323. opts.planName.should.equal "annual"
  324. done()
  325. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  326. describe "processUpgradeToAnnualPlan", ->
  327. beforeEach ->
  328. it "should tell the subscription handler to update the subscription with the annual plan and apply a coupon code", (done)->
  329. @req.body =
  330. planName:"student"
  331. @res.send = ()=>
  332. @SubscriptionHandler.updateSubscription.calledWith(@user, "student-annual", "STUDENTCODEHERE").should.equal true
  333. done()
  334. @SubscriptionController.processUpgradeToAnnualPlan @req, @res
  335. it "should get the collaborator coupon code", (done)->
  336. @req.body =
  337. planName:"collaborator"
  338. @res.send = (url)=>
  339. @SubscriptionHandler.updateSubscription.calledWith(@user, "collaborator-annual", "COLLABORATORCODEHERE").should.equal true
  340. done()
  341. @SubscriptionController.processUpgradeToAnnualPlan @req, @res