2020-06-23 13:30:03 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:29:59 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:30:16 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const path = require('path')
|
|
|
|
const modulePath = '../../../app/js/DocumentUpdaterManager'
|
2021-02-08 07:19:25 -05:00
|
|
|
const _ = require('underscore')
|
2020-06-23 13:30:16 -04:00
|
|
|
|
|
|
|
describe('DocumentUpdaterManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
let Timer
|
|
|
|
this.project_id = 'project-id-923'
|
|
|
|
this.doc_id = 'doc-id-394'
|
|
|
|
this.lines = ['one', 'two', 'three']
|
|
|
|
this.version = 42
|
|
|
|
this.settings = {
|
|
|
|
apis: { documentupdater: { url: 'http://doc-updater.example.com' } },
|
|
|
|
redis: {
|
|
|
|
documentupdater: {
|
|
|
|
key_schema: {
|
|
|
|
pendingUpdates({ doc_id }) {
|
|
|
|
return `PendingUpdates:${doc_id}`
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-23 13:30:16 -04:00
|
|
|
},
|
2021-02-08 07:19:25 -05:00
|
|
|
maxUpdateSize: 7 * 1024 * 1024,
|
2021-07-13 07:04:45 -04:00
|
|
|
pendingUpdateListShardCount: 10,
|
2020-06-23 13:30:16 -04:00
|
|
|
}
|
|
|
|
this.rclient = { auth() {} }
|
|
|
|
|
|
|
|
return (this.DocumentUpdaterManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-12 12:47:18 -04:00
|
|
|
'@overleaf/settings': this.settings,
|
2020-06-23 13:30:16 -04:00
|
|
|
request: (this.request = {}),
|
2020-11-10 06:32:06 -05:00
|
|
|
'@overleaf/redis-wrapper': { createClient: () => this.rclient },
|
2020-11-25 06:57:22 -05:00
|
|
|
'@overleaf/metrics': (this.Metrics = {
|
2020-06-23 13:30:16 -04:00
|
|
|
summary: sinon.stub(),
|
|
|
|
Timer: (Timer = class Timer {
|
|
|
|
done() {}
|
2021-07-13 07:04:45 -04:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
},
|
2020-06-23 13:30:16 -04:00
|
|
|
}))
|
|
|
|
}) // avoid modifying JSON object directly
|
|
|
|
|
|
|
|
describe('getDocument', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.body = JSON.stringify({
|
|
|
|
lines: this.lines,
|
|
|
|
version: this.version,
|
|
|
|
ops: (this.ops = ['mock-op-1', 'mock-op-2']),
|
2021-07-13 07:04:45 -04:00
|
|
|
ranges: (this.ranges = { mock: 'ranges' }),
|
2020-06-23 13:30:16 -04:00
|
|
|
})
|
|
|
|
this.fromVersion = 2
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 200 }, this.body)
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should get the document from the document updater', function () {
|
|
|
|
const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}?fromVersion=${this.fromVersion}`
|
|
|
|
return this.request.get.calledWith(url).should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should call the callback with the lines, version, ranges and ops', function () {
|
|
|
|
return this.callback
|
|
|
|
.calledWith(null, this.lines, this.version, this.ranges, this.ops)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the document updater API returns an error', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
(this.error = new Error('something went wrong')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return an error to the callback', function () {
|
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 07:04:45 -04:00
|
|
|
;[404, 422].forEach(statusCode =>
|
2020-06-23 13:30:16 -04:00
|
|
|
describe(`when the document updater returns a ${statusCode} status code`, function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode }, '')
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the callback with an error', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-08-20 07:59:52 -04:00
|
|
|
this.callback
|
|
|
|
.calledWith(
|
|
|
|
sinon.match({
|
|
|
|
message: 'doc updater could not load requested ops',
|
2021-07-13 07:04:45 -04:00
|
|
|
info: { statusCode },
|
2020-08-20 07:59:52 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-06-23 13:30:16 -04:00
|
|
|
this.logger.error.called.should.equal(false)
|
2020-08-20 07:59:52 -04:00
|
|
|
this.logger.warn.called.should.equal(false)
|
2020-06-23 13:30:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return describe('when the document updater returns a failure error code', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.get = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 500 }, '')
|
|
|
|
return this.DocumentUpdaterManager.getDocument(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.fromVersion,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the callback with an error', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-08-20 07:52:59 -04:00
|
|
|
this.callback
|
|
|
|
.calledWith(
|
|
|
|
sinon.match({
|
|
|
|
message: 'doc updater returned a non-success status code',
|
|
|
|
info: {
|
|
|
|
action: 'getDocument',
|
2021-07-13 07:04:45 -04:00
|
|
|
statusCode: 500,
|
|
|
|
},
|
2020-08-20 07:52:59 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
this.logger.error.called.should.equal(false)
|
2020-06-23 13:30:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('flushProjectToMongoAndDelete', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.del = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 204 }, '')
|
|
|
|
return this.DocumentUpdaterManager.flushProjectToMongoAndDelete(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should delete the project from the document updater', function () {
|
|
|
|
const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}?background=true`
|
|
|
|
return this.request.del.calledWith(url).should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should call the callback with no error', function () {
|
|
|
|
return this.callback.calledWith(null).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the document updater API returns an error', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.del = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(
|
|
|
|
1,
|
|
|
|
(this.error = new Error('something went wrong')),
|
|
|
|
null,
|
|
|
|
null
|
|
|
|
)
|
|
|
|
return this.DocumentUpdaterManager.flushProjectToMongoAndDelete(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return an error to the callback', function () {
|
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when the document updater returns a failure error code', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.request.del = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, { statusCode: 500 }, '')
|
|
|
|
return this.DocumentUpdaterManager.flushProjectToMongoAndDelete(
|
|
|
|
this.project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return the callback with an error', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-08-20 07:52:59 -04:00
|
|
|
this.callback
|
|
|
|
.calledWith(
|
|
|
|
sinon.match({
|
|
|
|
message: 'doc updater returned a non-success status code',
|
|
|
|
info: {
|
|
|
|
action: 'flushProjectToMongoAndDelete',
|
2021-07-13 07:04:45 -04:00
|
|
|
statusCode: 500,
|
|
|
|
},
|
2020-08-20 07:52:59 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-06-23 13:30:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-08 07:19:25 -05:00
|
|
|
describe('queueChange', function () {
|
2020-06-23 13:30:16 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.change = {
|
|
|
|
doc: '1234567890',
|
|
|
|
op: [{ d: 'test', p: 345 }],
|
2021-07-13 07:04:45 -04:00
|
|
|
v: 789,
|
2020-06-23 13:30:16 -04:00
|
|
|
}
|
|
|
|
this.rclient.rpush = sinon.stub().yields()
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2021-02-08 07:19:25 -05:00
|
|
|
this.pendingUpdateListKey = `pending-updates-list-key-${Math.random()}`
|
|
|
|
|
|
|
|
this.DocumentUpdaterManager._getPendingUpdateListKey = sinon
|
|
|
|
.stub()
|
|
|
|
.returns(this.pendingUpdateListKey)
|
|
|
|
this.DocumentUpdaterManager.queueChange(
|
2020-06-23 13:30:16 -04:00
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.change,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the change', function () {
|
2021-02-08 07:19:25 -05:00
|
|
|
this.rclient.rpush
|
2020-06-23 13:30:16 -04:00
|
|
|
.calledWith(
|
|
|
|
`PendingUpdates:${this.doc_id}`,
|
|
|
|
JSON.stringify(this.change)
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-02-08 07:19:25 -05:00
|
|
|
it('should notify the doc updater of the change via the pending-updates-list queue', function () {
|
|
|
|
this.rclient.rpush
|
2020-06-23 13:30:16 -04:00
|
|
|
.calledWith(
|
2021-02-08 07:19:25 -05:00
|
|
|
this.pendingUpdateListKey,
|
2020-06-23 13:30:16 -04:00
|
|
|
`${this.project_id}:${this.doc_id}`
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with error talking to redis during rpush', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.rclient.rpush = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(new Error('something went wrong'))
|
|
|
|
return this.DocumentUpdaterManager.queueChange(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.change,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should return an error', function () {
|
|
|
|
return this.callback
|
|
|
|
.calledWithExactly(sinon.match(Error))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with null byte corruption', function () {
|
|
|
|
beforeEach(function () {
|
2021-03-18 16:19:31 -04:00
|
|
|
this.stringifyStub = sinon
|
|
|
|
.stub(JSON, 'stringify')
|
|
|
|
.callsFake(() => '["bad bytes! \u0000 <- here"]')
|
2020-06-23 13:30:16 -04:00
|
|
|
return this.DocumentUpdaterManager.queueChange(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.change,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-03-18 16:19:31 -04:00
|
|
|
afterEach(function () {
|
|
|
|
this.stringifyStub.restore()
|
|
|
|
})
|
|
|
|
|
2020-06-23 13:30:16 -04:00
|
|
|
it('should return an error', function () {
|
|
|
|
return this.callback
|
|
|
|
.calledWithExactly(sinon.match(Error))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should not push the change onto the pending-updates-list queue', function () {
|
|
|
|
return this.rclient.rpush.called.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when the update is too large', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.change = {
|
2021-07-13 07:04:45 -04:00
|
|
|
op: { p: 12, t: 'update is too large'.repeat(1024 * 400) },
|
2020-06-23 13:30:16 -04:00
|
|
|
}
|
|
|
|
return this.DocumentUpdaterManager.queueChange(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.change,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return an error', function () {
|
|
|
|
return this.callback
|
|
|
|
.calledWithExactly(sinon.match(Error))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add the size to the error', function () {
|
2020-08-20 06:16:26 -04:00
|
|
|
return this.callback.args[0][0].info.updateSize.should.equal(7782422)
|
2020-06-23 13:30:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return it('should not push the change onto the pending-updates-list queue', function () {
|
|
|
|
return this.rclient.rpush.called.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-02-08 07:19:25 -05:00
|
|
|
describe('with invalid keys', function () {
|
2020-06-23 13:30:16 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.change = {
|
|
|
|
op: [{ d: 'test', p: 345 }],
|
2021-07-13 07:04:45 -04:00
|
|
|
version: 789, // not a valid key
|
2020-06-23 13:30:16 -04:00
|
|
|
}
|
|
|
|
return this.DocumentUpdaterManager.queueChange(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
this.change,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-02-08 07:19:25 -05:00
|
|
|
it('should remove the invalid keys from the change', function () {
|
2020-06-23 13:30:16 -04:00
|
|
|
return this.rclient.rpush
|
|
|
|
.calledWith(
|
|
|
|
`PendingUpdates:${this.doc_id}`,
|
|
|
|
JSON.stringify({ op: this.change.op })
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-02-08 07:19:25 -05:00
|
|
|
|
|
|
|
describe('_getPendingUpdateListKey', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
const keys = _.times(
|
|
|
|
10000,
|
|
|
|
this.DocumentUpdaterManager._getPendingUpdateListKey
|
|
|
|
)
|
|
|
|
this.keys = _.unique(keys)
|
|
|
|
})
|
|
|
|
it('should return normal pending updates key', function () {
|
|
|
|
_.contains(this.keys, 'pending-updates-list').should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return pending-updates-list-n keys', function () {
|
|
|
|
_.contains(this.keys, 'pending-updates-list-1').should.equal(true)
|
|
|
|
_.contains(this.keys, 'pending-updates-list-3').should.equal(true)
|
2021-02-09 05:48:40 -05:00
|
|
|
_.contains(this.keys, 'pending-updates-list-9').should.equal(true)
|
2021-02-08 07:19:25 -05:00
|
|
|
})
|
2021-02-09 05:48:40 -05:00
|
|
|
|
2021-02-08 07:19:25 -05:00
|
|
|
it('should not include pending-updates-list-0 key', function () {
|
|
|
|
_.contains(this.keys, 'pending-updates-list-0').should.equal(false)
|
|
|
|
})
|
2021-02-09 05:48:40 -05:00
|
|
|
|
|
|
|
it('should not include maximum as pendingUpdateListShardCount value', function () {
|
|
|
|
_.contains(this.keys, 'pending-updates-list-10').should.equal(false)
|
|
|
|
})
|
2021-02-08 07:19:25 -05:00
|
|
|
})
|
2020-06-23 13:30:16 -04:00
|
|
|
})
|