UpdateTrimmerTests.coffee 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. expect = chai.expect
  5. modulePath = "../../../../app/js/UpdateTrimmer.js"
  6. SandboxedModule = require('sandboxed-module')
  7. tk = require "timekeeper"
  8. describe "UpdateTrimmer", ->
  9. beforeEach ->
  10. @now = new Date()
  11. tk.freeze(@now)
  12. @UpdateTrimmer = SandboxedModule.require modulePath, requires:
  13. "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
  14. "./WebApiManager": @WebApiManager = {}
  15. "./MongoManager": @MongoManager = {}
  16. @callback = sinon.stub()
  17. @project_id = "mock-project-id"
  18. afterEach ->
  19. tk.reset()
  20. describe "_shouldTrimUpdates", ->
  21. beforeEach ->
  22. @metadata = {}
  23. @details =
  24. features: {}
  25. @MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, @metadata)
  26. @MongoManager.setProjectMetaData = sinon.stub().callsArgWith(2)
  27. @WebApiManager.getProjectDetails = sinon.stub().callsArgWith(1, null, @details)
  28. describe "with preserveHistory set in the project meta data", ->
  29. beforeEach ->
  30. @metadata.preserveHistory = true
  31. @UpdateTrimmer._shouldTrimUpdates @project_id, @callback
  32. it "should look up the meta data", ->
  33. @MongoManager.getProjectMetaData
  34. .calledWith(@project_id)
  35. .should.equal true
  36. it "should not look up the project details", ->
  37. @WebApiManager.getProjectDetails
  38. .called
  39. .should.equal false
  40. it "should return false", ->
  41. @callback.calledWith(null, false).should.equal true
  42. describe "without preserveHistory set in the project meta data", ->
  43. beforeEach ->
  44. @metadata.preserveHistory = false
  45. describe "when the project has the versioning feature", ->
  46. beforeEach ->
  47. @details.features.versioning = true
  48. @UpdateTrimmer._shouldTrimUpdates @project_id, @callback
  49. it "should look up the meta data", ->
  50. @MongoManager.getProjectMetaData
  51. .calledWith(@project_id)
  52. .should.equal true
  53. it "should look up the project details", ->
  54. @WebApiManager.getProjectDetails
  55. .calledWith(@project_id)
  56. .should.equal true
  57. it "should insert preserveHistory into the metadata", ->
  58. @MongoManager.setProjectMetaData
  59. .calledWith(@project_id, {preserveHistory: true})
  60. .should.equal true
  61. it "should return false", ->
  62. @callback.calledWith(null, false).should.equal true
  63. describe "when the project does not have the versioning feature", ->
  64. beforeEach ->
  65. @details.features.versioning = false
  66. @UpdateTrimmer._shouldTrimUpdates @project_id, @callback
  67. it "should return true", ->
  68. @callback.calledWith(null, true).should.equal true
  69. describe "without any meta data", ->
  70. beforeEach ->
  71. @MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, null)
  72. describe "when the project has the versioning feature", ->
  73. beforeEach ->
  74. @details.features.versioning = true
  75. @UpdateTrimmer._shouldTrimUpdates @project_id, @callback
  76. it "should insert preserveHistory into the metadata", ->
  77. @MongoManager.setProjectMetaData
  78. .calledWith(@project_id, {preserveHistory: true})
  79. .should.equal true
  80. it "should return false", ->
  81. @callback.calledWith(null, false).should.equal true
  82. describe "when the project does not have the versioning feature", ->
  83. beforeEach ->
  84. @details.features.versioning = false
  85. @UpdateTrimmer._shouldTrimUpdates @project_id, @callback
  86. it "should return true", ->
  87. @callback.calledWith(null, true).should.equal true
  88. describe "deleteOldProjectUpdates", ->
  89. beforeEach ->
  90. @oneWeek = 7 * 24 * 60 * 60 * 1000
  91. @MongoManager.deleteOldProjectUpdates = sinon.stub().callsArg(2)
  92. describe "when the updates should be trimmed", ->
  93. beforeEach ->
  94. @UpdateTrimmer._shouldTrimUpdates = sinon.stub().callsArgWith(1, null, true)
  95. @UpdateTrimmer.deleteOldProjectUpdates @project_id, @callback
  96. it "should delete week old updates in mongo", ->
  97. before = Date.now() - @oneWeek
  98. @MongoManager.deleteOldProjectUpdates
  99. .calledWith(@project_id, before)
  100. .should.equal true
  101. it 'should call the callback', ->
  102. @callback.called.should.equal true
  103. describe "when the updates should not be trimmed", ->
  104. beforeEach ->
  105. @UpdateTrimmer._shouldTrimUpdates = sinon.stub().callsArgWith(1, null, false)
  106. @UpdateTrimmer.deleteOldProjectUpdates @project_id, @callback
  107. it "should not delete any updates in mongo", ->
  108. @MongoManager.deleteOldProjectUpdates
  109. .called
  110. .should.equal false
  111. it 'should call the callback', ->
  112. @callback.called.should.equal true