MongoManager.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import sinon from 'sinon'
  2. import mongodb from 'mongodb-legacy'
  3. import path from 'node:path'
  4. import { assert, beforeEach, describe, expect, it, vi } from 'vitest'
  5. import Errors from '../../../app/js/Errors.js'
  6. const { ObjectId, BSON } = mongodb
  7. const modulePath = path.join(
  8. import.meta.dirname,
  9. '../../../app/js/MongoManager'
  10. )
  11. describe('MongoManager', () => {
  12. beforeEach(async ctx => {
  13. ctx.db = {
  14. docs: {
  15. updateOne: sinon.stub().resolves({ matchedCount: 1 }),
  16. insertOne: sinon.stub().resolves(),
  17. },
  18. }
  19. vi.doMock('../../../app/js/mongodb', () => ({
  20. default: {
  21. db: ctx.db,
  22. ObjectId,
  23. BSON,
  24. },
  25. }))
  26. vi.doMock('@overleaf/settings', () => ({
  27. default: {
  28. max_deleted_docs: 42,
  29. docstore: { archivingLockDurationMs: 5000 },
  30. },
  31. }))
  32. vi.doMock('../../../app/js/Errors', () => ({
  33. default: Errors,
  34. }))
  35. ctx.MongoManager = (await import(modulePath)).default
  36. ctx.projectId = new ObjectId().toString()
  37. ctx.docId = new ObjectId().toString()
  38. ctx.rev = 42
  39. ctx.stubbedErr = new Error('hello world')
  40. ctx.lines = ['Three French hens', 'Two turtle doves']
  41. })
  42. describe('findDoc', () => {
  43. beforeEach(async ctx => {
  44. ctx.doc = { name: 'mock-doc' }
  45. ctx.db.docs.findOne = sinon.stub().resolves(ctx.doc)
  46. ctx.filter = { lines: true }
  47. ctx.result = await ctx.MongoManager.findDoc(
  48. ctx.projectId,
  49. ctx.docId,
  50. ctx.filter
  51. )
  52. })
  53. it('should find the doc', ctx => {
  54. ctx.db.docs.findOne
  55. .calledWith(
  56. {
  57. _id: new ObjectId(ctx.docId),
  58. project_id: new ObjectId(ctx.projectId),
  59. },
  60. {
  61. projection: ctx.filter,
  62. }
  63. )
  64. .should.equal(true)
  65. })
  66. it('should return the doc', ctx => {
  67. expect(ctx.doc).to.deep.equal(ctx.doc)
  68. })
  69. })
  70. describe('patchDoc', () => {
  71. beforeEach(async ctx => {
  72. ctx.meta = { name: 'foo.tex' }
  73. await ctx.MongoManager.patchDoc(ctx.projectId, ctx.docId, ctx.meta)
  74. })
  75. it('should pass the parameter along', ctx => {
  76. ctx.db.docs.updateOne.should.have.been.calledWith(
  77. {
  78. _id: new ObjectId(ctx.docId),
  79. project_id: new ObjectId(ctx.projectId),
  80. },
  81. {
  82. $set: ctx.meta,
  83. }
  84. )
  85. })
  86. })
  87. describe('getProjectsDocs', () => {
  88. beforeEach(ctx => {
  89. ctx.filter = { lines: true }
  90. ctx.doc1 = { name: 'mock-doc1' }
  91. ctx.doc2 = { name: 'mock-doc2' }
  92. ctx.doc3 = { name: 'mock-doc3' }
  93. ctx.doc4 = { name: 'mock-doc4' }
  94. ctx.db.docs.find = sinon.stub().returns({
  95. toArray: sinon.stub().resolves([ctx.doc, ctx.doc3, ctx.doc4]),
  96. })
  97. })
  98. describe('with included_deleted = false', () => {
  99. beforeEach(async ctx => {
  100. ctx.result = await ctx.MongoManager.getProjectsDocs(
  101. ctx.projectId,
  102. { include_deleted: false },
  103. ctx.filter
  104. )
  105. })
  106. it('should find the non-deleted docs via the project_id', ctx => {
  107. ctx.db.docs.find
  108. .calledWith(
  109. {
  110. project_id: new ObjectId(ctx.projectId),
  111. deleted: { $ne: true },
  112. },
  113. {
  114. projection: ctx.filter,
  115. }
  116. )
  117. .should.equal(true)
  118. })
  119. it('should call return the docs', ctx => {
  120. expect(ctx.result).to.deep.equal([ctx.doc, ctx.doc3, ctx.doc4])
  121. })
  122. })
  123. describe('with included_deleted = true', () => {
  124. beforeEach(async ctx => {
  125. ctx.result = await ctx.MongoManager.getProjectsDocs(
  126. ctx.projectId,
  127. { include_deleted: true },
  128. ctx.filter
  129. )
  130. })
  131. it('should find all via the project_id', ctx => {
  132. ctx.db.docs.find
  133. .calledWith(
  134. {
  135. project_id: new ObjectId(ctx.projectId),
  136. },
  137. {
  138. projection: ctx.filter,
  139. }
  140. )
  141. .should.equal(true)
  142. })
  143. it('should return the docs', ctx => {
  144. expect(ctx.result).to.deep.equal([ctx.doc, ctx.doc3, ctx.doc4])
  145. })
  146. })
  147. })
  148. describe('getProjectsDeletedDocs', () => {
  149. beforeEach(async ctx => {
  150. ctx.filter = { name: true }
  151. ctx.doc1 = { _id: '1', name: 'mock-doc1.tex' }
  152. ctx.doc2 = { _id: '2', name: 'mock-doc2.tex' }
  153. ctx.doc3 = { _id: '3', name: 'mock-doc3.tex' }
  154. ctx.db.docs.find = sinon.stub().returns({
  155. toArray: sinon.stub().resolves([ctx.doc1, ctx.doc2, ctx.doc3]),
  156. })
  157. ctx.result = await ctx.MongoManager.getProjectsDeletedDocs(
  158. ctx.projectId,
  159. ctx.filter
  160. )
  161. })
  162. it('should find the deleted docs via the project_id', ctx => {
  163. ctx.db.docs.find
  164. .calledWith({
  165. project_id: new ObjectId(ctx.projectId),
  166. deleted: true,
  167. })
  168. .should.equal(true)
  169. })
  170. it('should filter, sort by deletedAt and limit', ctx => {
  171. ctx.db.docs.find
  172. .calledWith(sinon.match.any, {
  173. projection: ctx.filter,
  174. sort: { deletedAt: -1 },
  175. limit: 42,
  176. })
  177. .should.equal(true)
  178. })
  179. it('should return the docs', ctx => {
  180. expect(ctx.result).to.deep.equal([ctx.doc1, ctx.doc2, ctx.doc3])
  181. })
  182. })
  183. describe('upsertIntoDocCollection', () => {
  184. beforeEach(ctx => {
  185. ctx.oldRev = 77
  186. })
  187. it('should upsert the document', async ctx => {
  188. await ctx.MongoManager.upsertIntoDocCollection(
  189. ctx.projectId,
  190. ctx.docId,
  191. ctx.oldRev,
  192. { lines: ctx.lines }
  193. )
  194. const args = ctx.db.docs.updateOne.args[0]
  195. assert.deepEqual(args[0], {
  196. _id: new ObjectId(ctx.docId),
  197. project_id: new ObjectId(ctx.projectId),
  198. rev: ctx.oldRev,
  199. })
  200. assert.equal(args[1].$set.lines, ctx.lines)
  201. assert.equal(args[1].$inc.rev, 1)
  202. })
  203. it('should handle update error', async ctx => {
  204. ctx.db.docs.updateOne.rejects(ctx.stubbedErr)
  205. await expect(
  206. ctx.MongoManager.upsertIntoDocCollection(
  207. ctx.projectId,
  208. ctx.docId,
  209. ctx.rev,
  210. {
  211. lines: ctx.lines,
  212. }
  213. )
  214. ).to.be.rejectedWith(ctx.stubbedErr)
  215. })
  216. it('should insert without a previous rev', async ctx => {
  217. await ctx.MongoManager.upsertIntoDocCollection(
  218. ctx.projectId,
  219. ctx.docId,
  220. null,
  221. { lines: ctx.lines, ranges: ctx.ranges }
  222. )
  223. expect(ctx.db.docs.insertOne).to.have.been.calledWith({
  224. _id: new ObjectId(ctx.docId),
  225. project_id: new ObjectId(ctx.projectId),
  226. rev: 1,
  227. lines: ctx.lines,
  228. ranges: ctx.ranges,
  229. })
  230. })
  231. it('should handle generic insert error', async ctx => {
  232. ctx.db.docs.insertOne.rejects(ctx.stubbedErr)
  233. await expect(
  234. ctx.MongoManager.upsertIntoDocCollection(
  235. ctx.projectId,
  236. ctx.docId,
  237. null,
  238. { lines: ctx.lines, ranges: ctx.ranges }
  239. )
  240. ).to.be.rejectedWith(ctx.stubbedErr)
  241. })
  242. it('should handle duplicate insert error', async ctx => {
  243. ctx.db.docs.insertOne.rejects({ code: 11000 })
  244. await expect(
  245. ctx.MongoManager.upsertIntoDocCollection(
  246. ctx.projectId,
  247. ctx.docId,
  248. null,
  249. { lines: ctx.lines, ranges: ctx.ranges }
  250. )
  251. ).to.be.rejectedWith(Errors.DocRevValueError)
  252. })
  253. })
  254. describe('destroyProject', () => {
  255. beforeEach(async ctx => {
  256. ctx.projectId = new ObjectId()
  257. ctx.db.docs.deleteMany = sinon.stub().resolves()
  258. await ctx.MongoManager.destroyProject(ctx.projectId)
  259. })
  260. it('should destroy all docs', ctx => {
  261. sinon.assert.calledWith(ctx.db.docs.deleteMany, {
  262. project_id: ctx.projectId,
  263. })
  264. })
  265. })
  266. describe('checkRevUnchanged', ctx => {
  267. beforeEach(ctx => {
  268. ctx.doc = { _id: new ObjectId(), name: 'mock-doc', rev: 1 }
  269. })
  270. it('should not error when the rev has not changed', async ctx => {
  271. ctx.db.docs.findOne = sinon.stub().resolves({ rev: 1 })
  272. await ctx.MongoManager.checkRevUnchanged(ctx.doc)
  273. })
  274. it('should return an error when the rev has changed', async ctx => {
  275. ctx.db.docs.findOne = sinon.stub().resolves({ rev: 2 })
  276. await expect(
  277. ctx.MongoManager.checkRevUnchanged(ctx.doc)
  278. ).to.be.rejectedWith(Errors.DocModifiedError)
  279. })
  280. it('should return a value error if incoming rev is NaN', async ctx => {
  281. ctx.db.docs.findOne = sinon.stub().resolves({ rev: 2 })
  282. ctx.doc = { _id: new ObjectId(), name: 'mock-doc', rev: NaN }
  283. await expect(
  284. ctx.MongoManager.checkRevUnchanged(ctx.doc)
  285. ).to.be.rejectedWith(Errors.DocRevValueError)
  286. })
  287. it('should return a value error if checked doc rev is NaN', async ctx => {
  288. ctx.db.docs.findOne = sinon.stub().resolves({ rev: NaN })
  289. await expect(
  290. ctx.MongoManager.checkRevUnchanged(ctx.doc)
  291. ).to.be.rejectedWith(Errors.DocRevValueError)
  292. })
  293. })
  294. describe('restoreArchivedDoc', () => {
  295. beforeEach(ctx => {
  296. ctx.archivedDoc = {
  297. lines: ['a', 'b', 'c'],
  298. ranges: { some: 'ranges' },
  299. rev: 2,
  300. }
  301. })
  302. describe('complete doc', () => {
  303. beforeEach(async ctx => {
  304. await ctx.MongoManager.restoreArchivedDoc(
  305. ctx.projectId,
  306. ctx.docId,
  307. ctx.archivedDoc
  308. )
  309. })
  310. it('updates Mongo', ctx => {
  311. expect(ctx.db.docs.updateOne).to.have.been.calledWith(
  312. {
  313. _id: new ObjectId(ctx.docId),
  314. project_id: new ObjectId(ctx.projectId),
  315. rev: ctx.archivedDoc.rev,
  316. },
  317. {
  318. $set: {
  319. lines: ctx.archivedDoc.lines,
  320. ranges: ctx.archivedDoc.ranges,
  321. },
  322. $unset: {
  323. inS3: true,
  324. },
  325. }
  326. )
  327. })
  328. })
  329. describe('without ranges', () => {
  330. beforeEach(async ctx => {
  331. delete ctx.archivedDoc.ranges
  332. await ctx.MongoManager.restoreArchivedDoc(
  333. ctx.projectId,
  334. ctx.docId,
  335. ctx.archivedDoc
  336. )
  337. })
  338. it('sets ranges to an empty object', ctx => {
  339. expect(ctx.db.docs.updateOne).to.have.been.calledWith(
  340. {
  341. _id: new ObjectId(ctx.docId),
  342. project_id: new ObjectId(ctx.projectId),
  343. rev: ctx.archivedDoc.rev,
  344. },
  345. {
  346. $set: {
  347. lines: ctx.archivedDoc.lines,
  348. ranges: {},
  349. },
  350. $unset: {
  351. inS3: true,
  352. },
  353. }
  354. )
  355. })
  356. })
  357. describe("when the update doesn't succeed", () => {
  358. it('throws a DocRevValueError', async ctx => {
  359. ctx.db.docs.updateOne.resolves({ matchedCount: 0 })
  360. await expect(
  361. ctx.MongoManager.restoreArchivedDoc(
  362. ctx.projectId,
  363. ctx.docId,
  364. ctx.archivedDoc
  365. )
  366. ).to.be.rejectedWith(Errors.DocRevValueError)
  367. })
  368. })
  369. })
  370. })