MongoManagerTests.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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.db = {}
  24. this.MongoManager = SandboxedModule.require(modulePath, {
  25. requires: {
  26. './mongodb': {
  27. getCollection: sinon.stub().callsFake((name) => {
  28. return Promise.resolve((this.db[name] = {}))
  29. }),
  30. db: (this.db = { docs: {}, docOps: {} }),
  31. ObjectId
  32. },
  33. 'metrics-sharelatex': { timeAsyncMethod: sinon.stub() },
  34. 'logger-sharelatex': { log() {} }
  35. },
  36. globals: {
  37. console
  38. }
  39. })
  40. this.project_id = ObjectId().toString()
  41. this.doc_id = ObjectId().toString()
  42. return (this.stubbedErr = new Error('hello world'))
  43. })
  44. describe('findDoc', function () {
  45. beforeEach(function (done) {
  46. this.callback = sinon.stub().callsFake(() => done())
  47. this.doc = { name: 'mock-doc' }
  48. this.db.docs.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  49. this.filter = { lines: true }
  50. return this.MongoManager.findDoc(
  51. this.project_id,
  52. this.doc_id,
  53. this.filter,
  54. this.callback
  55. )
  56. })
  57. it('should find the doc', function () {
  58. this.db.docs.findOne
  59. .calledWith(
  60. {
  61. _id: ObjectId(this.doc_id),
  62. project_id: ObjectId(this.project_id)
  63. },
  64. this.filter
  65. )
  66. .should.equal(true)
  67. })
  68. return it('should call the callback with the doc', function () {
  69. return this.callback.calledWith(null, this.doc).should.equal(true)
  70. })
  71. })
  72. describe('getProjectsDocs', function () {
  73. beforeEach(function () {
  74. this.filter = { lines: true }
  75. this.doc1 = { name: 'mock-doc1' }
  76. this.doc2 = { name: 'mock-doc2' }
  77. this.doc3 = { name: 'mock-doc3' }
  78. this.doc4 = { name: 'mock-doc4' }
  79. this.db.docs.find = sinon.stub().returns({
  80. toArray: sinon
  81. .stub()
  82. .callsArgWith(0, null, [this.doc, this.doc3, this.doc4])
  83. })
  84. })
  85. describe('with included_deleted = false', function () {
  86. beforeEach(function (done) {
  87. this.callback = sinon.stub().callsFake(() => done())
  88. return this.MongoManager.getProjectsDocs(
  89. this.project_id,
  90. { include_deleted: false },
  91. this.filter,
  92. this.callback
  93. )
  94. })
  95. it('should find the non-deleted docs via the project_id', function () {
  96. return this.db.docs.find
  97. .calledWith(
  98. {
  99. project_id: ObjectId(this.project_id),
  100. deleted: { $ne: true }
  101. },
  102. this.filter
  103. )
  104. .should.equal(true)
  105. })
  106. return it('should call the callback with the docs', function () {
  107. return this.callback
  108. .calledWith(null, [this.doc, this.doc3, this.doc4])
  109. .should.equal(true)
  110. })
  111. })
  112. return describe('with included_deleted = true', function () {
  113. beforeEach(function (done) {
  114. this.callback = sinon.stub().callsFake(() => done())
  115. return this.MongoManager.getProjectsDocs(
  116. this.project_id,
  117. { include_deleted: true },
  118. this.filter,
  119. this.callback
  120. )
  121. })
  122. it('should find all via the project_id', function () {
  123. return this.db.docs.find
  124. .calledWith(
  125. {
  126. project_id: ObjectId(this.project_id)
  127. },
  128. this.filter
  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 (done) {
  222. this.callback = sinon.stub().callsFake(() => done())
  223. this.doc = { version: (this.version = 42) }
  224. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, this.doc)
  225. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  226. })
  227. it('should look for the doc in the database', function () {
  228. return this.db.docOps.findOne
  229. .calledWith({ doc_id: ObjectId(this.doc_id) }, { version: 1 })
  230. .should.equal(true)
  231. })
  232. return it('should call the callback with the version', function () {
  233. return this.callback.calledWith(null, this.version).should.equal(true)
  234. })
  235. })
  236. return describe("when the doc doesn't exist", function () {
  237. beforeEach(function (done) {
  238. this.callback = sinon.stub().callsFake(() => done())
  239. this.db.docOps.findOne = sinon.stub().callsArgWith(2, null, null)
  240. return this.MongoManager.getDocVersion(this.doc_id, this.callback)
  241. })
  242. return it('should call the callback with 0', function () {
  243. return this.callback.calledWith(null, 0).should.equal(true)
  244. })
  245. })
  246. })
  247. return describe('setDocVersion', function () {
  248. beforeEach(function (done) {
  249. this.callback = sinon.stub().callsFake(() => done())
  250. this.version = 42
  251. this.db.docOps.updateOne = sinon.stub().callsArg(3)
  252. return this.MongoManager.setDocVersion(
  253. this.doc_id,
  254. this.version,
  255. this.callback
  256. )
  257. })
  258. it('should update the doc version', function () {
  259. return this.db.docOps.updateOne
  260. .calledWith(
  261. {
  262. doc_id: ObjectId(this.doc_id)
  263. },
  264. {
  265. $set: {
  266. version: this.version
  267. }
  268. },
  269. {
  270. upsert: true
  271. }
  272. )
  273. .should.equal(true)
  274. })
  275. return it('should call the callback', function () {
  276. return this.callback.called.should.equal(true)
  277. })
  278. })
  279. })