MongoManagerTests.coffee 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/MongoManager.js"
  6. packModulePath = "../../../../app/js/PackManager.js"
  7. SandboxedModule = require('sandboxed-module')
  8. {ObjectId} = require("mongojs")
  9. tk = require "timekeeper"
  10. describe "MongoManager", ->
  11. beforeEach ->
  12. tk.freeze(new Date())
  13. @MongoManager = SandboxedModule.require modulePath, requires:
  14. "./mongojs" : { db: @db = {}, ObjectId: ObjectId }
  15. "./PackManager" : SandboxedModule.require packModulePath, requires:
  16. "./LockManager" : {}
  17. "./mongojs": {db: bson: BSON = sinon.stub(), ObjectId}
  18. "logger-sharelatex": {}
  19. @callback = sinon.stub()
  20. @doc_id = ObjectId().toString()
  21. @project_id = ObjectId().toString()
  22. afterEach ->
  23. tk.reset()
  24. describe "getLastCompressedUpdate", ->
  25. beforeEach ->
  26. @update = "mock-update"
  27. @db.docHistory = {}
  28. @db.docHistory.find = sinon.stub().returns @db.docHistory
  29. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  30. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  31. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, [@update])
  32. @MongoManager.getLastCompressedUpdate @doc_id, @callback
  33. it "should find the updates for the doc", ->
  34. @db.docHistory.find
  35. .calledWith(doc_id: ObjectId(@doc_id))
  36. .should.equal true
  37. it "should limit to one result", ->
  38. @db.docHistory.limit
  39. .calledWith(1)
  40. .should.equal true
  41. it "should sort in descending version order", ->
  42. @db.docHistory.sort
  43. .calledWith(v: -1)
  44. .should.equal true
  45. it "should call the call back with the update", ->
  46. @callback.calledWith(null, @update).should.equal true
  47. describe "peekLastCompressedUpdate", ->
  48. describe "when there is no last update", ->
  49. beforeEach ->
  50. @db.docHistoryStats = {}
  51. @db.docHistoryStats.findOne = sinon.stub().callsArgWith(2, null, null)
  52. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, null)
  53. @MongoManager.peekLastCompressedUpdate @doc_id, @callback
  54. it "should get the last update", ->
  55. @MongoManager.getLastCompressedUpdate
  56. .calledWith(@doc_id)
  57. .should.equal true
  58. it "should call the callback with no update", ->
  59. @callback.calledWith(null, null).should.equal true
  60. describe "when there is an update", ->
  61. beforeEach ->
  62. @update = { _id: Object() }
  63. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
  64. @MongoManager.peekLastCompressedUpdate @doc_id, @callback
  65. it "should get the last update", ->
  66. @MongoManager.getLastCompressedUpdate
  67. .calledWith(@doc_id)
  68. .should.equal true
  69. it "should call the callback with the update", ->
  70. @callback.calledWith(null, @update).should.equal true
  71. describe "when there is a last update in S3", ->
  72. beforeEach ->
  73. @update = { _id: Object(), v: 12345}
  74. @db.docHistoryStats = {}
  75. @db.docHistoryStats.findOne = sinon.stub().callsArgWith(2, null, {inS3:true, lastVersion: @update.v})
  76. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null)
  77. @MongoManager.peekLastCompressedUpdate @doc_id, @callback
  78. it "should get the last update", ->
  79. @MongoManager.getLastCompressedUpdate
  80. .calledWith(@doc_id)
  81. .should.equal true
  82. it "should call the callback with a null update and the correct version", ->
  83. @callback.calledWith(null, null, @update.v).should.equal true
  84. describe "getDocUpdates", ->
  85. beforeEach ->
  86. @results = [
  87. {foo: "mock-update", v: 56, doc_id: 100, project_id: 1},
  88. {foo: "mock-update", v: 55, doc_id: 100, project_id: 1},
  89. {pack: [ {foo: "mock-update", v: 54, doc_id: 100, project_id: 1},
  90. {foo: "mock-update", v: 53, doc_id: 100, project_id: 1},
  91. {foo: "mock-update", v: 52, doc_id: 100, project_id: 1} ]
  92. , v: 52, doc_id: 100, project_id: 1},
  93. {foo: "mock-update", v: 42, doc_id: 100, project_id: 1},
  94. {foo: "mock-update", v: 41, doc_id: 100, project_id: 1}
  95. ]
  96. @updates_between = [
  97. {foo: "mock-update", v: 55, doc_id: 100, project_id: 1},
  98. {foo: "mock-update", v: 54, doc_id: 100, project_id: 1},
  99. {foo: "mock-update", v: 53, doc_id: 100, project_id: 1},
  100. {foo: "mock-update", v: 52, doc_id: 100, project_id: 1},
  101. {foo: "mock-update", v: 42, doc_id: 100, project_id: 1}
  102. ]
  103. @updates_after = [
  104. {foo: "mock-update", v: 56, doc_id: 100, project_id: 1},
  105. {foo: "mock-update", v: 55, doc_id: 100, project_id: 1},
  106. {foo: "mock-update", v: 54, doc_id: 100, project_id: 1},
  107. {foo: "mock-update", v: 53, doc_id: 100, project_id: 1},
  108. {foo: "mock-update", v: 52, doc_id: 100, project_id: 1},
  109. {foo: "mock-update", v: 42, doc_id: 100, project_id: 1}
  110. ]
  111. @db.docHistory = {}
  112. @db.docHistory.find = sinon.stub().returns @db.docHistory
  113. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  114. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  115. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @results)
  116. @from = 42
  117. @to = 55
  118. describe "with a to version", ->
  119. beforeEach ->
  120. @MongoManager.getDocUpdates @doc_id, from: @from, to: @to, @callback
  121. it "should find the all updates between the to and from versions", ->
  122. @db.docHistory.find
  123. .calledWith({
  124. doc_id: ObjectId(@doc_id)
  125. v: { $gte: @from, $lte: @to }
  126. })
  127. .should.equal true
  128. it "should sort in descending version order", ->
  129. @db.docHistory.sort
  130. .calledWith("v": -1)
  131. .should.equal true
  132. #it "should not limit the results", ->
  133. # @db.docHistory.limit
  134. # .called.should.equal false
  135. it "should call the call back with the results", ->
  136. @callback.calledWith(null, @updates_between).should.equal true
  137. describe "without a to version", ->
  138. beforeEach ->
  139. @MongoManager.getDocUpdates @doc_id, from: @from, @callback
  140. it "should find the all updates after the from version", ->
  141. @db.docHistory.find
  142. .calledWith({
  143. doc_id: ObjectId(@doc_id)
  144. v: { $gte: @from }
  145. })
  146. .should.equal true
  147. it "should call the call back with the updates", ->
  148. @callback.calledWith(null, @updates_after).should.equal true
  149. describe "with a limit", ->
  150. beforeEach ->
  151. @MongoManager.getDocUpdates @doc_id, from: @from, limit: @limit = 10, @callback
  152. it "should limit the results", ->
  153. @db.docHistory.limit
  154. .calledWith(@limit)
  155. .should.equal true
  156. describe "getDocUpdates", ->
  157. beforeEach ->
  158. @results = [
  159. {foo: "mock-update", v: 56, meta: {end_ts: 110}, doc_id: 100, project_id: 1},
  160. {foo: "mock-update", v: 55, meta: {end_ts: 100}, doc_id: 100, project_id: 1},
  161. {pack: [
  162. {foo: "mock-update", v: 54, meta: {end_ts: 99}, doc_id: 300, project_id: 1},
  163. {foo: "mock-update", v: 53, meta: {end_ts: 98}, doc_id: 300, project_id: 1},
  164. {foo: "mock-update", v: 52, meta: {end_ts: 97}, doc_id: 300, project_id: 1} ]
  165. , v: 52, meta: {end_ts: 100}, doc_id: 300, project_id: 1},
  166. {pack: [
  167. {foo: "mock-update", v: 54, meta: {end_ts: 103}, doc_id: 200, project_id: 1},
  168. {foo: "mock-update", v: 53, meta: {end_ts: 101}, doc_id: 200, project_id: 1},
  169. {foo: "mock-update", v: 52, meta: {end_ts: 99}, doc_id: 200, project_id: 1} ]
  170. , v: 52, meta: {end_ts: 103}, doc_id: 200, project_id: 1},
  171. {foo: "mock-update", v: 42, meta:{end_ts: 90}, doc_id: 100, project_id: 1}
  172. ]
  173. @updates_before = [
  174. {foo: "mock-update", v: 55, meta: {end_ts: 100}, doc_id: 100, project_id: 1},
  175. {foo: "mock-update", v: 52, meta: {end_ts: 99}, doc_id: 200, project_id: 1},
  176. {foo: "mock-update", v: 54, meta: {end_ts: 99}, doc_id: 300, project_id: 1},
  177. {foo: "mock-update", v: 53, meta: {end_ts: 98}, doc_id: 300, project_id: 1},
  178. {foo: "mock-update", v: 52, meta: {end_ts: 97}, doc_id: 300, project_id: 1},
  179. {foo: "mock-update", v: 42, meta: {end_ts: 90}, doc_id: 100, project_id: 1},
  180. ]
  181. @updates_all = [
  182. {foo: "mock-update", v: 56, meta: {end_ts: 110}, doc_id: 100, project_id: 1},
  183. {foo: "mock-update", v: 54, meta: {end_ts: 103}, doc_id: 200, project_id: 1},
  184. {foo: "mock-update", v: 53, meta: {end_ts: 101}, doc_id: 200, project_id: 1},
  185. {foo: "mock-update", v: 55, meta: {end_ts: 100}, doc_id: 100, project_id: 1},
  186. {foo: "mock-update", v: 52, meta: {end_ts: 99}, doc_id: 200, project_id: 1},
  187. {foo: "mock-update", v: 54, meta: {end_ts: 99}, doc_id: 300, project_id: 1},
  188. {foo: "mock-update", v: 53, meta: {end_ts: 98}, doc_id: 300, project_id: 1},
  189. {foo: "mock-update", v: 52, meta: {end_ts: 97}, doc_id: 300, project_id: 1},
  190. {foo: "mock-update", v: 42, meta: {end_ts: 90}, doc_id: 100, project_id: 1}
  191. ]
  192. @db.docHistory = {}
  193. @db.docHistory.find = sinon.stub().returns @db.docHistory
  194. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  195. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  196. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @results)
  197. @before = 101
  198. describe "with a before timestamp", ->
  199. beforeEach ->
  200. @MongoManager.getProjectUpdates @project_id, before: @before, @callback
  201. it "should find the all updates before the timestamp", ->
  202. @db.docHistory.find
  203. .calledWith({
  204. project_id: ObjectId(@project_id)
  205. "meta.end_ts": { $lt: @before }
  206. })
  207. .should.equal true
  208. it "should sort in descending version order", ->
  209. @db.docHistory.sort
  210. .calledWith("meta.end_ts": -1)
  211. .should.equal true
  212. it "should not limit the results", ->
  213. @db.docHistory.limit
  214. .called.should.equal false
  215. it "should call the call back with the updates", ->
  216. @callback.calledWith(null, @updates_before).should.equal true
  217. describe "without a before timestamp", ->
  218. beforeEach ->
  219. @MongoManager.getProjectUpdates @project_id, {}, @callback
  220. it "should find the all updates", ->
  221. @db.docHistory.find
  222. .calledWith({
  223. project_id: ObjectId(@project_id)
  224. })
  225. .should.equal true
  226. it "should call the call back with the updates", ->
  227. @callback.calledWith(null, @updates_all).should.equal true
  228. describe "with a limit", ->
  229. beforeEach ->
  230. @MongoManager.getProjectUpdates @project_id, before: @before, limit: @limit = 10, @callback
  231. it "should limit the results", ->
  232. @db.docHistory.limit
  233. .calledWith(@limit)
  234. .should.equal true
  235. describe "backportProjectId", ->
  236. beforeEach ->
  237. @db.docHistory =
  238. update: sinon.stub().callsArg(3)
  239. @MongoManager.backportProjectId @project_id, @doc_id, @callback
  240. it "should insert the project_id into all entries for the doc_id which don't have it set", ->
  241. @db.docHistory.update
  242. .calledWith({
  243. doc_id: ObjectId(@doc_id)
  244. project_id: { $exists: false }
  245. }, {
  246. $set: { project_id: ObjectId(@project_id) }
  247. }, {
  248. multi: true
  249. })
  250. .should.equal true
  251. it "should call the callback", ->
  252. @callback.called.should.equal true
  253. describe "getProjectMetaData", ->
  254. beforeEach ->
  255. @metadata = { "mock": "metadata" }
  256. @db.projectHistoryMetaData =
  257. find: sinon.stub().callsArgWith(1, null, [@metadata])
  258. @MongoManager.getProjectMetaData @project_id, @callback
  259. it "should look up the meta data in the db", ->
  260. @db.projectHistoryMetaData.find
  261. .calledWith({ project_id: ObjectId(@project_id) })
  262. .should.equal true
  263. it "should return the metadata", ->
  264. @callback.calledWith(null, @metadata).should.equal true
  265. describe "setProjectMetaData", ->
  266. beforeEach ->
  267. @metadata = { "mock": "metadata" }
  268. @db.projectHistoryMetaData =
  269. update: sinon.stub().callsArgWith(3, null, [@metadata])
  270. @MongoManager.setProjectMetaData @project_id, @metadata, @callback
  271. it "should upsert the metadata into the DB", ->
  272. @db.projectHistoryMetaData.update
  273. .calledWith({
  274. project_id: ObjectId(@project_id)
  275. }, {
  276. $set: @metadata
  277. }, {
  278. upsert: true
  279. })
  280. .should.equal true
  281. it "should call the callback", ->
  282. @callback.called.should.equal true
  283. describe "getDocChangesCount", ->
  284. beforeEach ->
  285. @db.docHistory =
  286. count: sinon.stub().callsArg(1)
  287. @MongoManager.getDocChangesCount @doc_id, @callback
  288. it "should return if there is any doc changes", ->
  289. @db.docHistory.count
  290. .calledWith({
  291. doc_id: ObjectId(@doc_id)
  292. })
  293. .should.equal true
  294. it "should call the callback", ->
  295. @callback.called.should.equal true
  296. describe "getArchivedDocStatus", ->
  297. beforeEach ->
  298. @db.docHistoryStats =
  299. findOne: sinon.stub().callsArg(2)
  300. @MongoManager.getArchivedDocStatus @doc_id, @callback
  301. it "should return if there is any archived doc changes", ->
  302. @db.docHistoryStats.findOne
  303. .calledWith({
  304. doc_id: ObjectId(@doc_id)
  305. inS3: {$exists: true}
  306. })
  307. .should.equal true
  308. it "should call the callback", ->
  309. @callback.called.should.equal true
  310. describe "markDocHistoryAsArchived", ->
  311. beforeEach ->
  312. @update = { _id: ObjectId(), op: "op", meta: "meta", v: "v"}
  313. @db.docHistoryStats =
  314. update: sinon.stub().callsArg(3)
  315. @db.docHistory =
  316. remove: sinon.stub().callsArg(1)
  317. @MongoManager.markDocHistoryAsArchived @doc_id, @update.v, @callback
  318. it "should update doc status with inS3 flag", ->
  319. @db.docHistoryStats.update
  320. .calledWith({
  321. doc_id: ObjectId(@doc_id)
  322. },{
  323. $set : { inS3 : true }
  324. })
  325. .should.equal true
  326. it "should remove any other doc changes before last update", ->
  327. @db.docHistory.remove
  328. .calledWith({
  329. doc_id: ObjectId(@doc_id)
  330. v: { $lte : @update.v }
  331. expiresAt: {$exists : false}
  332. })
  333. .should.equal true
  334. it "should call the callback", ->
  335. @callback.called.should.equal true
  336. describe "markDocHistoryAsUnarchived", ->
  337. beforeEach ->
  338. @db.docHistoryStats =
  339. update: sinon.stub().callsArg(2)
  340. @MongoManager.markDocHistoryAsUnarchived @doc_id, @callback
  341. it "should remove any doc changes inS3 flag", ->
  342. @db.docHistoryStats.update
  343. .calledWith({
  344. doc_id: ObjectId(@doc_id)
  345. },{
  346. $unset : { inS3 : true, lastVersion: true }
  347. })
  348. .should.equal true
  349. it "should call the callback", ->
  350. @callback.called.should.equal true