SubscriptionControllerTests.coffee 18 KB

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