| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763 |
- /* eslint-disable
- no-return-assign,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * 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
- */
- import async from 'async'
- import { expect } from 'chai'
- import RealTimeClient from './helpers/RealTimeClient.js'
- import FixturesManager from './helpers/FixturesManager.js'
- import settings from '@overleaf/settings'
- import redis from '@overleaf/redis-wrapper'
- const rclient = redis.createClient(settings.redis.documentupdater)
- const redisSettings = settings.redis
- 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)
- }
- 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: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(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) {
- getPendingUpdatesList((error, ...rest) => {
- if (error) return done(error)
- const [docId] = Array.from(rest[0])
- docId.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) => {
- if (error) return done(error)
- let [update] = Array.from(rest[0])
- update = JSON.parse(update)
- update.op.should.deep.equal(this.update.op)
- update.meta.should.include({
- source: this.client.publicId,
- user_id: this.user_id,
- })
- return done()
- }
- )
- return null
- })
- return after(function (done) {
- return async.series(
- [
- cb => clearPendingUpdatesList(cb),
- 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: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(this.project_id, cb)
- return this.client.on('otUpdateError', otUpdateError => {
- this.otUpdateError = otUpdateError
- })
- },
- 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) => {
- if (error) return done(error)
- 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: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(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) => {
- if (error) return done(error)
- len.should.equal(0)
- return done()
- }
- )
- return null
- })
- })
- 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: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(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) {
- getPendingUpdatesList((error, ...rest) => {
- if (error) return done(error)
- const [docId] = Array.from(rest[0])
- docId.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) => {
- if (error) return done(error)
- let [update] = Array.from(rest[0])
- update = JSON.parse(update)
- update.op.should.deep.equal(this.comment_update.op)
- update.meta.should.include({
- source: this.client.publicId,
- user_id: this.user_id,
- })
- return done()
- }
- )
- return null
- })
- return after(function (done) {
- return async.series(
- [
- cb => clearPendingUpdatesList(cb),
- 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
- )
- })
- })
- describe('when authorized with an edit update to a missing doc field', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'readAndWrite',
- },
- (e, { project_id: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(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
- )
- })
- after(function (done) {
- async.series(
- [
- cb => clearPendingUpdatesList(cb),
- cb =>
- rclient.del(
- redisSettings.documentupdater.key_schema.pendingUpdates({
- doc_id: this.doc_id,
- }),
- cb
- ),
- ],
- done
- )
- })
- it('should not return an error', function () {
- expect(this.error).to.not.exist
- })
- it('should put the update in redis', function (done) {
- rclient.lrange(
- redisSettings.documentupdater.key_schema.pendingUpdates({
- doc_id: this.doc_id,
- }),
- 0,
- -1,
- (error, entries) => {
- if (error) return done(error)
- entries.length.should.equal(1)
- const update = JSON.parse(entries[0])
- update.should.include({ doc: this.doc_id })
- return done()
- }
- )
- return null
- })
- })
- describe('when authorized with an edit update to an invalid doc', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'readOnly',
- },
- (e, { project_id: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(this.project_id, cb)
- },
- cb => {
- return this.client.emit('joinDoc', this.doc_id, cb)
- },
- cb => {
- return this.client.emit(
- 'applyOtUpdate',
- 'invalid-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) => {
- if (error) return done(error)
- len.should.equal(0)
- return done()
- }
- )
- return null
- })
- })
- describe('when authorized with an edit update to a different doc', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'readAndWrite',
- },
- (e, { project_id: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(this.project_id, cb)
- },
- cb => {
- return this.client.emit('joinDoc', this.doc_id, cb)
- },
- cb => {
- return this.client.emit(
- 'applyOtUpdate',
- this.doc_id,
- { doc: 'other-doc' },
- error => {
- this.error = error
- return cb()
- }
- )
- },
- ],
- done
- )
- })
- it('should return an error', function () {
- expect(this.error).to.deep.equal({
- message:
- 'update.doc must be identical to docId parameter in applyOtUpdate(docId, update)',
- })
- })
- it('should not put the update in redis', function (done) {
- rclient.llen(
- redisSettings.documentupdater.key_schema.pendingUpdates({
- doc_id: this.doc_id,
- }),
- (error, len) => {
- if (error) return done(error)
- len.should.equal(0)
- return done()
- }
- )
- return null
- })
- })
- describe('when authorized with an invalid edit update', function () {
- before(function (done) {
- return async.series(
- [
- cb => {
- return FixturesManager.setUpProject(
- {
- privilegeLevel: 'readAndWrite',
- },
- (e, { project_id: projectId, user_id: userId }) => {
- this.project_id = projectId
- this.user_id = userId
- return cb(e)
- }
- )
- },
- cb => {
- return FixturesManager.setUpDoc(
- this.project_id,
- { lines: this.lines, version: this.version, ops: this.ops },
- (e, { doc_id: docId }) => {
- this.doc_id = docId
- return cb(e)
- }
- )
- },
- cb => {
- this.client = RealTimeClient.connect(this.project_id, cb)
- },
- cb => {
- return this.client.emit('joinDoc', this.doc_id, cb)
- },
- cb => {
- return this.client.emit(
- 'applyOtUpdate',
- this.doc_id,
- 'invalid-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) => {
- if (error) return done(error)
- len.should.equal(0)
- return done()
- }
- )
- return null
- })
- })
- })
|