| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- sinon = require('sinon')
- chai = require('chai')
- should = chai.should()
- modulePath = "../../../../app/js/ProjectManager.js"
- SandboxedModule = require('sandboxed-module')
- _ = require('underscore')
- describe "ProjectManager", ->
- beforeEach ->
- @ProjectManager = SandboxedModule.require modulePath, requires:
- "./RedisManager": @RedisManager = {}
- "./ProjectHistoryRedisManager": @ProjectHistoryRedisManager = {}
- "./DocumentManager": @DocumentManager = {}
- "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
- "./HistoryManager": @HistoryManager = {}
- "./Metrics": @Metrics =
- Timer: class Timer
- done: sinon.stub()
- @project_id = "project-id-123"
- @user_id = "user-id-123"
- @version = 1234567
- @HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(false)
- @HistoryManager.flushProjectChangesAsync = sinon.stub()
- @callback = sinon.stub()
- describe "updateProjectWithLocks", ->
- describe "rename operations", ->
- beforeEach ->
- @firstDocUpdate =
- id: 1
- pathname: 'foo'
- newPathname: 'foo'
- @secondDocUpdate =
- id: 2
- pathname: 'bar'
- newPathname: 'bar2'
- @docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
- @firstFileUpdate =
- id: 2
- pathname: 'bar'
- newPathname: 'bar2'
- @fileUpdates = [ @firstFileUpdate ]
- @DocumentManager.renameDocWithLock = sinon.stub().yields()
- @ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields()
- describe "successfully", ->
- beforeEach ->
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should rename the docs in the updates", ->
- firstDocUpdateWithVersion = _.extend({}, @firstDocUpdate, {version: "#{@version}.0"})
- secondDocUpdateWithVersion = _.extend({}, @secondDocUpdate, {version: "#{@version}.1"})
- @DocumentManager.renameDocWithLock
- .calledWith(@project_id, @firstDocUpdate.id, @user_id, firstDocUpdateWithVersion)
- .should.equal true
- @DocumentManager.renameDocWithLock
- .calledWith(@project_id, @secondDocUpdate.id, @user_id, secondDocUpdateWithVersion)
- .should.equal true
- it "should rename the files in the updates", ->
- firstFileUpdateWithVersion = _.extend({}, @firstFileUpdate, {version: "#{@version}.2"})
- @ProjectHistoryRedisManager.queueRenameEntity
- .calledWith(@project_id, 'file', @firstFileUpdate.id, @user_id, firstFileUpdateWithVersion)
- .should.equal true
- it "should not flush the history", ->
- @HistoryManager.flushProjectChangesAsync
- .calledWith(@project_id)
- .should.equal false
- it "should call the callback", ->
- @callback.called.should.equal true
- describe "when renaming a doc fails", ->
- beforeEach ->
- @error = new Error('error')
- @DocumentManager.renameDocWithLock = sinon.stub().yields(@error)
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should call the callback with the error", ->
- @callback.calledWith(@error).should.equal true
- describe "when renaming a file fails", ->
- beforeEach ->
- @error = new Error('error')
- @ProjectHistoryRedisManager.queueRenameEntity = sinon.stub().yields(@error)
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should call the callback with the error", ->
- @callback.calledWith(@error).should.equal true
- describe "with enough ops to flush", ->
- beforeEach ->
- @HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true)
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should flush the history", ->
- @HistoryManager.flushProjectChangesAsync
- .calledWith(@project_id)
- .should.equal true
- describe "add operations", ->
- beforeEach ->
- @firstDocUpdate =
- id: 1
- docLines: "a\nb"
- @secondDocUpdate =
- id: 2
- docLines: "a\nb"
- @docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
- @firstFileUpdate =
- id: 3
- url: 'filestore.example.com/2'
- @secondFileUpdate =
- id: 4
- url: 'filestore.example.com/3'
- @fileUpdates = [ @firstFileUpdate, @secondFileUpdate ]
- @ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields()
- describe "successfully", ->
- beforeEach ->
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should add the docs in the updates", ->
- firstDocUpdateWithVersion = _.extend({}, @firstDocUpdate, {version: "#{@version}.0"})
- secondDocUpdateWithVersion = _.extend({}, @secondDocUpdate, {version: "#{@version}.1"})
- @ProjectHistoryRedisManager.queueAddEntity.getCall(0)
- .calledWith(@project_id, 'doc', @firstDocUpdate.id, @user_id, firstDocUpdateWithVersion)
- .should.equal true
- @ProjectHistoryRedisManager.queueAddEntity.getCall(1)
- .calledWith(@project_id, 'doc', @secondDocUpdate.id, @user_id, secondDocUpdateWithVersion)
- .should.equal true
- it "should add the files in the updates", ->
- firstFileUpdateWithVersion = _.extend({}, @firstFileUpdate, {version: "#{@version}.2"})
- secondFileUpdateWithVersion = _.extend({}, @secondFileUpdate, {version: "#{@version}.3"})
- @ProjectHistoryRedisManager.queueAddEntity.getCall(2)
- .calledWith(@project_id, 'file', @firstFileUpdate.id, @user_id, firstFileUpdateWithVersion)
- .should.equal true
- @ProjectHistoryRedisManager.queueAddEntity.getCall(3)
- .calledWith(@project_id, 'file', @secondFileUpdate.id, @user_id, secondFileUpdateWithVersion)
- .should.equal true
- it "should not flush the history", ->
- @HistoryManager.flushProjectChangesAsync
- .calledWith(@project_id)
- .should.equal false
- it "should call the callback", ->
- @callback.called.should.equal true
- describe "when adding a doc fails", ->
- beforeEach ->
- @error = new Error('error')
- @ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields(@error)
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should call the callback with the error", ->
- @callback.calledWith(@error).should.equal true
- describe "when adding a file fails", ->
- beforeEach ->
- @error = new Error('error')
- @ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields(@error)
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should call the callback with the error", ->
- @callback.calledWith(@error).should.equal true
- describe "with enough ops to flush", ->
- beforeEach ->
- @HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true)
- @ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @version, @callback
- it "should flush the history", ->
- @HistoryManager.flushProjectChangesAsync
- .calledWith(@project_id)
- .should.equal true
|