From a50d76f4ea78f3f9e2ce8edd77acac71f1d34f11 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Fri, 8 Nov 2024 11:48:23 +0000 Subject: [PATCH] Remove web /spelling/check endpoint (#21740) GitOrigin-RevId: f10ebfec08ba1156eafce45078bc783ca934864d --- .../Features/Spelling/SpellingController.mjs | 40 ------ services/web/app/src/router.mjs | 5 - .../src/Spelling/SpellingControllerTests.mjs | 126 ------------------ 3 files changed, 171 deletions(-) delete mode 100644 services/web/test/unit/src/Spelling/SpellingControllerTests.mjs diff --git a/services/web/app/src/Features/Spelling/SpellingController.mjs b/services/web/app/src/Features/Spelling/SpellingController.mjs index aea23920a6..7790193579 100644 --- a/services/web/app/src/Features/Spelling/SpellingController.mjs +++ b/services/web/app/src/Features/Spelling/SpellingController.mjs @@ -1,14 +1,6 @@ -import request from 'request' -import Settings from '@overleaf/settings' -import logger from '@overleaf/logger' import SessionManager from '../Authentication/SessionManager.js' import LearnedWordsManager from './LearnedWordsManager.js' -const TEN_SECONDS = 1000 * 10 - -const languageCodeIsSupported = code => - Settings.languages.some(lang => lang.code === code && lang.server !== false) - export default { learn(req, res, next) { const { word } = req.body @@ -27,36 +19,4 @@ export default { res.sendStatus(204) }) }, - - proxyCheckRequestToSpellingApi(req, res) { - const { language } = req.body - - if (!language) { - logger.error({}, '"language" field should be included for spell checking') - return res.status(422).json({ misspellings: [] }) - } - - if (!languageCodeIsSupported(language)) { - // this log statement can be changed to 'error' once projects with - // unsupported languages are removed from the DB - logger.debug({ language }, 'language not supported') - return res.status(422).json({ misspellings: [] }) - } - - const userId = SessionManager.getLoggedInUserId(req.session) - const url = `${Settings.apis.spelling.url}/user/${userId}/check` - req.headers.Host = Settings.apis.spelling.host - return request({ - url, - method: 'POST', - headers: req.headers, - json: req.body, - timeout: TEN_SECONDS, - }) - .on('error', function (error) { - logger.error({ err: error }, 'Spelling Check API error') - return res.status(500).end() - }) - .pipe(res) - }, } diff --git a/services/web/app/src/router.mjs b/services/web/app/src/router.mjs index b8b6b091f9..64b7aef2e9 100644 --- a/services/web/app/src/router.mjs +++ b/services/web/app/src/router.mjs @@ -1110,11 +1110,6 @@ async function initialize(webRouter, privateApiRouter, publicApiRouter) { TpdsController.deleteProjectContents ) - webRouter.post( - '/spelling/check', - AuthenticationController.requireLogin(), - SpellingController.proxyCheckRequestToSpellingApi - ) webRouter.post( '/spelling/learn', validate({ diff --git a/services/web/test/unit/src/Spelling/SpellingControllerTests.mjs b/services/web/test/unit/src/Spelling/SpellingControllerTests.mjs deleted file mode 100644 index 6e6191984a..0000000000 --- a/services/web/test/unit/src/Spelling/SpellingControllerTests.mjs +++ /dev/null @@ -1,126 +0,0 @@ -import esmock from 'esmock' -import sinon from 'sinon' -import MockResponse from '../helpers/MockResponse.js' -const modulePath = new URL( - '../../../../app/src/Features/Spelling/SpellingController.mjs', - import.meta.url -).pathname - -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(async 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 = await esmock.strict(modulePath, { - '../../../../app/src/Features/Spelling/LearnedWordsManager': {}, - request: this.request, - '@overleaf/settings': { - languages: [ - { name: 'English', code: 'en' }, - { name: 'French', code: 'fr' }, - ], - apis: { spelling: { host: SPELLING_HOST, url: SPELLING_URL } }, - }, - '../../../../app/src/Features/Authentication/AuthenticationController': - this.AuthenticationController, - }) - this.req = { - url: '/spelling/check', - method: 'POST', - params: {}, - session: { - user: { - _id: userId, - }, - }, - headers: { Host: SPELLING_HOST }, - } - - this.res = new MockResponse() - }) - - describe('proxyCheckRequestToSpellingApi', 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.proxyCheckRequestToSpellingApi(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.proxyCheckRequestToSpellingApi(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.json.calledWith({ misspellings: [] }).should.equal(true) - }) - - it('should return a 422 status', 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.proxyCheckRequestToSpellingApi(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.json.calledWith({ misspellings: [] }).should.equal(true) - }) - - it('should return a 422 status', function () { - this.res.status.calledWith(422).should.equal(true) - }) - }) - }) -})