From 0b938927afee2aec7347c65f645c665b275a9fe3 Mon Sep 17 00:00:00 2001 From: Simon Detheridge Date: Sat, 20 Jul 2019 13:20:35 +0100 Subject: [PATCH] Fix response when learning custom words GitOrigin-RevId: fe7c8a2862532b83e650d3ecfae2ae5245394620 --- .../Features/Spelling/SpellingController.js | 23 +++--- .../src/Spelling/SpellingControllerTests.js | 72 +++++++++++++------ 2 files changed, 65 insertions(+), 30 deletions(-) diff --git a/services/web/app/src/Features/Spelling/SpellingController.js b/services/web/app/src/Features/Spelling/SpellingController.js index b20efe0a71..f488f7d246 100644 --- a/services/web/app/src/Features/Spelling/SpellingController.js +++ b/services/web/app/src/Features/Spelling/SpellingController.js @@ -12,20 +12,23 @@ 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: [] })) - } + let url = req.url.slice('/spelling'.length) - if (!languageCodeIsSupported(language)) { - // 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: [] })) + if (url === '/check') { + if (!language) { + logger.error('"language" field should be included for spell checking') + return res.status(422).send(JSON.stringify({ misspellings: [] })) + } + + if (!languageCodeIsSupported(language)) { + // 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: [] })) + } } const userId = AuthenticationController.getLoggedInUserId(req) - let url = req.url.slice('/spelling'.length) url = `/user/${userId}${url}` req.headers['Host'] = Settings.apis.spelling.host return request({ diff --git a/services/web/test/unit/src/Spelling/SpellingControllerTests.js b/services/web/test/unit/src/Spelling/SpellingControllerTests.js index 0671c5fe25..7c1d0ba219 100644 --- a/services/web/test/unit/src/Spelling/SpellingControllerTests.js +++ b/services/web/test/unit/src/Spelling/SpellingControllerTests.js @@ -97,21 +97,37 @@ describe('SpellingController', function() { beforeEach(function() { this.req.session.user._id = this.userId = 'user-id-123' this.req.body = { language: 'fi', 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) + describe('when the request is a check request', function() { + beforeEach(function() { + 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) + }) }) - it('should return an empty misspellings array', function() { - this.res.send - .calledWith(JSON.stringify({ misspellings: [] })) - .should.equal(true) - }) + describe('when the request is not a check request', function() { + beforeEach(function() { + this.req.url = '/spelling/learn' + this.controller.proxyRequestToSpellingApi(this.req, this.res) + }) - it('should return a 422 status', function() { - this.res.status.calledWith(422).should.equal(true) + it('should send a request to the spelling host', function() { + this.request.called.should.equal(true) + }) }) }) @@ -119,21 +135,37 @@ describe('SpellingController', 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) + describe('when the request is a check request', function() { + beforeEach(function() { + 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) + }) }) - it('should return an empty misspellings array', function() { - this.res.send - .calledWith(JSON.stringify({ misspellings: [] })) - .should.equal(true) - }) + describe('when the request is not a check request', function() { + beforeEach(function() { + this.req.url = '/spelling/learn' + this.controller.proxyRequestToSpellingApi(this.req, this.res) + }) - it('should return a 422 status', function() { - this.res.status.calledWith(422).should.equal(true) + it('should send a request to the spelling host', function() { + this.request.called.should.equal(true) + }) }) }) })