DocumentUpdaterHandlerTests.coffee 11 KB

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