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

server returns 422 on no language indicated

GitOrigin-RevId: e2b0d1bee77b5d7aef5673e9d80f90265b3841e3
mserranom 7 лет назад
Родитель
Сommit
d1011b74c0

+ 9 - 1
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: [] }))
     }
 

+ 23 - 1
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)
+      })
+    })
   })
 })