MongoManagerTests.js 8.5 KB

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