From 762fde5ae748ac0e494eca7fde3f08fedcbd18d9 Mon Sep 17 00:00:00 2001 From: mserranom Date: Tue, 16 Jul 2019 11:03:17 +0000 Subject: [PATCH] disabled call to spelling service for unsupported languages GitOrigin-RevId: 329bf8cd95d5800a6850ece2887477348a104b27 --- .../Features/Spelling/SpellingController.js | 30 +++-- .../src/Spelling/SpellingControllerTests.js | 108 ++++++++++++++++++ 2 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 services/web/test/unit/src/Spelling/SpellingControllerTests.js diff --git a/services/web/app/src/Features/Spelling/SpellingController.js b/services/web/app/src/Features/Spelling/SpellingController.js index 3f30ef4b20..11df06cac5 100644 --- a/services/web/app/src/Features/Spelling/SpellingController.js +++ b/services/web/app/src/Features/Spelling/SpellingController.js @@ -1,16 +1,3 @@ -/* eslint-disable - camelcase, - max-len, - no-unused-vars, -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -/* - * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ -let SpellingController const request = require('request') const Settings = require('settings-sharelatex') const logger = require('logger-sharelatex') @@ -18,11 +5,20 @@ const AuthenticationController = require('../Authentication/AuthenticationContro const TEN_SECONDS = 1000 * 10 -module.exports = SpellingController = { - proxyRequestToSpellingApi(req, res, next) { - const user_id = AuthenticationController.getLoggedInUserId(req) +const languageCodeIsSupported = code => + Settings.languages.some(lang => lang.code === code) + +module.exports = { + proxyRequestToSpellingApi(req, res) { + const { language } = req.body + if (language && !languageCodeIsSupported(language)) { + logger.warn(`language_code=${language} not supported`) + return res.status(200).send(JSON.stringify({ misspellings: [] })) + } + + const userId = AuthenticationController.getLoggedInUserId(req) let url = req.url.slice('/spelling'.length) - url = `/user/${user_id}${url}` + url = `/user/${userId}${url}` req.headers['Host'] = Settings.apis.spelling.host return request({ url: Settings.apis.spelling.url + url, diff --git a/services/web/test/unit/src/Spelling/SpellingControllerTests.js b/services/web/test/unit/src/Spelling/SpellingControllerTests.js new file mode 100644 index 0000000000..40d7776f9e --- /dev/null +++ b/services/web/test/unit/src/Spelling/SpellingControllerTests.js @@ -0,0 +1,108 @@ +const SandboxedModule = require('sandboxed-module') +require('chai').should() +const sinon = require('sinon') +const modulePath = require('path').join( + __dirname, + '../../../../app/src/Features/Spelling/SpellingController.js' +) + +const TEN_SECONDS = 1000 * 10 + +const SPELLING_HOST = 'http://spelling.service.test' +const SPELLING_URL = 'http://spelling.service.test' + +describe('SpellingController', function() { + const userId = '123nd3ijdks' + + beforeEach(function() { + this.requestStreamPipe = sinon.stub() + this.requestStreamOn = sinon + .stub() + .returns({ pipe: this.requestStreamPipe }) + this.request = sinon.stub().returns({ + on: this.requestStreamOn + }) + + this.AuthenticationController = { + getLoggedInUserId: req => req.session.user._id + } + this.controller = SandboxedModule.require(modulePath, { + requires: { + request: this.request, + 'logger-sharelatex': { + warn() {}, + err() {} + }, + 'settings-sharelatex': { + languages: [ + { name: 'English', code: 'en' }, + { name: 'French', code: 'fr' } + ], + apis: { spelling: { host: SPELLING_HOST, url: SPELLING_URL } } + }, + '../Authentication/AuthenticationController': this + .AuthenticationController + } + }) + this.req = { + url: '/spelling/check', + method: 'POST', + params: {}, + session: { + user: { + _id: userId + } + }, + headers: { Host: SPELLING_HOST } + } + + this.res = { + send: sinon.stub() + } + this.res.status = sinon.stub().returns(this.res) + this.res.end = sinon.stub() + this.res.json = sinon.stub() + }) + + describe('proxyRequestToSpellingApi', function() { + describe('on successful call', function() { + beforeEach(function() { + this.req.session.user._id = this.userId = 'user-id-123' + this.req.body = { language: 'en', words: ['blab'] } + this.controller.proxyRequestToSpellingApi(this.req, this.res) + }) + + it('should send a request to the spelling host', function() { + this.request + .calledWith({ + url: `${SPELLING_URL}/user/${this.userId}/check`, + method: this.req.method, + headers: this.req.headers, + json: this.req.body, + timeout: TEN_SECONDS + }) + .should.equal(true) + }) + + it('should stream the response to the request', function() { + this.requestStreamPipe.calledWith(this.res).should.equal(true) + }) + + it('should add an error callback to the request', function() { + this.requestStreamOn.calledWith('error').should.equal(true) + }) + }) + + describe('when the requested language is not supported', 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) + }) + }) + }) +})