SubscriptionControllerTests.coffee 14 KB

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