SubscriptionControllerTests.coffee 17 KB

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