MongoManagerTests.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  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 sinon = require('sinon')
  13. const chai = require('chai')
  14. const should = chai.should()
  15. const { expect } = chai
  16. const modulePath = '../../../../app/js/MongoManager.js'
  17. const packModulePath = '../../../../app/js/PackManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. const { ObjectId } = require('mongojs')
  20. const tk = require('timekeeper')
  21. describe('MongoManager', function () {
  22. beforeEach(function () {
  23. tk.freeze(new Date())
  24. this.MongoManager = SandboxedModule.require(modulePath, {
  25. requires: {
  26. './mongojs': { db: (this.db = {}), ObjectId },
  27. './PackManager': (this.PackManager = {}),
  28. 'metrics-sharelatex': { timeAsyncMethod() {} },
  29. 'logger-sharelatex': { log() {} }
  30. }
  31. })
  32. this.callback = sinon.stub()
  33. this.doc_id = ObjectId().toString()
  34. return (this.project_id = ObjectId().toString())
  35. })
  36. afterEach(function () {
  37. return tk.reset()
  38. })
  39. describe('getLastCompressedUpdate', function () {
  40. beforeEach(function () {
  41. this.update = 'mock-update'
  42. this.db.docHistory = {}
  43. this.db.docHistory.find = sinon.stub().returns(this.db.docHistory)
  44. this.db.docHistory.findOne = sinon.stub().returns(this.db.docHistory)
  45. this.db.docHistory.sort = sinon.stub().returns(this.db.docHistory)
  46. this.db.docHistory.limit = sinon.stub().returns(this.db.docHistory)
  47. this.db.docHistory.toArray = sinon
  48. .stub()
  49. .callsArgWith(0, null, [this.update])
  50. return this.MongoManager.getLastCompressedUpdate(
  51. this.doc_id,
  52. this.callback
  53. )
  54. })
  55. it('should find the updates for the doc', function () {
  56. return this.db.docHistory.find
  57. .calledWith({ doc_id: ObjectId(this.doc_id) })
  58. .should.equal(true)
  59. })
  60. it('should limit to one result', function () {
  61. return this.db.docHistory.limit.calledWith(1).should.equal(true)
  62. })
  63. it('should sort in descending version order', function () {
  64. return this.db.docHistory.sort.calledWith({ v: -1 }).should.equal(true)
  65. })
  66. return it('should call the call back with the update', function () {
  67. return this.callback.calledWith(null, this.update).should.equal(true)
  68. })
  69. })
  70. describe('peekLastCompressedUpdate', function () {
  71. describe('when there is no last update', function () {
  72. beforeEach(function () {
  73. this.PackManager.getLastPackFromIndex = sinon
  74. .stub()
  75. .callsArgWith(1, null, null)
  76. this.MongoManager.getLastCompressedUpdate = sinon
  77. .stub()
  78. .callsArgWith(1, null, null)
  79. return this.MongoManager.peekLastCompressedUpdate(
  80. this.doc_id,
  81. this.callback
  82. )
  83. })
  84. it('should get the last update', function () {
  85. return this.MongoManager.getLastCompressedUpdate
  86. .calledWith(this.doc_id)
  87. .should.equal(true)
  88. })
  89. return it('should call the callback with no update', function () {
  90. return this.callback.calledWith(null, null).should.equal(true)
  91. })
  92. })
  93. describe('when there is an update', function () {
  94. beforeEach(function () {
  95. this.update = { _id: Object() }
  96. this.MongoManager.getLastCompressedUpdate = sinon
  97. .stub()
  98. .callsArgWith(1, null, this.update)
  99. return this.MongoManager.peekLastCompressedUpdate(
  100. this.doc_id,
  101. this.callback
  102. )
  103. })
  104. it('should get the last update', function () {
  105. return this.MongoManager.getLastCompressedUpdate
  106. .calledWith(this.doc_id)
  107. .should.equal(true)
  108. })
  109. return it('should call the callback with the update', function () {
  110. return this.callback.calledWith(null, this.update).should.equal(true)
  111. })
  112. })
  113. return describe('when there is a last update in S3', function () {
  114. beforeEach(function () {
  115. this.update = { _id: Object(), v: 12345, v_end: 12345, inS3: true }
  116. this.PackManager.getLastPackFromIndex = sinon
  117. .stub()
  118. .callsArgWith(1, null, this.update)
  119. this.MongoManager.getLastCompressedUpdate = sinon
  120. .stub()
  121. .callsArgWith(1, null)
  122. return this.MongoManager.peekLastCompressedUpdate(
  123. this.doc_id,
  124. this.callback
  125. )
  126. })
  127. it('should get the last update', function () {
  128. return this.MongoManager.getLastCompressedUpdate
  129. .calledWith(this.doc_id)
  130. .should.equal(true)
  131. })
  132. return it('should call the callback with a null update and the correct version', function () {
  133. return this.callback
  134. .calledWith(null, null, this.update.v_end)
  135. .should.equal(true)
  136. })
  137. })
  138. })
  139. describe('backportProjectId', function () {
  140. beforeEach(function () {
  141. this.db.docHistory = { update: sinon.stub().callsArg(3) }
  142. return this.MongoManager.backportProjectId(
  143. this.project_id,
  144. this.doc_id,
  145. this.callback
  146. )
  147. })
  148. it("should insert the project_id into all entries for the doc_id which don't have it set", function () {
  149. return this.db.docHistory.update
  150. .calledWith(
  151. {
  152. doc_id: ObjectId(this.doc_id),
  153. project_id: { $exists: false }
  154. },
  155. {
  156. $set: { project_id: ObjectId(this.project_id) }
  157. },
  158. {
  159. multi: true
  160. }
  161. )
  162. .should.equal(true)
  163. })
  164. return it('should call the callback', function () {
  165. return this.callback.called.should.equal(true)
  166. })
  167. })
  168. describe('getProjectMetaData', function () {
  169. beforeEach(function () {
  170. this.metadata = { mock: 'metadata' }
  171. this.db.projectHistoryMetaData = {
  172. findOne: sinon.stub().callsArgWith(1, null, this.metadata)
  173. }
  174. return this.MongoManager.getProjectMetaData(
  175. this.project_id,
  176. this.callback
  177. )
  178. })
  179. it('should look up the meta data in the db', function () {
  180. return this.db.projectHistoryMetaData.findOne
  181. .calledWith({ project_id: ObjectId(this.project_id) })
  182. .should.equal(true)
  183. })
  184. return it('should return the metadata', function () {
  185. return this.callback.calledWith(null, this.metadata).should.equal(true)
  186. })
  187. })
  188. return describe('setProjectMetaData', function () {
  189. beforeEach(function () {
  190. this.metadata = { mock: 'metadata' }
  191. this.db.projectHistoryMetaData = {
  192. update: sinon.stub().callsArgWith(3, null, [this.metadata])
  193. }
  194. return this.MongoManager.setProjectMetaData(
  195. this.project_id,
  196. this.metadata,
  197. this.callback
  198. )
  199. })
  200. it('should upsert the metadata into the DB', function () {
  201. return this.db.projectHistoryMetaData.update
  202. .calledWith(
  203. {
  204. project_id: ObjectId(this.project_id)
  205. },
  206. {
  207. $set: this.metadata
  208. },
  209. {
  210. upsert: true
  211. }
  212. )
  213. .should.equal(true)
  214. })
  215. return it('should call the callback', function () {
  216. return this.callback.called.should.equal(true)
  217. })
  218. })
  219. })