MongoManagerTests.js 8.2 KB

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