SubscriptionControllerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require 'sinon'
  3. should = require("chai").should()
  4. expect = require("chai").expect
  5. MockRequest = require "../helpers/MockRequest"
  6. MockResponse = require "../helpers/MockResponse"
  7. modulePath = '../../../../app/js/Features/Subscription/SubscriptionController'
  8. mockSubscriptions =
  9. "subscription-123-active":
  10. uuid: "subscription-123-active"
  11. plan:
  12. name: "Gold"
  13. plan_code: "gold"
  14. current_period_ends_at: new Date()
  15. state: "active"
  16. unit_amount_in_cents: 999
  17. account:
  18. account_code: "user-123"
  19. describe "SubscriptionController", ->
  20. beforeEach ->
  21. @user = {email:"tom@yahoo.com", _id: 'one', signUpDate: new Date('2000-10-01')}
  22. @activeRecurlySubscription = mockSubscriptions["subscription-123-active"]
  23. @AuthenticationController =
  24. getLoggedInUser: sinon.stub().callsArgWith(1, null, @user)
  25. getLoggedInUserId: sinon.stub().returns(@user._id)
  26. getSessionUser: sinon.stub().returns(@user)
  27. isUserLoggedIn: sinon.stub().returns(true)
  28. @SubscriptionHandler =
  29. createSubscription: sinon.stub().callsArgWith(3)
  30. updateSubscription: sinon.stub().callsArgWith(3)
  31. reactivateSubscription: sinon.stub().callsArgWith(1)
  32. cancelSubscription: sinon.stub().callsArgWith(1)
  33. recurlyCallback: sinon.stub().callsArgWith(1)
  34. startFreeTrial: sinon.stub()
  35. @PlansLocator =
  36. findLocalPlanInSettings: sinon.stub()
  37. @LimitationsManager =
  38. hasPaidSubscription: sinon.stub()
  39. userHasV1OrV2Subscription : sinon.stub()
  40. userHasV2Subscription: sinon.stub()
  41. @SubscriptionViewModelBuilder =
  42. buildUsersSubscriptionViewModel:sinon.stub().callsArgWith(1, null, {})
  43. buildViewModel: sinon.stub()
  44. @settings =
  45. coupon_codes:
  46. upgradeToAnnualPromo:
  47. student:"STUDENTCODEHERE"
  48. collaborator:"COLLABORATORCODEHERE"
  49. apis:
  50. recurly:
  51. subdomain:"sl"
  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. "logger-sharelatex":
  68. log:->
  69. warn:->
  70. "settings-sharelatex": @settings
  71. "./SubscriptionDomainHandler":@SubscriptionDomainHandler
  72. "../User/UserGetter": @UserGetter
  73. "./RecurlyWrapper": @RecurlyWrapper = {}
  74. "./FeaturesUpdater": @FeaturesUpdater = {}
  75. "./GroupPlansData": @GroupPlansData = {}
  76. @res = new MockResponse()
  77. @req = new MockRequest()
  78. @req.body = {}
  79. @req.query =
  80. planCode:"123123"
  81. @stubbedCurrencyCode = "GBP"
  82. describe "plansPage", ->
  83. beforeEach ->
  84. @req.ip = "1234.3123.3131.333 313.133.445.666 653.5345.5345.534"
  85. @GeoIpLookup.getCurrencyCode.callsArgWith(1, null, @stubbedCurrencyCode)
  86. describe 'when user is logged in', (done) ->
  87. beforeEach (done) ->
  88. @res.callback = done
  89. @SubscriptionController.plansPage(@req, @res)
  90. it 'should fetch the current user', (done) ->
  91. @UserGetter.getUser.callCount.should.equal 1
  92. done()
  93. describe 'not dependant on logged in state', (done) ->
  94. # these could have been put in 'when user is not logged in' too
  95. it "should set the recommended currency from the geoiplookup", (done)->
  96. @res.renderedVariables.recomendedCurrency.should.equal(@stubbedCurrencyCode)
  97. @GeoIpLookup.getCurrencyCode.calledWith(@req.ip).should.equal true
  98. done()
  99. it 'should include data for features table', (done) ->
  100. @res.renderedVariables.planFeatures.length.should.not.equal 0
  101. done()
  102. describe 'when user is not logged in', (done) ->
  103. beforeEach (done) ->
  104. @UserGetter =
  105. getUser: sinon.stub().callsArgWith(2, null, null)
  106. @res.callback = done
  107. @AuthenticationController =
  108. getLoggedInUser: sinon.stub().callsArgWith(1, null, null)
  109. getLoggedInUserId: sinon.stub().returns(null)
  110. getSessionUser: sinon.stub().returns(null)
  111. isUserLoggedIn: sinon.stub().returns(false)
  112. @SubscriptionController = SandboxedModule.require modulePath, requires:
  113. '../Authentication/AuthenticationController': @AuthenticationController
  114. './SubscriptionHandler': @SubscriptionHandler
  115. "./PlansLocator": @PlansLocator
  116. './SubscriptionViewModelBuilder': @SubscriptionViewModelBuilder
  117. "./LimitationsManager": @LimitationsManager
  118. "../../infrastructure/GeoIpLookup":@GeoIpLookup
  119. "logger-sharelatex":
  120. log:->
  121. warn:->
  122. "settings-sharelatex": @settings
  123. "./SubscriptionDomainHandler":@SubscriptionDomainHandler
  124. "../User/UserGetter": @UserGetter
  125. "./RecurlyWrapper": @RecurlyWrapper = {}
  126. "./FeaturesUpdater": @FeaturesUpdater = {}
  127. "./GroupPlansData": @GroupPlansData
  128. @SubscriptionController.plansPage(@req, @res)
  129. it 'should not fetch the current user', (done) ->
  130. @UserGetter.getUser.callCount.should.equal 0
  131. done()
  132. describe "paymentPage", ->
  133. beforeEach ->
  134. @req.headers = {}
  135. @RecurlyWrapper.sign = sinon.stub().yields(null, @signature = "signature")
  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.userHasV1OrV2Subscription.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.userHasV1OrV2Subscription.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.userHasV1OrV2Subscription.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.userHasV1OrV2Subscription.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.userHasV1OrV2Subscription.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. beforeEach (done) ->
  201. @SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(1, null, {
  202. personalSubscription: @personalSubscription = { 'personal-subscription': 'mock' }
  203. memberGroupSubscriptions: @memberGroupSubscriptions = { 'group-subscriptions': 'mock' }
  204. v1Subscriptions: @v1Subscriptions = { 'v1-subscriptions': 'mock' }
  205. })
  206. @SubscriptionViewModelBuilder.buildViewModel.returns(@plans = {'plans': 'mock'})
  207. @res.render = (view, @data) =>
  208. expect(view).to.equal 'subscriptions/dashboard'
  209. done()
  210. @SubscriptionController.userSubscriptionPage @req, @res
  211. it "should load the personal, groups and v1 subscriptions", ->
  212. expect(@data.personalSubscription).to.deep.equal @personalSubscription
  213. expect(@data.memberGroupSubscriptions).to.deep.equal @memberGroupSubscriptions
  214. expect(@data.v1Subscriptions).to.deep.equal @v1Subscriptions
  215. it "should load the user", ->
  216. expect(@data.user).to.deep.equal @user
  217. it "should load the plans", ->
  218. expect(@data.plans).to.deep.equal @plans
  219. describe "createSubscription", ->
  220. beforeEach (done)->
  221. @res =
  222. sendStatus:->
  223. done()
  224. sinon.spy @res, "sendStatus"
  225. @subscriptionDetails =
  226. card:"1234"
  227. cvv:"123"
  228. @req.body.recurly_token_id = "1234"
  229. @req.body.subscriptionDetails = @subscriptionDetails
  230. @LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
  231. @SubscriptionController.createSubscription @req, @res
  232. it "should send the user and subscriptionId to the handler", (done)->
  233. @SubscriptionHandler.createSubscription.calledWith(@user, @subscriptionDetails, @req.body.recurly_token_id).should.equal true
  234. done()
  235. it "should redurect to the subscription page", (done)->
  236. @res.sendStatus.calledWith(201).should.equal true
  237. done()
  238. describe "updateSubscription via post", ->
  239. beforeEach (done)->
  240. @res =
  241. redirect:->
  242. done()
  243. sinon.spy @res, "redirect"
  244. @plan_code = "1234"
  245. @req.body.plan_code = @plan_code
  246. @SubscriptionController.updateSubscription @req, @res
  247. it "should send the user and subscriptionId to the handler", (done)->
  248. @SubscriptionHandler.updateSubscription.calledWith(@user, @plan_code).should.equal true
  249. done()
  250. it "should redurect to the subscription page", (done)->
  251. @res.redirect.calledWith("/user/subscription").should.equal true
  252. done()
  253. describe "reactivateSubscription", ->
  254. beforeEach (done)->
  255. @res =
  256. redirect:->
  257. done()
  258. sinon.spy @res, "redirect"
  259. @SubscriptionController.reactivateSubscription @req, @res
  260. it "should tell the handler to reactivate this user", (done)->
  261. @SubscriptionHandler.reactivateSubscription.calledWith(@user).should.equal true
  262. done()
  263. it "should redurect to the subscription page", (done)->
  264. @res.redirect.calledWith("/user/subscription").should.equal true
  265. done()
  266. describe "cancelSubscription", ->
  267. beforeEach (done)->
  268. @res =
  269. redirect:->
  270. done()
  271. sinon.spy @res, "redirect"
  272. @SubscriptionController.cancelSubscription @req, @res
  273. it "should tell the handler to cancel this user", (done)->
  274. @SubscriptionHandler.cancelSubscription.calledWith(@user).should.equal true
  275. done()
  276. it "should redurect to the subscription page", (done)->
  277. @res.redirect.calledWith("/user/subscription").should.equal true
  278. done()
  279. describe "recurly callback", ->
  280. describe "with a actionable request", ->
  281. beforeEach (done)->
  282. @req =
  283. body:
  284. expired_subscription_notification:
  285. subscription:
  286. uuid: @activeRecurlySubscription.uuid
  287. @res = sendStatus:->
  288. done()
  289. sinon.spy @res, "sendStatus"
  290. @SubscriptionController.recurlyCallback @req, @res
  291. it "should tell the SubscriptionHandler to process the recurly callback", (done)->
  292. @SubscriptionHandler.recurlyCallback.called.should.equal true
  293. done()
  294. it "should send a 200", (done)->
  295. @res.sendStatus.calledWith(200)
  296. done()
  297. describe "with a non-actionable request", ->
  298. beforeEach (done) ->
  299. @user.id = @activeRecurlySubscription.account.account_code
  300. @req =
  301. body:
  302. new_subscription_notification:
  303. subscription:
  304. uuid: @activeRecurlySubscription.uuid
  305. @res = sendStatus:->
  306. done()
  307. sinon.spy @res, "sendStatus"
  308. @SubscriptionController.recurlyCallback @req, @res
  309. it "should not call the subscriptionshandler", ->
  310. @SubscriptionHandler.recurlyCallback.called.should.equal false
  311. it "should respond with a 200 status", ->
  312. @res.sendStatus.calledWith(200)
  313. describe "renderUpgradeToAnnualPlanPage", ->
  314. it "should redirect to the plans page if the user does not have a subscription", (done)->
  315. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, false)
  316. @res.redirect = (url)->
  317. url.should.equal "/user/subscription/plans"
  318. done()
  319. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  320. it "should pass the plan code to the view - student", (done)->
  321. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, {planCode:"Student free trial 14 days"})
  322. @res.render = (view, opts)->
  323. view.should.equal "subscriptions/upgradeToAnnual"
  324. opts.planName.should.equal "student"
  325. done()
  326. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  327. it "should pass the plan code to the view - collaborator", (done)->
  328. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, {planCode:"free trial for Collaborator free trial 14 days"})
  329. @res.render = (view, opts)->
  330. opts.planName.should.equal "collaborator"
  331. done()
  332. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  333. it "should pass annual as the plan name if the user is already on an annual plan", (done)->
  334. @LimitationsManager.userHasV2Subscription.callsArgWith(1, null, true, {planCode:"student annual with free trial"})
  335. @res.render = (view, opts)->
  336. opts.planName.should.equal "annual"
  337. done()
  338. @SubscriptionController.renderUpgradeToAnnualPlanPage @req, @res
  339. describe "processUpgradeToAnnualPlan", ->
  340. beforeEach ->
  341. it "should tell the subscription handler to update the subscription with the annual plan and apply a coupon code", (done)->
  342. @req.body =
  343. planName:"student"
  344. @res.sendStatus = ()=>
  345. @SubscriptionHandler.updateSubscription.calledWith(@user, "student-annual", "STUDENTCODEHERE").should.equal true
  346. done()
  347. @SubscriptionController.processUpgradeToAnnualPlan @req, @res
  348. it "should get the collaborator coupon code", (done)->
  349. @req.body =
  350. planName:"collaborator"
  351. @res.sendStatus = (url)=>
  352. @SubscriptionHandler.updateSubscription.calledWith(@user, "collaborator-annual", "COLLABORATORCODEHERE").should.equal true
  353. done()
  354. @SubscriptionController.processUpgradeToAnnualPlan @req, @res