setDocInWebTests.coffee 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. @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, version: @version))
  27. @PersistenceManager.setDocInWeb(@project_id, @doc_id, @lines, @callback)
  28. it "should call the web api", ->
  29. @request
  30. .calledWith({
  31. url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
  32. body: JSON.stringify
  33. lines: @lines
  34. method: "POST"
  35. headers:
  36. "content-type": "application/json"
  37. auth:
  38. user: @user
  39. pass: @pass
  40. sendImmediately: true
  41. jar: false
  42. })
  43. .should.equal true
  44. it "should call the callback without error", ->
  45. @callback.calledWith(null).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.setDocInWeb(@project_id, @doc_id, @lines, @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.setDocInWeb(@project_id, @doc_id, @lines, @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.setDocInWeb(@project_id, @doc_id, @lines, @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