Просмотр исходного кода

disabled call to spelling service for unsupported languages

GitOrigin-RevId: 329bf8cd95d5800a6850ece2887477348a104b27
mserranom 7 лет назад
Родитель
Сommit
762fde5ae7

+ 13 - 17
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 request = require('request')
 const Settings = require('settings-sharelatex')
 const Settings = require('settings-sharelatex')
 const logger = require('logger-sharelatex')
 const logger = require('logger-sharelatex')
@@ -18,11 +5,20 @@ const AuthenticationController = require('../Authentication/AuthenticationContro
 
 
 const TEN_SECONDS = 1000 * 10
 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)
     let url = req.url.slice('/spelling'.length)
-    url = `/user/${user_id}${url}`
+    url = `/user/${userId}${url}`
     req.headers['Host'] = Settings.apis.spelling.host
     req.headers['Host'] = Settings.apis.spelling.host
     return request({
     return request({
       url: Settings.apis.spelling.url + url,
       url: Settings.apis.spelling.url + url,

+ 108 - 0
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)
+      })
+    })
+  })
+})