2020-05-06 06:11:22 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:10:51 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-06 06:11:36 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const should = chai.should()
|
|
|
|
const modulePath = '../../../../app/js/ProjectManager.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const _ = require('lodash')
|
|
|
|
|
|
|
|
describe('ProjectManager', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
let Timer
|
|
|
|
this.ProjectManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./RedisManager': (this.RedisManager = {}),
|
|
|
|
'./ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
|
|
|
|
'./DocumentManager': (this.DocumentManager = {}),
|
|
|
|
'logger-sharelatex': (this.logger = {
|
|
|
|
log: sinon.stub(),
|
|
|
|
error: sinon.stub()
|
|
|
|
}),
|
|
|
|
'./HistoryManager': (this.HistoryManager = {}),
|
|
|
|
'./Metrics': (this.Metrics = {
|
|
|
|
Timer: (Timer = (function () {
|
|
|
|
Timer = class Timer {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.done = sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Timer.initClass()
|
|
|
|
return Timer
|
|
|
|
})())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.projectHistoryId = 'history-id-123'
|
|
|
|
this.user_id = 'user-id-123'
|
|
|
|
this.version = 1234567
|
|
|
|
this.HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(false)
|
|
|
|
this.HistoryManager.flushProjectChangesAsync = sinon.stub()
|
2020-05-15 08:31:06 -04:00
|
|
|
this.callback = sinon.stub()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('updateProjectWithLocks', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
describe('rename operations', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.firstDocUpdate = {
|
|
|
|
id: 1,
|
|
|
|
pathname: 'foo',
|
|
|
|
newPathname: 'foo'
|
|
|
|
}
|
|
|
|
this.secondDocUpdate = {
|
|
|
|
id: 2,
|
|
|
|
pathname: 'bar',
|
|
|
|
newPathname: 'bar2'
|
|
|
|
}
|
|
|
|
this.docUpdates = [this.firstDocUpdate, this.secondDocUpdate]
|
|
|
|
this.firstFileUpdate = {
|
|
|
|
id: 2,
|
|
|
|
pathname: 'bar',
|
|
|
|
newPathname: 'bar2'
|
|
|
|
}
|
|
|
|
this.fileUpdates = [this.firstFileUpdate]
|
|
|
|
this.DocumentManager.renameDocWithLock = sinon.stub().yields()
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity = sinon
|
2020-05-06 06:11:36 -04:00
|
|
|
.stub()
|
2020-05-15 08:31:06 -04:00
|
|
|
.yields()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should rename the docs in the updates', function () {
|
|
|
|
const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
|
|
|
|
version: `${this.version}.0`
|
|
|
|
})
|
|
|
|
const secondDocUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.secondDocUpdate,
|
|
|
|
{ version: `${this.version}.1` }
|
|
|
|
)
|
|
|
|
this.DocumentManager.renameDocWithLock
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.firstDocUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
firstDocUpdateWithVersion,
|
|
|
|
this.projectHistoryId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.DocumentManager.renameDocWithLock
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.secondDocUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
secondDocUpdateWithVersion,
|
|
|
|
this.projectHistoryId
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should rename the files in the updates', function () {
|
|
|
|
const firstFileUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.firstFileUpdate,
|
|
|
|
{ version: `${this.version}.2` }
|
|
|
|
)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.firstFileUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
firstFileUpdateWithVersion
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush the history', function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when renaming a doc fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
|
|
|
this.DocumentManager.renameDocWithLock = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when renaming a file fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
|
|
|
this.ProjectHistoryRedisManager.queueRenameEntity = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('with enough ops to flush', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should flush the history', function () {
|
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('add operations', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.firstDocUpdate = {
|
|
|
|
id: 1,
|
|
|
|
docLines: 'a\nb'
|
|
|
|
}
|
|
|
|
this.secondDocUpdate = {
|
|
|
|
id: 2,
|
|
|
|
docLines: 'a\nb'
|
|
|
|
}
|
|
|
|
this.docUpdates = [this.firstDocUpdate, this.secondDocUpdate]
|
|
|
|
this.firstFileUpdate = {
|
|
|
|
id: 3,
|
|
|
|
url: 'filestore.example.com/2'
|
|
|
|
}
|
|
|
|
this.secondFileUpdate = {
|
|
|
|
id: 4,
|
|
|
|
url: 'filestore.example.com/3'
|
|
|
|
}
|
|
|
|
this.fileUpdates = [this.firstFileUpdate, this.secondFileUpdate]
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity = sinon.stub().yields()
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('successfully', function () {
|
|
|
|
beforeEach(function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add the docs in the updates', function () {
|
|
|
|
const firstDocUpdateWithVersion = _.extend({}, this.firstDocUpdate, {
|
|
|
|
version: `${this.version}.0`
|
|
|
|
})
|
|
|
|
const secondDocUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.secondDocUpdate,
|
|
|
|
{ version: `${this.version}.1` }
|
|
|
|
)
|
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
|
|
|
.getCall(0)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
this.firstDocUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
firstDocUpdateWithVersion
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
2020-05-06 06:11:36 -04:00
|
|
|
.getCall(1)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'doc',
|
|
|
|
this.secondDocUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
secondDocUpdateWithVersion
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add the files in the updates', function () {
|
|
|
|
const firstFileUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.firstFileUpdate,
|
|
|
|
{ version: `${this.version}.2` }
|
|
|
|
)
|
|
|
|
const secondFileUpdateWithVersion = _.extend(
|
|
|
|
{},
|
|
|
|
this.secondFileUpdate,
|
|
|
|
{ version: `${this.version}.3` }
|
|
|
|
)
|
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
|
|
|
.getCall(2)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.firstFileUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
firstFileUpdateWithVersion
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity
|
2020-05-06 06:11:36 -04:00
|
|
|
.getCall(3)
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
'file',
|
|
|
|
this.secondFileUpdate.id,
|
|
|
|
this.user_id,
|
|
|
|
secondFileUpdateWithVersion
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not flush the history', function () {
|
2020-05-15 08:31:06 -04:00
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback', function () {
|
|
|
|
this.callback.called.should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when adding a doc fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when adding a file fails', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.error = new Error('error')
|
|
|
|
this.ProjectHistoryRedisManager.queueAddEntity = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(this.error)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should call the callback with the error', function () {
|
|
|
|
this.callback.calledWith(this.error).should.equal(true)
|
2020-05-06 06:11:36 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
describe('with enough ops to flush', function () {
|
2020-05-06 06:11:36 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
this.HistoryManager.shouldFlushHistoryOps = sinon.stub().returns(true)
|
2020-05-15 08:31:06 -04:00
|
|
|
this.ProjectManager.updateProjectWithLocks(
|
2020-05-06 06:11:36 -04:00
|
|
|
this.project_id,
|
|
|
|
this.projectHistoryId,
|
|
|
|
this.user_id,
|
|
|
|
this.docUpdates,
|
|
|
|
this.fileUpdates,
|
|
|
|
this.version,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-05-15 08:31:06 -04:00
|
|
|
it('should flush the history', function () {
|
|
|
|
this.HistoryManager.flushProjectChangesAsync
|
2020-05-06 06:11:36 -04:00
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|