SettingADocumentTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 does not exist in the doc updater', function () {
  183. before(function (done) {
  184. this.project_id = DocUpdaterClient.randomId()
  185. this.doc_id = DocUpdaterClient.randomId()
  186. numberOfReceivedUpdates = 0
  187. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  188. lines: this.lines,
  189. version: this.version,
  190. })
  191. DocUpdaterClient.setDocLines(
  192. this.project_id,
  193. this.doc_id,
  194. this.newLines,
  195. this.source,
  196. this.user_id,
  197. false,
  198. (error, res, body) => {
  199. if (error) {
  200. return done(error)
  201. }
  202. this.statusCode = res.statusCode
  203. this.body = body
  204. setTimeout(done, 200)
  205. }
  206. )
  207. })
  208. after(function () {
  209. MockProjectHistoryApi.flushProject.resetHistory()
  210. MockWebApi.setDocument.resetHistory()
  211. })
  212. it('should return a 200 status code', function () {
  213. this.statusCode.should.equal(200)
  214. })
  215. it('should emit an update', function () {
  216. expect(numberOfReceivedUpdates).to.equal(1)
  217. })
  218. it('should send the updated doc lines to the web api', function () {
  219. MockWebApi.setDocument
  220. .calledWith(this.project_id, this.doc_id, this.newLines)
  221. .should.equal(true)
  222. })
  223. it('should flush project history', function () {
  224. MockProjectHistoryApi.flushProject
  225. .calledWith(this.project_id)
  226. .should.equal(true)
  227. })
  228. it('should remove the document from redis', function (done) {
  229. docUpdaterRedis.get(
  230. Keys.docLines({ doc_id: this.doc_id }),
  231. (error, lines) => {
  232. if (error) {
  233. throw error
  234. }
  235. expect(lines).to.not.exist
  236. done()
  237. }
  238. )
  239. })
  240. it('should return the mongo rev in the json response', function () {
  241. this.body.should.deep.equal({ rev: '123' })
  242. })
  243. })
  244. const DOC_TOO_LARGE_TEST_CASES = [
  245. {
  246. desc: 'when the updated doc is too large for the body parser',
  247. size: Settings.maxJsonRequestSize,
  248. expectedStatusCode: 413,
  249. },
  250. {
  251. desc: 'when the updated doc is larger than the HTTP controller limit',
  252. size: Settings.max_doc_length,
  253. expectedStatusCode: 406,
  254. },
  255. ]
  256. DOC_TOO_LARGE_TEST_CASES.forEach(testCase => {
  257. describe(testCase.desc, function () {
  258. before(function (done) {
  259. this.project_id = DocUpdaterClient.randomId()
  260. this.doc_id = DocUpdaterClient.randomId()
  261. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  262. lines: this.lines,
  263. version: this.version,
  264. })
  265. this.newLines = []
  266. while (JSON.stringify(this.newLines).length <= testCase.size) {
  267. this.newLines.push('(a long line of text)'.repeat(10000))
  268. }
  269. DocUpdaterClient.setDocLines(
  270. this.project_id,
  271. this.doc_id,
  272. this.newLines,
  273. this.source,
  274. this.user_id,
  275. false,
  276. (error, res, body) => {
  277. if (error) {
  278. return done(error)
  279. }
  280. this.statusCode = res.statusCode
  281. setTimeout(done, 200)
  282. }
  283. )
  284. })
  285. after(function () {
  286. MockProjectHistoryApi.flushProject.resetHistory()
  287. MockWebApi.setDocument.resetHistory()
  288. })
  289. it(`should return a ${testCase.expectedStatusCode} status code`, function () {
  290. this.statusCode.should.equal(testCase.expectedStatusCode)
  291. })
  292. it('should not send the updated doc lines to the web api', function () {
  293. MockWebApi.setDocument.called.should.equal(false)
  294. })
  295. it('should not flush project history', function () {
  296. MockProjectHistoryApi.flushProject.called.should.equal(false)
  297. })
  298. })
  299. })
  300. describe('when the updated doc is large but under the bodyParser and HTTPController size limit', function () {
  301. before(function (done) {
  302. this.project_id = DocUpdaterClient.randomId()
  303. this.doc_id = DocUpdaterClient.randomId()
  304. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  305. lines: this.lines,
  306. version: this.version,
  307. })
  308. this.newLines = []
  309. while (JSON.stringify(this.newLines).length < 2 * 1024 * 1024) {
  310. // limit in HTTPController
  311. this.newLines.push('(a long line of text)'.repeat(10000))
  312. }
  313. this.newLines.pop() // remove the line which took it over the limit
  314. DocUpdaterClient.setDocLines(
  315. this.project_id,
  316. this.doc_id,
  317. this.newLines,
  318. this.source,
  319. this.user_id,
  320. false,
  321. (error, res, body) => {
  322. if (error) {
  323. return done(error)
  324. }
  325. this.statusCode = res.statusCode
  326. this.body = body
  327. setTimeout(done, 200)
  328. }
  329. )
  330. })
  331. after(function () {
  332. MockProjectHistoryApi.flushProject.resetHistory()
  333. MockWebApi.setDocument.resetHistory()
  334. })
  335. it('should return a 200 status code', function () {
  336. this.statusCode.should.equal(200)
  337. })
  338. it('should send the updated doc lines to the web api', function () {
  339. MockWebApi.setDocument
  340. .calledWith(this.project_id, this.doc_id, this.newLines)
  341. .should.equal(true)
  342. })
  343. it('should return the mongo rev in the json response', function () {
  344. this.body.should.deep.equal({ rev: '123' })
  345. })
  346. })
  347. describe('with track changes', function () {
  348. before(function () {
  349. this.lines = ['one', 'one and a half', 'two', 'three']
  350. this.id_seed = '587357bd35e64f6157'
  351. this.update = {
  352. doc: this.doc_id,
  353. op: [
  354. {
  355. d: 'one and a half\n',
  356. p: 4,
  357. },
  358. ],
  359. meta: {
  360. tc: this.id_seed,
  361. user_id: this.user_id,
  362. },
  363. v: this.version,
  364. }
  365. })
  366. describe('with the undo flag', function () {
  367. before(function (done) {
  368. this.project_id = DocUpdaterClient.randomId()
  369. this.doc_id = DocUpdaterClient.randomId()
  370. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  371. lines: this.lines,
  372. version: this.version,
  373. })
  374. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  375. if (error) {
  376. throw error
  377. }
  378. DocUpdaterClient.sendUpdate(
  379. this.project_id,
  380. this.doc_id,
  381. this.update,
  382. error => {
  383. if (error) {
  384. throw error
  385. }
  386. // Go back to old lines, with undo flag
  387. DocUpdaterClient.setDocLines(
  388. this.project_id,
  389. this.doc_id,
  390. this.lines,
  391. this.source,
  392. this.user_id,
  393. true,
  394. (error, res, body) => {
  395. if (error) {
  396. return done(error)
  397. }
  398. this.statusCode = res.statusCode
  399. setTimeout(done, 200)
  400. }
  401. )
  402. }
  403. )
  404. })
  405. })
  406. after(function () {
  407. MockProjectHistoryApi.flushProject.resetHistory()
  408. MockWebApi.setDocument.resetHistory()
  409. })
  410. it('should undo the tracked changes', function (done) {
  411. DocUpdaterClient.getDoc(
  412. this.project_id,
  413. this.doc_id,
  414. (error, res, data) => {
  415. if (error) {
  416. throw error
  417. }
  418. const { ranges } = data
  419. expect(ranges.changes).to.be.undefined
  420. done()
  421. }
  422. )
  423. })
  424. })
  425. describe('without the undo flag', function () {
  426. before(function (done) {
  427. this.project_id = DocUpdaterClient.randomId()
  428. this.doc_id = DocUpdaterClient.randomId()
  429. MockWebApi.insertDoc(this.project_id, this.doc_id, {
  430. lines: this.lines,
  431. version: this.version,
  432. })
  433. DocUpdaterClient.preloadDoc(this.project_id, this.doc_id, error => {
  434. if (error) {
  435. throw error
  436. }
  437. DocUpdaterClient.sendUpdate(
  438. this.project_id,
  439. this.doc_id,
  440. this.update,
  441. error => {
  442. if (error) {
  443. throw error
  444. }
  445. // Go back to old lines, without undo flag
  446. DocUpdaterClient.setDocLines(
  447. this.project_id,
  448. this.doc_id,
  449. this.lines,
  450. this.source,
  451. this.user_id,
  452. false,
  453. (error, res, body) => {
  454. if (error) {
  455. return done(error)
  456. }
  457. this.statusCode = res.statusCode
  458. setTimeout(done, 200)
  459. }
  460. )
  461. }
  462. )
  463. })
  464. })
  465. after(function () {
  466. MockProjectHistoryApi.flushProject.resetHistory()
  467. MockWebApi.setDocument.resetHistory()
  468. })
  469. it('should not undo the tracked changes', function (done) {
  470. DocUpdaterClient.getDoc(
  471. this.project_id,
  472. this.doc_id,
  473. (error, res, data) => {
  474. if (error) {
  475. throw error
  476. }
  477. const { ranges } = data
  478. expect(ranges.changes.length).to.equal(1)
  479. done()
  480. }
  481. )
  482. })
  483. })
  484. })
  485. })