ApplyUpdateTests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /* eslint-disable
  2. camelcase,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS201: Simplify complex destructure assignments
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const async = require('async')
  15. const { expect } = require('chai')
  16. const RealTimeClient = require('./helpers/RealTimeClient')
  17. const FixturesManager = require('./helpers/FixturesManager')
  18. const settings = require('@overleaf/settings')
  19. const redis = require('@overleaf/redis-wrapper')
  20. const rclient = redis.createClient(settings.redis.documentupdater)
  21. const redisSettings = settings.redis
  22. const PENDING_UPDATES_LIST_KEYS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map(n => {
  23. let key = 'pending-updates-list'
  24. if (n !== 0) {
  25. key += `-${n}`
  26. }
  27. return key
  28. })
  29. function getPendingUpdatesList(cb) {
  30. Promise.all(PENDING_UPDATES_LIST_KEYS.map(key => rclient.lrange(key, 0, -1)))
  31. .then(results => {
  32. cb(
  33. null,
  34. results.reduce((acc, more) => {
  35. if (more.length) {
  36. acc.push(...more)
  37. }
  38. return acc
  39. }, [])
  40. )
  41. })
  42. .catch(cb)
  43. }
  44. function clearPendingUpdatesList(cb) {
  45. Promise.all(PENDING_UPDATES_LIST_KEYS.map(key => rclient.del(key)))
  46. .then(() => cb(null))
  47. .catch(cb)
  48. }
  49. describe('applyOtUpdate', function () {
  50. before(function () {
  51. return (this.update = {
  52. op: [{ i: 'foo', p: 42 }],
  53. })
  54. })
  55. describe('when authorized', function () {
  56. before(function (done) {
  57. return async.series(
  58. [
  59. cb => {
  60. return FixturesManager.setUpProject(
  61. {
  62. privilegeLevel: 'readAndWrite',
  63. },
  64. (e, { project_id, user_id }) => {
  65. this.project_id = project_id
  66. this.user_id = user_id
  67. return cb(e)
  68. }
  69. )
  70. },
  71. cb => {
  72. return FixturesManager.setUpDoc(
  73. this.project_id,
  74. { lines: this.lines, version: this.version, ops: this.ops },
  75. (e, { doc_id }) => {
  76. this.doc_id = doc_id
  77. return cb(e)
  78. }
  79. )
  80. },
  81. cb => {
  82. this.client = RealTimeClient.connect()
  83. return this.client.on('connectionAccepted', cb)
  84. },
  85. cb => {
  86. return this.client.emit(
  87. 'joinProject',
  88. { project_id: this.project_id },
  89. cb
  90. )
  91. },
  92. cb => {
  93. return this.client.emit('joinDoc', this.doc_id, cb)
  94. },
  95. cb => {
  96. return this.client.emit(
  97. 'applyOtUpdate',
  98. this.doc_id,
  99. this.update,
  100. cb
  101. )
  102. },
  103. ],
  104. done
  105. )
  106. })
  107. it('should push the doc into the pending updates list', function (done) {
  108. getPendingUpdatesList((error, ...rest) => {
  109. if (error) return done(error)
  110. const [doc_id] = Array.from(rest[0])
  111. doc_id.should.equal(`${this.project_id}:${this.doc_id}`)
  112. return done()
  113. })
  114. return null
  115. })
  116. it('should push the update into redis', function (done) {
  117. rclient.lrange(
  118. redisSettings.documentupdater.key_schema.pendingUpdates({
  119. doc_id: this.doc_id,
  120. }),
  121. 0,
  122. -1,
  123. (error, ...rest) => {
  124. if (error) return done(error)
  125. let [update] = Array.from(rest[0])
  126. update = JSON.parse(update)
  127. update.op.should.deep.equal(this.update.op)
  128. update.meta.should.deep.equal({
  129. source: this.client.publicId,
  130. user_id: this.user_id,
  131. })
  132. return done()
  133. }
  134. )
  135. return null
  136. })
  137. return after(function (done) {
  138. return async.series(
  139. [
  140. cb => clearPendingUpdatesList(cb),
  141. cb =>
  142. rclient.del(
  143. 'DocsWithPendingUpdates',
  144. `${this.project_id}:${this.doc_id}`,
  145. cb
  146. ),
  147. cb =>
  148. rclient.del(
  149. redisSettings.documentupdater.key_schema.pendingUpdates(
  150. this.doc_id
  151. ),
  152. cb
  153. ),
  154. ],
  155. done
  156. )
  157. })
  158. })
  159. describe('when authorized with a huge edit update', function () {
  160. before(function (done) {
  161. this.update = {
  162. op: {
  163. p: 12,
  164. t: 'update is too large'.repeat(1024 * 400), // >7MB
  165. },
  166. }
  167. return async.series(
  168. [
  169. cb => {
  170. return FixturesManager.setUpProject(
  171. {
  172. privilegeLevel: 'readAndWrite',
  173. },
  174. (e, { project_id, user_id }) => {
  175. this.project_id = project_id
  176. this.user_id = user_id
  177. return cb(e)
  178. }
  179. )
  180. },
  181. cb => {
  182. return FixturesManager.setUpDoc(
  183. this.project_id,
  184. { lines: this.lines, version: this.version, ops: this.ops },
  185. (e, { doc_id }) => {
  186. this.doc_id = doc_id
  187. return cb(e)
  188. }
  189. )
  190. },
  191. cb => {
  192. this.client = RealTimeClient.connect()
  193. this.client.on('connectionAccepted', cb)
  194. return this.client.on('otUpdateError', otUpdateError => {
  195. this.otUpdateError = otUpdateError
  196. })
  197. },
  198. cb => {
  199. return this.client.emit(
  200. 'joinProject',
  201. { project_id: this.project_id },
  202. cb
  203. )
  204. },
  205. cb => {
  206. return this.client.emit('joinDoc', this.doc_id, cb)
  207. },
  208. cb => {
  209. return this.client.emit(
  210. 'applyOtUpdate',
  211. this.doc_id,
  212. this.update,
  213. error => {
  214. this.error = error
  215. return cb()
  216. }
  217. )
  218. },
  219. ],
  220. done
  221. )
  222. })
  223. it('should not return an error', function () {
  224. return expect(this.error).to.not.exist
  225. })
  226. it('should send an otUpdateError to the client', function (done) {
  227. return setTimeout(() => {
  228. expect(this.otUpdateError).to.exist
  229. return done()
  230. }, 300)
  231. })
  232. it('should disconnect the client', function (done) {
  233. return setTimeout(() => {
  234. this.client.socket.connected.should.equal(false)
  235. return done()
  236. }, 300)
  237. })
  238. return it('should not put the update in redis', function (done) {
  239. rclient.llen(
  240. redisSettings.documentupdater.key_schema.pendingUpdates({
  241. doc_id: this.doc_id,
  242. }),
  243. (error, len) => {
  244. if (error) return done(error)
  245. len.should.equal(0)
  246. return done()
  247. }
  248. )
  249. return null
  250. })
  251. })
  252. describe('when authorized to read-only with an edit update', function () {
  253. before(function (done) {
  254. return async.series(
  255. [
  256. cb => {
  257. return FixturesManager.setUpProject(
  258. {
  259. privilegeLevel: 'readOnly',
  260. },
  261. (e, { project_id, user_id }) => {
  262. this.project_id = project_id
  263. this.user_id = user_id
  264. return cb(e)
  265. }
  266. )
  267. },
  268. cb => {
  269. return FixturesManager.setUpDoc(
  270. this.project_id,
  271. { lines: this.lines, version: this.version, ops: this.ops },
  272. (e, { doc_id }) => {
  273. this.doc_id = doc_id
  274. return cb(e)
  275. }
  276. )
  277. },
  278. cb => {
  279. this.client = RealTimeClient.connect()
  280. return this.client.on('connectionAccepted', cb)
  281. },
  282. cb => {
  283. return this.client.emit(
  284. 'joinProject',
  285. { project_id: this.project_id },
  286. cb
  287. )
  288. },
  289. cb => {
  290. return this.client.emit('joinDoc', this.doc_id, cb)
  291. },
  292. cb => {
  293. return this.client.emit(
  294. 'applyOtUpdate',
  295. this.doc_id,
  296. this.update,
  297. error => {
  298. this.error = error
  299. return cb()
  300. }
  301. )
  302. },
  303. ],
  304. done
  305. )
  306. })
  307. it('should return an error', function () {
  308. return expect(this.error).to.exist
  309. })
  310. it('should disconnect the client', function (done) {
  311. return setTimeout(() => {
  312. this.client.socket.connected.should.equal(false)
  313. return done()
  314. }, 300)
  315. })
  316. return it('should not put the update in redis', function (done) {
  317. rclient.llen(
  318. redisSettings.documentupdater.key_schema.pendingUpdates({
  319. doc_id: this.doc_id,
  320. }),
  321. (error, len) => {
  322. if (error) return done(error)
  323. len.should.equal(0)
  324. return done()
  325. }
  326. )
  327. return null
  328. })
  329. })
  330. describe('when authorized to read-only with a comment update', function () {
  331. before(function (done) {
  332. this.comment_update = {
  333. op: [{ c: 'foo', p: 42 }],
  334. }
  335. return async.series(
  336. [
  337. cb => {
  338. return FixturesManager.setUpProject(
  339. {
  340. privilegeLevel: 'readOnly',
  341. },
  342. (e, { project_id, user_id }) => {
  343. this.project_id = project_id
  344. this.user_id = user_id
  345. return cb(e)
  346. }
  347. )
  348. },
  349. cb => {
  350. return FixturesManager.setUpDoc(
  351. this.project_id,
  352. { lines: this.lines, version: this.version, ops: this.ops },
  353. (e, { doc_id }) => {
  354. this.doc_id = doc_id
  355. return cb(e)
  356. }
  357. )
  358. },
  359. cb => {
  360. this.client = RealTimeClient.connect()
  361. return this.client.on('connectionAccepted', cb)
  362. },
  363. cb => {
  364. return this.client.emit(
  365. 'joinProject',
  366. { project_id: this.project_id },
  367. cb
  368. )
  369. },
  370. cb => {
  371. return this.client.emit('joinDoc', this.doc_id, cb)
  372. },
  373. cb => {
  374. return this.client.emit(
  375. 'applyOtUpdate',
  376. this.doc_id,
  377. this.comment_update,
  378. cb
  379. )
  380. },
  381. ],
  382. done
  383. )
  384. })
  385. it('should push the doc into the pending updates list', function (done) {
  386. getPendingUpdatesList((error, ...rest) => {
  387. if (error) return done(error)
  388. const [doc_id] = Array.from(rest[0])
  389. doc_id.should.equal(`${this.project_id}:${this.doc_id}`)
  390. return done()
  391. })
  392. return null
  393. })
  394. it('should push the update into redis', function (done) {
  395. rclient.lrange(
  396. redisSettings.documentupdater.key_schema.pendingUpdates({
  397. doc_id: this.doc_id,
  398. }),
  399. 0,
  400. -1,
  401. (error, ...rest) => {
  402. if (error) return done(error)
  403. let [update] = Array.from(rest[0])
  404. update = JSON.parse(update)
  405. update.op.should.deep.equal(this.comment_update.op)
  406. update.meta.should.deep.equal({
  407. source: this.client.publicId,
  408. user_id: this.user_id,
  409. })
  410. return done()
  411. }
  412. )
  413. return null
  414. })
  415. return after(function (done) {
  416. return async.series(
  417. [
  418. cb => clearPendingUpdatesList(cb),
  419. cb =>
  420. rclient.del(
  421. 'DocsWithPendingUpdates',
  422. `${this.project_id}:${this.doc_id}`,
  423. cb
  424. ),
  425. cb =>
  426. rclient.del(
  427. redisSettings.documentupdater.key_schema.pendingUpdates({
  428. doc_id: this.doc_id,
  429. }),
  430. cb
  431. ),
  432. ],
  433. done
  434. )
  435. })
  436. })
  437. describe('when authorized with an edit update to an invalid doc', function () {
  438. before(function (done) {
  439. return async.series(
  440. [
  441. cb => {
  442. return FixturesManager.setUpProject(
  443. {
  444. privilegeLevel: 'readOnly',
  445. },
  446. (e, { project_id, user_id }) => {
  447. this.project_id = project_id
  448. this.user_id = user_id
  449. return cb(e)
  450. }
  451. )
  452. },
  453. cb => {
  454. return FixturesManager.setUpDoc(
  455. this.project_id,
  456. { lines: this.lines, version: this.version, ops: this.ops },
  457. (e, { doc_id }) => {
  458. this.doc_id = doc_id
  459. return cb(e)
  460. }
  461. )
  462. },
  463. cb => {
  464. this.client = RealTimeClient.connect()
  465. return this.client.on('connectionAccepted', cb)
  466. },
  467. cb => {
  468. return this.client.emit(
  469. 'joinProject',
  470. { project_id: this.project_id },
  471. cb
  472. )
  473. },
  474. cb => {
  475. return this.client.emit('joinDoc', this.doc_id, cb)
  476. },
  477. cb => {
  478. return this.client.emit(
  479. 'applyOtUpdate',
  480. 'invalid-doc-id',
  481. this.update,
  482. error => {
  483. this.error = error
  484. return cb()
  485. }
  486. )
  487. },
  488. ],
  489. done
  490. )
  491. })
  492. it('should return an error', function () {
  493. return expect(this.error).to.exist
  494. })
  495. it('should disconnect the client', function (done) {
  496. return setTimeout(() => {
  497. this.client.socket.connected.should.equal(false)
  498. return done()
  499. }, 300)
  500. })
  501. return it('should not put the update in redis', function (done) {
  502. rclient.llen(
  503. redisSettings.documentupdater.key_schema.pendingUpdates({
  504. doc_id: this.doc_id,
  505. }),
  506. (error, len) => {
  507. if (error) return done(error)
  508. len.should.equal(0)
  509. return done()
  510. }
  511. )
  512. return null
  513. })
  514. })
  515. describe('when authorized with an invalid edit update', function () {
  516. before(function (done) {
  517. return async.series(
  518. [
  519. cb => {
  520. return FixturesManager.setUpProject(
  521. {
  522. privilegeLevel: 'readAndWrite',
  523. },
  524. (e, { project_id, user_id }) => {
  525. this.project_id = project_id
  526. this.user_id = user_id
  527. return cb(e)
  528. }
  529. )
  530. },
  531. cb => {
  532. return FixturesManager.setUpDoc(
  533. this.project_id,
  534. { lines: this.lines, version: this.version, ops: this.ops },
  535. (e, { doc_id }) => {
  536. this.doc_id = doc_id
  537. return cb(e)
  538. }
  539. )
  540. },
  541. cb => {
  542. this.client = RealTimeClient.connect()
  543. return this.client.on('connectionAccepted', cb)
  544. },
  545. cb => {
  546. return this.client.emit(
  547. 'joinProject',
  548. { project_id: this.project_id },
  549. cb
  550. )
  551. },
  552. cb => {
  553. return this.client.emit('joinDoc', this.doc_id, cb)
  554. },
  555. cb => {
  556. return this.client.emit(
  557. 'applyOtUpdate',
  558. this.doc_id,
  559. 'invalid-update',
  560. error => {
  561. this.error = error
  562. return cb()
  563. }
  564. )
  565. },
  566. ],
  567. done
  568. )
  569. })
  570. it('should return an error', function () {
  571. return expect(this.error).to.exist
  572. })
  573. it('should disconnect the client', function (done) {
  574. return setTimeout(() => {
  575. this.client.socket.connected.should.equal(false)
  576. return done()
  577. }, 300)
  578. })
  579. return it('should not put the update in redis', function (done) {
  580. rclient.llen(
  581. redisSettings.documentupdater.key_schema.pendingUpdates({
  582. doc_id: this.doc_id,
  583. }),
  584. (error, len) => {
  585. if (error) return done(error)
  586. len.should.equal(0)
  587. return done()
  588. }
  589. )
  590. return null
  591. })
  592. })
  593. })