SubscriptionControllerTests.coffee 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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", ->
  19. beforeEach ->
  20. @user = {email:"tom@yahoo.com", _id: 'one', signUpDate: new Date('2000-10-01')}
  21. @activeRecurlySubscription = mockSubscriptions["subscription-123-active"]
  22. @AuthenticationController =
  23. getLoggedInUser: sinon.stub().callsArgWith(1, null, @user)
  24. getLoggedInUserId: sinon.stub().returns(@user._id)
  25. getSessionUser: sinon.stub().returns(@user)
  26. isUserLoggedIn: sinon.stub().returns(true)
  27. @SubscriptionHandler =
  28. createSubscription: sinon.stub().callsArgWith(3)
  29. updateSubscription: sinon.stub().callsArgWith(3)
  30. reactivateSubscription: sinon.stub().callsArgWith(1)
  31. cancelSubscription: sinon.stub().callsArgWith(1)
  32. recurlyCallback: sinon.stub().callsArgWith(1)
  33. startFreeTrial: sinon.stub()
  34. @PlansLocator =
  35. findLocalPlanInSettings: sinon.stub()
  36. @LimitationsManager =
  37. userHasSubscriptionOrIsGroupMember: sinon.stub()
  38. userHasSubscription : sinon.stub()
  39. @RecurlyWrapper =
  40. sign: sinon.stub().callsArgWith(1, null, "somthing")
  41. @SubscriptionViewModelBuilder =
  42. buildUsersSubscriptionViewModel:sinon.stub().callsArgWith(1, null, @activeRecurlySubscription)
  43. buildViewModel: sinon.stub()
  44. @settings =
  45. coupon_codes:
  46. upgradeToAnnualPromo:
  47. student:"STUDENTCODEHERE"
  48. collaborator:"COLLABORATORCODEHERE"
  49. apis:
  50. recurly:
  51. subdomain:"sl"
  52. siteUrl: "http://de.sharelatex.dev:3000"
  53. gaExperiments:{}
  54. @GeoIpLookup =
  55. getCurrencyCode:sinon.stub()
  56. @SubscriptionDomainHandler =
  57. getDomainLicencePage:sinon.stub()
  58. @UserGetter =
  59. getUser: sinon.stub().callsArgWith(2, null, @user)
  60. @SubscriptionController = SandboxedModule.require modulePath, requires:
  61. '../Authentication/AuthenticationController': @AuthenticationController
  62. './SubscriptionHandler': @SubscriptionHandler
  63. "./PlansLocator": @PlansLocator
  64. './SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder
  65. "./LimitationsManager": @LimitationsManager
  66. "../../infrastructure/GeoIpLookup":@GeoIpLookup
  67. './RecurlyWrapper': @RecurlyWrapper
  68. "logger-sharelatex":
  69. log:->
  70. warn:->
  71. "settings-sharelatex": @settings
  72. "./SubscriptionDomainHandler":@SubscriptionDomainHandler
  73. "../User/UserGetter": @UserGetter
  74. @res = new MockResponse()
  75. @req = new MockRequest()
  76. @req.body = {}
  77. @req.query =
  78. planCode:"123123"
  79. @stubbedCurrencyCode = "GBP"
  80. describe "plansPage", ->
  81. beforeEach (done) ->
  82. @req.ip = "1234.3123.3131.333 313.133.445.666 653.5345.5345.534"
  83. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  84. @res.callback = done
  85. @SubscriptionController.plansPage(@req, @res)
  86. @UserGetter.getUser = sinon.stub().callsArgWith(2, null, @user)
  87. it "should set the recommended currency from the geoiplookup", (done)->
  88. @res.renderedVariables.recomendedCurrency.should.equal(@stubbedCurrencyCode)
  89. @GeoIpLookup.getCurrencyCode.calledWith(@req.ip).should.equal true
  90. done()
  91. it 'should fetch the current user', (done) ->
  92. @UserGetter.getUser.callCount.should.equal 1
  93. done()
  94. it 'should decide not to AB test the plans', (done) ->
  95. @res.renderedVariables.shouldABTestPlans.should.equal false
  96. done()
  97. describe 'when user is not logged in', (done) ->
  98. beforeEach ->
  99. @AuthenticationController.getLoggedInUserId.returns(null)
  100. it 'should not fetch the current user', (done) ->
  101. @UserGetter.getUser.callCount.should.equal 0
  102. done()
  103. describe "editBillingDetailsPage", ->
  104. describe "with a user with a subscription", ->
  105. beforeEach (done) ->
  106. @settings.apis.recurly.subdomain = 'test'
  107. @userSub = {account: {hosted_login_token: 'abcd'}}
  108. @LimitationsManager.userHasSubscription
  109. .callsArgWith(1, null, true, @activeRecurlySubscription)
  110. @RecurlyWrapper.getSubscription = sinon.stub()
  111. .callsArgWith(2, null, @userSub)
  112. @user._id = @activeRecurlySubscription.account.account_code
  113. @res.callback = done
  114. @SubscriptionController.editBillingDetailsPage(@req, @res)
  115. it "should render the edit billing details page", ->
  116. @res.rendered.should.equal true
  117. @res.renderedTemplate.should.equal "subscriptions/edit-billing-details"
  118. it "should set the correct variables for the template", ->
  119. should.exist @res.renderedVariables.hostedBillingDetailsPageLink
  120. @res.renderedVariables.hostedBillingDetailsPageLink.should.equal(
  121. "https://test.recurly.com/account/billing_info/edit?ht=abcd"
  122. )
  123. @res.renderedVariables.user.id.should.equal @user._id
  124. describe "with a user without subscription", ->
  125. beforeEach (done) ->
  126. @res.callback = done
  127. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  128. @SubscriptionController.reactivateSubscription @req, @res
  129. it "should redirect to the subscription dashboard", ->
  130. @res.redirected.should.equal true
  131. @res.redirectedTo.should.equal "/user/subscription"
  132. describe "paymentPage", ->
  133. beforeEach ->
  134. @req.headers = {}
  135. @SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, true)
  136. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  137. describe "with a user without a subscription", ->
  138. beforeEach ->
  139. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  140. @PlansLocator.findLocalPlanInSettings.returns({})
  141. describe "with a valid plan code", ->
  142. it "should render the new subscription page", (done)->
  143. @res.render = (page, opts)=>
  144. page.should.equal "subscriptions/new"
  145. done()
  146. @SubscriptionController.paymentPage @req, @res
  147. describe "with a user with subscription", ->
  148. it "should redirect to the subscription dashboard", (done)->
  149. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true)
  150. @res.redirect = (url)=>
  151. url.should.equal "/user/subscription"
  152. done()
  153. @SubscriptionController.paymentPage(@req, @res)
  154. describe "with an invalid plan code", ->
  155. it "should redirect to the subscription dashboard", (done)->
  156. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  157. @PlansLocator.findLocalPlanInSettings.returns(null)
  158. @res.redirect = (url)=>
  159. url.should.equal "/user/subscription"
  160. done()
  161. @SubscriptionController.paymentPage(@req, @res)
  162. describe "which currency to use", ->
  163. beforeEach ->
  164. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  165. @PlansLocator.findLocalPlanInSettings.returns({})
  166. it "should use the set currency from the query string", (done)->
  167. @req.query.currency = "EUR"
  168. @res.render = (page, opts)=>
  169. opts.currency.should.equal "EUR"
  170. opts.currency.should.not.equal @stubbedCurrencyCode
  171. done()
  172. @SubscriptionController.paymentPage @req, @res
  173. it "should upercase the currency code", (done)->
  174. @req.query.currency = "eur"
  175. @res.render = (page, opts)=>
  176. opts.currency.should.equal "EUR"
  177. done()
  178. @SubscriptionController.paymentPage @req, @res
  179. it "should use the geo ip currency if non is provided", (done)->
  180. @req.query.currency = null
  181. @res.render = (page, opts)=>
  182. opts.currency.should.equal @stubbedCurrencyCode
  183. done()
  184. @SubscriptionController.paymentPage @req, @res
  185. describe "with a recurly subscription already", ->
  186. it "should redirect to the subscription dashboard", (done)->
  187. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  188. @SubscriptionHandler.validateNoSubscriptionInRecurly = sinon.stub().yields(null, false)
  189. @res.redirect = (url)=>
  190. url.should.equal "/user/subscription"
  191. done()
  192. @SubscriptionController.paymentPage(@req, @res)
  193. describe "successful_subscription", ->
  194. beforeEach (done) ->
  195. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, {})
  196. @res.callback = done
  197. @SubscriptionController.successful_subscription @req, @res
  198. describe "userSubscriptionPage", ->
  199. describe "with a user without a subscription", ->
  200. beforeEach (done) ->
  201. @res.callback = done
  202. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
  203. @SubscriptionController.userSubscriptionPage @req, @res
  204. it "should redirect to the plans page", ->
  205. @res.redirected.should.equal true
  206. @res.redirectedTo.should.equal "/user/subscription/plans"
  207. describe "with a potential domain licence", ->
  208. beforeEach () ->
  209. @groupUrl = "/go/over-here"
  210. @SubscriptionDomainHandler.getDomainLicencePage.returns(@groupUrl)
  211. describe "without an existing subscription", ->
  212. beforeEach (done)->
  213. @res.callback = done
  214. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, false)
  215. @SubscriptionController.userSubscriptionPage @req, @res
  216. it "should redirect to the group invite url", ->
  217. @res.redirected.should.equal true
  218. @res.redirectedTo.should.equal @groupUrl
  219. describe "with an existing subscription", ->
  220. beforeEach (done)->
  221. @res.callback = done
  222. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  223. @SubscriptionController.userSubscriptionPage @req, @res
  224. it "should render the dashboard", ->
  225. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  226. describe "with a user with a paid subscription", ->
  227. beforeEach (done) ->
  228. @res.callback = done
  229. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  230. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  231. @SubscriptionController.userSubscriptionPage @req, @res
  232. it "should render the dashboard", (done)->
  233. @res.rendered.should.equal true
  234. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  235. done()
  236. it "should set the correct subscription details", ->
  237. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  238. describe "with a user with a free trial", ->
  239. beforeEach (done) ->
  240. @res.callback = done
  241. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, @activeRecurlySubscription)
  242. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true)
  243. @SubscriptionController.userSubscriptionPage @req, @res
  244. it "should render the dashboard", ->
  245. @res.renderedTemplate.should.equal "subscriptions/dashboard"
  246. it "should set the correct subscription details", ->
  247. @res.renderedVariables.subscription.should.deep.equal @activeRecurlySubscription
  248. describe "when its a custom subscription which is non recurly", ->
  249. beforeEach ()->
  250. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true, {customAccount:true})
  251. @SubscriptionController.userSubscriptionPage @req, @res
  252. it "should redirect to /user/subscription/custom_account", ->
  253. @res.redirectedTo.should.equal("/user/subscription/custom_account")
  254. describe "userCustomSubscriptionPage", ->
  255. beforeEach (done) ->
  256. @res.callback = done
  257. @LimitationsManager.userHasSubscriptionOrIsGroupMember.callsArgWith(1, null, true, {})
  258. @SubscriptionController.userCustomSubscriptionPage @req, @res
  259. it "should render the page", (done)->
  260. @res.rendered.should.equal true
  261. @res.renderedTemplate.should.equal "subscriptions/custom_account"
  262. done()
  263. describe "createSubscription", ->
  264. beforeEach (done)->
  265. @res =
  266. sendStatus:->
  267. done()
  268. sinon.spy @res, "sendStatus"
  269. @subscriptionDetails =
  270. card:"1234"
  271. cvv:"123"
  272. @req.body.recurly_token_id = "1234"
  273. @req.body.subscriptionDetails = @subscriptionDetails
  274. @SubscriptionController.createSubscription @req, @res
  275. it "should send the user and subscriptionId to the handler", (done)->
  276. @SubscriptionHandler.createSubscription.calledWith(@user, @subscriptionDetails, @req.body.recurly_token_id).should.equal true
  277. done()
  278. it "should redurect to the subscription page", (done)->
  279. @res.sendStatus.calledWith(201).should.equal true
  280. done()
  281. describe "updateSubscription via post", ->
  282. beforeEach (done)->
  283. @res =
  284. redirect:->
  285. done()
  286. sinon.spy @res, "redirect"
  287. @plan_code = "1234"
  288. @req.body.plan_code = @plan_code
  289. @SubscriptionController.updateSubscription @req, @res
  290. it "should send the user and subscriptionId to the handler", (done)->
  291. @SubscriptionHandler.updateSubscription.calledWith(@user, @plan_code).should.equal true
  292. done()
  293. it "should redurect to the subscription page", (done)->
  294. @res.redirect.calledWith("/user/subscription").should.equal true
  295. done()
  296. describe "reactivateSubscription", ->
  297. beforeEach (done)->
  298. @res =
  299. redirect:->
  300. done()
  301. sinon.spy @res, "redirect"
  302. @SubscriptionController.reactivateSubscription @req, @res
  303. it "should tell the handler to reactivate this user", (done)->
  304. @SubscriptionHandler.reactivateSubscription.calledWith(@user).should.equal true
  305. done()
  306. it "should redurect to the subscription page", (done)->
  307. @res.redirect.calledWith("/user/subscription").should.equal true
  308. done()
  309. describe "cancelSubscription", ->
  310. beforeEach (done)->
  311. @res =
  312. redirect:->
  313. done()
  314. sinon.spy @res, "redirect"
  315. @SubscriptionController.cancelSubscription @req, @res
  316. it "should tell the handler to cancel this user", (done)->
  317. @SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
  318. done()
  319. it "should redurect to the subscription page", (done)->
  320. @res.redirect.calledWith("/user/subscription").should.equal true
  321. done()
  322. describe "recurly callback", ->
  323. describe "with a actionable request", ->
  324. beforeEach (done)->
  325. @req =
  326. body:
  327. expired_subscription_notification:
  328. subscription:
  329. uuid: @activeRecurlySubscription.uuid
  330. @res = sendStatus:->
  331. done()
  332. sinon.spy @res, "sendStatus"
  333. @SubscriptionController.recurlyCallback @req, @res
  334. it "should tell the SubscriptionHandler to process the recurly callback", (done)->
  335. @SubscriptionHandler.recurlyCallback.called.should.equal true
  336. done()
  337. it "should send a 200", (done)->
  338. @res.sendStatus.calledWith(200)
  339. done()
  340. describe "with a non-actionable request", ->
  341. beforeEach (done) ->
  342. @user.id = @activeRecurlySubscription.account.account_code
  343. @req =
  344. body:
  345. new_subscription_notification:
  346. subscription:
  347. uuid: @activeRecurlySubscription.uuid
  348. @res = sendStatus:->
  349. done()
  350. sinon.spy @res, "sendStatus"
  351. @SubscriptionController.recurlyCallback @req, @res
  352. it "should not call the subscriptionshandler", ->
  353. @SubscriptionHandler.recurlyCallback.called.should.equal false
  354. it "should respond with a 200 status", ->
  355. @res.sendStatus.calledWith(200)
  356. describe "renderUpgradeToAnnualPlanPage", ->
  357. it "should redirect to the plans page if the user does not have a subscription", (done)->
  358. @LimitationsManager.userHasSubscription.callsArgWith(1, null, false)
  359. @res.redirect = (url)->
  360. url.should.equal "/user/subscription/plans"
  361. done()
  362. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  363. it "should pass the plan code to the view - student", (done)->
  364. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"Student free trial 14 days"})
  365. @res.render = (view, opts)->
  366. view.should.equal "subscriptions/upgradeToAnnual"
  367. opts.planName.should.equal "student"
  368. done()
  369. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  370. it "should pass the plan code to the view - collaborator", (done)->
  371. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"free trial for Collaborator free trial 14 days"})
  372. @res.render = (view, opts)->
  373. opts.planName.should.equal "collaborator"
  374. done()
  375. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  376. it "should pass annual as the plan name if the user is already on an annual plan", (done)->
  377. @LimitationsManager.userHasSubscription.callsArgWith(1, null, true, {planCode:"student annual with free trial"})
  378. @res.render = (view, opts)->
  379. opts.planName.should.equal "annual"
  380. done()
  381. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  382. describe "processUpgradeToAnnualPlan", ->
  383. beforeEach ->
  384. it "should tell the subscription handler to update the subscription with the annual plan and apply a coupon code", (done)->
  385. @req.body =
  386. planName:"student"
  387. @res.sendStatus = ()=>
  388. @SubscriptionHandler.updateSubscription.calledWith(@user, "student-annual", "STUDENTCODEHERE").should.equal true
  389. done()
  390. @SubscriptionController.processUpgradeToAnnualPlan @req, @res
  391. it "should get the collaborator coupon code", (done)->
  392. @req.body =
  393. planName:"collaborator"
  394. @res.sendStatus = (url)=>
  395. @SubscriptionHandler.updateSubscription.calledWith(@user, "collaborator-annual", "COLLABORATORCODEHERE").should.equal true
  396. done()
  397. @SubscriptionController.processUpgradeToAnnualPlan @req, @res