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