MongoManagerTests.js 11 KB

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