SubscriptionControllerTests.coffee 14 KB

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