SubscriptionControllerTests.coffee 15 KB

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