SubscriptionControllerTests.coffee 17 KB

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