DocArchiveManagerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../app/js/DocArchiveManager.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const { ObjectId } = require('mongodb')
  6. const Errors = require('../../../app/js/Errors')
  7. describe('DocArchiveManager', function () {
  8. let DocArchiveManager,
  9. PersistorManager,
  10. MongoManager,
  11. RangeManager,
  12. Settings,
  13. Crypto,
  14. StreamUtils,
  15. HashDigest,
  16. HashUpdate,
  17. archivedDocs,
  18. mongoDocs,
  19. archivedDoc,
  20. archivedDocJson,
  21. md5Sum,
  22. projectId,
  23. readStream,
  24. stream
  25. beforeEach(function () {
  26. md5Sum = 'decafbad'
  27. RangeManager = {
  28. jsonRangesToMongo: sinon.stub().returns({ mongo: 'ranges' }),
  29. }
  30. Settings = {
  31. docstore: {
  32. backend: 'gcs',
  33. bucket: 'wombat',
  34. },
  35. parallelArchiveJobs: 3,
  36. }
  37. HashDigest = sinon.stub().returns(md5Sum)
  38. HashUpdate = sinon.stub().returns({ digest: HashDigest })
  39. Crypto = {
  40. createHash: sinon.stub().returns({ update: HashUpdate }),
  41. }
  42. StreamUtils = {
  43. ReadableString: sinon.stub().returns({ stream: 'readStream' }),
  44. }
  45. projectId = new ObjectId()
  46. archivedDocs = [
  47. {
  48. _id: new ObjectId(),
  49. inS3: true,
  50. rev: 2,
  51. },
  52. {
  53. _id: new ObjectId(),
  54. inS3: true,
  55. rev: 4,
  56. },
  57. {
  58. _id: new ObjectId(),
  59. inS3: true,
  60. rev: 6,
  61. },
  62. ]
  63. mongoDocs = [
  64. {
  65. _id: new ObjectId(),
  66. lines: ['one', 'two', 'three'],
  67. rev: 2,
  68. },
  69. {
  70. _id: new ObjectId(),
  71. lines: ['aaa', 'bbb', 'ccc'],
  72. rev: 4,
  73. },
  74. {
  75. _id: new ObjectId(),
  76. inS3: true,
  77. rev: 6,
  78. },
  79. {
  80. _id: new ObjectId(),
  81. inS3: true,
  82. rev: 6,
  83. },
  84. {
  85. _id: new ObjectId(),
  86. lines: ['111', '222', '333'],
  87. rev: 6,
  88. },
  89. ]
  90. archivedDoc = {
  91. lines: mongoDocs[0].lines,
  92. rev: mongoDocs[0].rev,
  93. }
  94. archivedDocJson = JSON.stringify({ ...archivedDoc, schema_v: 1 })
  95. stream = {
  96. on: sinon.stub(),
  97. resume: sinon.stub(),
  98. }
  99. stream.on.withArgs('data').yields(Buffer.from(archivedDocJson, 'utf8'))
  100. stream.on.withArgs('end').yields()
  101. readStream = {
  102. stream: 'readStream',
  103. }
  104. PersistorManager = {
  105. getObjectStream: sinon.stub().resolves(stream),
  106. sendStream: sinon.stub().resolves(),
  107. getObjectMd5Hash: sinon.stub().resolves(md5Sum),
  108. deleteObject: sinon.stub().resolves(),
  109. deleteDirectory: sinon.stub().resolves(),
  110. }
  111. const getNonArchivedProjectDocIds = sinon.stub()
  112. getNonArchivedProjectDocIds
  113. .onCall(0)
  114. .resolves(mongoDocs.filter(doc => !doc.inS3).map(doc => doc._id))
  115. getNonArchivedProjectDocIds.onCall(1).resolves([])
  116. const getArchivedProjectDocs = sinon.stub()
  117. getArchivedProjectDocs.onCall(0).resolves(archivedDocs)
  118. getArchivedProjectDocs.onCall(1).resolves([])
  119. const fakeGetDoc = async (_projectId, _docId) => {
  120. if (_projectId.equals(projectId)) {
  121. for (const mongoDoc of mongoDocs.concat(archivedDocs)) {
  122. if (mongoDoc._id.equals(_docId)) {
  123. return mongoDoc
  124. }
  125. }
  126. }
  127. throw new Errors.NotFoundError()
  128. }
  129. MongoManager = {
  130. promises: {
  131. markDocAsArchived: sinon.stub().resolves(),
  132. restoreArchivedDoc: sinon.stub().resolves(),
  133. upsertIntoDocCollection: sinon.stub().resolves(),
  134. getProjectsDocs: sinon.stub().resolves(mongoDocs),
  135. getNonDeletedArchivedProjectDocs: getArchivedProjectDocs,
  136. getNonArchivedProjectDocIds,
  137. getArchivedProjectDocs,
  138. findDoc: sinon.stub().callsFake(fakeGetDoc),
  139. getDocForArchiving: sinon.stub().callsFake(fakeGetDoc),
  140. destroyProject: sinon.stub().resolves(),
  141. },
  142. }
  143. DocArchiveManager = SandboxedModule.require(modulePath, {
  144. requires: {
  145. '@overleaf/settings': Settings,
  146. crypto: Crypto,
  147. '@overleaf/stream-utils': StreamUtils,
  148. './MongoManager': MongoManager,
  149. './RangeManager': RangeManager,
  150. './PersistorManager': PersistorManager,
  151. './Errors': Errors,
  152. },
  153. })
  154. })
  155. describe('archiveDoc', function () {
  156. it('should resolve when passed a valid document', async function () {
  157. await expect(
  158. DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  159. ).to.eventually.be.fulfilled
  160. })
  161. it('should throw an error if the doc has no lines', async function () {
  162. const doc = mongoDocs[0]
  163. doc.lines = null
  164. await expect(
  165. DocArchiveManager.promises.archiveDoc(projectId, doc._id)
  166. ).to.eventually.be.rejectedWith('doc has no lines')
  167. })
  168. it('should add the schema version', async function () {
  169. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[1]._id)
  170. expect(StreamUtils.ReadableString).to.have.been.calledWith(
  171. sinon.match(/"schema_v":1/)
  172. )
  173. })
  174. it('should calculate the hex md5 sum of the content', async function () {
  175. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  176. expect(Crypto.createHash).to.have.been.calledWith('md5')
  177. expect(HashUpdate).to.have.been.calledWith(archivedDocJson)
  178. expect(HashDigest).to.have.been.calledWith('hex')
  179. })
  180. it('should pass the md5 hash to the object persistor for verification', async function () {
  181. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  182. expect(PersistorManager.sendStream).to.have.been.calledWith(
  183. sinon.match.any,
  184. sinon.match.any,
  185. sinon.match.any,
  186. { sourceMd5: md5Sum }
  187. )
  188. })
  189. it('should pass the correct bucket and key to the persistor', async function () {
  190. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  191. expect(PersistorManager.sendStream).to.have.been.calledWith(
  192. Settings.docstore.bucket,
  193. `${projectId}/${mongoDocs[0]._id}`
  194. )
  195. })
  196. it('should create a stream from the encoded json and send it', async function () {
  197. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  198. expect(StreamUtils.ReadableString).to.have.been.calledWith(
  199. archivedDocJson
  200. )
  201. expect(PersistorManager.sendStream).to.have.been.calledWith(
  202. sinon.match.any,
  203. sinon.match.any,
  204. readStream
  205. )
  206. })
  207. it('should mark the doc as archived', async function () {
  208. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  209. expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
  210. projectId,
  211. mongoDocs[0]._id,
  212. mongoDocs[0].rev
  213. )
  214. })
  215. describe('when archiving is not configured', function () {
  216. beforeEach(function () {
  217. Settings.docstore.backend = undefined
  218. })
  219. it('should bail out early', async function () {
  220. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  221. expect(MongoManager.promises.getDocForArchiving).to.not.have.been.called
  222. })
  223. })
  224. describe('with null bytes in the result', function () {
  225. const _stringify = JSON.stringify
  226. beforeEach(function () {
  227. JSON.stringify = sinon.stub().returns('{"bad": "\u0000"}')
  228. })
  229. afterEach(function () {
  230. JSON.stringify = _stringify
  231. })
  232. it('should return an error', async function () {
  233. await expect(
  234. DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  235. ).to.eventually.be.rejectedWith('null bytes detected')
  236. })
  237. })
  238. })
  239. describe('unarchiveDoc', function () {
  240. let docId, lines, rev
  241. describe('when the doc is in S3', function () {
  242. beforeEach(function () {
  243. MongoManager.promises.findDoc = sinon
  244. .stub()
  245. .resolves({ inS3: true, rev })
  246. docId = mongoDocs[0]._id
  247. lines = ['doc', 'lines']
  248. rev = 123
  249. })
  250. it('should resolve when passed a valid document', async function () {
  251. await expect(DocArchiveManager.promises.unarchiveDoc(projectId, docId))
  252. .to.eventually.be.fulfilled
  253. })
  254. it('should test md5 validity with the raw buffer', async function () {
  255. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  256. expect(HashUpdate).to.have.been.calledWith(
  257. sinon.match.instanceOf(Buffer)
  258. )
  259. })
  260. it('should throw an error if the md5 does not match', async function () {
  261. PersistorManager.getObjectMd5Hash.resolves('badf00d')
  262. await expect(
  263. DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  264. ).to.eventually.be.rejected.and.be.instanceof(Errors.Md5MismatchError)
  265. })
  266. it('should restore the doc in Mongo', async function () {
  267. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  268. expect(
  269. MongoManager.promises.restoreArchivedDoc
  270. ).to.have.been.calledWith(projectId, docId, archivedDoc)
  271. })
  272. describe('when archiving is not configured', function () {
  273. beforeEach(function () {
  274. Settings.docstore.backend = undefined
  275. })
  276. it('should error out on archived doc', async function () {
  277. await expect(
  278. DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  279. ).to.eventually.be.rejected.and.match(
  280. /found archived doc, but archiving backend is not configured/
  281. )
  282. })
  283. it('should return early on non-archived doc', async function () {
  284. MongoManager.promises.findDoc = sinon.stub().resolves({ rev })
  285. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  286. expect(PersistorManager.getObjectMd5Hash).to.not.have.been.called
  287. })
  288. })
  289. describe('doc contents', function () {
  290. let archivedDoc
  291. describe('when the doc has the old schema', function () {
  292. beforeEach(function () {
  293. archivedDoc = lines
  294. archivedDocJson = JSON.stringify(archivedDoc)
  295. stream.on
  296. .withArgs('data')
  297. .yields(Buffer.from(archivedDocJson, 'utf8'))
  298. })
  299. it('should return the docs lines', async function () {
  300. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  301. expect(
  302. MongoManager.promises.restoreArchivedDoc
  303. ).to.have.been.calledWith(projectId, docId, { lines, rev })
  304. })
  305. })
  306. describe('with the new schema and ranges', function () {
  307. beforeEach(function () {
  308. archivedDoc = {
  309. lines,
  310. ranges: { json: 'ranges' },
  311. rev: 456,
  312. schema_v: 1,
  313. }
  314. archivedDocJson = JSON.stringify(archivedDoc)
  315. stream.on
  316. .withArgs('data')
  317. .yields(Buffer.from(archivedDocJson, 'utf8'))
  318. })
  319. it('should return the doc lines and ranges', async function () {
  320. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  321. expect(
  322. MongoManager.promises.restoreArchivedDoc
  323. ).to.have.been.calledWith(projectId, docId, {
  324. lines,
  325. ranges: { mongo: 'ranges' },
  326. rev: 456,
  327. })
  328. })
  329. })
  330. describe('with the new schema and no ranges', function () {
  331. beforeEach(function () {
  332. archivedDoc = { lines, rev: 456, schema_v: 1 }
  333. archivedDocJson = JSON.stringify(archivedDoc)
  334. stream.on
  335. .withArgs('data')
  336. .yields(Buffer.from(archivedDocJson, 'utf8'))
  337. })
  338. it('should return only the doc lines', async function () {
  339. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  340. expect(
  341. MongoManager.promises.restoreArchivedDoc
  342. ).to.have.been.calledWith(projectId, docId, { lines, rev: 456 })
  343. })
  344. })
  345. describe('with the new schema and no rev', function () {
  346. beforeEach(function () {
  347. archivedDoc = { lines, schema_v: 1 }
  348. archivedDocJson = JSON.stringify(archivedDoc)
  349. stream.on
  350. .withArgs('data')
  351. .yields(Buffer.from(archivedDocJson, 'utf8'))
  352. })
  353. it('should use the rev obtained from Mongo', async function () {
  354. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  355. expect(
  356. MongoManager.promises.restoreArchivedDoc
  357. ).to.have.been.calledWith(projectId, docId, { lines, rev })
  358. })
  359. })
  360. describe('with an unrecognised schema', function () {
  361. beforeEach(function () {
  362. archivedDoc = { lines, schema_v: 2 }
  363. archivedDocJson = JSON.stringify(archivedDoc)
  364. stream.on
  365. .withArgs('data')
  366. .yields(Buffer.from(archivedDocJson, 'utf8'))
  367. })
  368. it('should throw an error', async function () {
  369. await expect(
  370. DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  371. ).to.eventually.be.rejectedWith(
  372. "I don't understand the doc format in s3"
  373. )
  374. })
  375. })
  376. })
  377. })
  378. it('should not do anything if the file is already unarchived', async function () {
  379. MongoManager.promises.findDoc.resolves({ inS3: false })
  380. await DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  381. expect(PersistorManager.getObjectStream).not.to.have.been.called
  382. })
  383. it('should throw an error if the file is not found', async function () {
  384. PersistorManager.getObjectStream = sinon
  385. .stub()
  386. .rejects(new Errors.NotFoundError())
  387. await expect(
  388. DocArchiveManager.promises.unarchiveDoc(projectId, docId)
  389. ).to.eventually.be.rejected.and.be.instanceof(Errors.NotFoundError)
  390. })
  391. })
  392. describe('destroyProject', function () {
  393. describe('when archiving is enabled', function () {
  394. beforeEach(async function () {
  395. await DocArchiveManager.promises.destroyProject(projectId)
  396. })
  397. it('should delete the project in Mongo', function () {
  398. expect(MongoManager.promises.destroyProject).to.have.been.calledWith(
  399. projectId
  400. )
  401. })
  402. it('should delete the project in the persistor', function () {
  403. expect(PersistorManager.deleteDirectory).to.have.been.calledWith(
  404. Settings.docstore.bucket,
  405. projectId
  406. )
  407. })
  408. })
  409. describe('when archiving is disabled', function () {
  410. beforeEach(async function () {
  411. Settings.docstore.backend = ''
  412. await DocArchiveManager.promises.destroyProject(projectId)
  413. })
  414. it('should delete the project in Mongo', function () {
  415. expect(MongoManager.promises.destroyProject).to.have.been.calledWith(
  416. projectId
  417. )
  418. })
  419. it('should not delete the project in the persistor', function () {
  420. expect(PersistorManager.deleteDirectory).not.to.have.been.called
  421. })
  422. })
  423. })
  424. describe('archiveAllDocs', function () {
  425. it('should resolve with valid arguments', async function () {
  426. await expect(DocArchiveManager.promises.archiveAllDocs(projectId)).to
  427. .eventually.be.fulfilled
  428. })
  429. it('should archive all project docs which are not in s3', async function () {
  430. await DocArchiveManager.promises.archiveAllDocs(projectId)
  431. // not inS3
  432. expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
  433. projectId,
  434. mongoDocs[0]._id
  435. )
  436. expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
  437. projectId,
  438. mongoDocs[1]._id
  439. )
  440. expect(MongoManager.promises.markDocAsArchived).to.have.been.calledWith(
  441. projectId,
  442. mongoDocs[4]._id
  443. )
  444. // inS3
  445. expect(
  446. MongoManager.promises.markDocAsArchived
  447. ).not.to.have.been.calledWith(projectId, mongoDocs[2]._id)
  448. expect(
  449. MongoManager.promises.markDocAsArchived
  450. ).not.to.have.been.calledWith(projectId, mongoDocs[3]._id)
  451. })
  452. describe('when archiving is not configured', function () {
  453. beforeEach(function () {
  454. Settings.docstore.backend = undefined
  455. })
  456. it('should bail out early', async function () {
  457. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  458. expect(MongoManager.promises.getNonArchivedProjectDocIds).to.not.have
  459. .been.called
  460. })
  461. })
  462. })
  463. describe('unArchiveAllDocs', function () {
  464. it('should resolve with valid arguments', async function () {
  465. await expect(DocArchiveManager.promises.unArchiveAllDocs(projectId)).to
  466. .eventually.be.fulfilled
  467. })
  468. it('should unarchive all inS3 docs', async function () {
  469. await DocArchiveManager.promises.unArchiveAllDocs(projectId)
  470. for (const doc of archivedDocs) {
  471. expect(PersistorManager.getObjectStream).to.have.been.calledWith(
  472. Settings.docstore.bucket,
  473. `${projectId}/${doc._id}`
  474. )
  475. }
  476. })
  477. describe('when archiving is not configured', function () {
  478. beforeEach(function () {
  479. Settings.docstore.backend = undefined
  480. })
  481. it('should bail out early', async function () {
  482. await DocArchiveManager.promises.archiveDoc(projectId, mongoDocs[0]._id)
  483. expect(MongoManager.promises.getNonDeletedArchivedProjectDocs).to.not
  484. .have.been.called
  485. })
  486. })
  487. })
  488. })