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