| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- chai = require('chai')
- chai.should()
- sinon = require("sinon")
- Errors = require "../../../../app/js/Features/Errors/Errors"
- modulePath = "../../../../app/js/Features/History/HistoryController"
- SandboxedModule = require('sandboxed-module')
- describe "HistoryController", ->
- beforeEach ->
- @callback = sinon.stub()
- @user_id = "user-id-123"
- @AuthenticationController =
- getLoggedInUserId: sinon.stub().returns(@user_id)
- @HistoryController = SandboxedModule.require modulePath, requires:
- "request" : @request = sinon.stub()
- "settings-sharelatex": @settings = {}
- "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
- "../Authentication/AuthenticationController": @AuthenticationController
- "../Errors/Errors": Errors
- "./HistoryManager": @HistoryManager = {}
- "../Project/ProjectDetailsHandler": @ProjectDetailsHandler = {}
- "../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {}
- "./RestoreManager": @RestoreManager = {}
- @settings.apis =
- trackchanges:
- enabled: false
- url: "http://trackchanges.example.com"
- project_history:
- url: "http://project_history.example.com"
- describe "selectHistoryApi", ->
- beforeEach ->
- @req = { url: "/mock/url", method: "POST" }
- @res = "mock-res"
- @next = sinon.stub()
- describe "for a project with project history", ->
- beforeEach ->
- @ProjectDetailsHandler.getDetails = sinon.stub().callsArgWith(1, null, {overleaf:{history:{id: 42, display:true}}})
- @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
- describe "proxyToHistoryApi", ->
- beforeEach ->
- @req = { url: "/mock/url", method: "POST" }
- @res = "mock-res"
- @next = sinon.stub()
- @proxy =
- events: {}
- pipe: sinon.stub()
- on: (event, handler) -> @events[event] = handler
- @request.returns @proxy
- describe "for a project with the project history flag", ->
- beforeEach ->
- @req.useProjectHistory = true
- @HistoryController.proxyToHistoryApi @req, @res, @next
- 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", ->
- beforeEach ->
- @req.useProjectHistory = false
- @HistoryController.proxyToHistoryApi @req, @res, @next
- 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
- describe "with an error", ->
- beforeEach ->
- @HistoryController.proxyToHistoryApi @req, @res, @next
- @proxy.events["error"].call(@proxy, @error = new Error("oops"))
- it "should pass the error up the call chain", ->
- @next.calledWith(@error).should.equal true
- describe "proxyToHistoryApiAndInjectUserDetails", ->
- beforeEach ->
- @req = { url: "/mock/url", method: "POST" }
- @res =
- json: sinon.stub()
- @next = sinon.stub()
- @request.yields(null, {statusCode: 200}, @data = "mock-data")
- @HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
- describe "for a project with the project history flag", ->
- beforeEach ->
- @req.useProjectHistory = true
- @HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
- 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
- json: true
- headers:
- "X-User-Id": @user_id
- })
- .should.equal true
- it "should inject the user data", ->
- @HistoryManager.injectUserDetails
- .calledWith(@data)
- .should.equal true
- it "should return the data with users to the client", ->
- @res.json.calledWith(@data_with_users).should.equal true
- describe "for a project without the project history flag", ->
- beforeEach ->
- @req.useProjectHistory = false
- @HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
- 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
- json: true
- headers:
- "X-User-Id": @user_id
- })
- .should.equal true
- it "should inject the user data", ->
- @HistoryManager.injectUserDetails
- .calledWith(@data)
- .should.equal true
- it "should return the data with users to the client", ->
- @res.json.calledWith(@data_with_users).should.equal true
- describe "proxyToHistoryApiAndInjectUserDetails (with the history API failing)", ->
- beforeEach ->
- @req = { url: "/mock/url", method: "POST", useProjectHistory: true }
- @res = { json: sinon.stub() }
- @next = sinon.stub()
- @request.yields(null, {statusCode: 500}, @data = "mock-data")
- @HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
- @HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
- it "should not inject the user data", ->
- @HistoryManager.injectUserDetails
- .calledWith(@data)
- .should.equal false
- it "should not return the data with users to the client", ->
- @res.json.calledWith(@data_with_users).should.equal false
- describe "resyncProjectHistory", ->
- describe "for a project without project-history enabled", ->
- beforeEach ->
- @project_id = 'mock-project-id'
- @req = params: Project_id: @project_id
- @res = sendStatus: sinon.stub()
- @next = sinon.stub()
- @error = new Errors.ProjectHistoryDisabledError()
- @ProjectEntityUpdateHandler.resyncProjectHistory = sinon.stub().yields(@error)
- @HistoryController.resyncProjectHistory @req, @res, @next
- it "response with a 404", ->
- @res.sendStatus
- .calledWith(404)
- .should.equal true
- describe "for a project with project-history enabled", ->
- beforeEach ->
- @project_id = 'mock-project-id'
- @req = params: Project_id: @project_id
- @res = sendStatus: sinon.stub()
- @next = sinon.stub()
- @ProjectEntityUpdateHandler.resyncProjectHistory = sinon.stub().yields()
- @HistoryController.resyncProjectHistory @req, @res, @next
- it "resyncs the project", ->
- @ProjectEntityUpdateHandler.resyncProjectHistory
- .calledWith(@project_id)
- .should.equal true
- it "responds with a 204", ->
- @res.sendStatus
- .calledWith(204)
- .should.equal true
|