2014-03-28 12:01:34 -04:00
|
|
|
sinon = require('sinon')
|
|
|
|
chai = require('chai')
|
|
|
|
should = chai.should()
|
|
|
|
expect = chai.expect
|
|
|
|
modulePath = "../../../../app/js/UpdateTrimmer.js"
|
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
tk = require "timekeeper"
|
|
|
|
|
|
|
|
describe "UpdateTrimmer", ->
|
|
|
|
beforeEach ->
|
|
|
|
@now = new Date()
|
|
|
|
tk.freeze(@now)
|
|
|
|
|
|
|
|
@UpdateTrimmer = SandboxedModule.require modulePath, requires:
|
|
|
|
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
|
|
|
|
"./WebApiManager": @WebApiManager = {}
|
|
|
|
"./MongoManager": @MongoManager = {}
|
|
|
|
|
|
|
|
@callback = sinon.stub()
|
|
|
|
@project_id = "mock-project-id"
|
|
|
|
|
|
|
|
afterEach ->
|
|
|
|
tk.reset()
|
|
|
|
|
2014-05-16 10:59:12 -04:00
|
|
|
describe "shouldTrimUpdates", ->
|
2014-03-28 12:01:34 -04:00
|
|
|
beforeEach ->
|
|
|
|
@metadata = {}
|
|
|
|
@details =
|
|
|
|
features: {}
|
|
|
|
@MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, @metadata)
|
|
|
|
@MongoManager.setProjectMetaData = sinon.stub().callsArgWith(2)
|
|
|
|
@WebApiManager.getProjectDetails = sinon.stub().callsArgWith(1, null, @details)
|
|
|
|
|
|
|
|
describe "with preserveHistory set in the project meta data", ->
|
|
|
|
beforeEach ->
|
|
|
|
@metadata.preserveHistory = true
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates @project_id, @callback
|
2014-03-28 12:01:34 -04:00
|
|
|
|
|
|
|
it "should look up the meta data", ->
|
|
|
|
@MongoManager.getProjectMetaData
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should not look up the project details", ->
|
|
|
|
@WebApiManager.getProjectDetails
|
|
|
|
.called
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
|
|
|
|
|
|
|
describe "without preserveHistory set in the project meta data", ->
|
|
|
|
beforeEach ->
|
|
|
|
@metadata.preserveHistory = false
|
|
|
|
|
|
|
|
describe "when the project has the versioning feature", ->
|
|
|
|
beforeEach ->
|
|
|
|
@details.features.versioning = true
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates @project_id, @callback
|
2014-03-28 12:01:34 -04:00
|
|
|
|
|
|
|
it "should look up the meta data", ->
|
|
|
|
@MongoManager.getProjectMetaData
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should look up the project details", ->
|
|
|
|
@WebApiManager.getProjectDetails
|
|
|
|
.calledWith(@project_id)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should insert preserveHistory into the metadata", ->
|
|
|
|
@MongoManager.setProjectMetaData
|
|
|
|
.calledWith(@project_id, {preserveHistory: true})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
|
|
|
|
|
|
|
describe "when the project does not have the versioning feature", ->
|
|
|
|
beforeEach ->
|
|
|
|
@details.features.versioning = false
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates @project_id, @callback
|
2014-03-28 12:01:34 -04:00
|
|
|
|
|
|
|
it "should return true", ->
|
|
|
|
@callback.calledWith(null, true).should.equal true
|
|
|
|
|
|
|
|
describe "without any meta data", ->
|
|
|
|
beforeEach ->
|
|
|
|
@MongoManager.getProjectMetaData = sinon.stub().callsArgWith(1, null, null)
|
|
|
|
|
|
|
|
describe "when the project has the versioning feature", ->
|
|
|
|
beforeEach ->
|
|
|
|
@details.features.versioning = true
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates @project_id, @callback
|
2014-03-28 12:01:34 -04:00
|
|
|
|
|
|
|
it "should insert preserveHistory into the metadata", ->
|
|
|
|
@MongoManager.setProjectMetaData
|
|
|
|
.calledWith(@project_id, {preserveHistory: true})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should return false", ->
|
|
|
|
@callback.calledWith(null, false).should.equal true
|
|
|
|
|
|
|
|
describe "when the project does not have the versioning feature", ->
|
|
|
|
beforeEach ->
|
|
|
|
@details.features.versioning = false
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates @project_id, @callback
|
2014-03-28 12:01:34 -04:00
|
|
|
|
|
|
|
it "should return true", ->
|
|
|
|
@callback.calledWith(null, true).should.equal true
|
|
|
|
|
|
|
|
describe "deleteOldProjectUpdates", ->
|
|
|
|
beforeEach ->
|
|
|
|
@oneWeek = 7 * 24 * 60 * 60 * 1000
|
|
|
|
@MongoManager.deleteOldProjectUpdates = sinon.stub().callsArg(2)
|
|
|
|
|
|
|
|
describe "when the updates should be trimmed", ->
|
|
|
|
beforeEach ->
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, true)
|
2014-03-28 12:01:34 -04:00
|
|
|
@UpdateTrimmer.deleteOldProjectUpdates @project_id, @callback
|
|
|
|
|
|
|
|
it "should delete week old updates in mongo", ->
|
|
|
|
before = Date.now() - @oneWeek
|
|
|
|
@MongoManager.deleteOldProjectUpdates
|
|
|
|
.calledWith(@project_id, before)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it 'should call the callback', ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "when the updates should not be trimmed", ->
|
|
|
|
beforeEach ->
|
2014-05-16 10:59:12 -04:00
|
|
|
@UpdateTrimmer.shouldTrimUpdates = sinon.stub().callsArgWith(1, null, false)
|
2014-03-28 12:01:34 -04:00
|
|
|
@UpdateTrimmer.deleteOldProjectUpdates @project_id, @callback
|
|
|
|
|
|
|
|
it "should not delete any updates in mongo", ->
|
|
|
|
@MongoManager.deleteOldProjectUpdates
|
|
|
|
.called
|
|
|
|
.should.equal false
|
|
|
|
|
|
|
|
it 'should call the callback', ->
|
|
|
|
@callback.called.should.equal true
|