getDocFromWebTests.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
  16. @project_id = "project-id-123"
  17. @doc_id = "doc-id-123"
  18. @lines = ["one", "two", "three"]
  19. @callback = sinon.stub()
  20. @Settings.apis =
  21. web:
  22. url: @url = "www.example.com"
  23. user: @user = "sharelatex"
  24. pass: @pass = "password"
  25. describe "with a successful response from the web api", ->
  26. beforeEach ->
  27. @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines))
  28. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  29. it "should call the web api", ->
  30. @request
  31. .calledWith({
  32. url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
  33. method: "GET"
  34. headers:
  35. "accept": "application/json"
  36. auth:
  37. user: @user
  38. pass: @pass
  39. sendImmediately: true
  40. jar: false
  41. timeout: 5000
  42. })
  43. .should.equal true
  44. it "should call the callback with the doc lines", ->
  45. @callback.calledWith(null, @lines).should.equal true
  46. it "should time the execution", ->
  47. @Metrics.Timer::done.called.should.equal true
  48. describe "when request returns an error", ->
  49. beforeEach ->
  50. @request.callsArgWith(1, @error = new Error("oops"), null, null)
  51. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  52. it "should return the error", ->
  53. @callback.calledWith(@error).should.equal true
  54. it "should time the execution", ->
  55. @Metrics.Timer::done.called.should.equal true
  56. describe "when the request returns 404", ->
  57. beforeEach ->
  58. @request.callsArgWith(1, null, {statusCode: 404}, "")
  59. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  60. it "should return a NotFoundError", ->
  61. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  62. it "should time the execution", ->
  63. @Metrics.Timer::done.called.should.equal true
  64. describe "when the request returns an error status code", ->
  65. beforeEach ->
  66. @request.callsArgWith(1, null, {statusCode: 500}, "")
  67. @PersistenceManager.getDocFromWeb(@project_id, @doc_id, @callback)
  68. it "should return an error", ->
  69. @callback.calledWith(new Error("web api error")).should.equal true
  70. it "should time the execution", ->
  71. @Metrics.Timer::done.called.should.equal true