Proxy get doc requests to the docstore

This commit is contained in:
James Allen 2014-05-08 13:42:30 +01:00
parent c73b7fae69
commit 8315de58c7
6 changed files with 90 additions and 15 deletions

View file

@ -27,7 +27,23 @@ module.exports = DocstoreManager =
callback(null, docs)
else
error = new Error("docstore api responded with non-success code: #{res.statusCode}")
logger.error err: error, project_id: project_id, "error getting all docs in docstore"
logger.error err: error, project_id: project_id, "error getting all docs from docstore"
callback(error)
getDoc: (project_id, doc_id, callback = (error, lines, version, rev) ->) ->
logger.log project_id: project_id, doc_id: doc_id, "getting doc in docstore api"
url = "#{settings.apis.docstore.url}/project/#{project_id}/doc/#{doc_id}"
request.get {
url: url
json: true
}, (error, res, doc) ->
return callback(error) if error?
if 200 <= res.statusCode < 300
logger.log doc_id: doc_id, project_id: project_id, version: doc.version, rev: doc.rev, "got doc from docstore api"
callback(null, doc.lines, doc.version, doc.rev)
else
error = new Error("docstore api responded with non-success code: #{res.statusCode}")
logger.error err: error, project_id: project_id, doc_id: doc_id, "error getting doc from docstore"
callback(error)
updateDoc: (project_id, doc_id, lines, version, callback = (error, rev) ->) ->

View file

@ -1,25 +1,23 @@
ProjectLocator = require "../Project/ProjectLocator"
ProjectEntityHandler = require "../Project/ProjectEntityHandler"
Errors = require "../../errors"
logger = require("logger-sharelatex")
module.exports =
module.exports =
getDocument: (req, res, next = (error) ->) ->
project_id = req.params.Project_id
doc_id = req.params.doc_id
logger.log doc_id:doc_id, project_id:project_id, "receiving get document request from api (docupdater)"
ProjectLocator.findElement project_id: project_id, element_id: doc_id, type: "doc", (error, doc) ->
ProjectEntityHandler.getDoc project_id, doc_id, (error, lines, version, rev) ->
if error?
logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument"
return next(error)
res.type "json"
res.send JSON.stringify {
lines: doc.lines
lines: lines
version: version
}
req.session.destroy()
setDocument: (req, res, next = (error) ->) ->
project_id = req.params.Project_id
doc_id = req.params.doc_id

View file

@ -111,6 +111,9 @@ module.exports = ProjectEntityHandler =
logger.log sl_req_id: sl_req_id, project_id: project_id, "removing root doc"
Project.update {_id:project_id}, {$unset: {rootDoc_id: true}}, {}, callback
getDoc: (project_id, doc_id, callback = (error, lines, version, rev) ->) ->
DocstoreManager.getDoc project_id, doc_id, callback
addDoc: (project_or_id, folder_id, docName, docLines, sl_req_id, callback = (error, doc, folder_id) ->)=>
{callback, sl_req_id} = slReqIdHelper.getCallbackAndReqId(callback, sl_req_id)
Project.getProject project_or_id, "", (err, project) ->

View file

@ -92,6 +92,46 @@ describe "DocstoreManager", ->
}, "error updating doc in docstore")
.should.equal true
describe "getDoc", ->
beforeEach ->
@doc =
lines: @lines = ["mock", "doc", "lines"]
version: @version = 42
rev: @rev = 5
describe "with a successful response code", ->
beforeEach ->
@request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
@DocstoreManager.getDoc @project_id, @doc_id, @callback
it "should get the doc from the docstore api", ->
@request.get
.calledWith({
url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
json: true
})
.should.equal true
it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @version, @rev).should.equal true
describe "with a failed response code", ->
beforeEach ->
@request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
@DocstoreManager.getDoc @project_id, @doc_id, @callback
it "should call the callback with an error", ->
@callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
it "should log the error", ->
@logger.error
.calledWith({
err: new Error("docstore api responded with a non-success code: 500")
project_id: @project_id
doc_id: @doc_id
}, "error getting doc from docstore")
.should.equal true
describe "getAllDocs", ->
describe "with a successful response code", ->
beforeEach ->
@ -122,5 +162,5 @@ describe "DocstoreManager", ->
.calledWith({
err: new Error("docstore api responded with a non-success code: 500")
project_id: @project_id
}, "error getting all docs in docstore")
}, "error getting all docs from docstore")
.should.equal true

View file

@ -11,8 +11,7 @@ Errors = require "../../../../app/js/errors"
describe "DocumentController", ->
beforeEach ->
@DocumentController = SandboxedModule.require modulePath, requires:
"../Project/ProjectLocator": @ProjectLocator = {}
@DocumentController = SandboxedModule.require modulePath, requires:
"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
@res = new MockResponse()
@req = new MockRequest()
@ -20,32 +19,34 @@ describe "DocumentController", ->
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@doc_lines = ["one", "two", "three"]
@version = 42
@rev = 5
describe "getDocument", ->
beforeEach ->
@req.params =
Project_id: @project_id
doc_id: @doc_id
describe "when the document exists", ->
beforeEach ->
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, lines: @doc_lines)
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @version, @rev)
@DocumentController.getDocument(@req, @res, @next)
it "should get the document from Mongo", ->
@ProjectLocator.findElement
.calledWith(project_id: @project_id, element_id: @doc_id, type: "doc")
@ProjectEntityHandler.getDoc
.calledWith(@project_id, @doc_id)
.should.equal true
it "should return the document data to the client as JSON", ->
@res.type.should.equal "json"
@res.body.should.equal JSON.stringify
lines: @doc_lines
version: @version
describe "when the document doesn't exist", ->
beforeEach ->
@ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found"), null)
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, new Errors.NotFoundError("not found"), null)
@DocumentController.getDocument(@req, @res, @next)
it "should call next with the NotFoundError", ->

View file

@ -10,6 +10,7 @@ tk = require 'timekeeper'
describe 'ProjectEntityHandler', ->
project_id = '4eecb1c1bffa66588e0000a1'
doc_id = '4eecb1c1bffa66588e0000a2'
folder_id = "4eecaffcbffa66588e000008"
rootFolderId = "4eecaffcbffa66588e000007"
@ -279,6 +280,22 @@ describe 'ProjectEntityHandler', ->
done()
@ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
describe 'getDoc', ->
beforeEach ->
@lines = ["mock", "doc", "lines"]
@version = 42
@rev = 5
@DocstoreManager.getDoc = sinon.stub().callsArgWith(2, null, @lines, @version, @rev)
@ProjectEntityHandler.getDoc project_id, doc_id, @callback
it "should call the docstore", ->
@DocstoreManager.getDoc
.calledWith(project_id, doc_id)
.should.equal true
it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @version, @rev).should.equal true
describe 'addDoc', ->
beforeEach ->
@name = "some new doc"