MongoManagerTests.js 8.3 KB

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