SubscriptionControllerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. @AuthenticationController =
  23. getLoggedInUser: 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. @SubscriptionDomainHandler =
  54. getDomainLicencePage:sinon.stub()
  55. @SubscriptionController = SandboxedModule.require modulePath, requires:
  56. '../Authentication/AuthenticationController': @AuthenticationController
  57. './SubscriptionHandler': @SubscriptionHandler
  58. "./PlansLocator": @PlansLocator
  59. './SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder
  60. "./LimitationsManager": @LimitationsManager
  61. "../../infrastructure/GeoIpLookup":@GeoIpLookup
  62. './RecurlyWrapper': @RecurlyWrapper
  63. "logger-sharelatex":
  64. log:->
  65. warn:->
  66. "settings-sharelatex": @settings
  67. "./SubscriptionDomainHandler":@SubscriptionDomainHandler
  68. @res = new MockResponse()
  69. @req = new MockRequest()
  70. @req.body = {}
  71. @req.query =
  72. planCode:"123123"
  73. @stubbedCurrencyCode = "GBP"
  74. describe "plansPage", ->
  75. beforeEach (done) ->
  76. @req.ip = "1234.3123.3131.333 313.133.445.666 653.5345.5345.534"
  77. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  78. @res.callback = done
  79. @SubscriptionController.plansPage(@req, @res)
  80. it "should set the recommended currency from the geoiplookup", (done)->
  81. @res.renderedVariables.recomendedCurrency.should.equal(@stubbedCurrencyCode)
  82. @GeoIpLookup.getCurrencyCode.calledWith(@req.ip).should.equal true
  83. done()
  84. describe "editBillingDetailsPage", ->
  85. describe "with a user with a subscription", ->
  86. beforeEach (done) ->
  87. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  88. @user._id = @activeRecurlySubscription.account.account_code
  89. @res.callback = done
  90. @SubscriptionController.editBillingDetailsPage(@req, @res)
  91. it "should render the edit billing details page", ->
  92. @res.rendered.should.equal true
  93. @res.renderedTemplate.should.equal "subscriptions/edit-billing-details"
  94. it "should set the correct variables for the template", ->
  95. should.exist @res.renderedVariables.signature
  96. @res.renderedVariables.successURL.should.equal "#{@settings.siteUrl}/user/subscription/update"
  97. @res.renderedVariables.user.id.should.equal @user._id
  98. describe "with a user without subscription", ->
  99. beforeEach (done) ->
  100. @res.callback = done
  101. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  102. @SubscriptionController.reactivateSubscription @req, @res
  103. it "should redirect to the subscription dashboard", ->
  104. @res.redirected.should.equal true
  105. @res.redirectedTo.should.equal "/user/subscription"
  106. describe "paymentPage", ->
  107. beforeEach ->
  108. @req.headers = {}
  109. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  110. describe "with a user without a subscription", ->
  111. beforeEach ->
  112. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  113. @PlansLocator.findLocalPlanInSettings.returns({})
  114. describe "with a valid plan code", ->
  115. it "should render the new subscription page", (done)->
  116. @res.render = (page, opts)=>
  117. page.should.equal "subscriptions/new"
  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 "which currency to use", ->
  136. beforeEach ->
  137. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  138. @PlansLocator.findLocalPlanInSettings.returns({})
  139. it "should use the set currency from the query string", (done)->
  140. @req.query.currency = "EUR"
  141. @res.render = (page, opts)=>
  142. opts.currency.should.equal "EUR"
  143. opts.currency.should.not.equal @stubbedCurrencyCode
  144. done()
  145. @SubscriptionController.paymentPage @req, @res
  146. it "should upercase the currency code", (done)->
  147. @req.query.currency = "eur"
  148. @res.render = (page, opts)=>
  149. opts.currency.should.equal "EUR"
  150. done()
  151. @SubscriptionController.paymentPage @req, @res
  152. it "should use the geo ip currency if non is provided", (done)->
  153. @req.query.currency = null
  154. @res.render = (page, opts)=>
  155. opts.currency.should.equal @stubbedCurrencyCode
  156. done()
  157. @SubscriptionController.paymentPage @req, @res
  158. describe "successful_subscription", ->
  159. beforeEach (done) ->
  160. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, {})
  161. @res.callback = done
  162. @SubscriptionController.successful_subscription @req, @res
  163. describe "userSubscriptionPage", ->
  164. describe "with a user without a subscription", ->
  165. beforeEach (done) ->
  166. @res.callback = done
  167. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
  168. @SubscriptionController.userSubscriptionPage @req, @res
  169. it "should redirect to the plans page", ->
  170. @res.redirected.should.equal true
  171. @res.redirectedTo.should.equal "/user/subscription/plans"
  172. describe "with a potential domain licence", ->
  173. beforeEach () ->
  174. @groupUrl = "/go/over-here"
  175. @SubscriptionDomainHandler.getDomainLicencePage.returns(@groupUrl)
  176. describe "without an existing subscription", ->
  177. beforeEach (done)->
  178. @res.callback = done
  179. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
  180. @SubscriptionController.userSubscriptionPage @req, @res
  181. it "should redirect to the group invite url", ->
  182. @res.redirected.should.equal true
  183. @res.redirectedTo.should.equal @groupUrl
  184. describe "with an existing subscription", ->
  185. beforeEach (done)->
  186. @res.callback = done
  187. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  188. @SubscriptionController.userSubscriptionPage @req, @res
  189. it "should render the dashboard", ->
  190. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  191. describe "with a user with a paid subscription", ->
  192. beforeEach (done) ->
  193. @res.callback = done
  194. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  195. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  196. @SubscriptionController.userSubscriptionPage @req, @res
  197. it "should render the dashboard", (done)->
  198. @res.rendered.should.equal true
  199. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  200. done()
  201. it "should set the correct subscription details", ->
  202. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  203. describe "with a user with a free trial", ->
  204. beforeEach (done) ->
  205. @res.callback = done
  206. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  207. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  208. @SubscriptionController.userSubscriptionPage @req, @res
  209. it "should render the dashboard", ->
  210. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  211. it "should set the correct subscription details", ->
  212. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  213. describe "when its a custom subscription which is non recurly", ->
  214. beforeEach ()->
  215. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true, {customAccount:true})
  216. @SubscriptionController.userSubscriptionPage @req, @res
  217. it "should redirect to /user/subscription/custom_account", ->
  218. @res.redirectedTo.should.equal("/user/subscription/custom_account")
  219. describe "userCustomSubscriptionPage", ->
  220. beforeEach (done) ->
  221. @res.callback = done
  222. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true, {})
  223. @SubscriptionController.userCustomSubscriptionPage @req, @res
  224. it "should render the page", (done)->
  225. @res.rendered.should.equal true
  226. @res.renderedTemplate.should.equal "subscriptions/custom_account"
  227. done()
  228. describe "createSubscription", ->
  229. beforeEach (done)->
  230. @res =
  231. sendStatus:->
  232. done()
  233. sinon.spy @res, "sendStatus"
  234. @subscriptionDetails =
  235. card:"1234"
  236. cvv:"123"
  237. @req.body.recurly_token_id = "1234"
  238. @req.body.subscriptionDetails = @subscriptionDetails
  239. @SubscriptionController.createSubscription @req, @res
  240. it "should send the user and subscriptionId to the handler", (done)->
  241. @SubscriptionHandler.createSubscription.calledWith(@user, @subscriptionDetails, @req.body.recurly_token_id).should.equal true
  242. done()
  243. it "should redurect to the subscription page", (done)->
  244. @res.sendStatus.calledWith(201).should.equal true
  245. done()
  246. describe "updateSubscription via post", ->
  247. beforeEach (done)->
  248. @res =
  249. redirect:->
  250. done()
  251. sinon.spy @res, "redirect"
  252. @plan_code = "1234"
  253. @req.body.plan_code = @plan_code
  254. @SubscriptionController.updateSubscription @req, @res
  255. it "should send the user and subscriptionId to the handler", (done)->
  256. @SubscriptionHandler.updateSubscription.calledWith(@user, @plan_code).should.equal true
  257. done()
  258. it "should redurect to the subscription page", (done)->
  259. @res.redirect.calledWith("/user/subscription").should.equal true
  260. done()
  261. describe "reactivateSubscription", ->
  262. beforeEach (done)->
  263. @res =
  264. redirect:->
  265. done()
  266. sinon.spy @res, "redirect"
  267. @SubscriptionController.reactivateSubscription @req, @res
  268. it "should tell the handler to reactivate this user", (done)->
  269. @SubscriptionHandler.reactivateSubscription.calledWith(@user).should.equal true
  270. done()
  271. it "should redurect to the subscription page", (done)->
  272. @res.redirect.calledWith("/user/subscription").should.equal true
  273. done()
  274. describe "cancelSubscription", ->
  275. beforeEach (done)->
  276. @res =
  277. redirect:->
  278. done()
  279. sinon.spy @res, "redirect"
  280. @SubscriptionController.cancelSubscription @req, @res
  281. it "should tell the handler to cancel this user", (done)->
  282. @SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
  283. done()
  284. it "should redurect to the subscription page", (done)->
  285. @res.redirect.calledWith("/user/subscription").should.equal true
  286. done()
  287. describe "recurly callback", ->
  288. describe "with a actionable request", ->
  289. beforeEach (done)->
  290. @req =
  291. body:
  292. expired_subscription_notification:
  293. subscription:
  294. uuid: @activeRecurlySubscription.uuid
  295. @res = sendStatus:->
  296. done()
  297. sinon.spy @res, "sendStatus"
  298. @SubscriptionController.recurlyCallback @req, @res
  299. it "should tell the SubscriptionHandler to process the recurly callback", (done)->
  300. @SubscriptionHandler.recurlyCallback.called.should.equal true
  301. done()
  302. it "should send a 200", (done)->
  303. @res.sendStatus.calledWith(200)
  304. done()
  305. describe "with a non-actionable request", ->
  306. beforeEach (done) ->
  307. @user.id = @activeRecurlySubscription.account.account_code
  308. @req =
  309. body:
  310. new_subscription_notification:
  311. subscription:
  312. uuid: @activeRecurlySubscription.uuid
  313. @res = sendStatus:->
  314. done()
  315. sinon.spy @res, "sendStatus"
  316. @SubscriptionController.recurlyCallback @req, @res
  317. it "should not call the subscriptionshandler", ->
  318. @SubscriptionHandler.recurlyCallback.called.should.equal false
  319. it "should respond with a 200 status", ->
  320. @res.sendStatus.calledWith(200)
  321. describe "renderUpgradeToAnnualPlanPage", ->
  322. it "should redirect to the plans page if the user does not have a subscription", (done)->
  323. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  324. @res.redirect = (url)->
  325. url.should.equal "/user/subscription/plans"
  326. done()
  327. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  328. it "should pass the plan code to the view - student", (done)->
  329. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"Student free trial 14 days"})
  330. @res.render = (view, opts)->
  331. view.should.equal "subscriptions/upgradeToAnnual"
  332. opts.planName.should.equal "student"
  333. done()
  334. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  335. it "should pass the plan code to the view - collaborator", (done)->
  336. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"free trial for Collaborator free trial 14 days"})
  337. @res.render = (view, opts)->
  338. opts.planName.should.equal "collaborator"
  339. done()
  340. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  341. it "should pass annual as the plan name if the user is already on an annual plan", (done)->
  342. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"student annual with free trial"})
  343. @res.render = (view, opts)->
  344. opts.planName.should.equal "annual"
  345. done()
  346. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  347. describe "processUpgradeToAnnualPlan", ->
  348. beforeEach ->
  349. it "should tell the subscription handler to update the subscription with the annual plan and apply a coupon code", (done)->
  350. @req.body =
  351. planName:"student"
  352. @res.sendStatus = ()=>
  353. @SubscriptionHandler.updateSubscription.calledWith(@user, "student-annual", "STUDENTCODEHERE").should.equal true
  354. done()
  355. @SubscriptionController.processUpgradeToAnnualPlan @req, @res
  356. it "should get the collaborator coupon code", (done)->
  357. @req.body =
  358. planName:"collaborator"
  359. @res.sendStatus = (url)=>
  360. @SubscriptionHandler.updateSubscription.calledWith(@user, "collaborator-annual", "COLLABORATORCODEHERE").should.equal true
  361. done()
  362. @SubscriptionController.processUpgradeToAnnualPlan @req, @res