PersistenceManagerTests.coffee 5.8 KB

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