Add DocstoreManager

This commit is contained in:
James Allen 2014-04-29 17:22:23 +01:00
parent 0b93a719f0
commit f8dec76464
2 changed files with 70 additions and 0 deletions

View file

@ -0,0 +1,16 @@
request = require("request").defaults(jar: false)
logger = require "logger-sharelatex"
settings = require "settings-sharelatex"
module.exports = DocstoreManager =
deleteDoc: (project_id, doc_id, callback = (error) ->) ->
logger.log project_id: project_id, "deleting doc in docstore api"
url = "#{settings.apis.docstore.url}/project/#{project_id}/doc/#{doc_id}"
request.del url, (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 deleting doc in docstore"
callback(error)

View file

@ -0,0 +1,54 @@
chai = require('chai')
chai.should()
sinon = require("sinon")
modulePath = "../../../../app/js/Features/Docstore/DocstoreManager"
SandboxedModule = require('sandboxed-module')
describe "DocstoreManager", ->
beforeEach ->
@requestDefaults = sinon.stub().returns(@request = sinon.stub())
@DocstoreManager = SandboxedModule.require modulePath, requires:
"request" : defaults: @requestDefaults
"settings-sharelatex": @settings =
apis:
docstore:
url: "docstore.sharelatex.com"
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
@requestDefaults.calledWith(jar: false).should.equal true
@project_id = "project-id-123"
@doc_id = "doc-id-123"
@callback = sinon.stub()
describe "deleteDoc", ->
describe "with a successful response code", ->
beforeEach ->
@request.del = sinon.stub().callsArgWith(1, null, statusCode: 204, "")
@DocstoreManager.deleteDoc @project_id, @doc_id, @callback
it "should delete the doc in the docstore api", ->
@request.del
.calledWith("#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}")
.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.del = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
@DocstoreManager.deleteDoc @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 deleting doc in docstore")
.should.equal true