DocumentUpdaterHandlerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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', ->
  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. @settings =
  22. apis : documentupdater: url : "http://something.com"
  23. @handler = SandboxedModule.require modulePath, requires:
  24. 'request': defaults:=> return @request
  25. 'settings-sharelatex':@settings
  26. 'logger-sharelatex':{log:(->), error:(->)}
  27. '../Project/ProjectEntityHandler':@projectEntityHandler
  28. "../../models/Project": Project: @Project={}
  29. '../../Features/Project/ProjectLocator':{}
  30. "metrics-sharelatex":
  31. Timer:->
  32. done:->
  33. describe 'flushProjectToMongo', ->
  34. beforeEach ->
  35. @callback = sinon.stub()
  36. describe "successfully", ->
  37. beforeEach ->
  38. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  39. @handler.flushProjectToMongo @project_id, @callback
  40. it 'should flush the document from the document updater', ->
  41. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/flush"
  42. @request.post.calledWith(url).should.equal true
  43. it "should call the callback with no error", ->
  44. @callback.calledWith(null).should.equal true
  45. describe "when the document updater API returns an error", ->
  46. beforeEach ->
  47. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  48. @handler.flushProjectToMongo @project_id, @callback
  49. it "should return an error to the callback", ->
  50. @callback.calledWith(@error).should.equal true
  51. describe "when the document updater returns a failure error code", ->
  52. beforeEach ->
  53. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  54. @handler.flushProjectToMongo @project_id, @callback
  55. it "should return the callback with an error", ->
  56. @callback
  57. .calledWith(new Error("doc updater returned failure status code: 500"))
  58. .should.equal true
  59. describe 'flushProjectToMongoAndDelete', ->
  60. beforeEach ->
  61. @callback = sinon.stub()
  62. describe "successfully", ->
  63. beforeEach ->
  64. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  65. @handler.flushProjectToMongoAndDelete @project_id, @callback
  66. it 'should delete the project from the document updater', ->
  67. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  68. @request.del.calledWith(url).should.equal true
  69. it "should call the callback with no error", ->
  70. @callback.calledWith(null).should.equal true
  71. describe "when the document updater API returns an error", ->
  72. beforeEach ->
  73. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  74. @handler.flushProjectToMongoAndDelete @project_id, @callback
  75. it "should return an error to the callback", ->
  76. @callback.calledWith(@error).should.equal true
  77. describe "when the document updater returns a failure error code", ->
  78. beforeEach ->
  79. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  80. @handler.flushProjectToMongoAndDelete @project_id, @callback
  81. it "should return the callback with an error", ->
  82. @callback
  83. .calledWith(new Error("doc updater returned failure status code: 500"))
  84. .should.equal true
  85. describe 'flushDocToMongo', ->
  86. beforeEach ->
  87. @callback = sinon.stub()
  88. describe "successfully", ->
  89. beforeEach ->
  90. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  91. @handler.flushDocToMongo @project_id, @doc_id, @callback
  92. it 'should flush the document from the document updater', ->
  93. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/flush"
  94. @request.post.calledWith(url).should.equal true
  95. it "should call the callback with no error", ->
  96. @callback.calledWith(null).should.equal true
  97. describe "when the document updater API returns an error", ->
  98. beforeEach ->
  99. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  100. @handler.flushDocToMongo @project_id, @doc_id, @callback
  101. it "should return an error to the callback", ->
  102. @callback.calledWith(@error).should.equal true
  103. describe "when the document updater returns a failure error code", ->
  104. beforeEach ->
  105. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  106. @handler.flushDocToMongo @project_id, @doc_id, @callback
  107. it "should return the callback with an error", ->
  108. @callback
  109. .calledWith(new Error("doc updater returned failure status code: 500"))
  110. .should.equal true
  111. describe "deleteDoc", ->
  112. beforeEach ->
  113. @callback = sinon.stub()
  114. describe "successfully", ->
  115. beforeEach ->
  116. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  117. @handler.deleteDoc @project_id, @doc_id, @callback
  118. it 'should delete the document from the document updater', ->
  119. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  120. @request.del.calledWith(url).should.equal true
  121. it "should call the callback with no error", ->
  122. @callback.calledWith(null).should.equal true
  123. describe "when the document updater API returns an error", ->
  124. beforeEach ->
  125. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  126. @handler.deleteDoc @project_id, @doc_id, @callback
  127. it "should return an error to the callback", ->
  128. @callback.calledWith(@error).should.equal true
  129. describe "when the document updater returns a failure error code", ->
  130. beforeEach ->
  131. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  132. @handler.deleteDoc @project_id, @doc_id, @callback
  133. it "should return the callback with an error", ->
  134. @callback
  135. .calledWith(new Error("doc updater returned failure status code: 500"))
  136. .should.equal true
  137. describe "setDocument", ->
  138. beforeEach ->
  139. @callback = sinon.stub()
  140. @source = "dropbox"
  141. describe "successfully", ->
  142. beforeEach ->
  143. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  144. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  145. it 'should set the document in the document updater', ->
  146. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  147. @request.post
  148. .calledWith({
  149. url: url
  150. json:
  151. lines: @lines
  152. source: @source
  153. user_id: @user_id
  154. })
  155. .should.equal true
  156. it "should call the callback with no error", ->
  157. @callback.calledWith(null).should.equal true
  158. describe "when the document updater API returns an error", ->
  159. beforeEach ->
  160. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  161. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  162. it "should return an error to the callback", ->
  163. @callback.calledWith(@error).should.equal true
  164. describe "when the document updater returns a failure error code", ->
  165. beforeEach ->
  166. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  167. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  168. it "should return the callback with an error", ->
  169. @callback
  170. .calledWith(new Error("doc updater returned failure status code: 500"))
  171. .should.equal true
  172. describe "getDocument", ->
  173. beforeEach ->
  174. @callback = sinon.stub()
  175. describe "successfully", ->
  176. beforeEach ->
  177. @body = JSON.stringify
  178. lines: @lines
  179. version: @version
  180. ops: @ops = ["mock-op-1", "mock-op-2"]
  181. ranges: @ranges = {"mock":"ranges"}
  182. @fromVersion = 2
  183. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  184. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  185. it 'should get the document from the document updater', ->
  186. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}"
  187. @request.get.calledWith(url).should.equal true
  188. it "should call the callback with the lines and version", ->
  189. @callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true
  190. describe "when the document updater API returns an error", ->
  191. beforeEach ->
  192. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  193. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  194. it "should return an error to the callback", ->
  195. @callback.calledWith(@error).should.equal true
  196. describe "when the document updater returns a failure error code", ->
  197. beforeEach ->
  198. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  199. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  200. it "should return the callback with an error", ->
  201. @callback
  202. .calledWith(new Error("doc updater returned failure status code: 500"))
  203. .should.equal true
  204. describe "getProjectDocsIfMatch", ->
  205. beforeEach ->
  206. @callback = sinon.stub()
  207. @project_state_hash = "1234567890abcdef"
  208. describe "successfully", ->
  209. beforeEach ->
  210. @doc0 =
  211. _id: @doc_id
  212. lines: @lines
  213. v: @version
  214. @docs = [ @doc0, @doc0, @doc0 ]
  215. @body = JSON.stringify @docs
  216. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  217. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  218. it 'should get the documenst from the document updater', ->
  219. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc?state=#{@project_state_hash}"
  220. @request.get.calledWith(url).should.equal true
  221. it "should call the callback with the documents", ->
  222. @callback.calledWithExactly(null, @docs).should.equal true
  223. describe "when the document updater API returns an error", ->
  224. beforeEach ->
  225. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  226. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  227. it "should return an error to the callback", ->
  228. @callback.calledWith(@error).should.equal true
  229. describe "when the document updater returns a conflict error code", ->
  230. beforeEach ->
  231. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
  232. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  233. it "should return the callback with no documents", ->
  234. @callback
  235. .alwaysCalledWithExactly()
  236. .should.equal true
  237. describe "clearProjectState", ->
  238. beforeEach ->
  239. @callback = sinon.stub()
  240. describe "successfully", ->
  241. beforeEach ->
  242. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200})
  243. @handler.clearProjectState @project_id, @callback
  244. it 'should clear the project state from the document updater', ->
  245. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/clearState"
  246. @request.post.calledWith(url).should.equal true
  247. it "should call the callback", ->
  248. @callback.calledWithExactly().should.equal true
  249. describe "when the document updater API returns an error", ->
  250. beforeEach ->
  251. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  252. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  253. it "should return an error to the callback", ->
  254. @callback.calledWith(@error).should.equal true
  255. describe "when the document updater returns a conflict error code", ->
  256. beforeEach ->
  257. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
  258. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  259. it "should return the callback with no documents", ->
  260. @callback
  261. .alwaysCalledWithExactly()
  262. .should.equal true
  263. describe "acceptChanges", ->
  264. beforeEach ->
  265. @change_id = "mock-change-id-1"
  266. @callback = sinon.stub()
  267. describe "successfully", ->
  268. beforeEach ->
  269. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  270. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  271. it 'should accept the change in the document updater', ->
  272. req =
  273. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/change/accept"
  274. json:
  275. change_ids: [ @change_id ]
  276. @request.post.calledWith(req).should.equal true
  277. it "should call the callback", ->
  278. @callback.calledWith(null).should.equal true
  279. describe "when the document updater API returns an error", ->
  280. beforeEach ->
  281. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  282. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  283. it "should return an error to the callback", ->
  284. @callback.calledWith(@error).should.equal true
  285. describe "when the document updater returns a failure error code", ->
  286. beforeEach ->
  287. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  288. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  289. it "should return the callback with an error", ->
  290. @callback
  291. .calledWith(new Error("doc updater returned failure status code: 500"))
  292. .should.equal true
  293. describe "deleteThread", ->
  294. beforeEach ->
  295. @thread_id = "mock-thread-id-1"
  296. @callback = sinon.stub()
  297. describe "successfully", ->
  298. beforeEach ->
  299. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  300. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  301. it 'should delete the thread in the document updater', ->
  302. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/comment/#{@thread_id}"
  303. @request.del.calledWith(url).should.equal true
  304. it "should call the callback", ->
  305. @callback.calledWith(null).should.equal true
  306. describe "when the document updater API returns an error", ->
  307. beforeEach ->
  308. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  309. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  310. it "should return an error to the callback", ->
  311. @callback.calledWith(@error).should.equal true
  312. describe "when the document updater returns a failure error code", ->
  313. beforeEach ->
  314. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  315. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  316. it "should return the callback with an error", ->
  317. @callback
  318. .calledWith(new Error("doc updater returned failure status code: 500"))
  319. .should.equal true