mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Use docstore when creating a new doc
This commit is contained in:
parent
628fb65bc3
commit
c73b7fae69
4 changed files with 108 additions and 35 deletions
|
@ -29,3 +29,20 @@ module.exports = DocstoreManager =
|
|||
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"
|
||||
callback(error)
|
||||
|
||||
updateDoc: (project_id, doc_id, lines, version, callback = (error, rev) ->) ->
|
||||
logger.log project_id: project_id, doc_id: doc_id, version: version, "updating doc in docstore api"
|
||||
url = "#{settings.apis.docstore.url}/project/#{project_id}/doc/#{doc_id}"
|
||||
request.post {
|
||||
url: url
|
||||
json:
|
||||
lines: lines
|
||||
version: version
|
||||
}, (error, res, body) ->
|
||||
return callback(error) if error?
|
||||
if 200 <= res.statusCode < 300
|
||||
callback(null)
|
||||
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 updating doc in docstore"
|
||||
callback(error)
|
||||
|
|
|
@ -117,10 +117,20 @@ module.exports = ProjectEntityHandler =
|
|||
logger.log sl_req_id: sl_req_id, project: project._id, folder_id: folder_id, doc_name: docName, "adding doc"
|
||||
return callback(err) if err?
|
||||
confirmFolder project, folder_id, (folder_id)=>
|
||||
doc = new Doc name: docName, lines: docLines
|
||||
doc = new Doc name: docName
|
||||
Project.putElement project._id, folder_id, doc, "doc", (err, result)=>
|
||||
tpdsUpdateSender.addDoc {project_id:project._id, docLines:docLines, path:result.path.fileSystem, project_name:project.name, rev:doc.rev}, sl_req_id, ->
|
||||
callback(err, doc, folder_id)
|
||||
return callback(err) if err?
|
||||
DocstoreManager.updateDoc project._id.toString(), doc._id.toString(), docLines, 0, (err, rev) ->
|
||||
return callback(err) if err?
|
||||
tpdsUpdateSender.addDoc {
|
||||
project_id: project._id,
|
||||
docLines: docLines,
|
||||
path: result.path.fileSystem,
|
||||
project_name: project.name,
|
||||
rev: 0
|
||||
}, sl_req_id, (err) ->
|
||||
return callback(err) if err?
|
||||
callback(null, doc, folder_id)
|
||||
|
||||
addFile: (project_or_id, folder_id, fileName, path, sl_req_id, callback = (error, fileRef, folder_id) ->)->
|
||||
{callback, sl_req_id} = slReqIdHelper.getCallbackAndReqId(callback, sl_req_id)
|
||||
|
|
|
@ -52,6 +52,45 @@ describe "DocstoreManager", ->
|
|||
}, "error deleting doc in docstore")
|
||||
.should.equal true
|
||||
|
||||
describe "updateDoc", ->
|
||||
beforeEach ->
|
||||
@lines = ["mock", "doc", "lines"]
|
||||
@version = 42
|
||||
|
||||
describe "with a successful response code", ->
|
||||
beforeEach ->
|
||||
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, "")
|
||||
@DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback
|
||||
|
||||
it "should update the doc in the docstore api", ->
|
||||
@request.post
|
||||
.calledWith({
|
||||
url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
|
||||
json:
|
||||
lines: @lines
|
||||
version: @version
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
it "should call the callback without an error", ->
|
||||
@callback.calledWith(null).should.equal true
|
||||
|
||||
describe "with a failed response code", ->
|
||||
beforeEach ->
|
||||
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
|
||||
@DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @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 updating doc in docstore")
|
||||
.should.equal true
|
||||
|
||||
describe "getAllDocs", ->
|
||||
describe "with a successful response code", ->
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
chai = require('chai')
|
||||
assert = require('chai').assert
|
||||
should = chai.should()
|
||||
expect = chai.expect
|
||||
sinon = require 'sinon'
|
||||
modulePath = "../../../../app/js/Features/Project/ProjectEntityHandler"
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
|
@ -35,6 +36,7 @@ describe 'ProjectEntityHandler', ->
|
|||
@DocModel = class Doc
|
||||
constructor:(options)->
|
||||
{@name, @lines} = options
|
||||
@_id = "mock-id"
|
||||
@rev = 0
|
||||
@FileModel = class File
|
||||
constructor:(options)->
|
||||
|
@ -277,42 +279,47 @@ describe 'ProjectEntityHandler', ->
|
|||
done()
|
||||
@ProjectEntityHandler._removeElementFromMongoArray model, id, mongoPath, ->
|
||||
|
||||
describe 'adding doc', ->
|
||||
docName = "some new doc"
|
||||
docLines = ['1234','abc']
|
||||
describe 'addDoc', ->
|
||||
beforeEach ->
|
||||
@name = "some new doc"
|
||||
@lines = ['1234','abc']
|
||||
@path = "/path/to/doc"
|
||||
|
||||
it 'should call put element', (done)->
|
||||
@ProjectModel.putElement = (passedProject_id, passedFolder_id, passedDoc, passedType, callback)->
|
||||
passedProject_id.should.equal project_id
|
||||
passedFolder_id.should.equal folder_id
|
||||
passedDoc.name.should.equal docName
|
||||
passedDoc.lines[0].should.equal docLines[0]
|
||||
passedDoc.lines[1].should.equal docLines[1]
|
||||
passedType.should.equal 'doc'
|
||||
done()
|
||||
@ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)->
|
||||
@ProjectModel.putElement = sinon.stub().callsArgWith(4, null, {path:{fileSystem:@path}})
|
||||
@callback = sinon.stub()
|
||||
@tpdsUpdateSender.addDoc = sinon.stub().callsArg(2)
|
||||
@DocstoreManager.updateDoc = sinon.stub().callsArgWith(4, null, 0)
|
||||
|
||||
it 'should return doc and parent folder', (done)->
|
||||
@ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "", (err, doc, parentFolder)->
|
||||
parentFolder.should.equal folder_id
|
||||
doc.name.should.equal docName
|
||||
done()
|
||||
@ProjectEntityHandler.addDoc project_id, folder_id, @name, @lines, @callback
|
||||
|
||||
it 'should call third party data store', (done)->
|
||||
fileSystemPath = "/somehwere/#{docName}"
|
||||
@ProjectModel.putElement = (project_id, folder_id, doc, type, callback)-> callback(null, {path:{fileSystem:fileSystemPath}})
|
||||
# Created doc
|
||||
@doc = @ProjectModel.putElement.args[0][2]
|
||||
@doc.name.should.equal @name
|
||||
expect(@doc.lines).to.be.undefined
|
||||
|
||||
@tpdsUpdateSender.addDoc = (options)=>
|
||||
options.project_id.should.equal project_id
|
||||
options.docLines.should.equal docLines
|
||||
options.path.should.equal fileSystemPath
|
||||
options.project_name.should.equal @project.name
|
||||
options.rev.should.equal 0
|
||||
done()
|
||||
it 'should call put element', ->
|
||||
@ProjectModel.putElement
|
||||
.calledWith(project_id, folder_id, @doc)
|
||||
.should.equal true
|
||||
|
||||
@ProjectEntityHandler.addDoc project_id, folder_id, docName, docLines, "",->
|
||||
it 'should return doc and parent folder', ->
|
||||
@callback.calledWith(null, @doc, folder_id).should.equal true
|
||||
|
||||
it 'should call third party data store', ->
|
||||
@tpdsUpdateSender.addDoc
|
||||
.calledWith({
|
||||
project_id: project_id
|
||||
docLines: @lines
|
||||
path: @path
|
||||
project_name: @project.name
|
||||
rev: 0
|
||||
})
|
||||
.should.equal true
|
||||
|
||||
it "should send the doc lines to the doc store", ->
|
||||
@DocstoreManager.updateDoc
|
||||
.calledWith(project_id, @doc._id.toString(), @lines, 0)
|
||||
.should.equal true
|
||||
|
||||
describe 'adding file', ->
|
||||
fileName = "something.jpg"
|
||||
|
|
Loading…
Reference in a new issue