DocstoreManagerTests.coffee 7.5 KB

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