2020-06-23 13:30:34 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:30:29 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS201: Simplify complex destructure assignments
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-06-23 13:30:45 -04:00
|
|
|
const async = require('async')
|
2021-03-18 16:19:31 -04:00
|
|
|
const { expect } = require('chai')
|
2020-06-23 13:30:45 -04:00
|
|
|
|
|
|
|
const RealTimeClient = require('./helpers/RealTimeClient')
|
|
|
|
const FixturesManager = require('./helpers/FixturesManager')
|
|
|
|
|
|
|
|
const settings = require('settings-sharelatex')
|
2020-11-10 06:32:06 -05:00
|
|
|
const redis = require('@overleaf/redis-wrapper')
|
2020-06-23 13:30:45 -04:00
|
|
|
const rclient = redis.createClient(settings.redis.documentupdater)
|
|
|
|
|
|
|
|
const redisSettings = settings.redis
|
|
|
|
|
2021-02-25 18:58:50 -05:00
|
|
|
const PENDING_UPDATES_LIST_KEYS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map((n) => {
|
|
|
|
let key = 'pending-updates-list'
|
|
|
|
if (n !== 0) {
|
|
|
|
key += `-${n}`
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
})
|
|
|
|
|
|
|
|
function getPendingUpdatesList(cb) {
|
|
|
|
Promise.all(
|
|
|
|
PENDING_UPDATES_LIST_KEYS.map((key) => rclient.lrange(key, 0, -1))
|
|
|
|
)
|
|
|
|
.then((results) => {
|
|
|
|
cb(
|
|
|
|
null,
|
|
|
|
results.reduce((acc, more) => {
|
|
|
|
if (more.length) {
|
|
|
|
acc.push(...more)
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, [])
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.catch(cb)
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearPendingUpdatesList(cb) {
|
|
|
|
Promise.all(PENDING_UPDATES_LIST_KEYS.map((key) => rclient.del(key)))
|
|
|
|
.then(() => cb(null))
|
|
|
|
.catch(cb)
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:30:45 -04:00
|
|
|
describe('applyOtUpdate', function () {
|
|
|
|
before(function () {
|
|
|
|
return (this.update = {
|
|
|
|
op: [{ i: 'foo', p: 42 }]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('when authorized', function () {
|
|
|
|
before(function (done) {
|
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'readAndWrite'
|
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
|
|
|
(e, { doc_id }) => {
|
|
|
|
this.doc_id = doc_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
this.client = RealTimeClient.connect()
|
|
|
|
return this.client.on('connectionAccepted', cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'joinProject',
|
|
|
|
{ project_id: this.project_id },
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit('joinDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'applyOtUpdate',
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the doc into the pending updates list', function (done) {
|
2021-02-25 18:58:50 -05:00
|
|
|
getPendingUpdatesList((error, ...rest) => {
|
2020-06-23 13:30:45 -04:00
|
|
|
const [doc_id] = Array.from(rest[0])
|
|
|
|
doc_id.should.equal(`${this.project_id}:${this.doc_id}`)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the update into redis', function (done) {
|
|
|
|
rclient.lrange(
|
|
|
|
redisSettings.documentupdater.key_schema.pendingUpdates({
|
|
|
|
doc_id: this.doc_id
|
|
|
|
}),
|
|
|
|
0,
|
|
|
|
-1,
|
|
|
|
(error, ...rest) => {
|
|
|
|
let [update] = Array.from(rest[0])
|
|
|
|
update = JSON.parse(update)
|
|
|
|
update.op.should.deep.equal(this.update.op)
|
|
|
|
update.meta.should.deep.equal({
|
|
|
|
source: this.client.publicId,
|
|
|
|
user_id: this.user_id
|
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
return after(function (done) {
|
|
|
|
return async.series(
|
|
|
|
[
|
2021-02-25 18:58:50 -05:00
|
|
|
(cb) => clearPendingUpdatesList(cb),
|
2020-06-23 13:30:45 -04:00
|
|
|
(cb) =>
|
|
|
|
rclient.del(
|
|
|
|
'DocsWithPendingUpdates',
|
|
|
|
`${this.project_id}:${this.doc_id}`,
|
|
|
|
cb
|
|
|
|
),
|
|
|
|
(cb) =>
|
|
|
|
rclient.del(
|
|
|
|
redisSettings.documentupdater.key_schema.pendingUpdates(
|
|
|
|
this.doc_id
|
|
|
|
),
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when authorized with a huge edit update', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.update = {
|
|
|
|
op: {
|
|
|
|
p: 12,
|
|
|
|
t: 'update is too large'.repeat(1024 * 400) // >7MB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'readAndWrite'
|
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
|
|
|
(e, { doc_id }) => {
|
|
|
|
this.doc_id = doc_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
this.client = RealTimeClient.connect()
|
|
|
|
this.client.on('connectionAccepted', cb)
|
|
|
|
return this.client.on('otUpdateError', (otUpdateError) => {
|
|
|
|
this.otUpdateError = otUpdateError
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'joinProject',
|
|
|
|
{ project_id: this.project_id },
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit('joinDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'applyOtUpdate',
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
(error) => {
|
|
|
|
this.error = error
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not return an error', function () {
|
|
|
|
return expect(this.error).to.not.exist
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should send an otUpdateError to the client', function (done) {
|
|
|
|
return setTimeout(() => {
|
|
|
|
expect(this.otUpdateError).to.exist
|
|
|
|
return done()
|
|
|
|
}, 300)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should disconnect the client', function (done) {
|
|
|
|
return setTimeout(() => {
|
|
|
|
this.client.socket.connected.should.equal(false)
|
|
|
|
return done()
|
|
|
|
}, 300)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should not put the update in redis', function (done) {
|
|
|
|
rclient.llen(
|
|
|
|
redisSettings.documentupdater.key_schema.pendingUpdates({
|
|
|
|
doc_id: this.doc_id
|
|
|
|
}),
|
|
|
|
(error, len) => {
|
|
|
|
len.should.equal(0)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when authorized to read-only with an edit update', function () {
|
|
|
|
before(function (done) {
|
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'readOnly'
|
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
|
|
|
(e, { doc_id }) => {
|
|
|
|
this.doc_id = doc_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
this.client = RealTimeClient.connect()
|
|
|
|
return this.client.on('connectionAccepted', cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'joinProject',
|
|
|
|
{ project_id: this.project_id },
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit('joinDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'applyOtUpdate',
|
|
|
|
this.doc_id,
|
|
|
|
this.update,
|
|
|
|
(error) => {
|
|
|
|
this.error = error
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return an error', function () {
|
|
|
|
return expect(this.error).to.exist
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should disconnect the client', function (done) {
|
|
|
|
return setTimeout(() => {
|
|
|
|
this.client.socket.connected.should.equal(false)
|
|
|
|
return done()
|
|
|
|
}, 300)
|
|
|
|
})
|
|
|
|
|
|
|
|
return it('should not put the update in redis', function (done) {
|
|
|
|
rclient.llen(
|
|
|
|
redisSettings.documentupdater.key_schema.pendingUpdates({
|
|
|
|
doc_id: this.doc_id
|
|
|
|
}),
|
|
|
|
(error, len) => {
|
|
|
|
len.should.equal(0)
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return describe('when authorized to read-only with a comment update', function () {
|
|
|
|
before(function (done) {
|
|
|
|
this.comment_update = {
|
|
|
|
op: [{ c: 'foo', p: 42 }]
|
|
|
|
}
|
|
|
|
return async.series(
|
|
|
|
[
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpProject(
|
|
|
|
{
|
|
|
|
privilegeLevel: 'readOnly'
|
|
|
|
},
|
|
|
|
(e, { project_id, user_id }) => {
|
|
|
|
this.project_id = project_id
|
|
|
|
this.user_id = user_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return FixturesManager.setUpDoc(
|
|
|
|
this.project_id,
|
|
|
|
{ lines: this.lines, version: this.version, ops: this.ops },
|
|
|
|
(e, { doc_id }) => {
|
|
|
|
this.doc_id = doc_id
|
|
|
|
return cb(e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
this.client = RealTimeClient.connect()
|
|
|
|
return this.client.on('connectionAccepted', cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'joinProject',
|
|
|
|
{ project_id: this.project_id },
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit('joinDoc', this.doc_id, cb)
|
|
|
|
},
|
|
|
|
|
|
|
|
(cb) => {
|
|
|
|
return this.client.emit(
|
|
|
|
'applyOtUpdate',
|
|
|
|
this.doc_id,
|
|
|
|
this.comment_update,
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the doc into the pending updates list', function (done) {
|
2021-02-25 18:58:50 -05:00
|
|
|
getPendingUpdatesList((error, ...rest) => {
|
2020-06-23 13:30:45 -04:00
|
|
|
const [doc_id] = Array.from(rest[0])
|
|
|
|
doc_id.should.equal(`${this.project_id}:${this.doc_id}`)
|
|
|
|
return done()
|
|
|
|
})
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should push the update into redis', function (done) {
|
|
|
|
rclient.lrange(
|
|
|
|
redisSettings.documentupdater.key_schema.pendingUpdates({
|
|
|
|
doc_id: this.doc_id
|
|
|
|
}),
|
|
|
|
0,
|
|
|
|
-1,
|
|
|
|
(error, ...rest) => {
|
|
|
|
let [update] = Array.from(rest[0])
|
|
|
|
update = JSON.parse(update)
|
|
|
|
update.op.should.deep.equal(this.comment_update.op)
|
|
|
|
update.meta.should.deep.equal({
|
|
|
|
source: this.client.publicId,
|
|
|
|
user_id: this.user_id
|
|
|
|
})
|
|
|
|
return done()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
|
|
|
return after(function (done) {
|
|
|
|
return async.series(
|
|
|
|
[
|
2021-02-25 18:58:50 -05:00
|
|
|
(cb) => clearPendingUpdatesList(cb),
|
2020-06-23 13:30:45 -04:00
|
|
|
(cb) =>
|
|
|
|
rclient.del(
|
|
|
|
'DocsWithPendingUpdates',
|
|
|
|
`${this.project_id}:${this.doc_id}`,
|
|
|
|
cb
|
|
|
|
),
|
|
|
|
(cb) =>
|
|
|
|
rclient.del(
|
|
|
|
redisSettings.documentupdater.key_schema.pendingUpdates({
|
|
|
|
doc_id: this.doc_id
|
|
|
|
}),
|
|
|
|
cb
|
|
|
|
)
|
|
|
|
],
|
|
|
|
done
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|