overleaf/services/track-changes/test/unit/coffee/DocArchive/DocArchiveManager.coffee

121 lines
4.5 KiB
CoffeeScript
Raw Normal View History

2015-08-31 17:13:18 -04:00
chai = require('chai')
sinon = require("sinon")
should = chai.should()
modulePath = "../../../../app/js/DocArchiveManager.js"
SandboxedModule = require('sandboxed-module')
ObjectId = require("mongojs").ObjectId
describe "DocArchiveManager", ->
beforeEach ->
@DocArchiveManager = SandboxedModule.require modulePath, requires:
"./MongoManager" : @MongoManager = sinon.stub()
"./MongoAWS" : @MongoAWS = sinon.stub()
"./LockManager" : @LockManager = sinon.stub()
"./DocstoreHandler" : @DocstoreHandler = sinon.stub()
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
"settings-sharelatex": @settings =
filestore:
backend: 's3'
@mongoDocs = [{
_id: ObjectId()
}, {
_id: ObjectId()
}, {
_id: ObjectId()
}]
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@callback = sinon.stub()
describe "archiveAllDocsChanges", ->
it "should archive all project docs change", (done)->
@DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs)
@DocArchiveManager.archiveDocChangesWithLock = sinon.stub().callsArgWith(2, null)
@DocArchiveManager.archiveAllDocsChanges @project_id, (err)=>
@DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[0]._id).should.equal true
@DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[1]._id).should.equal true
@DocArchiveManager.archiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[2]._id).should.equal true
should.not.exist err
done()
2015-09-02 14:39:19 -04:00
describe "archiveDocChangesWithLock", ->
beforeEach ->
@DocArchiveManager.archiveDocChanges = sinon.stub().callsArg(2)
@LockManager.runWithLock = sinon.stub().callsArg(2)
@DocArchiveManager.archiveDocChangesWithLock @project_id, @doc_id, @callback
it "should run archiveDocChangesWithLock with the lock", ->
@LockManager.runWithLock
.calledWith(
2015-09-16 11:50:36 -04:00
"HistoryLock:#{@doc_id}"
2015-09-02 14:39:19 -04:00
)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "archiveDocChanges", ->
beforeEach ->
@update = { _id: ObjectId(), op: "op", meta: "meta", v: "v"}
@MongoManager.getDocChangesCount = sinon.stub().callsArg(1)
@MongoManager.getLastCompressedUpdate = sinon.stub().callsArgWith(1, null, @update)
@MongoAWS.archiveDocHistory = sinon.stub().callsArg(2)
@MongoManager.markDocHistoryAsArchived = sinon.stub().callsArg(2)
@DocArchiveManager.archiveDocChanges @project_id, @doc_id, @callback
it "should run markDocHistoryAsArchived with doc_id and update", ->
@MongoManager.markDocHistoryAsArchived
.calledWith(
@doc_id, @update
)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
describe "unArchiveAllDocsChanges", ->
it "should unarchive all project docs change", (done)->
@DocstoreHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @mongoDocs)
2015-09-16 17:39:07 -04:00
@DocArchiveManager.unArchiveDocChangesWithLock = sinon.stub().callsArgWith(2, null)
2015-09-02 14:39:19 -04:00
@DocArchiveManager.unArchiveAllDocsChanges @project_id, (err)=>
2015-09-16 17:39:07 -04:00
@DocArchiveManager.unArchiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[0]._id).should.equal true
@DocArchiveManager.unArchiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[1]._id).should.equal true
@DocArchiveManager.unArchiveDocChangesWithLock.calledWith(@project_id, @mongoDocs[2]._id).should.equal true
2015-09-02 14:39:19 -04:00
should.not.exist err
done()
2015-09-16 17:39:07 -04:00
describe "unArchiveDocChangesWithLock", ->
beforeEach ->
@DocArchiveManager.unArchiveDocChanges = sinon.stub().callsArg(2)
@LockManager.runWithLock = sinon.stub().callsArg(2)
@DocArchiveManager.unArchiveDocChangesWithLock @project_id, @doc_id, @callback
it "should run unArchiveDocChangesWithLock with the lock", ->
@LockManager.runWithLock
.calledWith(
"HistoryLock:#{@doc_id}"
)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true
2015-09-02 14:39:19 -04:00
describe "unArchiveDocChanges", ->
beforeEach ->
@MongoManager.getArchivedDocChanges = sinon.stub().callsArg(1)
@MongoAWS.unArchiveDocHistory = sinon.stub().callsArg(2)
@MongoManager.markDocHistoryAsUnarchived = sinon.stub().callsArg(1)
@DocArchiveManager.unArchiveDocChanges @project_id, @doc_id, @callback
it "should run markDocHistoryAsUnarchived with doc_id", ->
@MongoManager.markDocHistoryAsUnarchived
.calledWith(
@doc_id
)
.should.equal true
it "should call the callback", ->
@callback.called.should.equal true