MongoManagerTests.js 11 KB

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