2014-03-05 11:31:52 -05:00
|
|
|
chai = require('chai')
|
|
|
|
chai.should()
|
|
|
|
sinon = require("sinon")
|
2016-12-09 10:43:08 -05:00
|
|
|
modulePath = "../../../../app/js/Features/History/HistoryController"
|
2014-03-05 11:31:52 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
|
2016-12-09 10:43:08 -05:00
|
|
|
describe "HistoryController", ->
|
2014-03-05 11:31:52 -05:00
|
|
|
beforeEach ->
|
2017-10-24 06:48:21 -04:00
|
|
|
@callback = sinon.stub()
|
2016-09-07 11:40:49 -04:00
|
|
|
@user_id = "user-id-123"
|
|
|
|
@AuthenticationController =
|
|
|
|
getLoggedInUserId: sinon.stub().returns(@user_id)
|
2016-12-09 10:43:08 -05:00
|
|
|
@HistoryController = SandboxedModule.require modulePath, requires:
|
2014-03-11 08:13:46 -04:00
|
|
|
"request" : @request = sinon.stub()
|
2014-03-05 11:31:52 -05:00
|
|
|
"settings-sharelatex": @settings = {}
|
|
|
|
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
|
2016-09-07 11:40:49 -04:00
|
|
|
"../Authentication/AuthenticationController": @AuthenticationController
|
2017-11-03 12:44:37 -04:00
|
|
|
"../Project/ProjectDetailsHandler": @ProjectDetailsHandler = {}
|
2017-10-24 06:48:21 -04:00
|
|
|
@settings.apis =
|
|
|
|
trackchanges:
|
|
|
|
enabled: false
|
|
|
|
url: "http://trackchanges.example.com"
|
|
|
|
project_history:
|
|
|
|
url: "http://project_history.example.com"
|
2014-03-05 11:31:52 -05:00
|
|
|
|
2017-11-03 12:44:37 -04:00
|
|
|
describe "selectHistoryApi", ->
|
|
|
|
beforeEach ->
|
|
|
|
@req = { url: "/mock/url", method: "POST" }
|
|
|
|
@res = "mock-res"
|
|
|
|
@next = sinon.stub()
|
|
|
|
|
|
|
|
describe "for a project with project history", ->
|
|
|
|
beforeEach ->
|
2017-12-15 11:11:16 -05:00
|
|
|
@ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {overleaf:{history:{id: 42, display:true}}})
|
2017-11-03 12:44:37 -04:00
|
|
|
@HistoryController.selectHistoryApi @req, @res, @next
|
|
|
|
|
|
|
|
it "should set the flag for project history to true", ->
|
|
|
|
@req.useProjectHistory.should.equal true
|
|
|
|
|
|
|
|
describe "for any other project ", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {})
|
|
|
|
@HistoryController.selectHistoryApi @req, @res, @next
|
|
|
|
|
|
|
|
it "should not set the flag for project history to false", ->
|
|
|
|
@req.useProjectHistory.should.equal false
|
|
|
|
|
|
|
|
|
2016-12-09 10:43:08 -05:00
|
|
|
describe "proxyToHistoryApi", ->
|
2014-03-05 11:31:52 -05:00
|
|
|
beforeEach ->
|
2014-03-11 08:13:46 -04:00
|
|
|
@req = { url: "/mock/url", method: "POST" }
|
2014-03-05 11:31:52 -05:00
|
|
|
@res = "mock-res"
|
|
|
|
@next = sinon.stub()
|
|
|
|
@proxy =
|
|
|
|
events: {}
|
|
|
|
pipe: sinon.stub()
|
|
|
|
on: (event, handler) -> @events[event] = handler
|
2014-03-11 08:13:46 -04:00
|
|
|
@request.returns @proxy
|
2014-03-05 11:31:52 -05:00
|
|
|
|
2017-12-15 11:11:16 -05:00
|
|
|
describe "for a project with the project history flag", ->
|
2017-11-03 12:44:37 -04:00
|
|
|
beforeEach ->
|
2017-12-15 11:11:16 -05:00
|
|
|
@req.useProjectHistory = true
|
|
|
|
@HistoryController.proxyToHistoryApi @req, @res, @next
|
2017-11-03 12:44:37 -04:00
|
|
|
|
2017-12-15 11:11:16 -05:00
|
|
|
it "should get the user id", ->
|
|
|
|
@AuthenticationController.getLoggedInUserId
|
|
|
|
.calledWith(@req)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the project history api", ->
|
|
|
|
@request
|
|
|
|
.calledWith({
|
|
|
|
url: "#{@settings.apis.project_history.url}#{@req.url}"
|
|
|
|
method: @req.method
|
|
|
|
headers:
|
|
|
|
"X-User-Id": @user_id
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should pipe the response to the client", ->
|
|
|
|
@proxy.pipe
|
|
|
|
.calledWith(@res)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "for a project without the project history flag", ->
|
2017-11-03 12:44:37 -04:00
|
|
|
beforeEach ->
|
2017-12-15 11:11:16 -05:00
|
|
|
@req.useProjectHistory = false
|
|
|
|
@HistoryController.proxyToHistoryApi @req, @res, @next
|
2017-11-03 12:44:37 -04:00
|
|
|
|
2017-12-15 11:11:16 -05:00
|
|
|
it "should get the user id", ->
|
|
|
|
@AuthenticationController.getLoggedInUserId
|
|
|
|
.calledWith(@req)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the track changes api", ->
|
|
|
|
@request
|
|
|
|
.calledWith({
|
|
|
|
url: "#{@settings.apis.trackchanges.url}#{@req.url}"
|
|
|
|
method: @req.method
|
|
|
|
headers:
|
|
|
|
"X-User-Id": @user_id
|
|
|
|
})
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should pipe the response to the client", ->
|
|
|
|
@proxy.pipe
|
|
|
|
.calledWith(@res)
|
|
|
|
.should.equal true
|
2014-03-05 11:31:52 -05:00
|
|
|
|
|
|
|
describe "with an error", ->
|
|
|
|
beforeEach ->
|
2017-10-11 06:18:34 -04:00
|
|
|
@HistoryController.proxyToHistoryApi @req, @res, @next
|
2014-03-05 11:31:52 -05:00
|
|
|
@proxy.events["error"].call(@proxy, @error = new Error("oops"))
|
|
|
|
|
|
|
|
it "should pass the error up the call chain", ->
|
|
|
|
@next.calledWith(@error).should.equal true
|
2017-10-24 06:48:21 -04:00
|
|
|
|
|
|
|
describe "initializeProject", ->
|
|
|
|
describe "with project history enabled", ->
|
|
|
|
beforeEach ->
|
2017-12-15 11:11:16 -05:00
|
|
|
@settings.apis.project_history.initializeHistoryForNewProjects = true
|
2017-10-24 06:48:21 -04:00
|
|
|
|
|
|
|
describe "project history returns a successful response", ->
|
|
|
|
beforeEach ->
|
|
|
|
@overleaf_id = 1234
|
|
|
|
@res = statusCode: 200
|
|
|
|
@body = JSON.stringify(project: id: @overleaf_id)
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, @res, @body)
|
|
|
|
|
|
|
|
@HistoryController.initializeProject @callback
|
|
|
|
|
|
|
|
it "should call the project history api", ->
|
|
|
|
@request.post.calledWith(
|
|
|
|
url: "#{@settings.apis.project_history.url}/project"
|
|
|
|
).should.equal true
|
|
|
|
|
|
|
|
it "should return the callback with the overleaf id", ->
|
|
|
|
@callback.calledWithExactly(null, { @overleaf_id }).should.equal true
|
|
|
|
|
|
|
|
describe "project history returns a response without the project id", ->
|
|
|
|
beforeEach ->
|
|
|
|
@res = statusCode: 200
|
|
|
|
@body = JSON.stringify(project: {})
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, @res, @body)
|
|
|
|
|
|
|
|
@HistoryController.initializeProject @callback
|
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(sinon.match.has("message", "project-history did not provide an id"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "project history returns a unsuccessful response", ->
|
|
|
|
beforeEach ->
|
|
|
|
@res = statusCode: 404
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, null, @res)
|
|
|
|
|
|
|
|
@HistoryController.initializeProject @callback
|
|
|
|
|
|
|
|
it "should return the callback with an error", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(sinon.match.has("message", "project-history returned a non-success status code: 404"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "project history errors", ->
|
|
|
|
beforeEach ->
|
|
|
|
@error = sinon.stub()
|
|
|
|
@request.post = sinon.stub().callsArgWith(1, @error)
|
|
|
|
|
|
|
|
@HistoryController.initializeProject @callback
|
|
|
|
|
|
|
|
it "should return the callback with the error", ->
|
|
|
|
@callback.calledWithExactly(@error).should.equal true
|
|
|
|
|
|
|
|
describe "with project history disabled", ->
|
|
|
|
beforeEach ->
|
2017-12-15 11:11:16 -05:00
|
|
|
@settings.apis.project_history.initializeHistoryForNewProjects = false
|
2017-10-24 06:48:21 -04:00
|
|
|
@HistoryController.initializeProject @callback
|
|
|
|
|
|
|
|
it "should return the callback", ->
|
|
|
|
@callback.calledWithExactly().should.equal true
|