DocstoreManagerTests.coffee 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/Docstore/DocstoreManager"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocstoreManager", ->
  7. beforeEach ->
  8. @requestDefaults = sinon.stub().returns(@request = sinon.stub())
  9. @DocstoreManager = 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()}
  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 "deleteDoc", ->
  21. describe "with a successful response code", ->
  22. beforeEach ->
  23. @request.del = sinon.stub().callsArgWith(1, null, statusCode: 204, "")
  24. @DocstoreManager.deleteDoc @project_id, @doc_id, @callback
  25. it "should delete the doc in the docstore api", ->
  26. @request.del
  27. .calledWith("#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}")
  28. .should.equal true
  29. it "should call the callback without an error", ->
  30. @callback.calledWith(null).should.equal true
  31. describe "with a failed response code", ->
  32. beforeEach ->
  33. @request.del = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  34. @DocstoreManager.deleteDoc @project_id, @doc_id, @callback
  35. it "should call the callback with an error", ->
  36. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  37. it "should log the error", ->
  38. @logger.error
  39. .calledWith({
  40. err: new Error("docstore api responded with a non-success code: 500")
  41. project_id: @project_id
  42. doc_id: @doc_id
  43. }, "error deleting doc in docstore")
  44. .should.equal true
  45. describe "getAllDocs", ->
  46. describe "with a successful response code", ->
  47. beforeEach ->
  48. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @docs = [{ _id: "mock-doc-id" }])
  49. @DocstoreManager.getAllDocs @project_id, @callback
  50. it "should get all the project docs in the docstore api", ->
  51. @request.get
  52. .calledWith({
  53. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc"
  54. json: true
  55. })
  56. .should.equal true
  57. it "should call the callback with the docs", ->
  58. @callback.calledWith(null, @docs).should.equal true
  59. describe "with a failed response code", ->
  60. beforeEach ->
  61. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  62. @DocstoreManager.getAllDocs @project_id, @callback
  63. it "should call the callback with an error", ->
  64. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  65. it "should log the error", ->
  66. @logger.error
  67. .calledWith({
  68. err: new Error("docstore api responded with a non-success code: 500")
  69. project_id: @project_id
  70. }, "error getting all docs in docstore")
  71. .should.equal true