PersistenceManagerTests.coffee 6.3 KB

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