MongoManagerTests.js 7.2 KB

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