MongoManagerTests.js 8.3 KB

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