getDocFromWebTests.coffee 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/PersistenceManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. Errors = require "../../../../app/js/Errors"
  7. describe "PersistenceManager.getDocFromWeb", ->
  8. beforeEach ->
  9. @PersistenceManager = SandboxedModule.require modulePath, requires:
  10. "request": @request = sinon.stub()
  11. "settings-sharelatex": @Settings = {}
  12. "./Metrics": @Metrics =
  13. Timer: class Timer
  14. done: sinon.stub()
  15. @project_id = "project-id-123"
  16. @doc_id = "doc-id-123"
  17. @lines = ["one", "two", "three"]
  18. @callback = sinon.stub()
  19. @Settings.apis =
  20. web:
  21. url: @url = "www.example.com"
  22. user: @user = "sharelatex"
  23. pass: @pass = "password"
  24. describe "with a successful response from the web api", ->
  25. beforeEach ->
  26. @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines))
  27. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  28. it "should call the web api", ->
  29. @request
  30. .calledWith({
  31. url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
  32. method: "GET"
  33. headers:
  34. "accept": "application/json"
  35. auth:
  36. user: @user
  37. pass: @pass
  38. sendImmediately: true
  39. jar: false
  40. })
  41. .should.equal true
  42. it "should call the callback with the doc lines", ->
  43. @callback.calledWith(null, @lines).should.equal true
  44. it "should time the execution", ->
  45. @Metrics.Timer::done.called.should.equal true
  46. describe "when request returns an error", ->
  47. beforeEach ->
  48. @request.callsArgWith(1, @error = new Error("oops"), null, null)
  49. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  50. it "should return the error", ->
  51. @callback.calledWith(@error).should.equal true
  52. it "should time the execution", ->
  53. @Metrics.Timer::done.called.should.equal true
  54. describe "when the request returns 404", ->
  55. beforeEach ->
  56. @request.callsArgWith(1, null, {statusCode: 404}, "")
  57. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  58. it "should return a NotFoundError", ->
  59. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  60. it "should time the execution", ->
  61. @Metrics.Timer::done.called.should.equal true
  62. describe "when the request returns an error status code", ->
  63. beforeEach ->
  64. @request.callsArgWith(1, null, {statusCode: 500}, "")
  65. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  66. it "should return an error", ->
  67. @callback.calledWith(new Error("web api error")).should.equal true
  68. it "should time the execution", ->
  69. @Metrics.Timer::done.called.should.equal true