MongoManagerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. const SandboxedModule = require('sandboxed-module')
  12. const sinon = require('sinon')
  13. const modulePath = require('path').join(
  14. __dirname,
  15. '../../../app/js/MongoManager'
  16. )
  17. const { ObjectId } = require('mongodb')
  18. const { assert } = require('chai')
  19. const Errors = require('../../../app/js/Errors')
  20. describe('MongoManager', function () {
  21. beforeEach(function () {
  22. this.MongoManager = SandboxedModule.require(modulePath, {
  23. requires: {
  24. './mongodb': {
  25. db: (this.db = { docs: {}, docOps: {} }),
  26. ObjectId,
  27. },
  28. '@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
  29. '@overleaf/settings': { max_deleted_docs: 42 },
  30. './Errors': Errors,
  31. },
  32. })
  33. this.project_id = ObjectId().toString()
  34. this.doc_id = ObjectId().toString()
  35. this.callback = sinon.stub()
  36. return (this.stubbedErr = new Error('hello world'))
  37. })
  38. describe('findDoc', function () {
  39. beforeEach(function () {
  40. this.doc = { name: 'mock-doc' }
  41. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  42. this.filter = { lines: true }
  43. return this.MongoManager.findDoc(
  44. this.project_id,
  45. this.doc_id,
  46. this.filter,
  47. this.callback
  48. )
  49. })
  50. it('should find the doc', function () {
  51. this.db.docs.findOne
  52. .calledWith(
  53. {
  54. _id: ObjectId(this.doc_id),
  55. project_id: ObjectId(this.project_id),
  56. },
  57. {
  58. projection: this.filter,
  59. }
  60. )
  61. .should.equal(true)
  62. })
  63. return it('should call the callback with the doc', function () {
  64. return this.callback.calledWith(null, this.doc).should.equal(true)
  65. })
  66. })
  67. describe('patchDoc', function () {
  68. beforeEach(function (done) {
  69. this.db.docs.updateOne = sinon.stub().yields(null)
  70. this.meta = { name: 'foo.tex' }
  71. this.callback.callsFake(done)
  72. this.MongoManager.patchDoc(
  73. this.project_id,
  74. this.doc_id,
  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.doc_id),
  83. project_id: ObjectId(this.project_id),
  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. return this.MongoManager.getProjectsDocs(
  108. this.project_id,
  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. return this.db.docs.find
  116. .calledWith(
  117. {
  118. project_id: ObjectId(this.project_id),
  119. deleted: { $ne: true },
  120. },
  121. {
  122. projection: this.filter,
  123. }
  124. )
  125. .should.equal(true)
  126. })
  127. return it('should call the callback with the docs', function () {
  128. return this.callback
  129. .calledWith(null, [this.doc, this.doc3, this.doc4])
  130. .should.equal(true)
  131. })
  132. })
  133. return describe('with included_deleted = true', function () {
  134. beforeEach(function () {
  135. return this.MongoManager.getProjectsDocs(
  136. this.project_id,
  137. { include_deleted: true },
  138. this.filter,
  139. this.callback
  140. )
  141. })
  142. it('should find all via the project_id', function () {
  143. return this.db.docs.find
  144. .calledWith(
  145. {
  146. project_id: ObjectId(this.project_id),
  147. },
  148. {
  149. projection: this.filter,
  150. }
  151. )
  152. .should.equal(true)
  153. })
  154. return it('should call the callback with the docs', function () {
  155. return 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.project_id,
  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.project_id),
  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.db.docs.updateOne = sinon.stub().callsArgWith(3, this.stubbedErr)
  203. return (this.oldRev = 77)
  204. })
  205. it('should upsert the document', function (done) {
  206. return this.MongoManager.upsertIntoDocCollection(
  207. this.project_id,
  208. this.doc_id,
  209. { lines: this.lines },
  210. err => {
  211. assert.equal(err, this.stubbedErr)
  212. const args = this.db.docs.updateOne.args[0]
  213. assert.deepEqual(args[0], { _id: ObjectId(this.doc_id) })
  214. assert.equal(args[1].$set.lines, this.lines)
  215. assert.equal(args[1].$inc.rev, 1)
  216. assert.deepEqual(args[1].$set.project_id, ObjectId(this.project_id))
  217. return done()
  218. }
  219. )
  220. })
  221. return it('should return the error', function (done) {
  222. return this.MongoManager.upsertIntoDocCollection(
  223. this.project_id,
  224. this.doc_id,
  225. { lines: this.lines },
  226. err => {
  227. err.should.equal(this.stubbedErr)
  228. return done()
  229. }
  230. )
  231. })
  232. })
  233. describe('destroyDoc', function () {
  234. beforeEach(function (done) {
  235. this.db.docs.deleteOne = sinon.stub().yields()
  236. this.db.docOps.deleteOne = sinon.stub().yields()
  237. return this.MongoManager.destroyDoc('123456789012', done)
  238. })
  239. it('should destroy the doc', function () {
  240. return sinon.assert.calledWith(this.db.docs.deleteOne, {
  241. _id: ObjectId('123456789012'),
  242. })
  243. })
  244. return it('should destroy the docOps', function () {
  245. return sinon.assert.calledWith(this.db.docOps.deleteOne, {
  246. doc_id: ObjectId('123456789012'),
  247. })
  248. })
  249. })
  250. describe('getDocVersion', function () {
  251. describe('when the doc exists', function () {
  252. beforeEach(function () {
  253. this.doc = { version: (this.version = 42) }
  254. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  255. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  256. })
  257. it('should look for the doc in the database', function () {
  258. return this.db.docOps.findOne
  259. .calledWith(
  260. { doc_id: ObjectId(this.doc_id) },
  261. {
  262. projection: { version: 1 },
  263. }
  264. )
  265. .should.equal(true)
  266. })
  267. return it('should call the callback with the version', function () {
  268. return this.callback.calledWith(null, this.version).should.equal(true)
  269. })
  270. })
  271. return describe("when the doc doesn't exist", function () {
  272. beforeEach(function () {
  273. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
  274. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  275. })
  276. return it('should call the callback with 0', function () {
  277. return this.callback.calledWith(null, 0).should.equal(true)
  278. })
  279. })
  280. })
  281. describe('setDocVersion', function () {
  282. beforeEach(function () {
  283. this.version = 42
  284. this.db.docOps.updateOne = sinon.stub().callsArg(3)
  285. return this.MongoManager.setDocVersion(
  286. this.doc_id,
  287. this.version,
  288. this.callback
  289. )
  290. })
  291. it('should update the doc version', function () {
  292. return this.db.docOps.updateOne
  293. .calledWith(
  294. {
  295. doc_id: ObjectId(this.doc_id),
  296. },
  297. {
  298. $set: {
  299. version: this.version,
  300. },
  301. },
  302. {
  303. upsert: true,
  304. }
  305. )
  306. .should.equal(true)
  307. })
  308. return it('should call the callback', function () {
  309. return this.callback.called.should.equal(true)
  310. })
  311. })
  312. describe('withRevCheck', function () {
  313. this.beforeEach(function () {
  314. this.doc = { _id: ObjectId(), name: 'mock-doc', rev: 1 }
  315. this.testFunction = sinon.stub().yields(null, 'foo')
  316. })
  317. it('should call the callback when the rev has not changed', function (done) {
  318. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 1 })
  319. this.MongoManager.withRevCheck(
  320. this.doc,
  321. this.testFunction,
  322. (err, result) => {
  323. result.should.equal('foo')
  324. assert.isNull(err)
  325. done()
  326. }
  327. )
  328. })
  329. it('should return an error when the rev has changed', function (done) {
  330. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, { rev: 2 })
  331. this.MongoManager.withRevCheck(
  332. this.doc,
  333. this.testFunction,
  334. (err, result) => {
  335. err.should.be.instanceof(Errors.DocModifiedError)
  336. done()
  337. }
  338. )
  339. })
  340. })
  341. })