DocumentUpdaterHandlerTests.coffee 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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-sharelatex' : 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.multi = sinon.stub().returns @rclient
  40. @rclient.exec = sinon.stub().callsArg(0)
  41. @rclient.rpush = sinon.stub()
  42. @rclient.sadd = sinon.stub()
  43. @callback = sinon.stub()
  44. describe "successfully", ->
  45. beforeEach ->
  46. @handler.queueChange(@project_id, @doc_id, @change, @callback)
  47. it "should push the change", ->
  48. @rclient.rpush
  49. .calledWith("PendingUpdates:#{@doc_id}", JSON.stringify(@change))
  50. .should.equal true
  51. it "should notify the doc updater of the change via the pending-updates-list queue", ->
  52. @rclient.rpush
  53. .calledWith("pending-updates-list", "#{@project_id}:#{@doc_id}")
  54. .should.equal true
  55. it "should push the doc id into the pending updates set", ->
  56. @rclient.sadd
  57. .calledWith("DocsWithPendingUpdates", "#{@project_id}:#{@doc_id}")
  58. .should.equal true
  59. describe "with error connecting to redis during exec", ->
  60. beforeEach ->
  61. @rclient.exec = sinon.stub().callsArgWith(0, new Error("something went wrong"))
  62. @handler.queueChange(@project_id, @doc_id, @change, @callback)
  63. it "should return an error", ->
  64. @callback.calledWithExactly(sinon.match(Error)).should.equal true
  65. describe 'flushProjectToMongo', ->
  66. beforeEach ->
  67. @callback = sinon.stub()
  68. describe "successfully", ->
  69. beforeEach ->
  70. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  71. @handler.flushProjectToMongo @project_id, @callback
  72. it 'should flush the document from the document updater', ->
  73. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/flush"
  74. @request.post.calledWith(url).should.equal true
  75. it "should call the callback with no error", ->
  76. @callback.calledWith(null).should.equal true
  77. describe "when the document updater API returns an error", ->
  78. beforeEach ->
  79. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  80. @handler.flushProjectToMongo @project_id, @callback
  81. it "should return an error to the callback", ->
  82. @callback.calledWith(@error).should.equal true
  83. describe "when the document updater returns a failure error code", ->
  84. beforeEach ->
  85. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  86. @handler.flushProjectToMongo @project_id, @callback
  87. it "should return the callback with an error", ->
  88. @callback
  89. .calledWith(new Error("doc updater returned failure status code: 500"))
  90. .should.equal true
  91. describe 'flushProjectToMongoAndDelete', ->
  92. beforeEach ->
  93. @callback = sinon.stub()
  94. describe "successfully", ->
  95. beforeEach ->
  96. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  97. @handler.flushProjectToMongoAndDelete @project_id, @callback
  98. it 'should delete the project from the document updater', ->
  99. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  100. @request.del.calledWith(url).should.equal true
  101. it "should call the callback with no error", ->
  102. @callback.calledWith(null).should.equal true
  103. describe "when the document updater API returns an error", ->
  104. beforeEach ->
  105. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  106. @handler.flushProjectToMongoAndDelete @project_id, @callback
  107. it "should return an error to the callback", ->
  108. @callback.calledWith(@error).should.equal true
  109. describe "when the document updater returns a failure error code", ->
  110. beforeEach ->
  111. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  112. @handler.flushProjectToMongoAndDelete @project_id, @callback
  113. it "should return the callback with an error", ->
  114. @callback
  115. .calledWith(new Error("doc updater returned failure status code: 500"))
  116. .should.equal true
  117. describe 'flushDocToMongo', ->
  118. beforeEach ->
  119. @callback = sinon.stub()
  120. describe "successfully", ->
  121. beforeEach ->
  122. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  123. @handler.flushDocToMongo @project_id, @doc_id, @callback
  124. it 'should flush the document from the document updater', ->
  125. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/flush"
  126. @request.post.calledWith(url).should.equal true
  127. it "should call the callback with no error", ->
  128. @callback.calledWith(null).should.equal true
  129. describe "when the document updater API returns an error", ->
  130. beforeEach ->
  131. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  132. @handler.flushDocToMongo @project_id, @doc_id, @callback
  133. it "should return an error to the callback", ->
  134. @callback.calledWith(@error).should.equal true
  135. describe "when the document updater returns a failure error code", ->
  136. beforeEach ->
  137. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  138. @handler.flushDocToMongo @project_id, @doc_id, @callback
  139. it "should return the callback with an error", ->
  140. @callback
  141. .calledWith(new Error("doc updater returned failure status code: 500"))
  142. .should.equal true
  143. describe "deleteDoc", ->
  144. beforeEach ->
  145. @callback = sinon.stub()
  146. describe "successfully", ->
  147. beforeEach ->
  148. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  149. @handler.deleteDoc @project_id, @doc_id, @callback
  150. it 'should delete the document from the document updater', ->
  151. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  152. @request.del.calledWith(url).should.equal true
  153. it "should call the callback with no error", ->
  154. @callback.calledWith(null).should.equal true
  155. describe "when the document updater API returns an error", ->
  156. beforeEach ->
  157. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  158. @handler.deleteDoc @project_id, @doc_id, @callback
  159. it "should return an error to the callback", ->
  160. @callback.calledWith(@error).should.equal true
  161. describe "when the document updater returns a failure error code", ->
  162. beforeEach ->
  163. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  164. @handler.deleteDoc @project_id, @doc_id, @callback
  165. it "should return the callback with an error", ->
  166. @callback
  167. .calledWith(new Error("doc updater returned failure status code: 500"))
  168. .should.equal true
  169. describe "setDocument", ->
  170. beforeEach ->
  171. @callback = sinon.stub()
  172. @source = "dropbox"
  173. describe "successfully", ->
  174. beforeEach ->
  175. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  176. @handler.setDocument @project_id, @doc_id, @lines, @source, @callback
  177. it 'should set the document in the document updater', ->
  178. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  179. @request.post
  180. .calledWith({
  181. url: url
  182. json:
  183. lines: @lines
  184. source: @source
  185. })
  186. .should.equal true
  187. it "should call the callback with no error", ->
  188. @callback.calledWith(null).should.equal true
  189. describe "when the document updater API returns an error", ->
  190. beforeEach ->
  191. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  192. @handler.setDocument @project_id, @doc_id, @lines, @source, @callback
  193. it "should return an error to the callback", ->
  194. @callback.calledWith(@error).should.equal true
  195. describe "when the document updater returns a failure error code", ->
  196. beforeEach ->
  197. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  198. @handler.setDocument @project_id, @doc_id, @lines, @source, @callback
  199. it "should return the callback with an error", ->
  200. @callback
  201. .calledWith(new Error("doc updater returned failure status code: 500"))
  202. .should.equal true
  203. describe "getDocument", ->
  204. beforeEach ->
  205. @callback = sinon.stub()
  206. describe "successfully", ->
  207. beforeEach ->
  208. @body = JSON.stringify
  209. lines: @lines
  210. version: @version
  211. ops: @ops = ["mock-op-1", "mock-op-2"]
  212. @fromVersion = 2
  213. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  214. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  215. it 'should get the document from the document updater', ->
  216. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}"
  217. @request.get.calledWith(url).should.equal true
  218. it "should call the callback with the lines and version", ->
  219. @callback.calledWith(null, @lines, @version, @ops).should.equal true
  220. describe "when the document updater API returns an error", ->
  221. beforeEach ->
  222. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  223. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  224. it "should return an error to the callback", ->
  225. @callback.calledWith(@error).should.equal true
  226. describe "when the document updater returns a failure error code", ->
  227. beforeEach ->
  228. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  229. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  230. it "should return the callback with an error", ->
  231. @callback
  232. .calledWith(new Error("doc updater returned failure status code: 500"))
  233. .should.equal true