PersistenceManagerTests.coffee 6.6 KB

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