2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// 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
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const sinon = require('sinon')
|
2023-01-24 09:28:18 -05:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
|
|
|
|
|
|
|
const modulePath = '../../../../app/src/Features/History/HistoryController'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('HistoryController', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.user_id = 'user-id-123'
|
2021-07-28 04:51:20 -04:00
|
|
|
this.SessionManager = {
|
2021-04-27 03:52:58 -04:00
|
|
|
getLoggedInUserId: sinon.stub().returns(this.user_id),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2023-01-24 09:28:18 -05:00
|
|
|
this.Stream = {
|
|
|
|
pipeline: sinon.stub(),
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.HistoryController = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
request: (this.request = sinon.stub()),
|
2021-07-07 05:38:56 -04:00
|
|
|
'@overleaf/settings': (this.settings = {}),
|
2023-01-24 09:28:18 -05:00
|
|
|
stream: this.Stream,
|
2021-07-28 04:51:20 -04:00
|
|
|
'../Authentication/SessionManager': this.SessionManager,
|
2019-05-29 05:21:06 -04:00
|
|
|
'./HistoryManager': (this.HistoryManager = {}),
|
|
|
|
'../Project/ProjectDetailsHandler': (this.ProjectDetailsHandler = {}),
|
2022-01-10 05:23:05 -05:00
|
|
|
'../Project/ProjectEntityUpdateHandler':
|
|
|
|
(this.ProjectEntityUpdateHandler = {}),
|
2020-08-12 10:17:50 -04:00
|
|
|
'../User/UserGetter': (this.UserGetter = {}),
|
2021-04-27 03:52:58 -04:00
|
|
|
'./RestoreManager': (this.RestoreManager = {}),
|
2023-01-23 05:34:58 -05:00
|
|
|
'../../infrastructure/Features': (this.Features = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs('saas')
|
|
|
|
.returns(true)),
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
return (this.settings.apis = {
|
|
|
|
trackchanges: {
|
|
|
|
enabled: false,
|
2021-04-27 03:52:58 -04:00
|
|
|
url: 'http://trackchanges.example.com',
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
project_history: {
|
2021-04-27 03:52:58 -04:00
|
|
|
url: 'http://project_history.example.com',
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('selectHistoryApi', function () {
|
|
|
|
beforeEach(function () {
|
2020-11-30 08:02:12 -05:00
|
|
|
this.req = { url: '/mock/url', method: 'POST', params: {} }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.res = 'mock-res'
|
|
|
|
return (this.next = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for a project with project history', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectDetailsHandler.getDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, {
|
2021-04-27 03:52:58 -04:00
|
|
|
overleaf: { history: { id: 42, display: true } },
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
return this.HistoryController.selectHistoryApi(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the flag for project history to true', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.req.useProjectHistory.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for any other project ', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectDetailsHandler.getDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(1, null, {})
|
|
|
|
return this.HistoryController.selectHistoryApi(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not set the flag for project history to false', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.req.useProjectHistory.should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('proxyToHistoryApi', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req = { url: '/mock/url', method: 'POST' }
|
|
|
|
this.res = 'mock-res'
|
|
|
|
this.next = sinon.stub()
|
2023-01-24 09:28:18 -05:00
|
|
|
this.proxy = sinon.stub()
|
|
|
|
this.request.returns(this.proxy)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for a project with the project history flag', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.useProjectHistory = true
|
|
|
|
return this.HistoryController.proxyToHistoryApi(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the user id', function () {
|
2021-07-28 04:51:20 -04:00
|
|
|
return this.SessionManager.getLoggedInUserId
|
|
|
|
.calledWith(this.req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the project history api', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.request
|
|
|
|
.calledWith({
|
|
|
|
url: `${this.settings.apis.project_history.url}${this.req.url}`,
|
|
|
|
method: this.req.method,
|
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-User-Id': this.user_id,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should pipe the response to the client', function () {
|
2023-01-24 09:28:18 -05:00
|
|
|
expect(this.Stream.pipeline).to.have.been.calledWith(
|
|
|
|
this.proxy,
|
|
|
|
this.res
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for a project without the project history flag', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.useProjectHistory = false
|
|
|
|
return this.HistoryController.proxyToHistoryApi(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the user id', function () {
|
2021-07-28 04:51:20 -04:00
|
|
|
return this.SessionManager.getLoggedInUserId
|
|
|
|
.calledWith(this.req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the track changes api', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.request
|
|
|
|
.calledWith({
|
|
|
|
url: `${this.settings.apis.trackchanges.url}${this.req.url}`,
|
|
|
|
method: this.req.method,
|
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-User-Id': this.user_id,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should pipe the response to the client', function () {
|
2023-01-24 09:28:18 -05:00
|
|
|
expect(this.Stream.pipeline).to.have.been.calledWith(
|
2019-05-29 05:21:06 -04:00
|
|
|
this.proxy,
|
2023-01-24 09:28:18 -05:00
|
|
|
this.res
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('proxyToHistoryApiAndInjectUserDetails', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req = { url: '/mock/url', method: 'POST' }
|
|
|
|
this.res = { json: sinon.stub() }
|
|
|
|
this.next = sinon.stub()
|
|
|
|
this.request.yields(null, { statusCode: 200 }, (this.data = 'mock-data'))
|
|
|
|
return (this.HistoryManager.injectUserDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, (this.data_with_users = 'mock-injected-data')))
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for a project with the project history flag', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.useProjectHistory = true
|
|
|
|
return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the user id', function () {
|
2021-07-28 04:51:20 -04:00
|
|
|
return this.SessionManager.getLoggedInUserId
|
|
|
|
.calledWith(this.req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the project history api', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.request
|
|
|
|
.calledWith({
|
|
|
|
url: `${this.settings.apis.project_history.url}${this.req.url}`,
|
|
|
|
method: this.req.method,
|
|
|
|
json: true,
|
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-User-Id': this.user_id,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should inject the user data', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.HistoryManager.injectUserDetails
|
|
|
|
.calledWith(this.data)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the data with users to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.json.calledWith(this.data_with_users).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for a project without the project history flag', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req.useProjectHistory = false
|
|
|
|
return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the user id', function () {
|
2021-07-28 04:51:20 -04:00
|
|
|
return this.SessionManager.getLoggedInUserId
|
|
|
|
.calledWith(this.req.session)
|
2019-05-29 05:21:06 -04:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the track changes api', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.request
|
|
|
|
.calledWith({
|
|
|
|
url: `${this.settings.apis.trackchanges.url}${this.req.url}`,
|
|
|
|
method: this.req.method,
|
|
|
|
json: true,
|
|
|
|
headers: {
|
2021-04-27 03:52:58 -04:00
|
|
|
'X-User-Id': this.user_id,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should inject the user data', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.HistoryManager.injectUserDetails
|
|
|
|
.calledWith(this.data)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should return the data with users to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.json.calledWith(this.data_with_users).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('proxyToHistoryApiAndInjectUserDetails (with the history API failing)', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req = { url: '/mock/url', method: 'POST', useProjectHistory: true }
|
|
|
|
this.res = { json: sinon.stub() }
|
|
|
|
this.next = sinon.stub()
|
|
|
|
this.request.yields(null, { statusCode: 500 }, (this.data = 'mock-data'))
|
|
|
|
this.HistoryManager.injectUserDetails = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(null, (this.data_with_users = 'mock-injected-data'))
|
|
|
|
return this.HistoryController.proxyToHistoryApiAndInjectUserDetails(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not inject the user data', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.HistoryManager.injectUserDetails
|
|
|
|
.calledWith(this.data)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should not return the data with users to the client', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.json.calledWith(this.data_with_users).should.equal(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('resyncProjectHistory', function () {
|
|
|
|
describe('for a project without project-history enabled', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project_id = 'mock-project-id'
|
|
|
|
this.req = { params: { Project_id: this.project_id } }
|
2022-06-17 04:03:31 -04:00
|
|
|
this.res = { setTimeout: sinon.stub(), sendStatus: sinon.stub() }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.next = sinon.stub()
|
|
|
|
|
|
|
|
this.error = new Errors.ProjectHistoryDisabledError()
|
|
|
|
this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
|
|
|
|
.stub()
|
|
|
|
.yields(this.error)
|
|
|
|
|
|
|
|
return this.HistoryController.resyncProjectHistory(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('response with a 404', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.sendStatus.calledWith(404).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('for a project with project-history enabled', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.project_id = 'mock-project-id'
|
|
|
|
this.req = { params: { Project_id: this.project_id } }
|
2022-06-17 04:03:31 -04:00
|
|
|
this.res = { setTimeout: sinon.stub(), sendStatus: sinon.stub() }
|
2019-05-29 05:21:06 -04:00
|
|
|
this.next = sinon.stub()
|
|
|
|
|
|
|
|
this.ProjectEntityUpdateHandler.resyncProjectHistory = sinon
|
|
|
|
.stub()
|
|
|
|
.yields()
|
|
|
|
|
|
|
|
return this.HistoryController.resyncProjectHistory(
|
|
|
|
this.req,
|
|
|
|
this.res,
|
|
|
|
this.next
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-06-17 04:03:31 -04:00
|
|
|
it('sets an extended response timeout', function () {
|
|
|
|
this.res.setTimeout.should.have.been.calledWith(6 * 60 * 1000)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('resyncs the project', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.ProjectEntityUpdateHandler.resyncProjectHistory
|
|
|
|
.calledWith(this.project_id)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('responds with a 204', function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
return this.res.sendStatus.calledWith(204).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|