Merge pull request #5717 from overleaf/ab-sub-controller-uncaught-ex

Prevent uncaught error in SubscriptionController

GitOrigin-RevId: af8e986ce4c83c48d4af2a4af092c63eea092335
This commit is contained in:
Alexandre Bourdin 2021-11-08 16:07:42 +01:00 committed by Copybot
parent c881909258
commit 7836743754
2 changed files with 43 additions and 7 deletions

View file

@ -223,7 +223,7 @@ function successfulSubscription(req, res, next) {
const user = SessionManager.getSessionUser(req.session)
return SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel(
user,
function (error, { personalSubscription }) {
function (error, { personalSubscription } = {}) {
if (error) {
return next(error)
}

View file

@ -13,7 +13,7 @@
*/
const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
const { expect } = require('chai')
const { assert, expect } = require('chai')
const MockRequest = require('../helpers/MockRequest')
const MockResponse = require('../helpers/MockResponse')
const modulePath =
@ -358,17 +358,53 @@ describe('SubscriptionController', function () {
})
describe('successfulSubscription', function () {
beforeEach(function (done) {
it('without a personnal subscription', function (done) {
this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
1,
null,
{}
)
this.res.callback = done
return this.SubscriptionController.successfulSubscription(
this.req,
this.res
this.res.callback = () => {
assert.equal(this.res.redirectedTo, '/user/subscription/plans')
done()
}
this.SubscriptionController.successfulSubscription(this.req, this.res)
})
it('with a personnal subscription', function (done) {
this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
1,
null,
{ personalSubscription: 'foo' }
)
this.res.callback = () => {
assert.equal(
this.res.renderedTemplate,
'subscriptions/successful_subscription'
)
assert.deepEqual(this.res.renderedVariables, {
title: 'thank_you',
personalSubscription: 'foo',
})
done()
}
this.SubscriptionController.successfulSubscription(this.req, this.res)
})
it('with an error', function () {
const next = sinon.stub()
const error = new Error('test')
this.SubscriptionViewModelBuilder.buildUsersSubscriptionViewModel.callsArgWith(
1,
error,
undefined
)
this.SubscriptionController.successfulSubscription(
this.req,
this.res,
next
)
sinon.assert.calledWith(next, error)
})
})