MongoManagerTests.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. 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. }
  31. })
  32. this.project_id = ObjectId().toString()
  33. this.doc_id = ObjectId().toString()
  34. this.callback = sinon.stub()
  35. return (this.stubbedErr = new Error('hello world'))
  36. })
  37. describe('findDoc', function () {
  38. beforeEach(function () {
  39. this.doc = { name: 'mock-doc' }
  40. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  41. this.filter = { lines: true }
  42. return this.MongoManager.findDoc(
  43. this.project_id,
  44. this.doc_id,
  45. this.filter,
  46. this.callback
  47. )
  48. })
  49. it('should find the doc', function () {
  50. this.db.docs.findOne
  51. .calledWith(
  52. {
  53. _id: ObjectId(this.doc_id),
  54. project_id: ObjectId(this.project_id)
  55. },
  56. {
  57. projection: this.filter
  58. }
  59. )
  60. .should.equal(true)
  61. })
  62. return it('should call the callback with the doc', function () {
  63. return this.callback.calledWith(null, this.doc).should.equal(true)
  64. })
  65. })
  66. describe('patchDoc', function () {
  67. beforeEach(function (done) {
  68. this.db.docs.updateOne = sinon.stub().yields(null)
  69. this.meta = { name: 'foo.tex' }
  70. this.callback.callsFake(done)
  71. this.MongoManager.patchDoc(
  72. this.project_id,
  73. this.doc_id,
  74. this.meta,
  75. this.callback
  76. )
  77. })
  78. it('should pass the parameter along', function () {
  79. this.db.docs.updateOne.should.have.been.calledWith(
  80. {
  81. _id: ObjectId(this.doc_id),
  82. project_id: ObjectId(this.project_id)
  83. },
  84. {
  85. $set: this.meta
  86. },
  87. this.callback
  88. )
  89. })
  90. })
  91. describe('getProjectsDocs', function () {
  92. beforeEach(function () {
  93. this.filter = { lines: true }
  94. this.doc1 = { name: 'mock-doc1' }
  95. this.doc2 = { name: 'mock-doc2' }
  96. this.doc3 = { name: 'mock-doc3' }
  97. this.doc4 = { name: 'mock-doc4' }
  98. this.db.docs.find = sinon.stub().returns({
  99. toArray: sinon
  100. .stub()
  101. .callsArgWith(0, null, [this.doc, this.doc3, this.doc4])
  102. })
  103. })
  104. describe('with included_deleted = false', function () {
  105. beforeEach(function () {
  106. return this.MongoManager.getProjectsDocs(
  107. this.project_id,
  108. { include_deleted: false },
  109. this.filter,
  110. this.callback
  111. )
  112. })
  113. it('should find the non-deleted docs via the project_id', function () {
  114. return this.db.docs.find
  115. .calledWith(
  116. {
  117. project_id: ObjectId(this.project_id),
  118. deleted: { $ne: true }
  119. },
  120. {
  121. projection: this.filter
  122. }
  123. )
  124. .should.equal(true)
  125. })
  126. return it('should call the callback with the docs', function () {
  127. return this.callback
  128. .calledWith(null, [this.doc, this.doc3, this.doc4])
  129. .should.equal(true)
  130. })
  131. })
  132. return describe('with included_deleted = true', function () {
  133. beforeEach(function () {
  134. return this.MongoManager.getProjectsDocs(
  135. this.project_id,
  136. { include_deleted: true },
  137. this.filter,
  138. this.callback
  139. )
  140. })
  141. it('should find all via the project_id', function () {
  142. return this.db.docs.find
  143. .calledWith(
  144. {
  145. project_id: ObjectId(this.project_id)
  146. },
  147. {
  148. projection: this.filter
  149. }
  150. )
  151. .should.equal(true)
  152. })
  153. return it('should call the callback with the docs', function () {
  154. return this.callback
  155. .calledWith(null, [this.doc, this.doc3, this.doc4])
  156. .should.equal(true)
  157. })
  158. })
  159. })
  160. describe('getProjectsDeletedDocs', function () {
  161. beforeEach(function (done) {
  162. this.filter = { name: true }
  163. this.doc1 = { _id: '1', name: 'mock-doc1.tex' }
  164. this.doc2 = { _id: '2', name: 'mock-doc2.tex' }
  165. this.doc3 = { _id: '3', name: 'mock-doc3.tex' }
  166. this.db.docs.find = sinon.stub().returns({
  167. toArray: sinon.stub().yields(null, [this.doc1, this.doc2, this.doc3])
  168. })
  169. this.callback.callsFake(done)
  170. this.MongoManager.getProjectsDeletedDocs(
  171. this.project_id,
  172. this.filter,
  173. this.callback
  174. )
  175. })
  176. it('should find the deleted docs via the project_id', function () {
  177. this.db.docs.find
  178. .calledWith({
  179. project_id: ObjectId(this.project_id),
  180. deleted: true
  181. })
  182. .should.equal(true)
  183. })
  184. it('should filter, sort by deletedAt and limit', function () {
  185. this.db.docs.find
  186. .calledWith(sinon.match.any, {
  187. projection: this.filter,
  188. sort: { deletedAt: -1 },
  189. limit: 42
  190. })
  191. .should.equal(true)
  192. })
  193. it('should call the callback with the docs', function () {
  194. this.callback
  195. .calledWith(null, [this.doc1, this.doc2, this.doc3])
  196. .should.equal(true)
  197. })
  198. })
  199. describe('upsertIntoDocCollection', function () {
  200. beforeEach(function () {
  201. this.db.docs.updateOne = sinon.stub().callsArgWith(3, this.stubbedErr)
  202. return (this.oldRev = 77)
  203. })
  204. it('should upsert the document', function (done) {
  205. return this.MongoManager.upsertIntoDocCollection(
  206. this.project_id,
  207. this.doc_id,
  208. { lines: this.lines },
  209. (err) => {
  210. const args = this.db.docs.updateOne.args[0]
  211. assert.deepEqual(args[0], { _id: ObjectId(this.doc_id) })
  212. assert.equal(args[1].$set.lines, this.lines)
  213. assert.equal(args[1].$inc.rev, 1)
  214. assert.deepEqual(args[1].$set.project_id, ObjectId(this.project_id))
  215. return done()
  216. }
  217. )
  218. })
  219. return it('should return the error', function (done) {
  220. return this.MongoManager.upsertIntoDocCollection(
  221. this.project_id,
  222. this.doc_id,
  223. { lines: this.lines },
  224. (err) => {
  225. err.should.equal(this.stubbedErr)
  226. return done()
  227. }
  228. )
  229. })
  230. })
  231. describe('destroyDoc', function () {
  232. beforeEach(function (done) {
  233. this.db.docs.deleteOne = sinon.stub().yields()
  234. this.db.docOps.deleteOne = sinon.stub().yields()
  235. return this.MongoManager.destroyDoc('123456789012', done)
  236. })
  237. it('should destroy the doc', function () {
  238. return sinon.assert.calledWith(this.db.docs.deleteOne, {
  239. _id: ObjectId('123456789012')
  240. })
  241. })
  242. return it('should destroy the docOps', function () {
  243. return sinon.assert.calledWith(this.db.docOps.deleteOne, {
  244. doc_id: ObjectId('123456789012')
  245. })
  246. })
  247. })
  248. describe('getDocVersion', function () {
  249. describe('when the doc exists', function () {
  250. beforeEach(function () {
  251. this.doc = { version: (this.version = 42) }
  252. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  253. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  254. })
  255. it('should look for the doc in the database', function () {
  256. return this.db.docOps.findOne
  257. .calledWith(
  258. { doc_id: ObjectId(this.doc_id) },
  259. {
  260. projection: { version: 1 }
  261. }
  262. )
  263. .should.equal(true)
  264. })
  265. return it('should call the callback with the version', function () {
  266. return this.callback.calledWith(null, this.version).should.equal(true)
  267. })
  268. })
  269. return describe("when the doc doesn't exist", function () {
  270. beforeEach(function () {
  271. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
  272. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  273. })
  274. return it('should call the callback with 0', function () {
  275. return this.callback.calledWith(null, 0).should.equal(true)
  276. })
  277. })
  278. })
  279. return describe('setDocVersion', function () {
  280. beforeEach(function () {
  281. this.version = 42
  282. this.db.docOps.updateOne = sinon.stub().callsArg(3)
  283. return this.MongoManager.setDocVersion(
  284. this.doc_id,
  285. this.version,
  286. this.callback
  287. )
  288. })
  289. it('should update the doc version', function () {
  290. return this.db.docOps.updateOne
  291. .calledWith(
  292. {
  293. doc_id: ObjectId(this.doc_id)
  294. },
  295. {
  296. $set: {
  297. version: this.version
  298. }
  299. },
  300. {
  301. upsert: true
  302. }
  303. )
  304. .should.equal(true)
  305. })
  306. return it('should call the callback', function () {
  307. return this.callback.called.should.equal(true)
  308. })
  309. })
  310. })