DocArchiveManager.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. chai = require('chai')
  2. sinon = require("sinon")
  3. should = chai.should()
  4. modulePath = "../../../../app/js/DocArchiveManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. ObjectId = require("mongojs").ObjectId
  7. describe "DocArchiveManager", ->
  8. beforeEach ->
  9. @DocArchiveManager = SandboxedModule.require modulePath, requires:
  10. "./MongoManager" : @MongoManager = sinon.stub()
  11. "./MongoAWS" : @MongoAWS = sinon.stub()
  12. "./LockManager" : @LockManager = sinon.stub()
  13. "./DocstoreHandler" : @DocstoreHandler = sinon.stub()
  14. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  15. "settings-sharelatex": @settings =
  16. filestore:
  17. backend: 's3'
  18. @mongoDocs = [{
  19. _id: ObjectId()
  20. }, {
  21. _id: ObjectId()
  22. }, {
  23. _id: ObjectId()
  24. }]
  25. @project_id = "project-id-123"
  26. @doc_id = "doc-id-123"
  27. @callback = sinon.stub()
  28. describe "archiveAllDocsChanges", ->
  29. it "should archive all project docs change", (done)->
  30. @DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs)
  31. @DocArchiveManager.archiveDocChangesWithLock = sinon.stub().callsArgWith(2, null)
  32. @DocArchiveManager.archiveAllDocsChanges @project_id, (err)=>
  33. @DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[0]._id).should.equal true
  34. @DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[1]._id).should.equal true
  35. @DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[2]._id).should.equal true
  36. should.not.exist err
  37. done()
  38. describe "archiveDocChangesWithLock", ->
  39. beforeEach ->
  40. @DocArchiveManager.archiveDocChanges = sinon.stub().callsArg(2)
  41. @LockManager.runWithLock = sinon.stub().callsArg(2)
  42. @DocArchiveManager.archiveDocChangesWithLock @project_id, @doc_id, @callback
  43. it "should run archiveDocChangesWithLock with the lock", ->
  44. @LockManager.runWithLock
  45. .calledWith(
  46. "HistoryArchiveLock:#{@doc_id}"
  47. )
  48. .should.equal true
  49. it "should call the callback", ->
  50. @callback.called.should.equal true
  51. describe "archiveDocChanges", ->
  52. beforeEach ->
  53. @update = { _id: ObjectId(), op: "op", meta: "meta", v: "v"}
  54. @MongoManager.getDocChangesCount = sinon.stub().callsArg(1)
  55. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
  56. @MongoAWS.archiveDocHistory = sinon.stub().callsArg(2)
  57. @MongoManager.markDocHistoryAsArchived = sinon.stub().callsArg(2)
  58. @DocArchiveManager.archiveDocChanges @project_id, @doc_id, @callback
  59. it "should run markDocHistoryAsArchived with doc_id and update", ->
  60. @MongoManager.markDocHistoryAsArchived
  61. .calledWith(
  62. @doc_id, @update
  63. )
  64. .should.equal true
  65. it "should call the callback", ->
  66. @callback.called.should.equal true
  67. describe "unArchiveAllDocsChanges", ->
  68. it "should unarchive all project docs change", (done)->
  69. @DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs)
  70. @DocArchiveManager.unArchiveDocChanges = sinon.stub().callsArgWith(2, null)
  71. @DocArchiveManager.unArchiveAllDocsChanges @project_id, (err)=>
  72. @DocArchiveManager.unArchiveDocChanges.calledWith(@project_id, @mongoDocs[0]._id).should.equal true
  73. @DocArchiveManager.unArchiveDocChanges.calledWith(@project_id, @mongoDocs[1]._id).should.equal true
  74. @DocArchiveManager.unArchiveDocChanges.calledWith(@project_id, @mongoDocs[2]._id).should.equal true
  75. should.not.exist err
  76. done()
  77. describe "unArchiveDocChanges", ->
  78. beforeEach ->
  79. @MongoManager.getArchivedDocChanges = sinon.stub().callsArg(1)
  80. @MongoAWS.unArchiveDocHistory = sinon.stub().callsArg(2)
  81. @MongoManager.markDocHistoryAsUnarchived = sinon.stub().callsArg(1)
  82. @DocArchiveManager.unArchiveDocChanges @project_id, @doc_id, @callback
  83. it "should run markDocHistoryAsUnarchived with doc_id", ->
  84. @MongoManager.markDocHistoryAsUnarchived
  85. .calledWith(
  86. @doc_id
  87. )
  88. .should.equal true
  89. it "should call the callback", ->
  90. @callback.called.should.equal true