MongoManagerTests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. 'settings-sharelatex': { 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. name: { $exists: 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. const args = this.db.docs.updateOne.args[0]
  212. assert.deepEqual(args[0], { _id: ObjectId(this.doc_id) })
  213. assert.equal(args[1].$set.lines, this.lines)
  214. assert.equal(args[1].$inc.rev, 1)
  215. assert.deepEqual(args[1].$set.project_id, ObjectId(this.project_id))
  216. return done()
  217. }
  218. )
  219. })
  220. return it('should return the error', function (done) {
  221. return this.MongoManager.upsertIntoDocCollection(
  222. this.project_id,
  223. this.doc_id,
  224. { lines: this.lines },
  225. (err) => {
  226. err.should.equal(this.stubbedErr)
  227. return done()
  228. }
  229. )
  230. })
  231. })
  232. describe('markDocAsDeleted', function () {
  233. beforeEach(function () {
  234. this.db.docs.updateOne = sinon.stub().callsArgWith(2, this.stubbedErr)
  235. return (this.oldRev = 77)
  236. })
  237. it('should process the update', function (done) {
  238. return this.MongoManager.markDocAsDeleted(
  239. this.project_id,
  240. this.doc_id,
  241. (err) => {
  242. const args = this.db.docs.updateOne.args[0]
  243. assert.deepEqual(args[0], {
  244. _id: ObjectId(this.doc_id),
  245. project_id: ObjectId(this.project_id)
  246. })
  247. assert.equal(args[1].$set.deleted, true)
  248. return done()
  249. }
  250. )
  251. })
  252. return it('should return the error', function (done) {
  253. return this.MongoManager.markDocAsDeleted(
  254. this.project_id,
  255. this.doc_id,
  256. (err) => {
  257. err.should.equal(this.stubbedErr)
  258. return done()
  259. }
  260. )
  261. })
  262. })
  263. describe('destroyDoc', function () {
  264. beforeEach(function (done) {
  265. this.db.docs.deleteOne = sinon.stub().yields()
  266. this.db.docOps.deleteOne = sinon.stub().yields()
  267. return this.MongoManager.destroyDoc('123456789012', done)
  268. })
  269. it('should destroy the doc', function () {
  270. return sinon.assert.calledWith(this.db.docs.deleteOne, {
  271. _id: ObjectId('123456789012')
  272. })
  273. })
  274. return it('should destroy the docOps', function () {
  275. return sinon.assert.calledWith(this.db.docOps.deleteOne, {
  276. doc_id: ObjectId('123456789012')
  277. })
  278. })
  279. })
  280. describe('getDocVersion', function () {
  281. describe('when the doc exists', function () {
  282. beforeEach(function () {
  283. this.doc = { version: (this.version = 42) }
  284. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  285. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  286. })
  287. it('should look for the doc in the database', function () {
  288. return this.db.docOps.findOne
  289. .calledWith(
  290. { doc_id: ObjectId(this.doc_id) },
  291. {
  292. projection: { version: 1 }
  293. }
  294. )
  295. .should.equal(true)
  296. })
  297. return it('should call the callback with the version', function () {
  298. return this.callback.calledWith(null, this.version).should.equal(true)
  299. })
  300. })
  301. return describe("when the doc doesn't exist", function () {
  302. beforeEach(function () {
  303. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
  304. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  305. })
  306. return it('should call the callback with 0', function () {
  307. return this.callback.calledWith(null, 0).should.equal(true)
  308. })
  309. })
  310. })
  311. return describe('setDocVersion', function () {
  312. beforeEach(function () {
  313. this.version = 42
  314. this.db.docOps.updateOne = sinon.stub().callsArg(3)
  315. return this.MongoManager.setDocVersion(
  316. this.doc_id,
  317. this.version,
  318. this.callback
  319. )
  320. })
  321. it('should update the doc version', function () {
  322. return this.db.docOps.updateOne
  323. .calledWith(
  324. {
  325. doc_id: ObjectId(this.doc_id)
  326. },
  327. {
  328. $set: {
  329. version: this.version
  330. }
  331. },
  332. {
  333. upsert: true
  334. }
  335. )
  336. .should.equal(true)
  337. })
  338. return it('should call the callback', function () {
  339. return this.callback.called.should.equal(true)
  340. })
  341. })
  342. })