SubscriptionControllerTests.coffee 16 KB

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