setDocInWebTests.coffee 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.setDocInWeb", ->
  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, version: @version))
  28. @PersistenceManager.setDocInWeb(@project_id, @doc_id, @lines, @callback)
  29. it "should call the web api", ->
  30. @request
  31. .calledWith({
  32. url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
  33. body: JSON.stringify
  34. lines: @lines
  35. method: "POST"
  36. headers:
  37. "content-type": "application/json"
  38. auth:
  39. user: @user
  40. pass: @pass
  41. sendImmediately: true
  42. jar: false
  43. timeout: 5000
  44. })
  45. .should.equal true
  46. it "should call the callback without error", ->
  47. @callback.calledWith(null).should.equal true
  48. it "should time the execution", ->
  49. @Metrics.Timer::done.called.should.equal true
  50. describe "when request returns an error", ->
  51. beforeEach ->
  52. @request.callsArgWith(1, @error = new Error("oops"), null, null)
  53. @PersistenceManager.setDocInWeb(@project_id, @doc_id, @lines, @callback)
  54. it "should return the error", ->
  55. @callback.calledWith(@error).should.equal true
  56. it "should time the execution", ->
  57. @Metrics.Timer::done.called.should.equal true
  58. describe "when the request returns 404", ->
  59. beforeEach ->
  60. @request.callsArgWith(1, null, {statusCode: 404}, "")
  61. @PersistenceManager.setDocInWeb(@project_id, @doc_id, @lines, @callback)
  62. it "should return a NotFoundError", ->
  63. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  64. it "should time the execution", ->
  65. @Metrics.Timer::done.called.should.equal true
  66. describe "when the request returns an error status code", ->
  67. beforeEach ->
  68. @request.callsArgWith(1, null, {statusCode: 500}, "")
  69. @PersistenceManager.setDocInWeb(@project_id, @doc_id, @lines, @callback)
  70. it "should return an error", ->
  71. @callback.calledWith(new Error("web api error")).should.equal true
  72. it "should time the execution", ->
  73. @Metrics.Timer::done.called.should.equal true