diff --git a/services/document-updater/app.js b/services/document-updater/app.js index 65c9895377..f425872da5 100644 --- a/services/document-updater/app.js +++ b/services/document-updater/app.js @@ -212,6 +212,8 @@ app.use((error, req, res, next) => { return res.status(422).json(error.info) } else if (error instanceof Errors.FileTooLargeError) { return res.sendStatus(413) + } else if (error instanceof Errors.ProjectMigratedToHistoryOTError) { + return res.status(422).send(error.message) } else if (error.statusCode === 413) { return res.status(413).send('request entity too large') } else { diff --git a/services/document-updater/app/js/Errors.js b/services/document-updater/app/js/Errors.js index a43f69ad35..0416581b30 100644 --- a/services/document-updater/app/js/Errors.js +++ b/services/document-updater/app/js/Errors.js @@ -5,6 +5,7 @@ class OpRangeNotAvailableError extends OError {} class ProjectStateChangedError extends OError {} class DeleteMismatchError extends OError {} class FileTooLargeError extends OError {} +class ProjectMigratedToHistoryOTError extends OError {} module.exports = { NotFoundError, @@ -12,4 +13,5 @@ module.exports = { ProjectStateChangedError, DeleteMismatchError, FileTooLargeError, + ProjectMigratedToHistoryOTError, } diff --git a/services/document-updater/app/js/RedisManager.js b/services/document-updater/app/js/RedisManager.js index f8e97f38b4..c361f34165 100644 --- a/services/document-updater/app/js/RedisManager.js +++ b/services/document-updater/app/js/RedisManager.js @@ -324,6 +324,13 @@ const RedisManager = { } catch (e) { return callback(e) } + if (docLines != null && !Array.isArray(docLines)) { + return callback( + new Errors.ProjectMigratedToHistoryOTError( + 'refusing to process doc that was migrated to history-ot' + ) + ) + } version = parseInt(version || 0, 10) // check doc is in requested project diff --git a/services/document-updater/test/acceptance/js/GettingADocumentTests.js b/services/document-updater/test/acceptance/js/GettingADocumentTests.js index 65298932d9..6f52f49fb9 100644 --- a/services/document-updater/test/acceptance/js/GettingADocumentTests.js +++ b/services/document-updater/test/acceptance/js/GettingADocumentTests.js @@ -13,6 +13,11 @@ const { expect } = require('chai') const MockWebApi = require('./helpers/MockWebApi') const DocUpdaterClient = require('./helpers/DocUpdaterClient') const DocUpdaterApp = require('./helpers/DocUpdaterApp') +const Settings = require('@overleaf/settings') +const docUpdaterRedis = require('@overleaf/redis-wrapper').createClient( + Settings.redis.documentupdater +) +const Keys = Settings.redis.documentupdater.key_schema describe('Getting a document', function () { before(function (done) { @@ -109,6 +114,59 @@ describe('Getting a document', function () { }) }) + describe('when the document is migrated (history-ot)', function () { + before(function (done) { + ;[this.project_id, this.doc_id] = Array.from([ + DocUpdaterClient.randomId(), + DocUpdaterClient.randomId(), + ]) + + MockWebApi.insertDoc(this.project_id, this.doc_id, { + lines: this.lines, + version: this.version, + }) + DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => { + if (error != null) { + throw error + } + sinon.spy(MockWebApi, 'getDocument') + docUpdaterRedis.set( + Keys.docLines({ doc_id: this.doc_id }), + JSON.stringify({ content: this.lines.join('\n') }), + err => { + if (err) return done(err) + + DocUpdaterClient.getDoc( + this.project_id, + this.doc_id, + (error, res, body) => { + if (error) return done(error) + this.res = res + this.body = body + done() + } + ) + } + ) + }) + }) + + after(function () { + MockWebApi.getDocument.restore() + }) + + it('should not load the document from the web API', function () { + MockWebApi.getDocument.called.should.equal(false) + }) + + it('should return an error', function () { + expect(this.res.statusCode).to.equal(422) + expect(this.body).to.equal( + 'refusing to process doc that was migrated to history-ot' + ) + }) + }) + describe('when the request asks for some recent ops', function () { before(function (done) { ;[this.project_id, this.doc_id] = Array.from([