PersistenceManagerTests.coffee 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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", ->
  8. beforeEach ->
  9. @request = sinon.stub()
  10. @request.defaults = () => @request
  11. @PersistenceManager = SandboxedModule.require modulePath, requires:
  12. "requestretry": @request
  13. "settings-sharelatex": @Settings = {}
  14. "./Metrics": @Metrics =
  15. Timer: class Timer
  16. done: sinon.stub()
  17. "logger-sharelatex": @logger = {log: sinon.stub(), err: sinon.stub()}
  18. @project_id = "project-id-123"
  19. @doc_id = "doc-id-123"
  20. @lines = ["one", "two", "three"]
  21. @version = 42
  22. @callback = sinon.stub()
  23. @ranges = { comments: "mock", entries: "mock" }
  24. @pathname = '/a/b/c.tex'
  25. @Settings.apis =
  26. web:
  27. url: @url = "www.example.com"
  28. user: @user = "sharelatex"
  29. pass: @pass = "password"
  30. describe "getDoc", ->
  31. describe "with a successful response from the web api", ->
  32. beforeEach ->
  33. @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify({
  34. lines: @lines,
  35. version: @version,
  36. ranges: @ranges
  37. pathname: @pathname,
  38. }))
  39. @PersistenceManager.getDoc(@project_id, @doc_id, @callback)
  40. it "should call the web api", ->
  41. @request
  42. .calledWith({
  43. url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
  44. method: "GET"
  45. headers:
  46. "accept": "application/json"
  47. auth:
  48. user: @user
  49. pass: @pass
  50. sendImmediately: true
  51. jar: false
  52. timeout: 5000
  53. })
  54. .should.equal true
  55. it "should call the callback with the doc lines, version and ranges", ->
  56. @callback.calledWith(null, @lines, @version, @ranges, @pathname).should.equal true
  57. it "should time the execution", ->
  58. @Metrics.Timer::done.called.should.equal true
  59. describe "when request returns an error", ->
  60. beforeEach ->
  61. @request.callsArgWith(1, @error = new Error("oops"), null, null)
  62. @PersistenceManager.getDoc(@project_id, @doc_id, @callback)
  63. it "should return the error", ->
  64. @callback.calledWith(@error).should.equal true
  65. it "should time the execution", ->
  66. @Metrics.Timer::done.called.should.equal true
  67. describe "when the request returns 404", ->
  68. beforeEach ->
  69. @request.callsArgWith(1, null, {statusCode: 404}, "")
  70. @PersistenceManager.getDoc(@project_id, @doc_id, @callback)
  71. it "should return a NotFoundError", ->
  72. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  73. it "should time the execution", ->
  74. @Metrics.Timer::done.called.should.equal true
  75. describe "when the request returns an error status code", ->
  76. beforeEach ->
  77. @request.callsArgWith(1, null, {statusCode: 500}, "")
  78. @PersistenceManager.getDoc(@project_id, @doc_id, @callback)
  79. it "should return an error", ->
  80. @callback.calledWith(new Error("web api error")).should.equal true
  81. it "should time the execution", ->
  82. @Metrics.Timer::done.called.should.equal true
  83. describe "when request returns an doc without lines", ->
  84. beforeEach ->
  85. @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(version: @version))
  86. @PersistenceManager.getDoc(@project_id, @doc_id, @callback)
  87. it "should return and error", ->
  88. @callback.calledWith(new Error("web API response had no doc lines")).should.equal true
  89. describe "when request returns an doc without a version", ->
  90. beforeEach ->
  91. @request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines))
  92. @PersistenceManager.getDoc(@project_id, @doc_id, @callback)
  93. it "should return and error", ->
  94. @callback.calledWith(new Error("web API response had no valid doc version")).should.equal true
  95. describe "setDoc", ->
  96. describe "with a successful response from the web api", ->
  97. beforeEach ->
  98. @request.callsArgWith(1, null, {statusCode: 200})
  99. @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @callback)
  100. it "should call the web api", ->
  101. @request
  102. .calledWith({
  103. url: "#{@url}/project/#{@project_id}/doc/#{@doc_id}"
  104. json:
  105. lines: @lines
  106. version: @version
  107. ranges: @ranges
  108. method: "POST"
  109. auth:
  110. user: @user
  111. pass: @pass
  112. sendImmediately: true
  113. jar: false
  114. timeout: 5000
  115. })
  116. .should.equal true
  117. it "should call the callback without error", ->
  118. @callback.calledWith(null).should.equal true
  119. it "should time the execution", ->
  120. @Metrics.Timer::done.called.should.equal true
  121. describe "when request returns an error", ->
  122. beforeEach ->
  123. @request.callsArgWith(1, @error = new Error("oops"), null, null)
  124. @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @callback)
  125. it "should return the error", ->
  126. @callback.calledWith(@error).should.equal true
  127. it "should time the execution", ->
  128. @Metrics.Timer::done.called.should.equal true
  129. describe "when the request returns 404", ->
  130. beforeEach ->
  131. @request.callsArgWith(1, null, {statusCode: 404}, "")
  132. @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @callback)
  133. it "should return a NotFoundError", ->
  134. @callback.calledWith(new Errors.NotFoundError("not found")).should.equal true
  135. it "should time the execution", ->
  136. @Metrics.Timer::done.called.should.equal true
  137. describe "when the request returns an error status code", ->
  138. beforeEach ->
  139. @request.callsArgWith(1, null, {statusCode: 500}, "")
  140. @PersistenceManager.setDoc(@project_id, @doc_id, @lines, @version, @ranges, @callback)
  141. it "should return an error", ->
  142. @callback.calledWith(new Error("web api error")).should.equal true
  143. it "should time the execution", ->
  144. @Metrics.Timer::done.called.should.equal true