overleaf/services/document-updater/test/unit/coffee/ProjectManager/updateProjectTests.coffee

74 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2017-11-01 15:16:49 -04:00
sinon = require('sinon')
chai = require('chai')
should = chai.should()
modulePath = "../../../../app/js/ProjectManager.js"
SandboxedModule = require('sandboxed-module')
describe "ProjectManager", ->
beforeEach ->
@ProjectManager = SandboxedModule.require modulePath, requires:
"./RedisManager": @RedisManager = {}
"./DocumentManager": @DocumentManager = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
"./Metrics": @Metrics =
Timer: class Timer
done: sinon.stub()
@project_id = "project-id-123"
@user_id = "user-id-123"
@callback = sinon.stub()
describe "updateProjectWithLocks", ->
beforeEach ->
2017-11-06 11:14:27 -05:00
@firstDocUpdate =
2017-11-01 15:16:49 -04:00
id: 1
update: 'foo'
2017-11-06 11:14:27 -05:00
@secondDocUpdate =
2017-11-01 15:16:49 -04:00
id: 2
update: 'bar'
2017-11-06 11:14:27 -05:00
@docUpdates = [ @firstDocUpdate, @secondDocUpdate ]
@firstFileUpdate =
id: 2
update: 'bar'
@fileUpdates = [ @firstFileUpdate ]
@DocumentManager.renameDocWithLock = sinon.stub().yields()
@RedisManager.renameFile = sinon.stub().yields()
2017-11-01 15:16:49 -04:00
describe "successfully", ->
beforeEach ->
2017-11-06 11:14:27 -05:00
@ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
2017-11-01 15:16:49 -04:00
2017-11-06 11:14:27 -05:00
it "should rename the docs in the updates", ->
2017-11-01 15:16:49 -04:00
@DocumentManager.renameDocWithLock
2017-11-06 11:14:27 -05:00
.calledWith(@project_id, @firstDocUpdate.id, @user_id, @firstDocUpdate)
2017-11-01 15:16:49 -04:00
.should.equal true
@DocumentManager.renameDocWithLock
2017-11-06 11:14:27 -05:00
.calledWith(@project_id, @secondDocUpdate.id, @user_id, @secondDocUpdate)
.should.equal true
it "should rename the files in the updates", ->
@RedisManager.renameFile
.calledWith(@project_id, @firstFileUpdate.id, @user_id, @firstFileUpdate)
2017-11-01 15:16:49 -04:00
.should.equal true
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)
2017-11-06 11:14:27 -05:00
@ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @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')
@RedisManager.renameFile = sinon.stub().yields(@error)
@ProjectManager.updateProjectWithLocks @project_id, @user_id, @docUpdates, @fileUpdates, @callback
2017-11-01 15:16:49 -04:00
it "should call the callback with the error", ->
@callback.calledWith(@error).should.equal true