MongoManagerTests.coffee 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "insertCompressedUpdate", ->
  85. beforeEach ->
  86. @update = { op: "op", meta: "meta", v: "v"}
  87. @db.docHistory =
  88. insert: sinon.stub().callsArg(1)
  89. describe "temporarly", ->
  90. beforeEach ->
  91. @MongoManager.insertCompressedUpdate @project_id, @doc_id, @update, true, @callback
  92. it "should insert the update with a expiresAt field one week away", ->
  93. @db.docHistory.insert
  94. .calledWith({
  95. project_id: ObjectId(@project_id),
  96. doc_id: ObjectId(@doc_id),
  97. op: @update.op,
  98. meta: @update.meta,
  99. v: @update.v
  100. expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000)
  101. })
  102. .should.equal true
  103. it "should call the callback", ->
  104. @callback.called.should.equal true
  105. describe "permanenty", ->
  106. beforeEach ->
  107. @MongoManager.insertCompressedUpdate @project_id, @doc_id, @update, false, @callback
  108. it "should insert the update with no expiresAt field", ->
  109. @db.docHistory.insert
  110. .calledWith({
  111. project_id: ObjectId(@project_id),
  112. doc_id: ObjectId(@doc_id),
  113. op: @update.op,
  114. meta: @update.meta,
  115. v: @update.v
  116. })
  117. .should.equal true
  118. it "should call the callback", ->
  119. @callback.called.should.equal true
  120. describe "insertCompressedUpdates", ->
  121. beforeEach (done) ->
  122. @updates = [ "mock-update-1", "mock-update-2" ]
  123. @MongoManager.insertCompressedUpdate = sinon.stub().callsArg(4)
  124. @MongoManager.insertCompressedUpdates @project_id, @doc_id, @updates, @temporary = true, (args...) =>
  125. @callback(args...)
  126. done()
  127. it "should insert each update", ->
  128. for update in @updates
  129. @MongoManager.insertCompressedUpdate
  130. .calledWith(@project_id, @doc_id, update, @temporary)
  131. .should.equal true
  132. it "should call the callback", ->
  133. @callback.called.should.equal true
  134. describe "getDocUpdates", ->
  135. beforeEach ->
  136. @results = [
  137. {foo: "mock-update", v: 56, doc_id: 100, project_id: 1},
  138. {foo: "mock-update", v: 55, doc_id: 100, project_id: 1},
  139. {pack: [ {foo: "mock-update", v: 54, doc_id: 100, project_id: 1},
  140. {foo: "mock-update", v: 53, doc_id: 100, project_id: 1},
  141. {foo: "mock-update", v: 52, doc_id: 100, project_id: 1} ]
  142. , v: 52, doc_id: 100, project_id: 1},
  143. {foo: "mock-update", v: 42, doc_id: 100, project_id: 1},
  144. {foo: "mock-update", v: 41, doc_id: 100, project_id: 1}
  145. ]
  146. @updates_between = [
  147. {foo: "mock-update", v: 55, doc_id: 100, project_id: 1},
  148. {foo: "mock-update", v: 54, doc_id: 100, project_id: 1},
  149. {foo: "mock-update", v: 53, doc_id: 100, project_id: 1},
  150. {foo: "mock-update", v: 52, doc_id: 100, project_id: 1},
  151. {foo: "mock-update", v: 42, doc_id: 100, project_id: 1}
  152. ]
  153. @updates_after = [
  154. {foo: "mock-update", v: 56, doc_id: 100, project_id: 1},
  155. {foo: "mock-update", v: 55, doc_id: 100, project_id: 1},
  156. {foo: "mock-update", v: 54, doc_id: 100, project_id: 1},
  157. {foo: "mock-update", v: 53, doc_id: 100, project_id: 1},
  158. {foo: "mock-update", v: 52, doc_id: 100, project_id: 1},
  159. {foo: "mock-update", v: 42, doc_id: 100, project_id: 1}
  160. ]
  161. @db.docHistory = {}
  162. @db.docHistory.find = sinon.stub().returns @db.docHistory
  163. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  164. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  165. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @results)
  166. @from = 42
  167. @to = 55
  168. describe "with a to version", ->
  169. beforeEach ->
  170. @MongoManager.getDocUpdates @doc_id, from: @from, to: @to, @callback
  171. it "should find the all updates between the to and from versions", ->
  172. @db.docHistory.find
  173. .calledWith({
  174. doc_id: ObjectId(@doc_id)
  175. v: { $gte: @from, $lte: @to }
  176. })
  177. .should.equal true
  178. it "should sort in descending version order", ->
  179. @db.docHistory.sort
  180. .calledWith("v": -1)
  181. .should.equal true
  182. #it "should not limit the results", ->
  183. # @db.docHistory.limit
  184. # .called.should.equal false
  185. it "should call the call back with the results", ->
  186. @callback.calledWith(null, @updates_between).should.equal true
  187. describe "without a to version", ->
  188. beforeEach ->
  189. @MongoManager.getDocUpdates @doc_id, from: @from, @callback
  190. it "should find the all updates after the from version", ->
  191. @db.docHistory.find
  192. .calledWith({
  193. doc_id: ObjectId(@doc_id)
  194. v: { $gte: @from }
  195. })
  196. .should.equal true
  197. it "should call the call back with the updates", ->
  198. @callback.calledWith(null, @updates_after).should.equal true
  199. describe "with a limit", ->
  200. beforeEach ->
  201. @MongoManager.getDocUpdates @doc_id, from: @from, limit: @limit = 10, @callback
  202. it "should limit the results", ->
  203. @db.docHistory.limit
  204. .calledWith(@limit)
  205. .should.equal true
  206. describe "getDocUpdates", ->
  207. beforeEach ->
  208. @results = [
  209. {foo: "mock-update", v: 56, meta: {end_ts: 110}, doc_id: 100, project_id: 1},
  210. {foo: "mock-update", v: 55, meta: {end_ts: 100}, doc_id: 100, project_id: 1},
  211. {pack: [
  212. {foo: "mock-update", v: 54, meta: {end_ts: 99}, doc_id: 300, project_id: 1},
  213. {foo: "mock-update", v: 53, meta: {end_ts: 98}, doc_id: 300, project_id: 1},
  214. {foo: "mock-update", v: 52, meta: {end_ts: 97}, doc_id: 300, project_id: 1} ]
  215. , v: 52, meta: {end_ts: 100}, doc_id: 300, project_id: 1},
  216. {pack: [
  217. {foo: "mock-update", v: 54, meta: {end_ts: 103}, doc_id: 200, project_id: 1},
  218. {foo: "mock-update", v: 53, meta: {end_ts: 101}, doc_id: 200, project_id: 1},
  219. {foo: "mock-update", v: 52, meta: {end_ts: 99}, doc_id: 200, project_id: 1} ]
  220. , v: 52, meta: {end_ts: 103}, doc_id: 200, project_id: 1},
  221. {foo: "mock-update", v: 42, meta:{end_ts: 90}, doc_id: 100, project_id: 1}
  222. ]
  223. @updates_before = [
  224. {foo: "mock-update", v: 55, meta: {end_ts: 100}, doc_id: 100, project_id: 1},
  225. {foo: "mock-update", v: 52, meta: {end_ts: 99}, doc_id: 200, project_id: 1},
  226. {foo: "mock-update", v: 54, meta: {end_ts: 99}, doc_id: 300, project_id: 1},
  227. {foo: "mock-update", v: 53, meta: {end_ts: 98}, doc_id: 300, project_id: 1},
  228. {foo: "mock-update", v: 52, meta: {end_ts: 97}, doc_id: 300, project_id: 1},
  229. {foo: "mock-update", v: 42, meta: {end_ts: 90}, doc_id: 100, project_id: 1},
  230. ]
  231. @updates_all = [
  232. {foo: "mock-update", v: 56, meta: {end_ts: 110}, doc_id: 100, project_id: 1},
  233. {foo: "mock-update", v: 54, meta: {end_ts: 103}, doc_id: 200, project_id: 1},
  234. {foo: "mock-update", v: 53, meta: {end_ts: 101}, doc_id: 200, project_id: 1},
  235. {foo: "mock-update", v: 55, meta: {end_ts: 100}, doc_id: 100, project_id: 1},
  236. {foo: "mock-update", v: 52, meta: {end_ts: 99}, doc_id: 200, project_id: 1},
  237. {foo: "mock-update", v: 54, meta: {end_ts: 99}, doc_id: 300, project_id: 1},
  238. {foo: "mock-update", v: 53, meta: {end_ts: 98}, doc_id: 300, project_id: 1},
  239. {foo: "mock-update", v: 52, meta: {end_ts: 97}, doc_id: 300, project_id: 1},
  240. {foo: "mock-update", v: 42, meta: {end_ts: 90}, doc_id: 100, project_id: 1}
  241. ]
  242. @db.docHistory = {}
  243. @db.docHistory.find = sinon.stub().returns @db.docHistory
  244. @db.docHistory.sort = sinon.stub().returns @db.docHistory
  245. @db.docHistory.limit = sinon.stub().returns @db.docHistory
  246. @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @results)
  247. @before = 101
  248. describe "with a before timestamp", ->
  249. beforeEach ->
  250. @MongoManager.getProjectUpdates @project_id, before: @before, @callback
  251. it "should find the all updates before the timestamp", ->
  252. @db.docHistory.find
  253. .calledWith({
  254. project_id: ObjectId(@project_id)
  255. "meta.end_ts": { $lt: @before }
  256. })
  257. .should.equal true
  258. it "should sort in descending version order", ->
  259. @db.docHistory.sort
  260. .calledWith("meta.end_ts": -1)
  261. .should.equal true
  262. it "should not limit the results", ->
  263. @db.docHistory.limit
  264. .called.should.equal false
  265. it "should call the call back with the updates", ->
  266. @callback.calledWith(null, @updates_before).should.equal true
  267. describe "without a before timestamp", ->
  268. beforeEach ->
  269. @MongoManager.getProjectUpdates @project_id, {}, @callback
  270. it "should find the all updates", ->
  271. @db.docHistory.find
  272. .calledWith({
  273. project_id: ObjectId(@project_id)
  274. })
  275. .should.equal true
  276. it "should call the call back with the updates", ->
  277. @callback.calledWith(null, @updates_all).should.equal true
  278. describe "with a limit", ->
  279. beforeEach ->
  280. @MongoManager.getProjectUpdates @project_id, before: @before, limit: @limit = 10, @callback
  281. it "should limit the results", ->
  282. @db.docHistory.limit
  283. .calledWith(@limit)
  284. .should.equal true
  285. describe "backportProjectId", ->
  286. beforeEach ->
  287. @db.docHistory =
  288. update: sinon.stub().callsArg(3)
  289. @MongoManager.backportProjectId @project_id, @doc_id, @callback
  290. it "should insert the project_id into all entries for the doc_id which don't have it set", ->
  291. @db.docHistory.update
  292. .calledWith({
  293. doc_id: ObjectId(@doc_id)
  294. project_id: { $exists: false }
  295. }, {
  296. $set: { project_id: ObjectId(@project_id) }
  297. }, {
  298. multi: true
  299. })
  300. .should.equal true
  301. it "should call the callback", ->
  302. @callback.called.should.equal true
  303. describe "getProjectMetaData", ->
  304. beforeEach ->
  305. @metadata = { "mock": "metadata" }
  306. @db.projectHistoryMetaData =
  307. find: sinon.stub().callsArgWith(1, null, [@metadata])
  308. @MongoManager.getProjectMetaData @project_id, @callback
  309. it "should look up the meta data in the db", ->
  310. @db.projectHistoryMetaData.find
  311. .calledWith({ project_id: ObjectId(@project_id) })
  312. .should.equal true
  313. it "should return the metadata", ->
  314. @callback.calledWith(null, @metadata).should.equal true
  315. describe "setProjectMetaData", ->
  316. beforeEach ->
  317. @metadata = { "mock": "metadata" }
  318. @db.projectHistoryMetaData =
  319. update: sinon.stub().callsArgWith(3, null, [@metadata])
  320. @MongoManager.setProjectMetaData @project_id, @metadata, @callback
  321. it "should upsert the metadata into the DB", ->
  322. @db.projectHistoryMetaData.update
  323. .calledWith({
  324. project_id: ObjectId(@project_id)
  325. }, {
  326. $set: @metadata
  327. }, {
  328. upsert: true
  329. })
  330. .should.equal true
  331. it "should call the callback", ->
  332. @callback.called.should.equal true
  333. describe "getDocChangesCount", ->
  334. beforeEach ->
  335. @db.docHistory =
  336. count: sinon.stub().callsArg(1)
  337. @MongoManager.getDocChangesCount @doc_id, @callback
  338. it "should return if there is any doc changes", ->
  339. @db.docHistory.count
  340. .calledWith({
  341. doc_id: ObjectId(@doc_id)
  342. })
  343. .should.equal true
  344. it "should call the callback", ->
  345. @callback.called.should.equal true
  346. describe "getArchivedDocStatus", ->
  347. beforeEach ->
  348. @db.docHistoryStats =
  349. findOne: sinon.stub().callsArg(2)
  350. @MongoManager.getArchivedDocStatus @doc_id, @callback
  351. it "should return if there is any archived doc changes", ->
  352. @db.docHistoryStats.findOne
  353. .calledWith({
  354. doc_id: ObjectId(@doc_id)
  355. inS3: {$exists: true}
  356. })
  357. .should.equal true
  358. it "should call the callback", ->
  359. @callback.called.should.equal true
  360. describe "markDocHistoryAsArchived", ->
  361. beforeEach ->
  362. @update = { _id: ObjectId(), op: "op", meta: "meta", v: "v"}
  363. @db.docHistoryStats =
  364. update: sinon.stub().callsArg(3)
  365. @db.docHistory =
  366. remove: sinon.stub().callsArg(1)
  367. @MongoManager.markDocHistoryAsArchived @doc_id, @update.v, @callback
  368. it "should update doc status with inS3 flag", ->
  369. @db.docHistoryStats.update
  370. .calledWith({
  371. doc_id: ObjectId(@doc_id)
  372. },{
  373. $set : { inS3 : true }
  374. })
  375. .should.equal true
  376. it "should remove any other doc changes before last update", ->
  377. @db.docHistory.remove
  378. .calledWith({
  379. doc_id: ObjectId(@doc_id)
  380. v: { $lte : @update.v }
  381. expiresAt: {$exists : false}
  382. })
  383. .should.equal true
  384. it "should call the callback", ->
  385. @callback.called.should.equal true
  386. describe "markDocHistoryAsUnarchived", ->
  387. beforeEach ->
  388. @db.docHistoryStats =
  389. update: sinon.stub().callsArg(2)
  390. @MongoManager.markDocHistoryAsUnarchived @doc_id, @callback
  391. it "should remove any doc changes inS3 flag", ->
  392. @db.docHistoryStats.update
  393. .calledWith({
  394. doc_id: ObjectId(@doc_id)
  395. },{
  396. $unset : { inS3 : true, lastVersion: true }
  397. })
  398. .should.equal true
  399. it "should call the callback", ->
  400. @callback.called.should.equal true