From d1011b74c0d7bff00f40b63216ff10b5cd82db1b Mon Sep 17 00:00:00 2001 From: mserranom Date: Wed, 17 Jul 2019 09:36:37 +0000 Subject: [PATCH] server returns 422 on no language indicated GitOrigin-RevId: e2b0d1bee77b5d7aef5673e9d80f90265b3841e3 --- .../Features/Spelling/SpellingController.js | 10 +++++++- .../src/Spelling/SpellingControllerTests.js | 24 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) 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) + }) + }) }) })