SubscriptionControllerTests.coffee 18 KB

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