mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Take user id in from request header and pass to doc updater
This commit is contained in:
parent
3d1d962501
commit
064bdc3eea
9 changed files with 30 additions and 18 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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", ->
|
||||
|
|
|
@ -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", ->
|
||||
|
|
Loading…
Reference in a new issue