DocstoreManagerTests.coffee 10 KB

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