DocumentUpdaterHandlerTests.coffee 11 KB

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