diff --git a/services/track-changes/app/coffee/DocumentUpdaterManager.coffee b/services/track-changes/app/coffee/DocumentUpdaterManager.coffee index 6dea2b3f14..edc2f11c68 100644 --- a/services/track-changes/app/coffee/DocumentUpdaterManager.coffee +++ b/services/track-changes/app/coffee/DocumentUpdaterManager.coffee @@ -20,13 +20,15 @@ module.exports = DocumentUpdaterManager = logger.error err: error, project_id:project_id, doc_id:doc_id, url: url, "error accessing doc updater" callback error - setDocument: (project_id, doc_id, content, callback = (error) ->) -> + setDocument: (project_id, doc_id, content, user_id, callback = (error) ->) -> url = "#{Settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}" logger.log project_id:project_id, doc_id: doc_id, "setting doc in document updater" request.post { url: url json: lines: content.split("\n") + source: "restore" + user_id: user_id }, (error, res, body)-> if error? return callback(error) diff --git a/services/track-changes/app/coffee/HttpController.coffee b/services/track-changes/app/coffee/HttpController.coffee index f3878ab7cd..70b6ca2557 100644 --- a/services/track-changes/app/coffee/HttpController.coffee +++ b/services/track-changes/app/coffee/HttpController.coffee @@ -49,7 +49,8 @@ module.exports = HttpController = restore: (req, res, next = (error) ->) -> {doc_id, project_id, version} = req.params + user_id = req.headers["x-user-id"] version = parseInt(version, 10) - RestoreManager.restoreToBeforeVersion project_id, doc_id, version, (error) -> + RestoreManager.restoreToBeforeVersion project_id, doc_id, version, user_id, (error) -> return next(error) if error? res.send 204 diff --git a/services/track-changes/app/coffee/RestoreManager.coffee b/services/track-changes/app/coffee/RestoreManager.coffee index 6c5ccef6fb..cfabca2fb7 100644 --- a/services/track-changes/app/coffee/RestoreManager.coffee +++ b/services/track-changes/app/coffee/RestoreManager.coffee @@ -3,10 +3,10 @@ DiffManager = require "./DiffManager" logger = require "logger-sharelatex" module.exports = RestoreManager = - restoreToBeforeVersion: (project_id, doc_id, version, callback = (error) ->) -> - logger.log project_id: project_id, doc_id: doc_id, version: version, "restoring document" + restoreToBeforeVersion: (project_id, doc_id, version, user_id, callback = (error) ->) -> + logger.log project_id: project_id, doc_id: doc_id, version: version, user_id: user_id, "restoring document" DiffManager.getDocumentBeforeVersion project_id, doc_id, version, (error, content) -> return callback(error) if error? - DocumentUpdaterManager.setDocument project_id, doc_id, content, (error) -> + DocumentUpdaterManager.setDocument project_id, doc_id, content, user_id, (error) -> return callback(error) if error? callback() diff --git a/services/track-changes/test/acceptance/coffee/RestoringVersions.coffee b/services/track-changes/test/acceptance/coffee/RestoringVersions.coffee index 21666bab3d..03f4c8b8f7 100644 --- a/services/track-changes/test/acceptance/coffee/RestoringVersions.coffee +++ b/services/track-changes/test/acceptance/coffee/RestoringVersions.coffee @@ -54,7 +54,7 @@ describe "Restoring a version", -> TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) => throw error if error? - TrackChangesClient.restoreDoc @project_id, @doc_id, @beforeVersion, (error) => + TrackChangesClient.restoreDoc @project_id, @doc_id, @beforeVersion, @user_id, (error) => throw error if error? done() @@ -63,5 +63,5 @@ describe "Restoring a version", -> it "should set the doc in the doc updater", -> MockDocUpdaterApi.setDoc - .calledWith(@project_id, @doc_id, @restored_lines) + .calledWith(@project_id, @doc_id, @restored_lines, @user_id) .should.equal true diff --git a/services/track-changes/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee b/services/track-changes/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee index fcddace406..97b4d93772 100644 --- a/services/track-changes/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee +++ b/services/track-changes/test/acceptance/coffee/helpers/MockDocUpdaterApi.coffee @@ -7,7 +7,7 @@ module.exports = MockDocUpdaterApi = getDoc: (project_id, doc_id, callback = (error) ->) -> callback null, @docs[doc_id] - setDoc: (project_id, doc_id, lines, callback = (error) ->) -> + setDoc: (project_id, doc_id, lines, user_id, callback = (error) ->) -> @docs[doc_id] ||= {} @docs[doc_id].lines = lines callback() @@ -23,7 +23,7 @@ module.exports = MockDocUpdaterApi = res.send JSON.stringify doc app.post "/project/:project_id/doc/:doc_id", express.bodyParser(), (req, res, next) => - @setDoc req.params.project_id, req.params.doc_id, req.body.lines, (errr, doc) -> + @setDoc req.params.project_id, req.params.doc_id, req.body.lines, req.body.user_id, (errr, doc) -> if error? res.send 500 else diff --git a/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee b/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee index ab85b67171..6c65941570 100644 --- a/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee +++ b/services/track-changes/test/acceptance/coffee/helpers/TrackChangesClient.coffee @@ -30,9 +30,11 @@ module.exports = TrackChangesClient = response.statusCode.should.equal 200 callback null, JSON.parse(body) - restoreDoc: (project_id, doc_id, version, callback = (error) ->) -> + restoreDoc: (project_id, doc_id, version, user_id, callback = (error) ->) -> request.post { url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/version/#{version}/restore" + headers: + "X-User-Id": user_id }, (error, response, body) => response.statusCode.should.equal 204 callback null \ No newline at end of file diff --git a/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee b/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee index 25c61f6279..8c460f4757 100644 --- a/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee @@ -54,11 +54,12 @@ describe "DocumentUpdaterManager", -> describe "setDocument", -> beforeEach -> @content = "mock content" + @user_id = "user-id-123" describe "successfully", -> beforeEach -> @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}) - @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @callback + @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @user_id, @callback it 'should set the document in the document updater', -> url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}" @@ -67,6 +68,8 @@ describe "DocumentUpdaterManager", -> url: url json: lines: @content.split("\n") + source: "restore" + user_id: @user_id }).should.equal true it "should call the callback", -> @@ -75,7 +78,7 @@ describe "DocumentUpdaterManager", -> describe "when the document updater API returns an error", -> beforeEach -> @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null) - @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @callback + @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @user_id, @callback it "should return an error to the callback", -> @callback.calledWith(@error).should.equal true @@ -83,7 +86,7 @@ describe "DocumentUpdaterManager", -> describe "when the document updater returns a failure error code", -> beforeEach -> @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "") - @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @callback + @DocumentUpdaterManager.setDocument @project_id, @doc_id, @content, @user_id, @callback it "should return the callback with an error", -> @callback diff --git a/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee b/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee index 791bbd1d04..da84db026f 100644 --- a/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee +++ b/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee @@ -15,6 +15,7 @@ describe "HttpController", -> @doc_id = "doc-id-123" @project_id = "project-id-123" @next = sinon.stub() + @user_id = "mock-user-123" describe "flushUpdatesWithLock", -> beforeEach -> @@ -102,15 +103,17 @@ describe "HttpController", -> doc_id: @doc_id project_id: @project_id version: @version + headers: + "x-user-id": @user_id @res = send: sinon.stub() - @RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(3) + @RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4) @HttpController.restore @req, @res, @next it "should restore the document", -> @RestoreManager.restoreToBeforeVersion - .calledWith(@project_id, @doc_id, parseInt(@version, 10)) + .calledWith(@project_id, @doc_id, parseInt(@version, 10), @user_id) .should.equal true it "should return a success code", -> diff --git a/services/track-changes/test/unit/coffee/RestoreManager/RestoreManagerTests.coffee b/services/track-changes/test/unit/coffee/RestoreManager/RestoreManagerTests.coffee index 36088d4405..11941dab91 100644 --- a/services/track-changes/test/unit/coffee/RestoreManager/RestoreManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/RestoreManager/RestoreManagerTests.coffee @@ -14,14 +14,15 @@ describe "RestoreManager", -> @callback = sinon.stub() @project_id = "mock-project-id" @doc_id = "mock-doc-id" + @user_id = "mock-user-id" @version = 42 describe "restoreToBeforeVersion", -> beforeEach -> @content = "mock content" - @DocumentUpdaterManager.setDocument = sinon.stub().callsArg(3) + @DocumentUpdaterManager.setDocument = sinon.stub().callsArg(4) @DiffManager.getDocumentBeforeVersion = sinon.stub().callsArgWith(3, null, @content) - @RestoreManager.restoreToBeforeVersion @project_id, @doc_id, @version, @callback + @RestoreManager.restoreToBeforeVersion @project_id, @doc_id, @version, @user_id, @callback it "should get the content before the requested version", -> @DiffManager.getDocumentBeforeVersion @@ -30,7 +31,7 @@ describe "RestoreManager", -> it "should set the document in the document updater", -> @DocumentUpdaterManager.setDocument - .calledWith(@project_id, @doc_id, @content) + .calledWith(@project_id, @doc_id, @content, @user_id) .should.equal true it "should call the callback", ->