2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS206: Consider reworking classes to avoid initClass
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2021-03-31 08:20:55 -04:00
|
|
|
const { assert, expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const modulePath = '../../../../app/src/Features/Project/ProjectHistoryHandler'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('ProjectHistoryHandler', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
const project_id = '4eecb1c1bffa66588e0000a1'
|
|
|
|
const userId = 1234
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
let Project
|
2021-04-14 09:17:21 -04:00
|
|
|
this.ProjectModel = Project = (function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
Project = class Project {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.rootFolder = [this.rootFolder]
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
constructor(options) {
|
|
|
|
this._id = project_id
|
|
|
|
this.name = 'project_name_here'
|
|
|
|
this.rev = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Project.initClass()
|
|
|
|
return Project
|
|
|
|
})()
|
|
|
|
this.project = new this.ProjectModel()
|
|
|
|
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
|
|
|
|
return (this.ProjectHistoryHandler = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': (this.Settings = {}),
|
2019-05-29 05:21:06 -04:00
|
|
|
'../../models/Project': {
|
2021-04-27 03:52:58 -04:00
|
|
|
Project: this.ProjectModel,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
'./ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
|
|
|
|
'../History/HistoryManager': (this.HistoryManager = {}),
|
2021-04-27 03:52:58 -04:00
|
|
|
'./ProjectEntityUpdateHandler': (this.ProjectEntityUpdateHandler = {}),
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('starting history for an existing project', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.newHistoryId = 123456789
|
|
|
|
this.HistoryManager.initializeProject = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(0, null, { overleaf_id: this.newHistoryId })
|
|
|
|
this.HistoryManager.flushProject = sinon.stub().callsArg(1)
|
2021-08-12 11:19:56 -04:00
|
|
|
return (this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
|
2019-05-29 05:21:06 -04:00
|
|
|
.stub()
|
2021-08-12 11:19:56 -04:00
|
|
|
.callsArg(1))
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the history does not already exist', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectDetailsHandler.getDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(project_id)
|
|
|
|
.callsArgWith(1, null, this.project)
|
2020-11-03 04:19:05 -05:00
|
|
|
this.ProjectModel.updateOne = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, { n: 1 })
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
|
|
|
|
project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get any existing history id for the project', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.ProjectDetailsHandler.getDetails
|
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should initialize a new history in the v1 history service', function () {
|
2019-11-18 09:37:05 -05:00
|
|
|
return this.HistoryManager.initializeProject.called.should.equal(true)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the new history id on the project', function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
return this.ProjectModel.updateOne
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(
|
|
|
|
{ _id: project_id, 'overleaf.history.id': { $exists: false } },
|
|
|
|
{ 'overleaf.history.id': this.newHistoryId }
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-08-12 11:19:56 -04:00
|
|
|
it('should resync the project history', function () {
|
|
|
|
return this.ProjectEntityUpdateHandler.resyncProjectHistory
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-08-12 11:19:56 -04:00
|
|
|
it('should flush the project history', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.HistoryManager.flushProject
|
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the callback without an error', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('when the history already exists', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project.overleaf = { history: { id: 1234 } }
|
|
|
|
this.ProjectDetailsHandler.getDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(project_id)
|
|
|
|
.callsArgWith(1, null, this.project)
|
2020-11-03 04:19:05 -05:00
|
|
|
this.ProjectModel.updateOne = sinon.stub()
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.ProjectHistoryHandler.ensureHistoryExistsForProject(
|
|
|
|
project_id,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get any existing history id for the project', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.ProjectDetailsHandler.getDetails
|
|
|
|
.calledWith(project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not initialize a new history in the v1 history service', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.HistoryManager.initializeProject.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not set the new history id on the project', function () {
|
2020-11-03 04:19:05 -05:00
|
|
|
return this.ProjectModel.updateOne.called.should.equal(false)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-08-12 11:19:56 -04:00
|
|
|
it('should not resync the project history', function () {
|
|
|
|
return this.ProjectEntityUpdateHandler.resyncProjectHistory.called.should.equal(
|
|
|
|
false
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not flush the project history', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.HistoryManager.flushProject.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the callback', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.callback.calledWith().should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|