diff --git a/services/web/app/src/Features/Spelling/SpellingController.js b/services/web/app/src/Features/Spelling/SpellingController.js index c875b25dd8..0ea7dc24c6 100644 --- a/services/web/app/src/Features/Spelling/SpellingController.js +++ b/services/web/app/src/Features/Spelling/SpellingController.js @@ -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: [] })) } diff --git a/services/web/test/unit/src/Spelling/SpellingControllerTests.js b/services/web/test/unit/src/Spelling/SpellingControllerTests.js index ecb1afba1e..0671c5fe25 100644 --- a/services/web/test/unit/src/Spelling/SpellingControllerTests.js +++ b/services/web/test/unit/src/Spelling/SpellingControllerTests.js @@ -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) + }) + }) }) })