SubscriptionControllerTests.coffee 16 KB

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