MongoManagerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. const modulePath = require('path').join(
  15. __dirname,
  16. '../../../app/js/MongoManager'
  17. )
  18. const { ObjectId } = require('mongodb')
  19. const { assert } = require('chai')
  20. const Errors = require('../../../app/js/Errors')
  21. describe('MongoManager', function () {
  22. beforeEach(function () {
  23. this.MongoManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. './mongodb': {
  26. db: (this.db = { docs: {}, docOps: {} }),
  27. ObjectId,
  28. },
  29. '@overleaf/metrics': { timeAsyncMethod: sinon.stub() },
  30. '@overleaf/settings': { max_deleted_docs: 42 },
  31. './Errors': Errors,
  32. },
  33. })
  34. this.project_id = ObjectId().toString()
  35. this.doc_id = ObjectId().toString()
  36. this.callback = sinon.stub()
  37. return (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. return this.MongoManager.findDoc(
  45. this.project_id,
  46. this.doc_id,
  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.doc_id),
  56. project_id: ObjectId(this.project_id),
  57. },
  58. {
  59. projection: this.filter,
  60. }
  61. )
  62. .should.equal(true)
  63. })
  64. return it('should call the callback with the doc', function () {
  65. return this.callback.calledWith(null, this.doc).should.equal(true)
  66. })
  67. })
  68. describe('patchDoc', function () {
  69. beforeEach(function (done) {
  70. this.db.docs.updateOne = sinon.stub().yields(null)
  71. this.meta = { name: 'foo.tex' }
  72. this.callback.callsFake(done)
  73. this.MongoManager.patchDoc(
  74. this.project_id,
  75. this.doc_id,
  76. this.meta,
  77. this.callback
  78. )
  79. })
  80. it('should pass the parameter along', function () {
  81. this.db.docs.updateOne.should.have.been.calledWith(
  82. {
  83. _id: ObjectId(this.doc_id),
  84. project_id: ObjectId(this.project_id),
  85. },
  86. {
  87. $set: this.meta,
  88. },
  89. this.callback
  90. )
  91. })
  92. })
  93. describe('getProjectsDocs', function () {
  94. beforeEach(function () {
  95. this.filter = { lines: true }
  96. this.doc1 = { name: 'mock-doc1' }
  97. this.doc2 = { name: 'mock-doc2' }
  98. this.doc3 = { name: 'mock-doc3' }
  99. this.doc4 = { name: 'mock-doc4' }
  100. this.db.docs.find = sinon.stub().returns({
  101. toArray: sinon
  102. .stub()
  103. .callsArgWith(0, null, [this.doc, this.doc3, this.doc4]),
  104. })
  105. })
  106. describe('with included_deleted = false', function () {
  107. beforeEach(function () {
  108. return this.MongoManager.getProjectsDocs(
  109. this.project_id,
  110. { include_deleted: false },
  111. this.filter,
  112. this.callback
  113. )
  114. })
  115. it('should find the non-deleted docs via the project_id', function () {
  116. return this.db.docs.find
  117. .calledWith(
  118. {
  119. project_id: ObjectId(this.project_id),
  120. deleted: { $ne: true },
  121. },
  122. {
  123. projection: this.filter,
  124. }
  125. )
  126. .should.equal(true)
  127. })
  128. return it('should call the callback with the docs', function () {
  129. return this.callback
  130. .calledWith(null, [this.doc, this.doc3, this.doc4])
  131. .should.equal(true)
  132. })
  133. })
  134. return describe('with included_deleted = true', function () {
  135. beforeEach(function () {
  136. return this.MongoManager.getProjectsDocs(
  137. this.project_id,
  138. { include_deleted: true },
  139. this.filter,
  140. this.callback
  141. )
  142. })
  143. it('should find all via the project_id', function () {
  144. return this.db.docs.find
  145. .calledWith(
  146. {
  147. project_id: ObjectId(this.project_id),
  148. },
  149. {
  150. projection: this.filter,
  151. }
  152. )
  153. .should.equal(true)
  154. })
  155. return it('should call the callback with the docs', function () {
  156. return this.callback
  157. .calledWith(null, [this.doc, this.doc3, this.doc4])
  158. .should.equal(true)
  159. })
  160. })
  161. })
  162. describe('getProjectsDeletedDocs', function () {
  163. beforeEach(function (done) {
  164. this.filter = { name: true }
  165. this.doc1 = { _id: '1', name: 'mock-doc1.tex' }
  166. this.doc2 = { _id: '2', name: 'mock-doc2.tex' }
  167. this.doc3 = { _id: '3', name: 'mock-doc3.tex' }
  168. this.db.docs.find = sinon.stub().returns({
  169. toArray: sinon.stub().yields(null, [this.doc1, this.doc2, this.doc3]),
  170. })
  171. this.callback.callsFake(done)
  172. this.MongoManager.getProjectsDeletedDocs(
  173. this.project_id,
  174. this.filter,
  175. this.callback
  176. )
  177. })
  178. it('should find the deleted docs via the project_id', function () {
  179. this.db.docs.find
  180. .calledWith({
  181. project_id: ObjectId(this.project_id),
  182. deleted: true,
  183. })
  184. .should.equal(true)
  185. })
  186. it('should filter, sort by deletedAt and limit', function () {
  187. this.db.docs.find
  188. .calledWith(sinon.match.any, {
  189. projection: this.filter,
  190. sort: { deletedAt: -1 },
  191. limit: 42,
  192. })
  193. .should.equal(true)
  194. })
  195. it('should call the callback with the docs', function () {
  196. this.callback
  197. .calledWith(null, [this.doc1, this.doc2, this.doc3])
  198. .should.equal(true)
  199. })
  200. })
  201. describe('upsertIntoDocCollection', function () {
  202. beforeEach(function () {
  203. this.db.docs.updateOne = sinon.stub().callsArgWith(3, this.stubbedErr)
  204. return (this.oldRev = 77)
  205. })
  206. it('should upsert the document', function (done) {
  207. return this.MongoManager.upsertIntoDocCollection(
  208. this.project_id,
  209. this.doc_id,
  210. { lines: this.lines },
  211. err => {
  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. })