MongoManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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().yields(null, { matchedCount: 1 }),
  15. insertOne: sinon.stub().yields(null),
  16. },
  17. docOps: {},
  18. }
  19. this.MongoManager = SandboxedModule.require(modulePath, {
  20. requires: {
  21. './mongodb': {
  22. db: this.db,
  23. ObjectId,
  24. },
  25. '@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
  26. '@overleaf/settings': {
  27. max_deleted_docs: 42,
  28. docstore: { archivingLockDurationMs: 5000 },
  29. },
  30. './Errors': Errors,
  31. },
  32. })
  33. this.projectId = ObjectId().toString()
  34. this.docId = ObjectId().toString()
  35. this.rev = 42
  36. this.callback = sinon.stub()
  37. this.stubbedErr = new Error('hello world')
  38. })
  39. describe('findDoc', function () {
  40. beforeEach(function () {
  41. this.doc = { name: 'mock-doc' }
  42. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  43. this.filter = { lines: true }
  44. this.MongoManager.findDoc(
  45. this.projectId,
  46. this.docId,
  47. this.filter,
  48. this.callback
  49. )
  50. })
  51. it('should find the doc', function () {
  52. this.db.docs.findOne
  53. .calledWith(
  54. {
  55. _id: ObjectId(this.docId),
  56. project_id: ObjectId(this.projectId),
  57. },
  58. {
  59. projection: this.filter,
  60. }
  61. )
  62. .should.equal(true)
  63. })
  64. it('should call the callback with the doc', function () {
  65. this.callback.calledWith(null, this.doc).should.equal(true)
  66. })
  67. })
  68. describe('patchDoc', function () {
  69. beforeEach(function (done) {
  70. this.meta = { name: 'foo.tex' }
  71. this.callback.callsFake(done)
  72. this.MongoManager.patchDoc(
  73. this.projectId,
  74. this.docId,
  75. this.meta,
  76. this.callback
  77. )
  78. })
  79. it('should pass the parameter along', function () {
  80. this.db.docs.updateOne.should.have.been.calledWith(
  81. {
  82. _id: ObjectId(this.docId),
  83. project_id: ObjectId(this.projectId),
  84. },
  85. {
  86. $set: this.meta,
  87. },
  88. this.callback
  89. )
  90. })
  91. })
  92. describe('getProjectsDocs', function () {
  93. beforeEach(function () {
  94. this.filter = { lines: true }
  95. this.doc1 = { name: 'mock-doc1' }
  96. this.doc2 = { name: 'mock-doc2' }
  97. this.doc3 = { name: 'mock-doc3' }
  98. this.doc4 = { name: 'mock-doc4' }
  99. this.db.docs.find = sinon.stub().returns({
  100. toArray: sinon
  101. .stub()
  102. .callsArgWith(0, null, [this.doc, this.doc3, this.doc4]),
  103. })
  104. })
  105. describe('with included_deleted = false', function () {
  106. beforeEach(function () {
  107. this.MongoManager.getProjectsDocs(
  108. this.projectId,
  109. { include_deleted: false },
  110. this.filter,
  111. this.callback
  112. )
  113. })
  114. it('should find the non-deleted docs via the project_id', function () {
  115. this.db.docs.find
  116. .calledWith(
  117. {
  118. project_id: ObjectId(this.projectId),
  119. deleted: { $ne: true },
  120. },
  121. {
  122. projection: this.filter,
  123. }
  124. )
  125. .should.equal(true)
  126. })
  127. it('should call the callback with the docs', function () {
  128. this.callback
  129. .calledWith(null, [this.doc, this.doc3, this.doc4])
  130. .should.equal(true)
  131. })
  132. })
  133. describe('with included_deleted = true', function () {
  134. beforeEach(function () {
  135. this.MongoManager.getProjectsDocs(
  136. this.projectId,
  137. { include_deleted: true },
  138. this.filter,
  139. this.callback
  140. )
  141. })
  142. it('should find all via the project_id', function () {
  143. this.db.docs.find
  144. .calledWith(
  145. {
  146. project_id: ObjectId(this.projectId),
  147. },
  148. {
  149. projection: this.filter,
  150. }
  151. )
  152. .should.equal(true)
  153. })
  154. it('should call the callback with the docs', function () {
  155. this.callback
  156. .calledWith(null, [this.doc, this.doc3, this.doc4])
  157. .should.equal(true)
  158. })
  159. })
  160. })
  161. describe('getProjectsDeletedDocs', function () {
  162. beforeEach(function (done) {
  163. this.filter = { name: true }
  164. this.doc1 = { _id: '1', name: 'mock-doc1.tex' }
  165. this.doc2 = { _id: '2', name: 'mock-doc2.tex' }
  166. this.doc3 = { _id: '3', name: 'mock-doc3.tex' }
  167. this.db.docs.find = sinon.stub().returns({
  168. toArray: sinon.stub().yields(null, [this.doc1, this.doc2, this.doc3]),
  169. })
  170. this.callback.callsFake(done)
  171. this.MongoManager.getProjectsDeletedDocs(
  172. this.projectId,
  173. this.filter,
  174. this.callback
  175. )
  176. })
  177. it('should find the deleted docs via the project_id', function () {
  178. this.db.docs.find
  179. .calledWith({
  180. project_id: ObjectId(this.projectId),
  181. deleted: true,
  182. })
  183. .should.equal(true)
  184. })
  185. it('should filter, sort by deletedAt and limit', function () {
  186. this.db.docs.find
  187. .calledWith(sinon.match.any, {
  188. projection: this.filter,
  189. sort: { deletedAt: -1 },
  190. limit: 42,
  191. })
  192. .should.equal(true)
  193. })
  194. it('should call the callback with the docs', function () {
  195. this.callback
  196. .calledWith(null, [this.doc1, this.doc2, this.doc3])
  197. .should.equal(true)
  198. })
  199. })
  200. describe('upsertIntoDocCollection', function () {
  201. beforeEach(function () {
  202. this.oldRev = 77
  203. })
  204. it('should upsert the document', function (done) {
  205. this.MongoManager.upsertIntoDocCollection(
  206. this.projectId,
  207. this.docId,
  208. this.oldRev,
  209. { lines: this.lines },
  210. err => {
  211. assert.equal(err, null)
  212. const args = this.db.docs.updateOne.args[0]
  213. assert.deepEqual(args[0], {
  214. _id: ObjectId(this.docId),
  215. project_id: ObjectId(this.projectId),
  216. rev: this.oldRev,
  217. })
  218. assert.equal(args[1].$set.lines, this.lines)
  219. assert.equal(args[1].$inc.rev, 1)
  220. done()
  221. }
  222. )
  223. })
  224. it('should handle update error', function (done) {
  225. this.db.docs.updateOne.yields(this.stubbedErr)
  226. this.MongoManager.upsertIntoDocCollection(
  227. this.projectId,
  228. this.docId,
  229. this.rev,
  230. { lines: this.lines },
  231. err => {
  232. err.should.equal(this.stubbedErr)
  233. done()
  234. }
  235. )
  236. })
  237. it('should insert without a previous rev', function (done) {
  238. this.MongoManager.upsertIntoDocCollection(
  239. this.projectId,
  240. this.docId,
  241. null,
  242. { lines: this.lines, ranges: this.ranges },
  243. err => {
  244. expect(this.db.docs.insertOne).to.have.been.calledWith({
  245. _id: ObjectId(this.docId),
  246. project_id: ObjectId(this.projectId),
  247. rev: 1,
  248. lines: this.lines,
  249. ranges: this.ranges,
  250. })
  251. expect(err).to.not.exist
  252. done()
  253. }
  254. )
  255. })
  256. it('should handle generic insert error', function (done) {
  257. this.db.docs.insertOne.yields(this.stubbedErr)
  258. this.MongoManager.upsertIntoDocCollection(
  259. this.projectId,
  260. this.docId,
  261. null,
  262. { lines: this.lines, ranges: this.ranges },
  263. err => {
  264. expect(err).to.equal(this.stubbedErr)
  265. done()
  266. }
  267. )
  268. })
  269. it('should handle duplicate insert error', function (done) {
  270. this.db.docs.insertOne.yields({ code: 11000 })
  271. this.MongoManager.upsertIntoDocCollection(
  272. this.projectId,
  273. this.docId,
  274. null,
  275. { lines: this.lines, ranges: this.ranges },
  276. err => {
  277. expect(err).to.be.instanceof(Errors.DocRevValueError)
  278. done()
  279. }
  280. )
  281. })
  282. })
  283. describe('destroyProject', function () {
  284. beforeEach(function (done) {
  285. this.projectId = ObjectId()
  286. this.docIds = [ObjectId(), ObjectId()]
  287. this.db.docs.deleteMany = sinon.stub().yields()
  288. this.db.docOps.deleteMany = sinon.stub().yields()
  289. this.db.docs.find = sinon
  290. .stub()
  291. .withArgs({ project_id: this.projectId })
  292. .returns({
  293. toArray: sinon.stub().yields(
  294. null,
  295. this.docIds.map(id => ({
  296. _id: id,
  297. }))
  298. ),
  299. })
  300. this.MongoManager.destroyProject(this.projectId, done)
  301. })
  302. it('should destroy all docs', function () {
  303. sinon.assert.calledWith(this.db.docs.deleteMany, {
  304. project_id: this.projectId,
  305. })
  306. })
  307. it('should destroy the docOps', function () {
  308. sinon.assert.calledWith(this.db.docOps.deleteMany, {
  309. doc_id: { $in: this.docIds },
  310. })
  311. })
  312. })
  313. describe('getDocVersion', function () {
  314. describe('when the doc exists', function () {
  315. beforeEach(function () {
  316. this.doc = { version: (this.version = 42) }
  317. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  318. this.MongoManager.getDocVersion(this.docId, this.callback)
  319. })
  320. it('should look for the doc in the database', function () {
  321. this.db.docOps.findOne
  322. .calledWith(
  323. { doc_id: ObjectId(this.docId) },
  324. {
  325. projection: { version: 1 },
  326. }
  327. )
  328. .should.equal(true)
  329. })
  330. it('should call the callback with the version', function () {
  331. this.callback.calledWith(null, this.version).should.equal(true)
  332. })
  333. })
  334. describe("when the doc doesn't exist", function () {
  335. beforeEach(function () {
  336. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
  337. this.MongoManager.getDocVersion(this.docId, this.callback)
  338. })
  339. it('should call the callback with 0', function () {
  340. this.callback.calledWith(null, 0).should.equal(true)
  341. })
  342. })
  343. })
  344. describe('setDocVersion', function () {
  345. beforeEach(function () {
  346. this.version = 42
  347. this.db.docOps.updateOne = sinon.stub().callsArg(3)
  348. this.MongoManager.setDocVersion(this.docId, this.version, this.callback)
  349. })
  350. it('should update the doc version', function () {
  351. this.db.docOps.updateOne
  352. .calledWith(
  353. {
  354. doc_id: ObjectId(this.docId),
  355. },
  356. {
  357. $set: {
  358. version: this.version,
  359. },
  360. },
  361. {
  362. upsert: true,
  363. }
  364. )
  365. .should.equal(true)
  366. })
  367. it('should call the callback', function () {
  368. this.callback.called.should.equal(true)
  369. })
  370. })
  371. describe('withRevCheck', function () {
  372. this.beforeEach(function () {
  373. this.doc = { _id: ObjectId(), name: 'mock-doc', rev: 1 }
  374. this.testFunction = sinon.stub().yields(null, 'foo')
  375. })
  376. it('should call the callback when the rev has not changed', function (done) {
  377. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 1 })
  378. this.MongoManager.withRevCheck(
  379. this.doc,
  380. this.testFunction,
  381. (err, result) => {
  382. result.should.equal('foo')
  383. assert.isNull(err)
  384. done()
  385. }
  386. )
  387. })
  388. it('should return an error when the rev has changed', function (done) {
  389. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 })
  390. this.MongoManager.withRevCheck(
  391. this.doc,
  392. this.testFunction,
  393. (err, result) => {
  394. err.should.be.instanceof(Errors.DocModifiedError)
  395. done()
  396. }
  397. )
  398. })
  399. it('should return a value error if incoming rev is NaN', function (done) {
  400. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 })
  401. this.doc = { _id: ObjectId(), name: 'mock-doc', rev: NaN }
  402. this.MongoManager.withRevCheck(
  403. this.doc,
  404. this.testFunction,
  405. (err, result) => {
  406. err.should.be.instanceof(Errors.DocRevValueError)
  407. done()
  408. }
  409. )
  410. })
  411. it('should return a value error if checked doc rev is NaN', function (done) {
  412. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: NaN })
  413. this.MongoManager.withRevCheck(
  414. this.doc,
  415. this.testFunction,
  416. (err, result) => {
  417. err.should.be.instanceof(Errors.DocRevValueError)
  418. done()
  419. }
  420. )
  421. })
  422. })
  423. describe('restoreArchivedDoc', function () {
  424. beforeEach(function () {
  425. this.archivedDoc = {
  426. lines: ['a', 'b', 'c'],
  427. ranges: { some: 'ranges' },
  428. rev: 2,
  429. }
  430. })
  431. describe('complete doc', function () {
  432. beforeEach(function (done) {
  433. this.MongoManager.restoreArchivedDoc(
  434. this.projectId,
  435. this.docId,
  436. this.archivedDoc,
  437. done
  438. )
  439. })
  440. it('updates Mongo', function () {
  441. expect(this.db.docs.updateOne).to.have.been.calledWith(
  442. {
  443. _id: ObjectId(this.docId),
  444. project_id: ObjectId(this.projectId),
  445. rev: this.archivedDoc.rev,
  446. },
  447. {
  448. $set: {
  449. lines: this.archivedDoc.lines,
  450. ranges: this.archivedDoc.ranges,
  451. },
  452. $unset: {
  453. inS3: true,
  454. },
  455. }
  456. )
  457. })
  458. })
  459. describe('without ranges', function () {
  460. beforeEach(function (done) {
  461. delete this.archivedDoc.ranges
  462. this.MongoManager.restoreArchivedDoc(
  463. this.projectId,
  464. this.docId,
  465. this.archivedDoc,
  466. done
  467. )
  468. })
  469. it('sets ranges to an empty object', function () {
  470. expect(this.db.docs.updateOne).to.have.been.calledWith(
  471. {
  472. _id: ObjectId(this.docId),
  473. project_id: ObjectId(this.projectId),
  474. rev: this.archivedDoc.rev,
  475. },
  476. {
  477. $set: {
  478. lines: this.archivedDoc.lines,
  479. ranges: {},
  480. },
  481. $unset: {
  482. inS3: true,
  483. },
  484. }
  485. )
  486. })
  487. })
  488. describe("when the update doesn't succeed", function () {
  489. it('throws a DocRevValueError', function (done) {
  490. this.db.docs.updateOne.yields(null, { matchedCount: 0 })
  491. this.MongoManager.restoreArchivedDoc(
  492. this.projectId,
  493. this.docId,
  494. this.archivedDoc,
  495. err => {
  496. expect(err).to.be.instanceof(Errors.DocRevValueError)
  497. done()
  498. }
  499. )
  500. })
  501. })
  502. })
  503. })