PersistenceManagerTests.coffee 6.2 KB

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