DocstoreManagerTests.coffee 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. @ranges = { "mock": "ranges" }
  51. @modified = true
  52. describe "with a successful response code", ->
  53. beforeEach ->
  54. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, { modified: @modified, rev: @rev })
  55. @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @ranges, @callback
  56. it "should update the doc in the docstore api", ->
  57. @request.post
  58. .calledWith({
  59. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
  60. json:
  61. lines: @lines
  62. version: @version
  63. ranges: @ranges
  64. })
  65. .should.equal true
  66. it "should call the callback with the modified status and revision", ->
  67. @callback.calledWith(null, @modified, @rev).should.equal true
  68. describe "with a failed response code", ->
  69. beforeEach ->
  70. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  71. @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @ranges, @callback
  72. it "should call the callback with an error", ->
  73. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  74. it "should log the error", ->
  75. @logger.error
  76. .calledWith({
  77. err: new Error("docstore api responded with a non-success code: 500")
  78. project_id: @project_id
  79. doc_id: @doc_id
  80. }, "error updating doc in docstore")
  81. .should.equal true
  82. describe "getDoc", ->
  83. beforeEach ->
  84. @doc =
  85. lines: @lines = ["mock", "doc", "lines"]
  86. rev: @rev = 5
  87. version: @version = 42
  88. ranges: @ranges = { "mock": "ranges" }
  89. describe "with a successful response code", ->
  90. beforeEach ->
  91. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
  92. @DocstoreManager.getDoc @project_id, @doc_id, @callback
  93. it "should get the doc from the docstore api", ->
  94. @request.get
  95. .calledWith({
  96. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}"
  97. json: true
  98. })
  99. .should.equal true
  100. it "should call the callback with the lines, version and rev", ->
  101. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
  102. describe "with a failed response code", ->
  103. beforeEach ->
  104. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  105. @DocstoreManager.getDoc @project_id, @doc_id, @callback
  106. it "should call the callback with an error", ->
  107. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  108. it "should log the error", ->
  109. @logger.error
  110. .calledWith({
  111. err: new Error("docstore api responded with a non-success code: 500")
  112. project_id: @project_id
  113. doc_id: @doc_id
  114. }, "error getting doc from docstore")
  115. .should.equal true
  116. describe "with include_deleted=true", ->
  117. beforeEach ->
  118. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @doc)
  119. @DocstoreManager.getDoc @project_id, @doc_id, include_deleted: true, @callback
  120. it "should get the doc from the docstore api (including deleted)", ->
  121. @request.get
  122. .calledWith({
  123. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc/#{@doc_id}?include_deleted=true"
  124. json: true
  125. })
  126. .should.equal true
  127. it "should call the callback with the lines, version and rev", ->
  128. @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
  129. describe "getAllDocs", ->
  130. describe "with a successful response code", ->
  131. beforeEach ->
  132. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @docs = [{ _id: "mock-doc-id" }])
  133. @DocstoreManager.getAllDocs @project_id, @callback
  134. it "should get all the project docs in the docstore api", ->
  135. @request.get
  136. .calledWith({
  137. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/doc"
  138. json: true
  139. })
  140. .should.equal true
  141. it "should call the callback with the docs", ->
  142. @callback.calledWith(null, @docs).should.equal true
  143. describe "with a failed response code", ->
  144. beforeEach ->
  145. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  146. @DocstoreManager.getAllDocs @project_id, @callback
  147. it "should call the callback with an error", ->
  148. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  149. it "should log the error", ->
  150. @logger.error
  151. .calledWith({
  152. err: new Error("docstore api responded with a non-success code: 500")
  153. project_id: @project_id
  154. }, "error getting all docs from docstore")
  155. .should.equal true
  156. describe "getAllRanges", ->
  157. describe "with a successful response code", ->
  158. beforeEach ->
  159. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 204, @docs = [{ _id: "mock-doc-id", ranges: "mock-ranges" }])
  160. @DocstoreManager.getAllRanges @project_id, @callback
  161. it "should get all the project doc ranges in the docstore api", ->
  162. @request.get
  163. .calledWith({
  164. url: "#{@settings.apis.docstore.url}/project/#{@project_id}/ranges"
  165. json: true
  166. })
  167. .should.equal true
  168. it "should call the callback with the docs", ->
  169. @callback.calledWith(null, @docs).should.equal true
  170. describe "with a failed response code", ->
  171. beforeEach ->
  172. @request.get = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
  173. @DocstoreManager.getAllRanges @project_id, @callback
  174. it "should call the callback with an error", ->
  175. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  176. it "should log the error", ->
  177. @logger.error
  178. .calledWith({
  179. err: new Error("docstore api responded with a non-success code: 500")
  180. project_id: @project_id
  181. }, "error getting all doc ranges from docstore")
  182. .should.equal true
  183. describe "archiveProject", ->
  184. describe "with a successful response code", ->
  185. beforeEach ->
  186. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  187. @DocstoreManager.archiveProject @project_id, @callback
  188. it "should call the callback", ->
  189. @callback.called.should.equal true
  190. describe "with a failed response code", ->
  191. beforeEach ->
  192. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
  193. @DocstoreManager.archiveProject @project_id, @callback
  194. it "should call the callback with an error", ->
  195. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
  196. describe "unarchiveProject", ->
  197. describe "with a successful response code", ->
  198. beforeEach ->
  199. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
  200. @DocstoreManager.unarchiveProject @project_id, @callback
  201. it "should call the callback", ->
  202. @callback.called.should.equal true
  203. describe "with a failed response code", ->
  204. beforeEach ->
  205. @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
  206. @DocstoreManager.unarchiveProject @project_id, @callback
  207. it "should call the callback with an error", ->
  208. @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true