Merge pull request #2985 from overleaf/msm-oerror-remove-unprocessable-entity-error

Replace UnprocessableEntityError with calls to unprocessableEntity() handler

GitOrigin-RevId: 4bba389c8cdf87a40137d49db571fa81aaac4239
This commit is contained in:
Miguel Serrano
2020-07-16 08:47:46 +02:00
committed by Copybot
parent 397bd034c7
commit 6562f3003d
8 changed files with 167 additions and 51 deletions

View File

@@ -122,6 +122,9 @@ describe('EditorHttpController', function() {
convertDocToFile: sinon.stub().resolves(this.file)
}
}
this.HttpErrorHandler = {
unprocessableEntity: sinon.stub()
}
this.EditorHttpController = SandboxedModule.require(MODULE_PATH, {
globals: {
console: console
@@ -144,6 +147,7 @@ describe('EditorHttpController', function() {
'../../infrastructure/FileWriter': this.FileWriter,
'../Project/ProjectEntityUpdateHandler': this
.ProjectEntityUpdateHandler,
'../Errors/HttpErrorHandler': this.HttpErrorHandler,
'../Errors/Errors': Errors,
'@overleaf/o-error/http': HttpErrors
}
@@ -540,14 +544,19 @@ describe('EditorHttpController', function() {
})
describe('when the doc has ranges', function() {
it('should return a 422', function(done) {
it('should return a 422 - Unprocessable Entity', function(done) {
this.ProjectEntityUpdateHandler.promises.convertDocToFile.rejects(
new Errors.DocHasRangesError({})
)
this.EditorHttpController.convertDocToFile(this.req, this.res, err => {
expect(err).to.be.instanceof(HttpErrors.UnprocessableEntityError)
done()
})
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
(req, res, message) => {
expect(req).to.exist
expect(res).to.exist
expect(message).to.equal('Document has comments or tracked changes')
done()
}
)
this.EditorHttpController.convertDocToFile(this.req, this.res)
})
})
})

View File

@@ -45,4 +45,42 @@ describe('HttpErrorHandler', function() {
})
})
})
describe('unprocessableEntity', function() {
it('returns 422', function() {
this.HttpErrorHandler.unprocessableEntity(this.req, this.res)
expect(this.res.statusCode).to.equal(422)
})
it('should print a message when no content-type is included', function() {
this.HttpErrorHandler.unprocessableEntity(this.req, this.res)
expect(this.res.body).to.equal('unprocessable entity')
})
it("should render a template including the error message when content-type is 'html'", function() {
this.req.accepts = () => 'html'
this.HttpErrorHandler.unprocessableEntity(this.req, this.res, 'an error')
expect(this.res.renderedTemplate).to.equal('general/400')
expect(this.res.renderedVariables).to.deep.equal({
title: 'Client Error',
message: 'an error'
})
})
it("should return a json object when content-type is 'json'", function() {
this.req.accepts = () => 'json'
this.HttpErrorHandler.unprocessableEntity(
this.req,
this.res,
'an error',
{
foo: 'bar'
}
)
expect(JSON.parse(this.res.body)).to.deep.equal({
message: 'an error',
foo: 'bar'
})
})
})
})

View File

@@ -121,6 +121,9 @@ describe('SubscriptionController', function() {
'./FeaturesUpdater': (this.FeaturesUpdater = {}),
'./GroupPlansData': (this.GroupPlansData = {}),
'./V1SubscriptionManager': (this.V1SubscriptionManager = {}),
'../Errors/HttpErrorHandler': (this.HttpErrorHandler = {
unprocessableEntity: sinon.stub()
}),
'../Errors/Errors': Errors,
'./Errors': SubscriptionErrors,
'@overleaf/o-error/http': HttpErrors
@@ -240,18 +243,21 @@ describe('SubscriptionController', function() {
})
describe('with an invalid plan code', function() {
it('should return 422 error', function(done) {
it('should return 422 error - Unprocessable Entity', function(done) {
this.LimitationsManager.userHasV1OrV2Subscription.callsArgWith(
1,
null,
false
)
this.PlansLocator.findLocalPlanInSettings.returns(null)
this.next = error => {
expect(error).to.exist
expect(error.statusCode).to.equal(422)
done()
}
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
(req, res, message) => {
expect(req).to.exist
expect(res).to.exist
expect(message).to.deep.equal('Plan not found')
done()
}
)
return this.SubscriptionController.paymentPage(
this.req,
this.res,
@@ -470,6 +476,39 @@ describe('SubscriptionController', function() {
})
return done()
})
it('should handle recurly errors', function(done) {
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
this.SubscriptionHandler.createSubscription.yields(
new SubscriptionErrors.RecurlyTransactionError({})
)
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
(req, res, info) => {
expect(req).to.exist
expect(res).to.exist
expect(info).to.deep.equal('Unknown transaction error')
done()
}
)
return this.SubscriptionController.createSubscription(this.req, this.res)
})
it('should handle invalid error', function(done) {
this.LimitationsManager.userHasV1OrV2Subscription.yields(null, false)
this.SubscriptionHandler.createSubscription.yields(
new Errors.InvalidError({})
)
this.HttpErrorHandler.unprocessableEntity = sinon.spy((req, res) => {
expect(req).to.exist
expect(res).to.exist
done()
})
return this.SubscriptionController.createSubscription(this.req, this.res)
})
})
describe('updateSubscription via post', function() {

View File

@@ -70,6 +70,9 @@ describe('UserController', function() {
revokeAllUserSessions: sinon.stub().callsArgWith(2, null)
}
this.SudoModeHandler = { clearSudoMode: sinon.stub() }
this.HttpErrorHandler = {
unprocessableEntity: sinon.stub()
}
this.UserController = SandboxedModule.require(modulePath, {
globals: {
console: console
@@ -95,6 +98,7 @@ describe('UserController', function() {
'./UserHandler': this.UserHandler,
'./UserSessionsManager': this.UserSessionsManager,
'../SudoMode/SudoModeHandler': this.SudoModeHandler,
'../Errors/HttpErrorHandler': this.HttpErrorHandler,
'settings-sharelatex': this.settings,
'logger-sharelatex': {
log() {},
@@ -233,17 +237,19 @@ describe('UserController', function() {
.yields(new Errors.SubscriptionAdminDeletionError())
})
it('should return a json error', function(done) {
this.UserController.tryDeleteUser(this.req, null, error => {
expect(error).to.be.instanceof(HttpErrors.UnprocessableEntityError)
expect(
OError.hasCauseInstanceOf(
error,
Errors.SubscriptionAdminDeletionError
)
).to.be.true
done()
})
it('should return a HTTP Unprocessable Entity error', function(done) {
this.HttpErrorHandler.unprocessableEntity = sinon.spy(
(req, res, message, info) => {
expect(req).to.exist
expect(res).to.exist
expect(message).to.equal('error while deleting user account')
expect(info).to.deep.equal({
error: 'SubscriptionAdminDeletionError'
})
done()
}
)
this.UserController.tryDeleteUser(this.req, this.res)
})
})