SettingADocumentTests.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const Settings = require('@overleaf/settings')
  4. const docUpdaterRedis = require('@overleaf/redis-wrapper').createClient(
  5. Settings.redis.documentupdater
  6. )
  7. const Keys = Settings.redis.documentupdater.key_schema
  8. const MockProjectHistoryApi = require('./helpers/MockProjectHistoryApi')
  9. const MockWebApi = require('./helpers/MockWebApi')
  10. const DocUpdaterClient = require('./helpers/DocUpdaterClient')
  11. const DocUpdaterApp = require('./helpers/DocUpdaterApp')
  12. describe('Setting a document', function () {
  13. let numberOfReceivedUpdates = 0
  14. before(function (done) {
  15. DocUpdaterClient.subscribeToAppliedOps(() => {
  16. numberOfReceivedUpdates++
  17. })
  18. this.lines = ['one', 'two', 'three']
  19. this.version = 42
  20. this.update = {
  21. doc: this.doc_id,
  22. op: [
  23. {
  24. i: 'one and a half\n',
  25. p: 4,
  26. },
  27. ],
  28. v: this.version,
  29. }
  30. this.result = ['one', 'one and a half', 'two', 'three']
  31. this.newLines = ['these', 'are', 'the', 'new', 'lines']
  32. this.source = 'dropbox'
  33. this.user_id = 'user-id-123'
  34. sinon.spy(MockProjectHistoryApi, 'flushProject')
  35. sinon.spy(MockWebApi, 'setDocument')
  36. DocUpdaterApp.ensureRunning(done)
  37. })
  38. after(function () {
  39. MockProjectHistoryApi.flushProject.restore()
  40. MockWebApi.setDocument.restore()
  41. })
  42. describe('when the updated doc exists in the doc updater', function () {
  43. before(function (done) {
  44. numberOfReceivedUpdates = 0
  45. this.project_id = DocUpdaterClient.randomId()
  46. this.doc_id = DocUpdaterClient.randomId()
  47. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  48. lines: this.lines,
  49. version: this.version,
  50. })
  51. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  52. if (error) {
  53. throw error
  54. }
  55. DocUpdaterClient.sendUpdate(
  56. this.project_id,
  57. this.doc_id,
  58. this.update,
  59. error => {
  60. if (error) {
  61. throw error
  62. }
  63. setTimeout(() => {
  64. DocUpdaterClient.setDocLines(
  65. this.project_id,
  66. this.doc_id,
  67. this.newLines,
  68. this.source,
  69. this.user_id,
  70. false,
  71. (error, res, body) => {
  72. if (error) {
  73. return done(error)
  74. }
  75. this.statusCode = res.statusCode
  76. this.body = body
  77. done()
  78. }
  79. )
  80. }, 200)
  81. }
  82. )
  83. })
  84. })
  85. after(function () {
  86. MockProjectHistoryApi.flushProject.resetHistory()
  87. MockWebApi.setDocument.resetHistory()
  88. })
  89. it('should return a 200 status code', function () {
  90. this.statusCode.should.equal(200)
  91. })
  92. it('should emit two updates (from sendUpdate and setDocLines)', function () {
  93. expect(numberOfReceivedUpdates).to.equal(2)
  94. })
  95. it('should send the updated doc lines and version to the web api', function () {
  96. MockWebApi.setDocument
  97. .calledWith(this.project_id, this.doc_id, this.newLines)
  98. .should.equal(true)
  99. })
  100. it('should update the lines in the doc updater', function (done) {
  101. DocUpdaterClient.getDoc(
  102. this.project_id,
  103. this.doc_id,
  104. (error, res, doc) => {
  105. if (error) {
  106. return done(error)
  107. }
  108. doc.lines.should.deep.equal(this.newLines)
  109. done()
  110. }
  111. )
  112. })
  113. it('should bump the version in the doc updater', function (done) {
  114. DocUpdaterClient.getDoc(
  115. this.project_id,
  116. this.doc_id,
  117. (error, res, doc) => {
  118. if (error) {
  119. return done(error)
  120. }
  121. doc.version.should.equal(this.version + 2)
  122. done()
  123. }
  124. )
  125. })
  126. it('should leave the document in redis', function (done) {
  127. docUpdaterRedis.get(
  128. Keys.docLines({ doc_id: this.doc_id }),
  129. (error, lines) => {
  130. if (error) {
  131. throw error
  132. }
  133. expect(JSON.parse(lines)).to.deep.equal(this.newLines)
  134. done()
  135. }
  136. )
  137. })
  138. it('should return the mongo rev in the json response', function () {
  139. this.body.should.deep.equal({ rev: '123' })
  140. })
  141. describe('when doc has the same contents', function () {
  142. beforeEach(function (done) {
  143. numberOfReceivedUpdates = 0
  144. DocUpdaterClient.setDocLines(
  145. this.project_id,
  146. this.doc_id,
  147. this.newLines,
  148. this.source,
  149. this.user_id,
  150. false,
  151. (error, res, body) => {
  152. if (error) {
  153. return done(error)
  154. }
  155. this.statusCode = res.statusCode
  156. this.body = body
  157. done()
  158. }
  159. )
  160. })
  161. it('should not bump the version in doc updater', function (done) {
  162. DocUpdaterClient.getDoc(
  163. this.project_id,
  164. this.doc_id,
  165. (error, res, doc) => {
  166. if (error) {
  167. return done(error)
  168. }
  169. doc.version.should.equal(this.version + 2)
  170. done()
  171. }
  172. )
  173. })
  174. it('should not emit any updates', function (done) {
  175. setTimeout(() => {
  176. expect(numberOfReceivedUpdates).to.equal(0)
  177. done()
  178. }, 100) // delay by 100ms: make sure we do not check too early!
  179. })
  180. })
  181. })
  182. describe('when the updated doc exists in the doc updater (history-ot)', function () {
  183. before(function (done) {
  184. numberOfReceivedUpdates = 0
  185. this.project_id = DocUpdaterClient.randomId()
  186. this.doc_id = DocUpdaterClient.randomId()
  187. this.historyOTUpdate = {
  188. doc: this.doc_id,
  189. op: [{ textOperation: [4, 'one and a half\n', 9] }],
  190. v: this.version,
  191. meta: { source: 'random-publicId' },
  192. }
  193. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  194. lines: this.lines,
  195. version: this.version,
  196. otMigrationStage: 1,
  197. })
  198. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  199. if (error) {
  200. throw error
  201. }
  202. DocUpdaterClient.sendUpdate(
  203. this.project_id,
  204. this.doc_id,
  205. this.historyOTUpdate,
  206. error => {
  207. if (error) {
  208. throw error
  209. }
  210. setTimeout(() => {
  211. DocUpdaterClient.setDocLines(
  212. this.project_id,
  213. this.doc_id,
  214. this.newLines,
  215. this.source,
  216. this.user_id,
  217. false,
  218. (error, res, body) => {
  219. if (error) {
  220. return done(error)
  221. }
  222. this.statusCode = res.statusCode
  223. this.body = body
  224. done()
  225. }
  226. )
  227. }, 200)
  228. }
  229. )
  230. })
  231. })
  232. after(function () {
  233. MockProjectHistoryApi.flushProject.resetHistory()
  234. MockWebApi.setDocument.resetHistory()
  235. })
  236. it('should return a 200 status code', function () {
  237. this.statusCode.should.equal(200)
  238. })
  239. it('should emit two updates (from sendUpdate and setDocLines)', function () {
  240. expect(numberOfReceivedUpdates).to.equal(2)
  241. })
  242. it('should send the updated doc lines and version to the web api', function () {
  243. MockWebApi.setDocument
  244. .calledWith(this.project_id, this.doc_id, this.newLines)
  245. .should.equal(true)
  246. })
  247. it('should update the lines in the doc updater', function (done) {
  248. DocUpdaterClient.getDoc(
  249. this.project_id,
  250. this.doc_id,
  251. (error, res, doc) => {
  252. if (error) {
  253. return done(error)
  254. }
  255. doc.lines.should.deep.equal(this.newLines)
  256. done()
  257. }
  258. )
  259. })
  260. it('should bump the version in the doc updater', function (done) {
  261. DocUpdaterClient.getDoc(
  262. this.project_id,
  263. this.doc_id,
  264. (error, res, doc) => {
  265. if (error) {
  266. return done(error)
  267. }
  268. doc.version.should.equal(this.version + 2)
  269. done()
  270. }
  271. )
  272. })
  273. it('should leave the document in redis', function (done) {
  274. docUpdaterRedis.get(
  275. Keys.docLines({ doc_id: this.doc_id }),
  276. (error, lines) => {
  277. if (error) {
  278. throw error
  279. }
  280. expect(JSON.parse(lines)).to.deep.equal({
  281. content: this.newLines.join('\n'),
  282. })
  283. done()
  284. }
  285. )
  286. })
  287. it('should return the mongo rev in the json response', function () {
  288. this.body.should.deep.equal({ rev: '123' })
  289. })
  290. describe('when doc has the same contents', function () {
  291. beforeEach(function (done) {
  292. numberOfReceivedUpdates = 0
  293. DocUpdaterClient.setDocLines(
  294. this.project_id,
  295. this.doc_id,
  296. this.newLines,
  297. this.source,
  298. this.user_id,
  299. false,
  300. (error, res, body) => {
  301. if (error) {
  302. return done(error)
  303. }
  304. this.statusCode = res.statusCode
  305. this.body = body
  306. done()
  307. }
  308. )
  309. })
  310. it('should not bump the version in doc updater', function (done) {
  311. DocUpdaterClient.getDoc(
  312. this.project_id,
  313. this.doc_id,
  314. (error, res, doc) => {
  315. if (error) {
  316. return done(error)
  317. }
  318. doc.version.should.equal(this.version + 2)
  319. done()
  320. }
  321. )
  322. })
  323. it('should not emit any updates', function (done) {
  324. setTimeout(() => {
  325. expect(numberOfReceivedUpdates).to.equal(0)
  326. done()
  327. }, 100) // delay by 100ms: make sure we do not check too early!
  328. })
  329. })
  330. })
  331. describe('when the updated doc does not exist in the doc updater', function () {
  332. before(function (done) {
  333. this.project_id = DocUpdaterClient.randomId()
  334. this.doc_id = DocUpdaterClient.randomId()
  335. numberOfReceivedUpdates = 0
  336. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  337. lines: this.lines,
  338. version: this.version,
  339. })
  340. DocUpdaterClient.setDocLines(
  341. this.project_id,
  342. this.doc_id,
  343. this.newLines,
  344. this.source,
  345. this.user_id,
  346. false,
  347. (error, res, body) => {
  348. if (error) {
  349. return done(error)
  350. }
  351. this.statusCode = res.statusCode
  352. this.body = body
  353. setTimeout(done, 200)
  354. }
  355. )
  356. })
  357. after(function () {
  358. MockProjectHistoryApi.flushProject.resetHistory()
  359. MockWebApi.setDocument.resetHistory()
  360. })
  361. it('should return a 200 status code', function () {
  362. this.statusCode.should.equal(200)
  363. })
  364. it('should emit an update', function () {
  365. expect(numberOfReceivedUpdates).to.equal(1)
  366. })
  367. it('should send the updated doc lines to the web api', function () {
  368. MockWebApi.setDocument
  369. .calledWith(this.project_id, this.doc_id, this.newLines)
  370. .should.equal(true)
  371. })
  372. it('should flush project history', function () {
  373. MockProjectHistoryApi.flushProject
  374. .calledWith(this.project_id)
  375. .should.equal(true)
  376. })
  377. it('should remove the document from redis', function (done) {
  378. docUpdaterRedis.get(
  379. Keys.docLines({ doc_id: this.doc_id }),
  380. (error, lines) => {
  381. if (error) {
  382. throw error
  383. }
  384. expect(lines).to.not.exist
  385. done()
  386. }
  387. )
  388. })
  389. it('should return the mongo rev in the json response', function () {
  390. this.body.should.deep.equal({ rev: '123' })
  391. })
  392. })
  393. const DOC_TOO_LARGE_TEST_CASES = [
  394. {
  395. desc: 'when the updated doc is too large for the body parser',
  396. size: Settings.maxJsonRequestSize,
  397. expectedStatusCode: 413,
  398. },
  399. {
  400. desc: 'when the updated doc is larger than the HTTP controller limit',
  401. size: Settings.max_doc_length,
  402. expectedStatusCode: 406,
  403. },
  404. ]
  405. DOC_TOO_LARGE_TEST_CASES.forEach(testCase => {
  406. describe(testCase.desc, function () {
  407. before(function (done) {
  408. this.project_id = DocUpdaterClient.randomId()
  409. this.doc_id = DocUpdaterClient.randomId()
  410. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  411. lines: this.lines,
  412. version: this.version,
  413. })
  414. this.newLines = []
  415. while (JSON.stringify(this.newLines).length <= testCase.size) {
  416. this.newLines.push('(a long line of text)'.repeat(10000))
  417. }
  418. DocUpdaterClient.setDocLines(
  419. this.project_id,
  420. this.doc_id,
  421. this.newLines,
  422. this.source,
  423. this.user_id,
  424. false,
  425. (error, res, body) => {
  426. if (error) {
  427. return done(error)
  428. }
  429. this.statusCode = res.statusCode
  430. setTimeout(done, 200)
  431. }
  432. )
  433. })
  434. after(function () {
  435. MockProjectHistoryApi.flushProject.resetHistory()
  436. MockWebApi.setDocument.resetHistory()
  437. })
  438. it(`should return a ${testCase.expectedStatusCode} status code`, function () {
  439. this.statusCode.should.equal(testCase.expectedStatusCode)
  440. })
  441. it('should not send the updated doc lines to the web api', function () {
  442. MockWebApi.setDocument.called.should.equal(false)
  443. })
  444. it('should not flush project history', function () {
  445. MockProjectHistoryApi.flushProject.called.should.equal(false)
  446. })
  447. })
  448. })
  449. describe('when the updated doc is large but under the bodyParser and HTTPController size limit', function () {
  450. before(function (done) {
  451. this.project_id = DocUpdaterClient.randomId()
  452. this.doc_id = DocUpdaterClient.randomId()
  453. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  454. lines: this.lines,
  455. version: this.version,
  456. })
  457. this.newLines = []
  458. while (JSON.stringify(this.newLines).length < 2 * 1024 * 1024) {
  459. // limit in HTTPController
  460. this.newLines.push('(a long line of text)'.repeat(10000))
  461. }
  462. this.newLines.pop() // remove the line which took it over the limit
  463. DocUpdaterClient.setDocLines(
  464. this.project_id,
  465. this.doc_id,
  466. this.newLines,
  467. this.source,
  468. this.user_id,
  469. false,
  470. (error, res, body) => {
  471. if (error) {
  472. return done(error)
  473. }
  474. this.statusCode = res.statusCode
  475. this.body = body
  476. setTimeout(done, 200)
  477. }
  478. )
  479. })
  480. after(function () {
  481. MockProjectHistoryApi.flushProject.resetHistory()
  482. MockWebApi.setDocument.resetHistory()
  483. })
  484. it('should return a 200 status code', function () {
  485. this.statusCode.should.equal(200)
  486. })
  487. it('should send the updated doc lines to the web api', function () {
  488. MockWebApi.setDocument
  489. .calledWith(this.project_id, this.doc_id, this.newLines)
  490. .should.equal(true)
  491. })
  492. it('should return the mongo rev in the json response', function () {
  493. this.body.should.deep.equal({ rev: '123' })
  494. })
  495. })
  496. describe('with track changes', function () {
  497. before(function () {
  498. this.lines = ['one', 'one and a half', 'two', 'three']
  499. this.id_seed = '587357bd35e64f6157'
  500. this.update = {
  501. doc: this.doc_id,
  502. op: [
  503. {
  504. d: 'one and a half\n',
  505. p: 4,
  506. },
  507. ],
  508. meta: {
  509. tc: this.id_seed,
  510. user_id: this.user_id,
  511. },
  512. v: this.version,
  513. }
  514. })
  515. describe('with the undo flag', function () {
  516. before(function (done) {
  517. this.project_id = DocUpdaterClient.randomId()
  518. this.doc_id = DocUpdaterClient.randomId()
  519. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  520. lines: this.lines,
  521. version: this.version,
  522. })
  523. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  524. if (error) {
  525. throw error
  526. }
  527. DocUpdaterClient.sendUpdate(
  528. this.project_id,
  529. this.doc_id,
  530. this.update,
  531. error => {
  532. if (error) {
  533. throw error
  534. }
  535. // Go back to old lines, with undo flag
  536. DocUpdaterClient.setDocLines(
  537. this.project_id,
  538. this.doc_id,
  539. this.lines,
  540. this.source,
  541. this.user_id,
  542. true,
  543. (error, res, body) => {
  544. if (error) {
  545. return done(error)
  546. }
  547. this.statusCode = res.statusCode
  548. setTimeout(done, 200)
  549. }
  550. )
  551. }
  552. )
  553. })
  554. })
  555. after(function () {
  556. MockProjectHistoryApi.flushProject.resetHistory()
  557. MockWebApi.setDocument.resetHistory()
  558. })
  559. it('should undo the tracked changes', function (done) {
  560. DocUpdaterClient.getDoc(
  561. this.project_id,
  562. this.doc_id,
  563. (error, res, data) => {
  564. if (error) {
  565. throw error
  566. }
  567. const { ranges } = data
  568. expect(ranges.changes).to.be.undefined
  569. done()
  570. }
  571. )
  572. })
  573. })
  574. describe('without the undo flag', function () {
  575. before(function (done) {
  576. this.project_id = DocUpdaterClient.randomId()
  577. this.doc_id = DocUpdaterClient.randomId()
  578. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  579. lines: this.lines,
  580. version: this.version,
  581. })
  582. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  583. if (error) {
  584. throw error
  585. }
  586. DocUpdaterClient.sendUpdate(
  587. this.project_id,
  588. this.doc_id,
  589. this.update,
  590. error => {
  591. if (error) {
  592. throw error
  593. }
  594. // Go back to old lines, without undo flag
  595. DocUpdaterClient.setDocLines(
  596. this.project_id,
  597. this.doc_id,
  598. this.lines,
  599. this.source,
  600. this.user_id,
  601. false,
  602. (error, res, body) => {
  603. if (error) {
  604. return done(error)
  605. }
  606. this.statusCode = res.statusCode
  607. setTimeout(done, 200)
  608. }
  609. )
  610. }
  611. )
  612. })
  613. })
  614. after(function () {
  615. MockProjectHistoryApi.flushProject.resetHistory()
  616. MockWebApi.setDocument.resetHistory()
  617. })
  618. it('should not undo the tracked changes', function (done) {
  619. DocUpdaterClient.getDoc(
  620. this.project_id,
  621. this.doc_id,
  622. (error, res, data) => {
  623. if (error) {
  624. throw error
  625. }
  626. const { ranges } = data
  627. expect(ranges.changes.length).to.equal(1)
  628. done()
  629. }
  630. )
  631. })
  632. })
  633. })
  634. })