2020-03-03 08:59:09 -05:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-03-04 04:38:40 -05:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-03-03 08:59:09 -05:00
|
|
|
const MODULE_PATH = '../../../../app/src/Features/History/HistoryManager'
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
describe('HistoryManager', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.user_id = 'user-id-123'
|
|
|
|
this.AuthenticationController = {
|
|
|
|
getLoggedInUserId: sinon.stub().returns(this.user_id)
|
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
this.request = {
|
2020-03-04 04:38:40 -05:00
|
|
|
post: sinon.stub(),
|
|
|
|
delete: sinon.stub().resolves()
|
2020-03-03 08:59:09 -05:00
|
|
|
}
|
2020-03-04 04:38:40 -05:00
|
|
|
this.projectHistoryUrl = 'http://project_history.example.com'
|
2020-03-03 08:59:09 -05:00
|
|
|
this.settings = {
|
|
|
|
apis: {
|
|
|
|
trackchanges: {
|
|
|
|
enabled: false,
|
|
|
|
url: 'http://trackchanges.example.com'
|
|
|
|
},
|
|
|
|
project_history: {
|
2020-03-04 04:38:40 -05:00
|
|
|
url: this.projectHistoryUrl
|
2020-03-03 08:59:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.UserGetter = {
|
|
|
|
promises: {
|
|
|
|
getUsersByV1Ids: sinon.stub(),
|
|
|
|
getUsers: sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.HistoryManager = SandboxedModule.require(MODULE_PATH, {
|
2019-07-15 06:33:47 -04:00
|
|
|
globals: {
|
|
|
|
console: console
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
2020-03-03 08:59:09 -05:00
|
|
|
'request-promise-native': this.request,
|
|
|
|
'settings-sharelatex': this.settings,
|
|
|
|
'../User/UserGetter': this.UserGetter
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('initializeProject', function() {
|
|
|
|
describe('with project history enabled', function() {
|
|
|
|
beforeEach(function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
this.settings.apis.project_history.initializeHistoryForNewProjects = true
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('project history returns a successful response', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
beforeEach(async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.overleaf_id = 1234
|
2020-03-03 08:59:09 -05:00
|
|
|
this.request.post.resolves(
|
|
|
|
JSON.stringify({ project: { id: this.overleaf_id } })
|
|
|
|
)
|
|
|
|
this.result = await this.HistoryManager.promises.initializeProject()
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the project history api', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
this.request.post
|
2019-05-29 05:21:06 -04:00
|
|
|
.calledWith({
|
|
|
|
url: `${this.settings.apis.project_history.url}/project`
|
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should return the overleaf id', function() {
|
|
|
|
expect(this.result).to.deep.equal({ overleaf_id: this.overleaf_id })
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('project history returns a response without the project id', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should throw an error', async function() {
|
|
|
|
this.request.post.resolves(JSON.stringify({ project: {} }))
|
|
|
|
await expect(this.HistoryManager.promises.initializeProject()).to.be
|
|
|
|
.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('project history errors', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should propagate the error', async function() {
|
|
|
|
this.request.post.rejects(new Error('problem connecting'))
|
|
|
|
await expect(this.HistoryManager.promises.initializeProject()).to.be
|
|
|
|
.rejected
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('with project history disabled', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should return without errors', async function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.settings.apis.project_history.initializeHistoryForNewProjects = false
|
2020-03-03 08:59:09 -05:00
|
|
|
await expect(this.HistoryManager.promises.initializeProject()).to.be
|
|
|
|
.fulfilled
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('injectUserDetails', function() {
|
2019-05-29 05:21:06 -04:00
|
|
|
beforeEach(function() {
|
|
|
|
this.user1 = {
|
|
|
|
_id: (this.user_id1 = '123456'),
|
|
|
|
first_name: 'Jane',
|
|
|
|
last_name: 'Doe',
|
2019-07-29 05:47:44 -04:00
|
|
|
email: 'jane@example.com',
|
|
|
|
overleaf: { id: 5011 }
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.user1_view = {
|
|
|
|
id: this.user_id1,
|
|
|
|
first_name: 'Jane',
|
|
|
|
last_name: 'Doe',
|
|
|
|
email: 'jane@example.com'
|
|
|
|
}
|
|
|
|
this.user2 = {
|
|
|
|
_id: (this.user_id2 = 'abcdef'),
|
|
|
|
first_name: 'John',
|
|
|
|
last_name: 'Doe',
|
|
|
|
email: 'john@example.com'
|
|
|
|
}
|
|
|
|
this.user2_view = {
|
|
|
|
id: this.user_id2,
|
|
|
|
first_name: 'John',
|
|
|
|
last_name: 'Doe',
|
|
|
|
email: 'john@example.com'
|
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
this.UserGetter.promises.getUsersByV1Ids.resolves([this.user1])
|
|
|
|
this.UserGetter.promises.getUsers.resolves([this.user1, this.user2])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a diff', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should turn user_ids into user objects', async function() {
|
|
|
|
const diff = await this.HistoryManager.promises.injectUserDetails({
|
|
|
|
diff: [
|
|
|
|
{
|
|
|
|
i: 'foo',
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id1]
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
i: 'bar',
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
|
|
|
|
expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should handle v1 user ids', async function() {
|
|
|
|
const diff = await this.HistoryManager.promises.injectUserDetails({
|
|
|
|
diff: [
|
|
|
|
{
|
|
|
|
i: 'foo',
|
|
|
|
meta: {
|
|
|
|
users: [5011]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: 'bar',
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id2]
|
2019-07-29 05:47:44 -04:00
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
|
|
|
|
expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
|
2019-07-29 05:47:44 -04:00
|
|
|
})
|
|
|
|
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should leave user objects', async function() {
|
|
|
|
const diff = await this.HistoryManager.promises.injectUserDetails({
|
|
|
|
diff: [
|
|
|
|
{
|
|
|
|
i: 'foo',
|
|
|
|
meta: {
|
|
|
|
users: [this.user1_view]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: 'bar',
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id2]
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
expect(diff.diff[0].meta.users).to.deep.equal([this.user1_view])
|
|
|
|
expect(diff.diff[1].meta.users).to.deep.equal([this.user2_view])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-06-21 09:46:09 -04:00
|
|
|
describe('with a list of updates', function() {
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should turn user_ids into user objects', async function() {
|
|
|
|
const updates = await this.HistoryManager.promises.injectUserDetails({
|
|
|
|
updates: [
|
|
|
|
{
|
|
|
|
fromV: 5,
|
|
|
|
toV: 8,
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id1]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fromV: 4,
|
|
|
|
toV: 5,
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id2]
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
expect(updates.updates[0].meta.users).to.deep.equal([this.user1_view])
|
|
|
|
expect(updates.updates[1].meta.users).to.deep.equal([this.user2_view])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2020-03-03 08:59:09 -05:00
|
|
|
it('should leave user objects', async function() {
|
|
|
|
const updates = await this.HistoryManager.promises.injectUserDetails({
|
|
|
|
updates: [
|
|
|
|
{
|
|
|
|
fromV: 5,
|
|
|
|
toV: 8,
|
|
|
|
meta: {
|
|
|
|
users: [this.user1_view]
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-03-03 08:59:09 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fromV: 4,
|
|
|
|
toV: 5,
|
|
|
|
meta: {
|
|
|
|
users: [this.user_id2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
expect(updates.updates[0].meta.users).to.deep.equal([this.user1_view])
|
|
|
|
expect(updates.updates[1].meta.users).to.deep.equal([this.user2_view])
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2020-03-04 04:38:40 -05:00
|
|
|
|
|
|
|
describe('deleteProject', function() {
|
|
|
|
it('should call the project-history service', async function() {
|
|
|
|
const projectId = new ObjectId()
|
|
|
|
await this.HistoryManager.promises.deleteProject(projectId)
|
|
|
|
expect(this.request.delete).to.have.been.calledWith(
|
|
|
|
`${this.projectHistoryUrl}/project/${projectId}`
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|