SubscriptionControllerTests.coffee 16 KB

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