UpdateTrimmerTests.coffee 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. @MongoManager.upgradeHistory = sinon.stub().callsArgWith(1)
  28. @WebApiManager.getProjectDetails = sinon.stub().callsArgWith(1, null, @details)
  29. describe "with preserveHistory set in the project meta data", ->
  30. beforeEach ->
  31. @metadata.preserveHistory = true
  32. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  33. it "should look up the meta data", ->
  34. @MongoManager.getProjectMetaData
  35. .calledWith(@project_id)
  36. .should.equal true
  37. it "should not look up the project details", ->
  38. @WebApiManager.getProjectDetails
  39. .called
  40. .should.equal false
  41. it "should return false", ->
  42. @callback.calledWith(null, false).should.equal true
  43. describe "without preserveHistory set in the project meta data", ->
  44. beforeEach ->
  45. @metadata.preserveHistory = false
  46. describe "when the project has the versioning feature", ->
  47. beforeEach ->
  48. @details.features.versioning = true
  49. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  50. it "should look up the meta data", ->
  51. @MongoManager.getProjectMetaData
  52. .calledWith(@project_id)
  53. .should.equal true
  54. it "should look up the project details", ->
  55. @WebApiManager.getProjectDetails
  56. .calledWith(@project_id)
  57. .should.equal true
  58. it "should insert preserveHistory into the metadata", ->
  59. @MongoManager.setProjectMetaData
  60. .calledWith(@project_id, {preserveHistory: true})
  61. .should.equal true
  62. it "should upgrade any existing history", ->
  63. @MongoManager.upgradeHistory
  64. .calledWith(@project_id)
  65. .should.equal true
  66. it "should return false", ->
  67. @callback.calledWith(null, false).should.equal true
  68. describe "when the project does not have the versioning feature", ->
  69. beforeEach ->
  70. @details.features.versioning = false
  71. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  72. it "should return true", ->
  73. @callback.calledWith(null, true).should.equal true
  74. describe "without any meta data", ->
  75. beforeEach ->
  76. @MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, null)
  77. describe "when the project has the versioning feature", ->
  78. beforeEach ->
  79. @details.features.versioning = true
  80. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  81. it "should insert preserveHistory into the metadata", ->
  82. @MongoManager.setProjectMetaData
  83. .calledWith(@project_id, {preserveHistory: true})
  84. .should.equal true
  85. it "should upgrade any existing history", ->
  86. @MongoManager.upgradeHistory
  87. .calledWith(@project_id)
  88. .should.equal true
  89. it "should return false", ->
  90. @callback.calledWith(null, false).should.equal true
  91. describe "when the project does not have the versioning feature", ->
  92. beforeEach ->
  93. @details.features.versioning = false
  94. @UpdateTrimmer.shouldTrimUpdates @project_id, @callback
  95. it "should return true", ->
  96. @callback.calledWith(null, true).should.equal true