DocstoreHandler.coffee 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/DocstoreHandler.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocstoreHandler", ->
  7. beforeEach ->
  8. @requestDefaults = sinon.stub().returns(@request = sinon.stub())
  9. @DocstoreHandler = SandboxedModule.require modulePath, requires:
  10. "request" : defaults: @requestDefaults
  11. "settings-sharelatex": @settings =
  12. apis:
  13. docstore:
  14. url: "docstore.sharelatex.com"
  15. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  16. @requestDefaults.calledWith(jar: false).should.equal true
  17. @project_id = "project-id-123"
  18. @doc_id = "doc-id-123"
  19. @callback = sinon.stub()
  20. describe "getAllDocs", ->
  21. describe "with a successful response code", ->
  22. beforeEach ->
  23. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @docs = [{ _id: "mock-doc-id" }])
  24. @DocstoreHandler.getAllDocs @project_id, @callback
  25. it "should get all the project docs in the docstore api", ->
  26. @request.get
  27. .calledWith({
  28. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc"
  29. json: true
  30. })
  31. .should.equal true
  32. it "should call the callback with the docs", ->
  33. @callback.calledWith(null, @docs).should.equal true
  34. describe "with a failed response code", ->
  35. beforeEach ->
  36. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  37. @DocstoreHandler.getAllDocs @project_id, @callback
  38. it "should call the callback with an error", ->
  39. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  40. it "should log the error", ->
  41. @logger.error
  42. .calledWith({
  43. err: new Error("docstore api responded with a non-success code: 500")
  44. project_id: @project_id
  45. }, "error getting all docs from docstore")
  46. .should.equal true