server returns 422 on no language indicated

GitOrigin-RevId: e2b0d1bee77b5d7aef5673e9d80f90265b3841e3
This commit is contained in:
mserranom 2019-07-17 09:36:37 +00:00 committed by sharelatex
parent 111453c075
commit d1011b74c0
2 changed files with 32 additions and 2 deletions

View file

@ -11,8 +11,16 @@ const languageCodeIsSupported = code =>
module.exports = {
proxyRequestToSpellingApi(req, res) {
const { language } = req.body
if (!language) {
logger.error('"language" field should be included for spell checking')
return res.status(422).send(JSON.stringify({ misspellings: [] }))
}
if (language && !languageCodeIsSupported(language)) {
logger.info({ language }, `language not supported`)
// this log statement can be changed to 'error' once projects with
// unsupported languages are removed from the DB
logger.info({ language }, 'language not supported')
return res.status(422).send(JSON.stringify({ misspellings: [] }))
}

View file

@ -31,7 +31,7 @@ describe('SpellingController', function() {
request: this.request,
'logger-sharelatex': {
warn() {},
err() {},
error() {},
info() {}
},
'settings-sharelatex': {
@ -114,5 +114,27 @@ describe('SpellingController', function() {
this.res.status.calledWith(422).should.equal(true)
})
})
describe('when no language is indicated', function() {
beforeEach(function() {
this.req.session.user._id = this.userId = 'user-id-123'
this.req.body = { words: ['blab'] }
this.controller.proxyRequestToSpellingApi(this.req, this.res)
})
it('should not send a request to the spelling host', function() {
this.request.called.should.equal(false)
})
it('should return an empty misspellings array', function() {
this.res.send
.calledWith(JSON.stringify({ misspellings: [] }))
.should.equal(true)
})
it('should return a 422 status', function() {
this.res.status.calledWith(422).should.equal(true)
})
})
})
})