SyncTests.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  1. import async from 'async'
  2. import nock from 'nock'
  3. import { expect } from 'chai'
  4. import request from 'request'
  5. import assert from 'node:assert'
  6. import mongodb from 'mongodb-legacy'
  7. import logger from '@overleaf/logger'
  8. import Settings from '@overleaf/settings'
  9. import * as ProjectHistoryClient from './helpers/ProjectHistoryClient.js'
  10. import * as ProjectHistoryApp from './helpers/ProjectHistoryApp.js'
  11. import sinon from 'sinon'
  12. import { getFailure } from './helpers/ProjectHistoryClient.js'
  13. const { ObjectId } = mongodb
  14. const EMPTY_FILE_HASH = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  15. const MockHistoryStore = () => nock('http://127.0.0.1:3100')
  16. const MockFileStore = () => nock('http://127.0.0.1:3009')
  17. const MockWeb = () => nock('http://127.0.0.1:3000')
  18. describe('Syncing with web and doc-updater', function () {
  19. const historyId = new ObjectId().toString()
  20. let loggerWarn, loggerError
  21. beforeEach(function () {
  22. loggerWarn = sinon.spy(logger, 'warn')
  23. loggerError = sinon.spy(logger, 'error')
  24. })
  25. afterEach(function () {
  26. loggerWarn.restore()
  27. loggerError.restore()
  28. })
  29. beforeEach(function (done) {
  30. this.timestamp = new Date()
  31. ProjectHistoryApp.ensureRunning(error => {
  32. if (error) {
  33. throw error
  34. }
  35. this.project_id = new ObjectId().toString()
  36. this.doc_id = new ObjectId().toString()
  37. this.file_id = new ObjectId().toString()
  38. MockHistoryStore().post('/api/projects').reply(200, {
  39. projectId: historyId,
  40. })
  41. MockWeb()
  42. .get(`/project/${this.project_id}/details`)
  43. .reply(200, {
  44. name: 'Test Project',
  45. overleaf: {
  46. history: {
  47. id: historyId,
  48. },
  49. },
  50. })
  51. ProjectHistoryClient.initializeProject(historyId, done)
  52. })
  53. })
  54. afterEach(function () {
  55. nock.cleanAll()
  56. })
  57. describe('resyncing project history', function () {
  58. describe('without project-history enabled', function () {
  59. beforeEach(function () {
  60. MockWeb().post(`/project/${this.project_id}/history/resync`).reply(404)
  61. })
  62. it('404s if project-history is not enabled', function (done) {
  63. request.post(
  64. {
  65. url: `http://127.0.0.1:3054/project/${this.project_id}/resync`,
  66. },
  67. (error, res, body) => {
  68. if (error) {
  69. return done(error)
  70. }
  71. expect(res.statusCode).to.equal(404)
  72. done()
  73. }
  74. )
  75. })
  76. })
  77. describe('with project-history enabled', function () {
  78. beforeEach(function () {
  79. MockWeb().post(`/project/${this.project_id}/history/resync`).reply(204)
  80. })
  81. describe('when a doc is missing', function () {
  82. it('should send add doc updates to the history store', function (done) {
  83. MockHistoryStore()
  84. .get(`/api/projects/${historyId}/latest/history`)
  85. .reply(200, {
  86. chunk: {
  87. history: {
  88. snapshot: {
  89. files: {
  90. persistedDoc: { hash: EMPTY_FILE_HASH, stringLength: 0 },
  91. },
  92. },
  93. changes: [],
  94. },
  95. startVersion: 0,
  96. },
  97. })
  98. MockHistoryStore()
  99. .get(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`)
  100. .reply(200, '')
  101. const createBlob = MockHistoryStore()
  102. .put(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`, '')
  103. .reply(201)
  104. const addFile = MockHistoryStore()
  105. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  106. expect(body).to.deep.equal([
  107. {
  108. v2Authors: [],
  109. authors: [],
  110. timestamp: this.timestamp.toJSON(),
  111. operations: [
  112. {
  113. pathname: 'main.tex',
  114. file: {
  115. hash: EMPTY_FILE_HASH,
  116. },
  117. },
  118. ],
  119. origin: { kind: 'test-origin' },
  120. },
  121. ])
  122. return true
  123. })
  124. .query({ end_version: 0 })
  125. .reply(204)
  126. async.series(
  127. [
  128. cb => {
  129. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  130. },
  131. cb => {
  132. const update = {
  133. projectHistoryId: historyId,
  134. resyncProjectStructure: {
  135. docs: [
  136. { path: '/main.tex', doc: this.doc_id },
  137. { path: '/persistedDoc', doc: 'other-doc-id' },
  138. ],
  139. files: [],
  140. },
  141. meta: {
  142. ts: this.timestamp,
  143. },
  144. }
  145. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  146. },
  147. cb => {
  148. ProjectHistoryClient.flushProject(this.project_id, cb)
  149. },
  150. ],
  151. error => {
  152. if (error) {
  153. return done(error)
  154. }
  155. assert(
  156. createBlob.isDone(),
  157. '/api/projects/:historyId/blobs/:hash should have been called'
  158. )
  159. assert(
  160. addFile.isDone(),
  161. `/api/projects/${historyId}/changes should have been called`
  162. )
  163. done()
  164. }
  165. )
  166. })
  167. })
  168. describe('when a file is missing', function () {
  169. it('should send add file updates to the history store', function (done) {
  170. MockHistoryStore()
  171. .get(`/api/projects/${historyId}/latest/history`)
  172. .reply(200, {
  173. chunk: {
  174. history: {
  175. snapshot: {
  176. files: {
  177. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  178. },
  179. },
  180. changes: [],
  181. },
  182. startVersion: 0,
  183. },
  184. })
  185. const fileContents = Buffer.from([1, 2, 3])
  186. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  187. MockFileStore()
  188. .get(`/project/${this.project_id}/file/${this.file_id}`)
  189. .reply(200, fileContents)
  190. const headBlob = MockHistoryStore()
  191. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  192. .reply(404)
  193. const createBlob = MockHistoryStore()
  194. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  195. .reply(201)
  196. const addFile = MockHistoryStore()
  197. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  198. expect(body).to.deep.equal([
  199. {
  200. v2Authors: [],
  201. authors: [],
  202. timestamp: this.timestamp.toJSON(),
  203. operations: [
  204. {
  205. pathname: 'test.png',
  206. file: {
  207. hash: fileHash,
  208. },
  209. },
  210. ],
  211. origin: { kind: 'test-origin' },
  212. },
  213. ])
  214. return true
  215. })
  216. .query({ end_version: 0 })
  217. .reply(204)
  218. async.series(
  219. [
  220. cb => {
  221. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  222. },
  223. cb => {
  224. const update = {
  225. projectHistoryId: historyId,
  226. resyncProjectStructure: {
  227. docs: [],
  228. files: [
  229. {
  230. file: this.file_id,
  231. path: '/test.png',
  232. _hash: fileHash,
  233. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  234. },
  235. { path: '/persistedFile' },
  236. ],
  237. },
  238. meta: {
  239. ts: this.timestamp,
  240. },
  241. }
  242. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  243. },
  244. cb => {
  245. ProjectHistoryClient.flushProject(this.project_id, cb)
  246. },
  247. ],
  248. error => {
  249. if (error) {
  250. throw error
  251. }
  252. assert(!loggerWarn.called, 'no warning logged on 404')
  253. assert(
  254. headBlob.isDone(),
  255. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  256. )
  257. assert(
  258. createBlob.isDone(),
  259. '/api/projects/:historyId/blobs/:hash should have been called'
  260. )
  261. assert(
  262. addFile.isDone(),
  263. `/api/projects/${historyId}/changes should have been called`
  264. )
  265. done()
  266. }
  267. )
  268. })
  269. it('should skip HEAD on blob without hash', function (done) {
  270. MockHistoryStore()
  271. .get(`/api/projects/${historyId}/latest/history`)
  272. .reply(200, {
  273. chunk: {
  274. history: {
  275. snapshot: {
  276. files: {
  277. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  278. },
  279. },
  280. changes: [],
  281. },
  282. startVersion: 0,
  283. },
  284. })
  285. const fileContents = Buffer.from([1, 2, 3])
  286. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  287. MockFileStore()
  288. .get(`/project/${this.project_id}/file/${this.file_id}`)
  289. .reply(200, fileContents)
  290. const headBlob = MockHistoryStore()
  291. .head(`/api/projects/${historyId}/blobs/undefined`)
  292. .reply(500)
  293. const createBlob = MockHistoryStore()
  294. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  295. .reply(201)
  296. const addFile = MockHistoryStore()
  297. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  298. expect(body).to.deep.equal([
  299. {
  300. v2Authors: [],
  301. authors: [],
  302. timestamp: this.timestamp.toJSON(),
  303. operations: [
  304. {
  305. pathname: 'test.png',
  306. file: {
  307. hash: fileHash,
  308. },
  309. },
  310. ],
  311. origin: { kind: 'test-origin' },
  312. },
  313. ])
  314. return true
  315. })
  316. .query({ end_version: 0 })
  317. .reply(204)
  318. async.series(
  319. [
  320. cb => {
  321. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  322. },
  323. cb => {
  324. const update = {
  325. projectHistoryId: historyId,
  326. resyncProjectStructure: {
  327. docs: [],
  328. files: [
  329. {
  330. file: this.file_id,
  331. path: '/test.png',
  332. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  333. },
  334. { path: '/persistedFile' },
  335. ],
  336. },
  337. meta: {
  338. ts: this.timestamp,
  339. },
  340. }
  341. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  342. },
  343. cb => {
  344. ProjectHistoryClient.flushProject(this.project_id, cb)
  345. },
  346. ],
  347. error => {
  348. if (error) {
  349. throw error
  350. }
  351. assert(!loggerWarn.called, 'no warning logged on 404')
  352. assert(
  353. !headBlob.isDone(),
  354. 'HEAD /api/projects/:historyId/blobs/:hash should have been skipped'
  355. )
  356. assert(
  357. createBlob.isDone(),
  358. '/api/projects/:historyId/blobs/:hash should have been called'
  359. )
  360. assert(
  361. addFile.isDone(),
  362. `/api/projects/${historyId}/changes should have been called`
  363. )
  364. done()
  365. }
  366. )
  367. })
  368. it('should record error when checking blob fails with 500', function (done) {
  369. MockHistoryStore()
  370. .get(`/api/projects/${historyId}/latest/history`)
  371. .reply(200, {
  372. chunk: {
  373. history: {
  374. snapshot: {
  375. files: {
  376. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  377. },
  378. },
  379. changes: [],
  380. },
  381. startVersion: 0,
  382. },
  383. })
  384. const fileContents = Buffer.from([1, 2, 3])
  385. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  386. MockFileStore()
  387. .get(`/project/${this.project_id}/file/${this.file_id}`)
  388. .reply(200, fileContents)
  389. const headBlob = MockHistoryStore()
  390. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  391. .reply(500)
  392. const createBlob = MockHistoryStore()
  393. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  394. .reply(201)
  395. const addFile = MockHistoryStore()
  396. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  397. expect(body).to.deep.equal([
  398. {
  399. v2Authors: [],
  400. authors: [],
  401. timestamp: this.timestamp.toJSON(),
  402. operations: [
  403. {
  404. pathname: 'test.png',
  405. file: {
  406. hash: fileHash,
  407. },
  408. },
  409. ],
  410. origin: { kind: 'test-origin' },
  411. },
  412. ])
  413. return true
  414. })
  415. .query({ end_version: 0 })
  416. .reply(204)
  417. async.series(
  418. [
  419. cb => {
  420. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  421. },
  422. cb => {
  423. const update = {
  424. projectHistoryId: historyId,
  425. resyncProjectStructure: {
  426. docs: [],
  427. files: [
  428. {
  429. file: this.file_id,
  430. path: '/test.png',
  431. _hash: fileHash,
  432. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  433. },
  434. { path: '/persistedFile' },
  435. ],
  436. },
  437. meta: {
  438. ts: this.timestamp,
  439. },
  440. }
  441. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  442. },
  443. cb => {
  444. ProjectHistoryClient.flushProject(
  445. this.project_id,
  446. {
  447. allowErrors: true,
  448. },
  449. (err, res) => {
  450. if (err) return cb(err)
  451. assert(res.statusCode === 500, 'resync should have failed')
  452. cb()
  453. }
  454. )
  455. },
  456. ],
  457. error => {
  458. if (error) {
  459. throw error
  460. }
  461. assert(
  462. loggerError.calledWithMatch(
  463. sinon.match.any,
  464. 'error checking whether blob exists'
  465. ),
  466. 'error logged on 500'
  467. )
  468. assert(
  469. headBlob.isDone(),
  470. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  471. )
  472. assert(
  473. !createBlob.isDone(),
  474. '/api/projects/:historyId/blobs/:hash should have been skipped'
  475. )
  476. assert(
  477. !addFile.isDone(),
  478. `/api/projects/${historyId}/changes should have been skipped`
  479. )
  480. done()
  481. }
  482. )
  483. })
  484. it('should skip blob write when blob exists', function (done) {
  485. MockHistoryStore()
  486. .get(`/api/projects/${historyId}/latest/history`)
  487. .reply(200, {
  488. chunk: {
  489. history: {
  490. snapshot: {
  491. files: {
  492. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  493. },
  494. },
  495. changes: [],
  496. },
  497. startVersion: 0,
  498. },
  499. })
  500. const fileContents = Buffer.from([1, 2, 3])
  501. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  502. MockFileStore()
  503. .get(`/project/${this.project_id}/file/${this.file_id}`)
  504. .reply(200, fileContents)
  505. const headBlob = MockHistoryStore()
  506. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  507. .reply(200)
  508. const createBlob = MockHistoryStore()
  509. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  510. .reply(201)
  511. const addFile = MockHistoryStore()
  512. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  513. expect(body).to.deep.equal([
  514. {
  515. v2Authors: [],
  516. authors: [],
  517. timestamp: this.timestamp.toJSON(),
  518. operations: [
  519. {
  520. pathname: 'test.png',
  521. file: {
  522. hash: fileHash,
  523. },
  524. },
  525. ],
  526. origin: { kind: 'test-origin' },
  527. },
  528. ])
  529. return true
  530. })
  531. .query({ end_version: 0 })
  532. .reply(204)
  533. async.series(
  534. [
  535. cb => {
  536. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  537. },
  538. cb => {
  539. const update = {
  540. projectHistoryId: historyId,
  541. resyncProjectStructure: {
  542. docs: [],
  543. files: [
  544. {
  545. file: this.file_id,
  546. path: '/test.png',
  547. _hash: fileHash,
  548. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  549. },
  550. { path: '/persistedFile' },
  551. ],
  552. },
  553. meta: {
  554. ts: this.timestamp,
  555. },
  556. }
  557. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  558. },
  559. cb => {
  560. ProjectHistoryClient.flushProject(this.project_id, cb)
  561. },
  562. ],
  563. error => {
  564. if (error) {
  565. throw error
  566. }
  567. assert(!loggerWarn.called, 'no warning logged on 404')
  568. assert(
  569. headBlob.isDone(),
  570. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  571. )
  572. assert(
  573. !createBlob.isDone(),
  574. '/api/projects/:historyId/blobs/:hash should have been skipped'
  575. )
  576. assert(
  577. addFile.isDone(),
  578. `/api/projects/${historyId}/changes should have been called`
  579. )
  580. done()
  581. }
  582. )
  583. })
  584. it('should add file w/o url', function (done) {
  585. MockHistoryStore()
  586. .get(`/api/projects/${historyId}/latest/history`)
  587. .reply(200, {
  588. chunk: {
  589. history: {
  590. snapshot: {
  591. files: {
  592. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  593. },
  594. },
  595. changes: [],
  596. },
  597. startVersion: 0,
  598. },
  599. })
  600. const fileContents = Buffer.from([1, 2, 3])
  601. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  602. MockFileStore()
  603. .get(`/project/${this.project_id}/file/${this.file_id}`)
  604. .reply(200, fileContents)
  605. const headBlob = MockHistoryStore()
  606. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  607. .reply(200)
  608. const createBlob = MockHistoryStore()
  609. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  610. .reply(201)
  611. const addFile = MockHistoryStore()
  612. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  613. expect(body).to.deep.equal([
  614. {
  615. v2Authors: [],
  616. authors: [],
  617. timestamp: this.timestamp.toJSON(),
  618. operations: [
  619. {
  620. pathname: 'test.png',
  621. file: {
  622. hash: fileHash,
  623. },
  624. },
  625. ],
  626. origin: { kind: 'test-origin' },
  627. },
  628. ])
  629. return true
  630. })
  631. .query({ end_version: 0 })
  632. .reply(204)
  633. async.series(
  634. [
  635. cb => {
  636. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  637. },
  638. cb => {
  639. const update = {
  640. projectHistoryId: historyId,
  641. resyncProjectStructure: {
  642. docs: [],
  643. files: [
  644. {
  645. file: this.file_id,
  646. path: '/test.png',
  647. _hash: fileHash,
  648. createdBlob: true,
  649. },
  650. { path: '/persistedFile' },
  651. ],
  652. },
  653. meta: {
  654. ts: this.timestamp,
  655. },
  656. }
  657. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  658. },
  659. cb => {
  660. ProjectHistoryClient.flushProject(this.project_id, cb)
  661. },
  662. ],
  663. error => {
  664. if (error) {
  665. throw error
  666. }
  667. assert(!loggerWarn.called, 'no warning logged on 404')
  668. assert(
  669. headBlob.isDone(),
  670. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  671. )
  672. assert(
  673. !createBlob.isDone(),
  674. '/api/projects/:historyId/blobs/:hash should have been skipped'
  675. )
  676. assert(
  677. addFile.isDone(),
  678. `/api/projects/${historyId}/changes should have been called`
  679. )
  680. done()
  681. }
  682. )
  683. })
  684. describe('with filestore disabled', function () {
  685. before(function () {
  686. Settings.apis.filestore.enabled = false
  687. })
  688. after(function () {
  689. Settings.apis.filestore.enabled = true
  690. })
  691. it('should record error when blob is missing', function (done) {
  692. MockHistoryStore()
  693. .get(`/api/projects/${historyId}/latest/history`)
  694. .reply(200, {
  695. chunk: {
  696. history: {
  697. snapshot: {
  698. files: {
  699. persistedFile: { hash: EMPTY_FILE_HASH, byteLength: 0 },
  700. },
  701. },
  702. changes: [],
  703. },
  704. startVersion: 0,
  705. },
  706. })
  707. const fileContents = Buffer.from([1, 2, 3])
  708. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  709. MockFileStore()
  710. .get(`/project/${this.project_id}/file/${this.file_id}`)
  711. .reply(200, fileContents)
  712. const headBlob = MockHistoryStore()
  713. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  714. .times(3) // three retries
  715. .reply(404)
  716. const createBlob = MockHistoryStore()
  717. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  718. .reply(201)
  719. const addFile = MockHistoryStore()
  720. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  721. expect(body).to.deep.equal([
  722. {
  723. v2Authors: [],
  724. authors: [],
  725. timestamp: this.timestamp.toJSON(),
  726. operations: [
  727. {
  728. pathname: 'test.png',
  729. file: {
  730. hash: fileHash,
  731. },
  732. },
  733. ],
  734. origin: { kind: 'test-origin' },
  735. },
  736. ])
  737. return true
  738. })
  739. .query({ end_version: 0 })
  740. .reply(204)
  741. async.series(
  742. [
  743. cb => {
  744. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  745. },
  746. cb => {
  747. const update = {
  748. projectHistoryId: historyId,
  749. resyncProjectStructure: {
  750. docs: [],
  751. files: [
  752. {
  753. file: this.file_id,
  754. path: '/test.png',
  755. _hash: fileHash,
  756. url: `http://127.0.0.1:3009/project/${this.project_id}/file/${this.file_id}`,
  757. },
  758. { path: '/persistedFile' },
  759. ],
  760. },
  761. meta: {
  762. ts: this.timestamp,
  763. },
  764. }
  765. ProjectHistoryClient.pushRawUpdate(
  766. this.project_id,
  767. update,
  768. cb
  769. )
  770. },
  771. cb => {
  772. ProjectHistoryClient.flushProject(
  773. this.project_id,
  774. {
  775. allowErrors: true,
  776. },
  777. (err, res) => {
  778. if (err) return cb(err)
  779. assert(
  780. res.statusCode === 500,
  781. 'resync should have failed'
  782. )
  783. cb()
  784. }
  785. )
  786. },
  787. ],
  788. error => {
  789. if (error) {
  790. throw error
  791. }
  792. assert(
  793. loggerError.calledWithMatch(
  794. sinon.match.any,
  795. 'blocking filestore read'
  796. ),
  797. 'error logged on 500'
  798. )
  799. assert(
  800. headBlob.isDone(),
  801. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  802. )
  803. assert(
  804. !createBlob.isDone(),
  805. '/api/projects/:historyId/blobs/:hash should have been skipped'
  806. )
  807. assert(
  808. !addFile.isDone(),
  809. `/api/projects/${historyId}/changes should have been skipped`
  810. )
  811. done()
  812. }
  813. )
  814. })
  815. })
  816. })
  817. describe('when a file hash mismatches', function () {
  818. it('should remove and re-add file w/o url', function (done) {
  819. MockHistoryStore()
  820. .get(`/api/projects/${historyId}/latest/history`)
  821. .reply(200, {
  822. chunk: {
  823. history: {
  824. snapshot: {
  825. files: {
  826. 'test.png': { hash: EMPTY_FILE_HASH, byteLength: 0 },
  827. },
  828. },
  829. changes: [],
  830. },
  831. startVersion: 0,
  832. },
  833. })
  834. const fileContents = Buffer.from([1, 2, 3])
  835. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  836. MockFileStore()
  837. .get(`/project/${this.project_id}/file/${this.file_id}`)
  838. .reply(200, fileContents)
  839. const headBlob = MockHistoryStore()
  840. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  841. .reply(200)
  842. const createBlob = MockHistoryStore()
  843. .put(`/api/projects/${historyId}/blobs/${fileHash}`, fileContents)
  844. .reply(201)
  845. const addFile = MockHistoryStore()
  846. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  847. expect(body).to.deep.equal([
  848. {
  849. v2Authors: [],
  850. authors: [],
  851. timestamp: this.timestamp.toJSON(),
  852. operations: [
  853. {
  854. pathname: 'test.png',
  855. newPathname: '',
  856. },
  857. ],
  858. origin: { kind: 'test-origin' },
  859. },
  860. {
  861. v2Authors: [],
  862. authors: [],
  863. timestamp: this.timestamp.toJSON(),
  864. operations: [
  865. {
  866. pathname: 'test.png',
  867. file: {
  868. hash: fileHash,
  869. },
  870. },
  871. ],
  872. origin: { kind: 'test-origin' },
  873. },
  874. ])
  875. return true
  876. })
  877. .query({ end_version: 0 })
  878. .reply(204)
  879. async.series(
  880. [
  881. cb => {
  882. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  883. },
  884. cb => {
  885. const update = {
  886. projectHistoryId: historyId,
  887. resyncProjectStructure: {
  888. docs: [],
  889. files: [
  890. {
  891. file: this.file_id,
  892. path: '/test.png',
  893. _hash: fileHash,
  894. createdBlob: true,
  895. },
  896. ],
  897. },
  898. meta: {
  899. ts: this.timestamp,
  900. },
  901. }
  902. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  903. },
  904. cb => {
  905. ProjectHistoryClient.flushProject(this.project_id, cb)
  906. },
  907. ],
  908. error => {
  909. if (error) {
  910. throw error
  911. }
  912. assert(!loggerWarn.called, 'no warning logged on 404')
  913. assert(
  914. headBlob.isDone(),
  915. 'HEAD /api/projects/:historyId/blobs/:hash should have been called'
  916. )
  917. assert(
  918. !createBlob.isDone(),
  919. '/api/projects/:historyId/blobs/:hash should have been skipped'
  920. )
  921. assert(
  922. addFile.isDone(),
  923. `/api/projects/${historyId}/changes should have been called`
  924. )
  925. done()
  926. }
  927. )
  928. })
  929. })
  930. describe("when a file exists which shouldn't", function () {
  931. it('should send remove file updates to the history store', function (done) {
  932. MockHistoryStore()
  933. .get(`/api/projects/${historyId}/latest/history`)
  934. .reply(200, {
  935. chunk: {
  936. history: {
  937. snapshot: {
  938. files: {
  939. docToKeep: { hash: EMPTY_FILE_HASH, stringLength: 0 },
  940. docToDelete: { hash: EMPTY_FILE_HASH, stringLength: 0 },
  941. },
  942. },
  943. changes: [],
  944. },
  945. startVersion: 0,
  946. },
  947. })
  948. MockHistoryStore()
  949. .get(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`)
  950. .reply(200, '')
  951. .get(`/api/projects/${historyId}/blobs/${EMPTY_FILE_HASH}`)
  952. .reply(200, '') // blob is requested once for each file
  953. const deleteFile = MockHistoryStore()
  954. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  955. expect(body).to.deep.equal([
  956. {
  957. v2Authors: [],
  958. authors: [],
  959. timestamp: this.timestamp.toJSON(),
  960. operations: [
  961. {
  962. pathname: 'docToDelete',
  963. newPathname: '',
  964. },
  965. ],
  966. origin: { kind: 'test-origin' },
  967. },
  968. ])
  969. return true
  970. })
  971. .query({ end_version: 0 })
  972. .reply(204)
  973. async.series(
  974. [
  975. cb => {
  976. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  977. },
  978. cb => {
  979. const update = {
  980. projectHistoryId: historyId,
  981. resyncProjectStructure: {
  982. docs: [{ path: 'docToKeep' }],
  983. files: [],
  984. },
  985. meta: {
  986. ts: this.timestamp,
  987. },
  988. }
  989. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  990. },
  991. cb => {
  992. ProjectHistoryClient.flushProject(this.project_id, cb)
  993. },
  994. ],
  995. error => {
  996. if (error) {
  997. throw error
  998. }
  999. assert(
  1000. deleteFile.isDone(),
  1001. `/api/projects/${historyId}/changes should have been called`
  1002. )
  1003. done()
  1004. }
  1005. )
  1006. })
  1007. })
  1008. describe("when a doc's contents is not up to date", function () {
  1009. beforeEach(function () {
  1010. MockHistoryStore()
  1011. .get(`/api/projects/${historyId}/latest/history`)
  1012. .reply(200, {
  1013. chunk: {
  1014. history: {
  1015. snapshot: {
  1016. files: {
  1017. 'main.tex': {
  1018. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  1019. stringLength: 3,
  1020. },
  1021. },
  1022. },
  1023. changes: [],
  1024. },
  1025. startVersion: 0,
  1026. },
  1027. })
  1028. MockHistoryStore()
  1029. .get(
  1030. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  1031. )
  1032. .reply(200, 'a\nb')
  1033. })
  1034. it('should send test updates to the history store', function (done) {
  1035. const addFile = MockHistoryStore()
  1036. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1037. expect(body).to.deep.equal([
  1038. {
  1039. v2Authors: [],
  1040. authors: [],
  1041. timestamp: this.timestamp.toJSON(),
  1042. operations: [
  1043. {
  1044. pathname: 'main.tex',
  1045. textOperation: [3, '\nc'],
  1046. },
  1047. ],
  1048. origin: { kind: 'test-origin' },
  1049. },
  1050. ])
  1051. return true
  1052. })
  1053. .query({ end_version: 0 })
  1054. .reply(204)
  1055. async.series(
  1056. [
  1057. cb => {
  1058. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1059. },
  1060. cb => {
  1061. const update = {
  1062. projectHistoryId: historyId,
  1063. resyncProjectStructure: {
  1064. docs: [{ path: '/main.tex' }],
  1065. files: [],
  1066. },
  1067. meta: {
  1068. ts: this.timestamp,
  1069. },
  1070. }
  1071. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1072. },
  1073. cb => {
  1074. const update = {
  1075. path: '/main.tex',
  1076. projectHistoryId: historyId,
  1077. resyncDocContent: {
  1078. content: 'a\nb\nc',
  1079. },
  1080. doc: this.doc_id,
  1081. meta: {
  1082. ts: this.timestamp,
  1083. },
  1084. }
  1085. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1086. },
  1087. cb => {
  1088. ProjectHistoryClient.flushProject(this.project_id, cb)
  1089. },
  1090. ],
  1091. error => {
  1092. if (error) {
  1093. throw error
  1094. }
  1095. assert(
  1096. addFile.isDone(),
  1097. `/api/projects/${historyId}/changes should have been called`
  1098. )
  1099. done()
  1100. }
  1101. )
  1102. })
  1103. it('should strip non-BMP characters in updates before sending to the history store', function (done) {
  1104. const addFile = MockHistoryStore()
  1105. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1106. expect(body).to.deep.equal([
  1107. {
  1108. v2Authors: [],
  1109. authors: [],
  1110. timestamp: this.timestamp.toJSON(),
  1111. operations: [
  1112. {
  1113. pathname: 'main.tex',
  1114. textOperation: [3, '\n\uFFFD\uFFFDc'],
  1115. },
  1116. ],
  1117. origin: { kind: 'test-origin' },
  1118. },
  1119. ])
  1120. return true
  1121. })
  1122. .query({ end_version: 0 })
  1123. .reply(204)
  1124. async.series(
  1125. [
  1126. cb => {
  1127. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1128. },
  1129. cb => {
  1130. const update = {
  1131. projectHistoryId: historyId,
  1132. resyncProjectStructure: {
  1133. docs: [{ path: '/main.tex' }],
  1134. files: [],
  1135. },
  1136. meta: {
  1137. ts: this.timestamp,
  1138. },
  1139. }
  1140. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1141. },
  1142. cb => {
  1143. const update = {
  1144. path: '/main.tex',
  1145. projectHistoryId: historyId,
  1146. resyncDocContent: {
  1147. content: 'a\nb\n\uD800\uDC00c',
  1148. },
  1149. doc: this.doc_id,
  1150. meta: {
  1151. ts: this.timestamp,
  1152. },
  1153. }
  1154. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1155. },
  1156. cb => {
  1157. ProjectHistoryClient.flushProject(this.project_id, cb)
  1158. },
  1159. ],
  1160. error => {
  1161. if (error) {
  1162. throw error
  1163. }
  1164. assert(
  1165. addFile.isDone(),
  1166. `/api/projects/${historyId}/changes should have been called`
  1167. )
  1168. done()
  1169. }
  1170. )
  1171. })
  1172. it('should fix comments in the history store', function (done) {
  1173. const commentId = 'comment-id'
  1174. const addComment = MockHistoryStore()
  1175. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1176. expect(body).to.deep.equal([
  1177. {
  1178. v2Authors: [],
  1179. authors: [],
  1180. timestamp: this.timestamp.toJSON(),
  1181. operations: [
  1182. {
  1183. pathname: 'main.tex',
  1184. commentId,
  1185. ranges: [{ pos: 1, length: 10 }],
  1186. },
  1187. ],
  1188. origin: { kind: 'test-origin' },
  1189. },
  1190. ])
  1191. return true
  1192. })
  1193. .query({ end_version: 0 })
  1194. .reply(204)
  1195. async.series(
  1196. [
  1197. cb => {
  1198. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1199. },
  1200. cb => {
  1201. const update = {
  1202. projectHistoryId: historyId,
  1203. resyncProjectStructure: {
  1204. docs: [{ path: '/main.tex' }],
  1205. files: [],
  1206. },
  1207. meta: {
  1208. ts: this.timestamp,
  1209. },
  1210. }
  1211. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1212. },
  1213. cb => {
  1214. const update = {
  1215. path: '/main.tex',
  1216. projectHistoryId: historyId,
  1217. resyncDocContent: {
  1218. content: 'a\nb',
  1219. ranges: {
  1220. comments: [
  1221. {
  1222. id: commentId,
  1223. op: {
  1224. c: 'a',
  1225. p: 0,
  1226. hpos: 1,
  1227. hlen: 10,
  1228. t: commentId,
  1229. },
  1230. meta: {
  1231. user_id: 'user-id',
  1232. ts: this.timestamp,
  1233. },
  1234. },
  1235. ],
  1236. },
  1237. },
  1238. doc: this.doc_id,
  1239. meta: {
  1240. ts: this.timestamp,
  1241. },
  1242. }
  1243. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1244. },
  1245. cb => {
  1246. ProjectHistoryClient.flushProject(this.project_id, cb)
  1247. },
  1248. ],
  1249. error => {
  1250. if (error) {
  1251. return done(error)
  1252. }
  1253. assert(
  1254. addComment.isDone(),
  1255. `/api/projects/${historyId}/changes should have been called`
  1256. )
  1257. done()
  1258. }
  1259. )
  1260. })
  1261. })
  1262. describe('resyncProjectStructureOnly', function () {
  1263. it('should handle structure only updates', function (done) {
  1264. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  1265. MockHistoryStore()
  1266. .get(`/api/projects/${historyId}/latest/history`)
  1267. .reply(200, {
  1268. chunk: {
  1269. history: {
  1270. snapshot: {
  1271. files: {
  1272. 'main.tex': {
  1273. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  1274. stringLength: 3,
  1275. },
  1276. },
  1277. },
  1278. changes: [],
  1279. },
  1280. startVersion: 0,
  1281. },
  1282. })
  1283. const docContentRequest = MockHistoryStore()
  1284. .get(
  1285. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  1286. )
  1287. .reply(200, 'a\nb')
  1288. MockHistoryStore()
  1289. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  1290. .reply(200)
  1291. const addFile = MockHistoryStore()
  1292. .post(`/api/projects/${historyId}/legacy_changes`, body => {
  1293. expect(body).to.deep.equal([
  1294. {
  1295. v2Authors: [],
  1296. authors: [],
  1297. timestamp: this.timestamp.toJSON(),
  1298. operations: [
  1299. {
  1300. pathname: 'test.png',
  1301. file: {
  1302. hash: fileHash,
  1303. },
  1304. },
  1305. ],
  1306. origin: { kind: 'test-origin' },
  1307. },
  1308. ])
  1309. return true
  1310. })
  1311. .query({ end_version: 0 })
  1312. .reply(204)
  1313. // allow a 2nd resync
  1314. MockWeb()
  1315. .post(`/project/${this.project_id}/history/resync`)
  1316. .reply(204)
  1317. async.series(
  1318. [
  1319. cb => {
  1320. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1321. },
  1322. cb => {
  1323. const update = {
  1324. projectHistoryId: historyId,
  1325. resyncProjectStructureOnly: true,
  1326. resyncProjectStructure: {
  1327. docs: [{ path: '/main.tex' }],
  1328. files: [
  1329. {
  1330. file: this.file_id,
  1331. path: '/test.png',
  1332. _hash: fileHash,
  1333. createdBlob: true,
  1334. },
  1335. ],
  1336. },
  1337. meta: {
  1338. ts: this.timestamp,
  1339. },
  1340. }
  1341. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1342. },
  1343. cb => {
  1344. ProjectHistoryClient.flushProject(this.project_id, cb)
  1345. },
  1346. cb => {
  1347. // fails when previous resync did not finish
  1348. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1349. },
  1350. ],
  1351. error => {
  1352. if (error) {
  1353. throw error
  1354. }
  1355. assert(
  1356. addFile.isDone(),
  1357. `/api/projects/${historyId}/changes should have been called`
  1358. )
  1359. assert(
  1360. !docContentRequest.isDone(),
  1361. 'should not have requested doc content'
  1362. )
  1363. done()
  1364. }
  1365. )
  1366. })
  1367. it('should reject partial resync on docs', function (done) {
  1368. const fileHash = 'aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74'
  1369. MockHistoryStore()
  1370. .get(`/api/projects/${historyId}/latest/history`)
  1371. .reply(200, {
  1372. chunk: {
  1373. history: {
  1374. snapshot: {
  1375. files: {
  1376. 'main.tex': {
  1377. hash: '0a207c060e61f3b88eaee0a8cd0696f46fb155eb',
  1378. stringLength: 3,
  1379. },
  1380. },
  1381. },
  1382. changes: [],
  1383. },
  1384. startVersion: 0,
  1385. },
  1386. })
  1387. const docContentRequest = MockHistoryStore()
  1388. .get(
  1389. `/api/projects/${historyId}/blobs/0a207c060e61f3b88eaee0a8cd0696f46fb155eb`
  1390. )
  1391. .reply(200, 'a\nb')
  1392. MockHistoryStore()
  1393. .head(`/api/projects/${historyId}/blobs/${fileHash}`)
  1394. .reply(200)
  1395. const addFile = MockHistoryStore()
  1396. .post(`/api/projects/${historyId}/legacy_changes`)
  1397. .query({ end_version: 0 })
  1398. .reply(204)
  1399. // allow a 2nd resync
  1400. MockWeb()
  1401. .post(`/project/${this.project_id}/history/resync`)
  1402. .reply(204)
  1403. async.series(
  1404. [
  1405. cb => {
  1406. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1407. },
  1408. cb => {
  1409. const update = {
  1410. projectHistoryId: historyId,
  1411. resyncProjectStructureOnly: true,
  1412. resyncProjectStructure: {
  1413. docs: [{ path: '/main-renamed.tex' }],
  1414. files: [
  1415. {
  1416. file: this.file_id,
  1417. path: '/test.png',
  1418. _hash: fileHash,
  1419. createdBlob: true,
  1420. },
  1421. ],
  1422. },
  1423. meta: {
  1424. ts: this.timestamp,
  1425. },
  1426. }
  1427. ProjectHistoryClient.pushRawUpdate(this.project_id, update, cb)
  1428. },
  1429. cb => {
  1430. ProjectHistoryClient.flushProject(
  1431. this.project_id,
  1432. { allowErrors: true },
  1433. (err, res) => {
  1434. if (err) return cb(err)
  1435. expect(res.statusCode).to.equal(500)
  1436. expect(loggerError).to.have.been.calledWith(
  1437. sinon.match({
  1438. err: {
  1439. name: 'NeedFullProjectStructureResyncError',
  1440. message: 'aborting partial resync: touched doc',
  1441. },
  1442. })
  1443. )
  1444. getFailure(this.project_id, (err, failure) => {
  1445. if (err) return cb(err)
  1446. expect(failure).to.include({
  1447. error:
  1448. 'NeedFullProjectStructureResyncError: aborting partial resync: touched doc',
  1449. })
  1450. cb()
  1451. })
  1452. }
  1453. )
  1454. },
  1455. cb => {
  1456. // fails when previous resync did not finish
  1457. ProjectHistoryClient.resyncHistory(this.project_id, cb)
  1458. },
  1459. ],
  1460. error => {
  1461. if (error) {
  1462. throw error
  1463. }
  1464. assert(!addFile.isDone(), 'should not have persisted changes')
  1465. assert(
  1466. !docContentRequest.isDone(),
  1467. 'should not have requested doc content'
  1468. )
  1469. done()
  1470. }
  1471. )
  1472. })
  1473. })
  1474. })
  1475. })
  1476. })