SubscriptionControllerTests.coffee 16 KB

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