Take user id in from request header and pass to doc updater

This commit is contained in:
James Allen 2014-03-11 13:01:07 +00:00
parent 3d1d962501
commit 064bdc3eea
9 changed files with 30 additions and 18 deletions

View file

@ -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" logger.error err: error, project_id:project_id, doc_id:doc_id, url: url, "error accessing doc updater"
callback error 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}" 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" logger.log project_id:project_id, doc_id: doc_id, "setting doc in document updater"
request.post { request.post {
url: url url: url
json: json:
lines: content.split("\n") lines: content.split("\n")
source: "restore"
user_id: user_id
}, (error, res, body)-> }, (error, res, body)->
if error? if error?
return callback(error) return callback(error)

View file

@ -49,7 +49,8 @@ module.exports = HttpController =
restore: (req, res, next = (error) ->) -> restore: (req, res, next = (error) ->) ->
{doc_id, project_id, version} = req.params {doc_id, project_id, version} = req.params
user_id = req.headers["x-user-id"]
version = parseInt(version, 10) 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? return next(error) if error?
res.send 204 res.send 204

View file

@ -3,10 +3,10 @@ DiffManager = require "./DiffManager"
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
module.exports = RestoreManager = module.exports = RestoreManager =
restoreToBeforeVersion: (project_id, doc_id, version, callback = (error) ->) -> restoreToBeforeVersion: (project_id, doc_id, version, user_id, callback = (error) ->) ->
logger.log project_id: project_id, doc_id: doc_id, version: version, "restoring document" 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) -> DiffManager.getDocumentBeforeVersion project_id, doc_id, version, (error, content) ->
return callback(error) if error? 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? return callback(error) if error?
callback() callback()

View file

@ -54,7 +54,7 @@ describe "Restoring a version", ->
TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) => TrackChangesClient.pushRawUpdates @doc_id, @updates, (error) =>
throw error if 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? throw error if error?
done() done()
@ -63,5 +63,5 @@ describe "Restoring a version", ->
it "should set the doc in the doc updater", -> it "should set the doc in the doc updater", ->
MockDocUpdaterApi.setDoc MockDocUpdaterApi.setDoc
.calledWith(@project_id, @doc_id, @restored_lines) .calledWith(@project_id, @doc_id, @restored_lines, @user_id)
.should.equal true .should.equal true

View file

@ -7,7 +7,7 @@ module.exports = MockDocUpdaterApi =
getDoc: (project_id, doc_id, callback = (error) ->) -> getDoc: (project_id, doc_id, callback = (error) ->) ->
callback null, @docs[doc_id] 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] ||= {}
@docs[doc_id].lines = lines @docs[doc_id].lines = lines
callback() callback()
@ -23,7 +23,7 @@ module.exports = MockDocUpdaterApi =
res.send JSON.stringify doc res.send JSON.stringify doc
app.post "/project/:project_id/doc/:doc_id", express.bodyParser(), (req, res, next) => 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? if error?
res.send 500 res.send 500
else else

View file

@ -30,9 +30,11 @@ module.exports = TrackChangesClient =
response.statusCode.should.equal 200 response.statusCode.should.equal 200
callback null, JSON.parse(body) 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 { request.post {
url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/version/#{version}/restore" url: "http://localhost:3015/project/#{project_id}/doc/#{doc_id}/version/#{version}/restore"
headers:
"X-User-Id": user_id
}, (error, response, body) => }, (error, response, body) =>
response.statusCode.should.equal 204 response.statusCode.should.equal 204
callback null callback null

View file

@ -54,11 +54,12 @@ describe "DocumentUpdaterManager", ->
describe "setDocument", -> describe "setDocument", ->
beforeEach -> beforeEach ->
@content = "mock content" @content = "mock content"
@user_id = "user-id-123"
describe "successfully", -> describe "successfully", ->
beforeEach -> beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}) @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', -> it 'should set the document in the document updater', ->
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}" url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
@ -67,6 +68,8 @@ describe "DocumentUpdaterManager", ->
url: url url: url
json: json:
lines: @content.split("\n") lines: @content.split("\n")
source: "restore"
user_id: @user_id
}).should.equal true }).should.equal true
it "should call the callback", -> it "should call the callback", ->
@ -75,7 +78,7 @@ describe "DocumentUpdaterManager", ->
describe "when the document updater API returns an error", -> describe "when the document updater API returns an error", ->
beforeEach -> beforeEach ->
@request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null) @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", -> it "should return an error to the callback", ->
@callback.calledWith(@error).should.equal true @callback.calledWith(@error).should.equal true
@ -83,7 +86,7 @@ describe "DocumentUpdaterManager", ->
describe "when the document updater returns a failure error code", -> describe "when the document updater returns a failure error code", ->
beforeEach -> beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "") @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", -> it "should return the callback with an error", ->
@callback @callback

View file

@ -15,6 +15,7 @@ describe "HttpController", ->
@doc_id = "doc-id-123" @doc_id = "doc-id-123"
@project_id = "project-id-123" @project_id = "project-id-123"
@next = sinon.stub() @next = sinon.stub()
@user_id = "mock-user-123"
describe "flushUpdatesWithLock", -> describe "flushUpdatesWithLock", ->
beforeEach -> beforeEach ->
@ -102,15 +103,17 @@ describe "HttpController", ->
doc_id: @doc_id doc_id: @doc_id
project_id: @project_id project_id: @project_id
version: @version version: @version
headers:
"x-user-id": @user_id
@res = @res =
send: sinon.stub() send: sinon.stub()
@RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(3) @RestoreManager.restoreToBeforeVersion = sinon.stub().callsArg(4)
@HttpController.restore @req, @res, @next @HttpController.restore @req, @res, @next
it "should restore the document", -> it "should restore the document", ->
@RestoreManager.restoreToBeforeVersion @RestoreManager.restoreToBeforeVersion
.calledWith(@project_id, @doc_id, parseInt(@version, 10)) .calledWith(@project_id, @doc_id, parseInt(@version, 10), @user_id)
.should.equal true .should.equal true
it "should return a success code", -> it "should return a success code", ->

View file

@ -14,14 +14,15 @@ describe "RestoreManager", ->
@callback = sinon.stub() @callback = sinon.stub()
@project_id = "mock-project-id" @project_id = "mock-project-id"
@doc_id = "mock-doc-id" @doc_id = "mock-doc-id"
@user_id = "mock-user-id"
@version = 42 @version = 42
describe "restoreToBeforeVersion", -> describe "restoreToBeforeVersion", ->
beforeEach -> beforeEach ->
@content = "mock content" @content = "mock content"
@DocumentUpdaterManager.setDocument = sinon.stub().callsArg(3) @DocumentUpdaterManager.setDocument = sinon.stub().callsArg(4)
@DiffManager.getDocumentBeforeVersion = sinon.stub().callsArgWith(3, null, @content) @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", -> it "should get the content before the requested version", ->
@DiffManager.getDocumentBeforeVersion @DiffManager.getDocumentBeforeVersion
@ -30,7 +31,7 @@ describe "RestoreManager", ->
it "should set the document in the document updater", -> it "should set the document in the document updater", ->
@DocumentUpdaterManager.setDocument @DocumentUpdaterManager.setDocument
.calledWith(@project_id, @doc_id, @content) .calledWith(@project_id, @doc_id, @content, @user_id)
.should.equal true .should.equal true
it "should call the callback", -> it "should call the callback", ->