DocstoreManagerTests.coffee 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. chai = require('chai')
  2. chai.should()
  3. sinon = require("sinon")
  4. modulePath = "../../../../app/js/Features/Docstore/DocstoreManager"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "DocstoreManager", ->
  7. beforeEach ->
  8. @requestDefaults = sinon.stub().returns(@request = sinon.stub())
  9. @DocstoreManager = SandboxedModule.require modulePath, requires:
  10. "request" : defaults: @requestDefaults
  11. "settings-sharelatex": @settings =
  12. apis:
  13. docstore:
  14. url: "docstore.sharelatex.com"
  15. "logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub(), err:->}
  16. @requestDefaults.calledWith(jar: false).should.equal true
  17. @project_id = "project-id-123"
  18. @doc_id = "doc-id-123"
  19. @callback = sinon.stub()
  20. describe "deleteDoc", ->
  21. describe "with a successful response code", ->
  22. beforeEach ->
  23. @request.del = sinon.stub().callsArgWith(1, null, statusCode: 204, "")
  24. @DocstoreManager.deleteDoc @project_id, @doc_id, @callback
  25. it "should delete the doc in the docstore api", ->
  26. @request.del
  27. .calledWith("#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}")
  28. .should.equal true
  29. it "should call the callback without an error", ->
  30. @callback.calledWith(null).should.equal true
  31. describe "with a failed response code", ->
  32. beforeEach ->
  33. @request.del = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  34. @DocstoreManager.deleteDoc @project_id, @doc_id, @callback
  35. it "should call the callback with an error", ->
  36. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  37. it "should log the error", ->
  38. @logger.error
  39. .calledWith({
  40. err: new Error("docstore api responded with a non-success code: 500")
  41. project_id: @project_id
  42. doc_id: @doc_id
  43. }, "error deleting doc in docstore")
  44. .should.equal true
  45. describe "updateDoc", ->
  46. beforeEach ->
  47. @lines = ["mock", "doc", "lines"]
  48. @rev = 5
  49. @version = 42
  50. @modified = true
  51. describe "with a successful response code", ->
  52. beforeEach ->
  53. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, { modified: @modified, rev: @rev })
  54. @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback
  55. it "should update the doc in the docstore api", ->
  56. @request.post
  57. .calledWith({
  58. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
  59. json:
  60. lines: @lines
  61. version: @version
  62. })
  63. .should.equal true
  64. it "should call the callback with the modified status and revision", ->
  65. @callback.calledWith(null, @modified, @rev).should.equal true
  66. describe "with a failed response code", ->
  67. beforeEach ->
  68. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  69. @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback
  70. it "should call the callback with an error", ->
  71. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  72. it "should log the error", ->
  73. @logger.error
  74. .calledWith({
  75. err: new Error("docstore api responded with a non-success code: 500")
  76. project_id: @project_id
  77. doc_id: @doc_id
  78. }, "error updating doc in docstore")
  79. .should.equal true
  80. describe "getDoc", ->
  81. beforeEach ->
  82. @doc =
  83. lines: @lines = ["mock", "doc", "lines"]
  84. rev: @rev = 5
  85. version: @version = 42
  86. describe "with a successful response code", ->
  87. beforeEach ->
  88. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
  89. @DocstoreManager.getDoc @project_id, @doc_id, @callback
  90. it "should get the doc from the docstore api", ->
  91. @request.get
  92. .calledWith({
  93. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
  94. json: true
  95. })
  96. .should.equal true
  97. it "should call the callback with the lines, version and rev", ->
  98. @callback.calledWith(null, @lines, @rev, @version).should.equal true
  99. describe "with a failed response code", ->
  100. beforeEach ->
  101. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  102. @DocstoreManager.getDoc @project_id, @doc_id, @callback
  103. it "should call the callback with an error", ->
  104. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  105. it "should log the error", ->
  106. @logger.error
  107. .calledWith({
  108. err: new Error("docstore api responded with a non-success code: 500")
  109. project_id: @project_id
  110. doc_id: @doc_id
  111. }, "error getting doc from docstore")
  112. .should.equal true
  113. describe "with include_deleted=true", ->
  114. beforeEach ->
  115. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
  116. @DocstoreManager.getDoc @project_id, @doc_id, include_deleted: true, @callback
  117. it "should get the doc from the docstore api (including deleted)", ->
  118. @request.get
  119. .calledWith({
  120. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}?include_deleted=true"
  121. json: true
  122. })
  123. .should.equal true
  124. it "should call the callback with the lines, version and rev", ->
  125. @callback.calledWith(null, @lines, @rev, @version).should.equal true
  126. describe "getAllDocs", ->
  127. describe "with a successful response code", ->
  128. beforeEach ->
  129. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @docs = [{ _id: "mock-doc-id" }])
  130. @DocstoreManager.getAllDocs @project_id, @callback
  131. it "should get all the project docs in the docstore api", ->
  132. @request.get
  133. .calledWith({
  134. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc"
  135. json: true
  136. })
  137. .should.equal true
  138. it "should call the callback with the docs", ->
  139. @callback.calledWith(null, @docs).should.equal true
  140. describe "with a failed response code", ->
  141. beforeEach ->
  142. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  143. @DocstoreManager.getAllDocs @project_id, @callback
  144. it "should call the callback with an error", ->
  145. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  146. it "should log the error", ->
  147. @logger.error
  148. .calledWith({
  149. err: new Error("docstore api responded with a non-success code: 500")
  150. project_id: @project_id
  151. }, "error getting all docs from docstore")
  152. .should.equal true
  153. describe "archiveProject", ->
  154. describe "with a successful response code", ->
  155. beforeEach ->
  156. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  157. @DocstoreManager.archiveProject @project_id, @callback
  158. it "should call the callback", ->
  159. @callback.called.should.equal true
  160. describe "with a failed response code", ->
  161. beforeEach ->
  162. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
  163. @DocstoreManager.archiveProject @project_id, @callback
  164. it "should call the callback with an error", ->
  165. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  166. describe "unarchiveProject", ->
  167. describe "with a successful response code", ->
  168. beforeEach ->
  169. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  170. @DocstoreManager.unarchiveProject @project_id, @callback
  171. it "should call the callback", ->
  172. @callback.called.should.equal true
  173. describe "with a failed response code", ->
  174. beforeEach ->
  175. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
  176. @DocstoreManager.unarchiveProject @project_id, @callback
  177. it "should call the callback with an error", ->
  178. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true