Merge pull request #3021 from overleaf/msm-oerror-remove-conflict-error

Replace ConflictError thrown with calls to HttpErrorHandler.conflict()

GitOrigin-RevId: 3b4d98af1b31e49ceab4b1b55b94b8f0323c8a9b
This commit is contained in:
Eric Mc Sween 2020-07-20 09:21:28 -04:00 committed by Copybot
parent 4773494b55
commit f251d661ed
6 changed files with 109 additions and 28 deletions

View file

@ -27,6 +27,21 @@ module.exports = {
} }
}, },
conflict(req, res, message, info) {
res.status(409)
switch (req.accepts(['html', 'json'])) {
case 'html':
return res.render('general/400', {
title: 'Client Error',
message: message
})
case 'json':
return renderJSONError(res, message, info || {})
default:
return res.send('conflict')
}
},
forbidden(req, res, message = 'restricted', info = {}) { forbidden(req, res, message = 'restricted', info = {}) {
res.status(403) res.status(403)
switch (req.accepts(['html', 'json'])) { switch (req.accepts(['html', 'json'])) {

View file

@ -237,16 +237,14 @@ const UserController = {
UserUpdater.changeEmailAddress(userId, newEmail, err => { UserUpdater.changeEmailAddress(userId, newEmail, err => {
if (err) { if (err) {
let errorData = { let errorData = {
message: 'problem updaing users email address', message: 'problem updating users email address',
info: { userId, newEmail, public: {} } info: { userId, newEmail, public: {} }
} }
if (err instanceof Errors.EmailExistsError) { if (err instanceof Errors.EmailExistsError) {
errorData.info.public.message = req.i18n.translate( const translation = req.i18n.translate(
'email_already_registered' 'email_already_registered'
) )
return next( return HttpErrorHandler.conflict(req, res, translation)
new HttpErrors.ConflictError(errorData).withCause(err)
)
} else { } else {
errorData.info.public.message = req.i18n.translate( errorData.info.public.message = req.i18n.translate(
'problem_changing_email_address' 'problem_changing_email_address'

View file

@ -6,6 +6,7 @@ const EmailHelper = require('../Helpers/EmailHelper')
const UserEmailsConfirmationHandler = require('./UserEmailsConfirmationHandler') const UserEmailsConfirmationHandler = require('./UserEmailsConfirmationHandler')
const { endorseAffiliation } = require('../Institutions/InstitutionsAPI') const { endorseAffiliation } = require('../Institutions/InstitutionsAPI')
const Errors = require('../Errors/Errors') const Errors = require('../Errors/Errors')
const HttpErrorHandler = require('../Errors/HttpErrorHandler')
const HttpErrors = require('@overleaf/o-error/http') const HttpErrors = require('@overleaf/o-error/http')
function add(req, res, next) { function add(req, res, next) {
@ -158,31 +159,13 @@ module.exports = UserEmailsController = {
_handleEmailError(error, req, res, next) { _handleEmailError(error, req, res, next) {
if (error instanceof Errors.UnconfirmedEmailError) { if (error instanceof Errors.UnconfirmedEmailError) {
return next( return HttpErrorHandler.conflict(req, res, 'email must be confirmed')
new HttpErrors.ConflictError({
info: {
public: { message: 'email must be confirmed' }
}
}).withCause(error)
)
} else if (error instanceof Errors.EmailExistsError) { } else if (error instanceof Errors.EmailExistsError) {
return next( const message = req.i18n.translate('email_already_registered')
new HttpErrors.ConflictError({ return HttpErrorHandler.conflict(req, res, message)
info: {
public: { message: req.i18n.translate('email_already_registered') }
}
}).withCause(error)
)
} else if (error.message === '422: Email does not belong to university') { } else if (error.message === '422: Email does not belong to university') {
return next( const message = req.i18n.translate('email_does_not_belong_to_university')
new HttpErrors.ConflictError({ return HttpErrorHandler.conflict(req, res, message)
info: {
public: {
message: req.i18n.translate('email_does_not_belong_to_university')
}
}
}).withCause(error)
)
} }
next(new HttpErrors.InternalServerError().withCause(error)) next(new HttpErrors.InternalServerError().withCause(error))
} }

View file

@ -63,6 +63,44 @@ describe('HttpErrorHandler', function() {
}) })
}) })
describe('conflict', function() {
it('returns 409', function() {
this.HttpErrorHandler.conflict(this.req, this.res)
expect(this.res.statusCode).to.equal(409)
})
it('should print a message when no content-type is included', function() {
this.HttpErrorHandler.conflict(this.req, this.res)
expect(this.res.body).to.equal('conflict')
})
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'
})
})
})
describe('forbidden', function() { describe('forbidden', function() {
it('returns 403', function() { it('returns 403', function() {
this.HttpErrorHandler.forbidden(this.req, this.res) this.HttpErrorHandler.forbidden(this.req, this.res)

View file

@ -71,6 +71,7 @@ describe('UserController', function() {
} }
this.SudoModeHandler = { clearSudoMode: sinon.stub() } this.SudoModeHandler = { clearSudoMode: sinon.stub() }
this.HttpErrorHandler = { this.HttpErrorHandler = {
conflict: sinon.stub(),
unprocessableEntity: sinon.stub() unprocessableEntity: sinon.stub()
} }
this.UserController = SandboxedModule.require(modulePath, { this.UserController = SandboxedModule.require(modulePath, {
@ -428,6 +429,21 @@ describe('UserController', function() {
} }
this.UserController.updateUserSettings(this.req, this.res, next) this.UserController.updateUserSettings(this.req, this.res, next)
}) })
it('should call the HTTP conflict error handler when the email already exists', function(done) {
this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
expect(req).to.exist
expect(req).to.exist
message.should.equal('email_already_registered')
done()
})
this.req.body.email = this.newEmail.toUpperCase()
this.UserUpdater.changeEmailAddress.callsArgWith(
2,
new Errors.EmailExistsError()
)
this.UserController.updateUserSettings(this.req, this.res)
})
}) })
describe('when using an external auth source', function() { describe('when using an external auth source', function() {

View file

@ -6,11 +6,13 @@ const { assert } = chai
const modulePath = '../../../../app/src/Features/User/UserEmailsController.js' const modulePath = '../../../../app/src/Features/User/UserEmailsController.js'
const SandboxedModule = require('sandboxed-module') const SandboxedModule = require('sandboxed-module')
const MockRequest = require('../helpers/MockRequest') const MockRequest = require('../helpers/MockRequest')
const MockResponse = require('../helpers/MockResponse')
const Errors = require('../../../../app/src/Features/Errors/Errors') const Errors = require('../../../../app/src/Features/Errors/Errors')
describe('UserEmailsController', function() { describe('UserEmailsController', function() {
beforeEach(function() { beforeEach(function() {
this.req = new MockRequest() this.req = new MockRequest()
this.res = new MockResponse()
this.user = { _id: 'mock-user-id' } this.user = { _id: 'mock-user-id' }
this.UserGetter = { getUserFullEmails: sinon.stub() } this.UserGetter = { getUserFullEmails: sinon.stub() }
@ -38,6 +40,7 @@ describe('UserEmailsController', function() {
.withArgs('example.com') .withArgs('example.com')
.resolves({ sso_enabled: false }) .resolves({ sso_enabled: false })
} }
this.HttpErrorHandler = { conflict: sinon.stub() }
this.UserEmailsController = SandboxedModule.require(modulePath, { this.UserEmailsController = SandboxedModule.require(modulePath, {
globals: { globals: {
console: console console: console
@ -51,6 +54,7 @@ describe('UserEmailsController', function() {
'../Helpers/EmailHelper': this.EmailHelper, '../Helpers/EmailHelper': this.EmailHelper,
'./UserEmailsConfirmationHandler': (this.UserEmailsConfirmationHandler = {}), './UserEmailsConfirmationHandler': (this.UserEmailsConfirmationHandler = {}),
'../Institutions/InstitutionsAPI': this.InstitutionsAPI, '../Institutions/InstitutionsAPI': this.InstitutionsAPI,
'../Errors/HttpErrorHandler': this.HttpErrorHandler,
'../Errors/Errors': Errors, '../Errors/Errors': Errors,
'logger-sharelatex': { 'logger-sharelatex': {
log() { log() {
@ -142,6 +146,33 @@ describe('UserEmailsController', function() {
} }
}) })
}) })
it('should call the HTTP conflict handler when the email already exists', function(done) {
this.UserUpdater.addEmailAddress.callsArgWith(
3,
new Errors.EmailExistsError()
)
this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
req.should.exist
res.should.exist
message.should.equal('email_already_registered')
done()
})
this.UserEmailsController.add(this.req, this.res)
})
it("should call the HTTP conflict handler when there's a domain matching error", function(done) {
this.UserUpdater.addEmailAddress.callsArgWith(
3,
new Error('422: Email does not belong to university')
)
this.HttpErrorHandler.conflict = sinon.spy((req, res, message) => {
req.should.exist
res.should.exist
message.should.equal('email_does_not_belong_to_university')
done()
})
this.UserEmailsController.add(this.req, this.res)
})
}) })
describe('remove', function() { describe('remove', function() {