mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
disabled call to spelling service for unsupported languages
GitOrigin-RevId: 329bf8cd95d5800a6850ece2887477348a104b27
This commit is contained in:
parent
23ff7c23e8
commit
762fde5ae7
2 changed files with 121 additions and 17 deletions
|
@ -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,
|
||||
|
|
108
services/web/test/unit/src/Spelling/SpellingControllerTests.js
Normal file
108
services/web/test/unit/src/Spelling/SpellingControllerTests.js
Normal file
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue