DocArchiveManager.coffee 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "HistoryLock:#{@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.getArchivedDocChanges = sinon.stub().callsArgWith(1, null, 0)
  56. @MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
  57. @MongoAWS.archiveDocHistory = sinon.stub().callsArg(3)
  58. @MongoManager.markDocHistoryAsArchiveInProgress = sinon.stub().callsArg(2)
  59. @MongoManager.markDocHistoryAsArchived = sinon.stub().callsArg(2)
  60. @DocArchiveManager.archiveDocChanges @project_id, @doc_id, @callback
  61. it "should run markDocHistoryAsArchived with doc_id and update", ->
  62. @MongoManager.markDocHistoryAsArchived
  63. .calledWith(
  64. @doc_id, @update
  65. )
  66. .should.equal true
  67. it "should call the callback", ->
  68. @callback.called.should.equal true
  69. describe "unArchiveAllDocsChanges", ->
  70. it "should unarchive all project docs change", (done)->
  71. @DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs)
  72. @DocArchiveManager.unArchiveDocChangesWithLock = sinon.stub().callsArgWith(2, null)
  73. @DocArchiveManager.unArchiveAllDocsChanges @project_id, (err)=>
  74. @DocArchiveManager.unArchiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[0]._id).should.equal true
  75. @DocArchiveManager.unArchiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[1]._id).should.equal true
  76. @DocArchiveManager.unArchiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[2]._id).should.equal true
  77. should.not.exist err
  78. done()
  79. describe "unArchiveDocChangesWithLock", ->
  80. beforeEach ->
  81. @DocArchiveManager.unArchiveDocChanges = sinon.stub().callsArg(2)
  82. @LockManager.runWithLock = sinon.stub().callsArg(2)
  83. @DocArchiveManager.unArchiveDocChangesWithLock @project_id, @doc_id, @callback
  84. it "should run unArchiveDocChangesWithLock with the lock", ->
  85. @LockManager.runWithLock
  86. .calledWith(
  87. "HistoryLock:#{@doc_id}"
  88. )
  89. .should.equal true
  90. it "should call the callback", ->
  91. @callback.called.should.equal true
  92. describe "unArchiveDocChanges", ->
  93. beforeEach ->
  94. @MongoManager.getArchivedDocChanges = sinon.stub().callsArg(1)
  95. @MongoAWS.unArchiveDocHistory = sinon.stub().callsArg(2)
  96. @MongoManager.markDocHistoryAsUnarchived = sinon.stub().callsArg(1)
  97. @DocArchiveManager.unArchiveDocChanges @project_id, @doc_id, @callback
  98. it "should run markDocHistoryAsUnarchived with doc_id", ->
  99. @MongoManager.markDocHistoryAsUnarchived
  100. .calledWith(
  101. @doc_id
  102. )
  103. .should.equal true
  104. it "should call the callback", ->
  105. @callback.called.should.equal true