DocumentUpdaterHandlerTests.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. should = require('chai').should()
  2. spies = require('chai-spies')
  3. chai = require('chai').use(spies)
  4. sinon = require("sinon")
  5. SandboxedModule = require('sandboxed-module')
  6. assert = require('chai').assert
  7. path = require 'path'
  8. _ = require 'underscore'
  9. modulePath = path.join __dirname, '../../../../app/js/Features/DocumentUpdater/DocumentUpdaterHandler'
  10. describe 'Flushing documents :', ->
  11. beforeEach ->
  12. @project_id = "project-id-923"
  13. @doc_id = "doc-id-394"
  14. @lines = ["one", "two", "three"]
  15. @version = 42
  16. @project =
  17. _id: @project_id
  18. @request = {}
  19. @projectEntityHandler = {}
  20. @rclient = {auth:->}
  21. @settings =
  22. apis : documentupdater: url : "http://something.com"
  23. redis:{web:{}}
  24. @handler = SandboxedModule.require modulePath, requires:
  25. 'request': defaults:=> return @request
  26. 'settings-sharelatex':@settings
  27. 'logger-sharelatex':{log:(->), error:(->)}
  28. '../Project/ProjectEntityHandler':@projectEntityHandler
  29. "../../models/Project": Project: @Project={}
  30. '../../Features/Project/ProjectLocator':{}
  31. 'redis' : createClient: () => @rclient
  32. describe 'queueChange', ->
  33. beforeEach ->
  34. @change = {
  35. "action":"removeText",
  36. "range":{"start":{"row":2,"column":2},"end":{"row":2,"column":3}},
  37. "text":"e"
  38. }
  39. @rclient.rpush = sinon.stub().callsArg(2)
  40. @rclient.publish = sinon.stub().callsArg(2)
  41. @rclient.sadd = sinon.stub().callsArg(2)
  42. @callback = sinon.stub()
  43. describe "successfully", ->
  44. beforeEach ->
  45. @handler.queueChange(@project_id, @doc_id, @change, @callback)
  46. it "should push the change", ->
  47. @rclient.rpush
  48. .calledWith("PendingUpdates:#{@doc_id}", JSON.stringify(@change))
  49. .should.equal true
  50. it "should notify the doc updater of the change via pub/sub", ->
  51. @rclient.publish
  52. .calledWith("pending-updates", "#{@project_id}:#{@doc_id}")
  53. .should.equal true
  54. it "should push the doc id into the pending updates set", ->
  55. @rclient.sadd
  56. .calledWith("DocsWithPendingUpdates", "#{@project_id}:#{@doc_id}")
  57. .should.equal true
  58. describe "with error connecting to redis during push", ->
  59. beforeEach ->
  60. @rclient.rpush = sinon.stub().callsArgWith(2, new Error("something went wrong"))
  61. @handler.queueChange(@project_id, @doc_id, @change, @callback)
  62. it "should return an error", ->
  63. @callback.calledWithExactly(sinon.match(Error)).should.equal true
  64. describe "with error connecting to redis during publish", ->
  65. beforeEach ->
  66. @rclient.publish = sinon.stub().callsArgWith(2, new Error("something went wrong"))
  67. @handler.queueChange(@project_id, @doc_id, @change, @callback)
  68. it "should return an error", ->
  69. @callback.calledWithExactly(sinon.match(Error)).should.equal true
  70. describe "with error connecting to redis during sadd", ->
  71. beforeEach ->
  72. @rclient.sadd = sinon.stub().callsArgWith(2, new Error("something went wrong"))
  73. @handler.queueChange(@project_id, @doc_id, @change, @callback)
  74. it "should return an error", ->
  75. @callback.calledWithExactly(sinon.match(Error)).should.equal true
  76. describe 'flushProjectToMongo', ->
  77. beforeEach ->
  78. @callback = sinon.stub()
  79. describe "successfully", ->
  80. beforeEach ->
  81. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  82. @handler.flushProjectToMongo @project_id, @callback
  83. it 'should flush the document from the document updater', ->
  84. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/flush"
  85. @request.post.calledWith(url).should.equal true
  86. it "should call the callback with no error", ->
  87. @callback.calledWith(null).should.equal true
  88. describe "when the document updater API returns an error", ->
  89. beforeEach ->
  90. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  91. @handler.flushProjectToMongo @project_id, @callback
  92. it "should return an error to the callback", ->
  93. @callback.calledWith(@error).should.equal true
  94. describe "when the document updater returns a failure error code", ->
  95. beforeEach ->
  96. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  97. @handler.flushProjectToMongo @project_id, @callback
  98. it "should return the callback with an error", ->
  99. @callback
  100. .calledWith(new Error("doc updater returned failure status code: 500"))
  101. .should.equal true
  102. describe 'flushProjectToMongoAndDelete', ->
  103. beforeEach ->
  104. @callback = sinon.stub()
  105. describe "successfully", ->
  106. beforeEach ->
  107. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  108. @handler.flushProjectToMongoAndDelete @project_id, @callback
  109. it 'should delete the project from the document updater', ->
  110. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  111. @request.del.calledWith(url).should.equal true
  112. it "should call the callback with no error", ->
  113. @callback.calledWith(null).should.equal true
  114. describe "when the document updater API returns an error", ->
  115. beforeEach ->
  116. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  117. @handler.flushProjectToMongoAndDelete @project_id, @callback
  118. it "should return an error to the callback", ->
  119. @callback.calledWith(@error).should.equal true
  120. describe "when the document updater returns a failure error code", ->
  121. beforeEach ->
  122. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  123. @handler.flushProjectToMongoAndDelete @project_id, @callback
  124. it "should return the callback with an error", ->
  125. @callback
  126. .calledWith(new Error("doc updater returned failure status code: 500"))
  127. .should.equal true
  128. describe 'flushDocToMongo', ->
  129. beforeEach ->
  130. @callback = sinon.stub()
  131. describe "successfully", ->
  132. beforeEach ->
  133. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  134. @handler.flushDocToMongo @project_id, @doc_id, @callback
  135. it 'should flush the document from the document updater', ->
  136. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/flush"
  137. @request.post.calledWith(url).should.equal true
  138. it "should call the callback with no error", ->
  139. @callback.calledWith(null).should.equal true
  140. describe "when the document updater API returns an error", ->
  141. beforeEach ->
  142. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  143. @handler.flushDocToMongo @project_id, @doc_id, @callback
  144. it "should return an error to the callback", ->
  145. @callback.calledWith(@error).should.equal true
  146. describe "when the document updater returns a failure error code", ->
  147. beforeEach ->
  148. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  149. @handler.flushDocToMongo @project_id, @doc_id, @callback
  150. it "should return the callback with an error", ->
  151. @callback
  152. .calledWith(new Error("doc updater returned failure status code: 500"))
  153. .should.equal true
  154. describe "deleteDoc", ->
  155. beforeEach ->
  156. @callback = sinon.stub()
  157. describe "successfully", ->
  158. beforeEach ->
  159. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  160. @handler.deleteDoc @project_id, @doc_id, @callback
  161. it 'should delete the document from the document updater', ->
  162. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  163. @request.del.calledWith(url).should.equal true
  164. it "should call the callback with no error", ->
  165. @callback.calledWith(null).should.equal true
  166. describe "when the document updater API returns an error", ->
  167. beforeEach ->
  168. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  169. @handler.deleteDoc @project_id, @doc_id, @callback
  170. it "should return an error to the callback", ->
  171. @callback.calledWith(@error).should.equal true
  172. describe "when the document updater returns a failure error code", ->
  173. beforeEach ->
  174. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  175. @handler.deleteDoc @project_id, @doc_id, @callback
  176. it "should return the callback with an error", ->
  177. @callback
  178. .calledWith(new Error("doc updater returned failure status code: 500"))
  179. .should.equal true
  180. describe "setDocument", ->
  181. beforeEach ->
  182. @callback = sinon.stub()
  183. describe "successfully", ->
  184. beforeEach ->
  185. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  186. @handler.setDocument @project_id, @doc_id, @lines, @callback
  187. it 'should set the document in the document updater', ->
  188. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  189. @request.post
  190. .calledWith({
  191. url: url
  192. json:
  193. lines: @lines
  194. })
  195. .should.equal true
  196. it "should call the callback with no error", ->
  197. @callback.calledWith(null).should.equal true
  198. describe "when the document updater API returns an error", ->
  199. beforeEach ->
  200. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  201. @handler.setDocument @project_id, @doc_id, @lines, @callback
  202. it "should return an error to the callback", ->
  203. @callback.calledWith(@error).should.equal true
  204. describe "when the document updater returns a failure error code", ->
  205. beforeEach ->
  206. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  207. @handler.setDocument @project_id, @doc_id, @lines, @callback
  208. it "should return the callback with an error", ->
  209. @callback
  210. .calledWith(new Error("doc updater returned failure status code: 500"))
  211. .should.equal true
  212. describe "getDocument", ->
  213. beforeEach ->
  214. @callback = sinon.stub()
  215. describe "successfully", ->
  216. beforeEach ->
  217. @body = JSON.stringify
  218. lines: @lines
  219. version: @version
  220. ops: @ops = ["mock-op-1", "mock-op-2"]
  221. @fromVersion = 2
  222. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  223. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  224. it 'should get the document from the document updater', ->
  225. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}"
  226. @request.get.calledWith(url).should.equal true
  227. it "should call the callback with the lines and version", ->
  228. @callback.calledWith(null, @lines, @version, @ops).should.equal true
  229. describe "when the document updater API returns an error", ->
  230. beforeEach ->
  231. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  232. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  233. it "should return an error to the callback", ->
  234. @callback.calledWith(@error).should.equal true
  235. describe "when the document updater returns a failure error code", ->
  236. beforeEach ->
  237. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  238. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  239. it "should return the callback with an error", ->
  240. @callback
  241. .calledWith(new Error("doc updater returned failure status code: 500"))
  242. .should.equal true