DocumentUpdaterHandlerTests.coffee 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. ObjectId = require("mongojs").ObjectId;
  10. modulePath = path.join __dirname, '../../../../app/js/Features/DocumentUpdater/DocumentUpdaterHandler'
  11. describe 'DocumentUpdaterHandler', ->
  12. beforeEach ->
  13. @project_id = "project-id-923"
  14. @doc_id = "doc-id-394"
  15. @lines = ["one", "two", "three"]
  16. @version = 42
  17. @user_id = "mock-user-id-123"
  18. @project =
  19. _id: @project_id
  20. @request = {}
  21. @projectEntityHandler = {}
  22. @settings =
  23. apis:
  24. documentupdater:
  25. url : "http://document_updater.example.com"
  26. project_history:
  27. url: "http://project_history.example.com"
  28. @callback = sinon.stub()
  29. @handler = SandboxedModule.require modulePath, requires:
  30. 'request': defaults:=> return @request
  31. 'settings-sharelatex':@settings
  32. 'logger-sharelatex':{log:(->), error:(->)}
  33. '../Project/ProjectEntityHandler':@projectEntityHandler
  34. "../../models/Project": Project: @Project={}
  35. '../../Features/Project/ProjectLocator':{}
  36. "metrics-sharelatex":
  37. Timer:->
  38. done:->
  39. describe 'flushProjectToMongo', ->
  40. describe "successfully", ->
  41. beforeEach ->
  42. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  43. @handler.flushProjectToMongo @project_id, @callback
  44. it 'should flush the document from the document updater', ->
  45. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/flush"
  46. @request.post.calledWith(url).should.equal true
  47. it "should call the callback with no error", ->
  48. @callback.calledWith(null).should.equal true
  49. describe "when the document updater API returns an error", ->
  50. beforeEach ->
  51. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  52. @handler.flushProjectToMongo @project_id, @callback
  53. it "should return an error to the callback", ->
  54. @callback.calledWith(@error).should.equal true
  55. describe "when the document updater returns a failure error code", ->
  56. beforeEach ->
  57. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  58. @handler.flushProjectToMongo @project_id, @callback
  59. it "should return the callback with an error", ->
  60. @callback
  61. .calledWith(new Error("doc updater returned failure status code: 500"))
  62. .should.equal true
  63. describe 'flushProjectToMongoAndDelete', ->
  64. describe "successfully", ->
  65. beforeEach ->
  66. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  67. @handler.flushProjectToMongoAndDelete @project_id, @callback
  68. it 'should delete the project from the document updater', ->
  69. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  70. @request.del.calledWith(url).should.equal true
  71. it "should call the callback with no error", ->
  72. @callback.calledWith(null).should.equal true
  73. describe "when the document updater API returns an error", ->
  74. beforeEach ->
  75. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  76. @handler.flushProjectToMongoAndDelete @project_id, @callback
  77. it "should return an error to the callback", ->
  78. @callback.calledWith(@error).should.equal true
  79. describe "when the document updater returns a failure error code", ->
  80. beforeEach ->
  81. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  82. @handler.flushProjectToMongoAndDelete @project_id, @callback
  83. it "should return the callback with an error", ->
  84. @callback
  85. .calledWith(new Error("doc updater returned failure status code: 500"))
  86. .should.equal true
  87. describe 'flushDocToMongo', ->
  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. describe "successfully", ->
  113. beforeEach ->
  114. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  115. @handler.deleteDoc @project_id, @doc_id, @callback
  116. it 'should delete the document from the document updater', ->
  117. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  118. @request.del.calledWith(url).should.equal true
  119. it "should call the callback with no error", ->
  120. @callback.calledWith(null).should.equal true
  121. describe "when the document updater API returns an error", ->
  122. beforeEach ->
  123. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  124. @handler.deleteDoc @project_id, @doc_id, @callback
  125. it "should return an error to the callback", ->
  126. @callback.calledWith(@error).should.equal true
  127. describe "when the document updater returns a failure error code", ->
  128. beforeEach ->
  129. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  130. @handler.deleteDoc @project_id, @doc_id, @callback
  131. it "should return the callback with an error", ->
  132. @callback
  133. .calledWith(new Error("doc updater returned failure status code: 500"))
  134. .should.equal true
  135. describe "setDocument", ->
  136. beforeEach ->
  137. @source = "dropbox"
  138. describe "successfully", ->
  139. beforeEach ->
  140. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  141. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  142. it 'should set the document in the document updater', ->
  143. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  144. @request.post
  145. .calledWith({
  146. url: url
  147. json:
  148. lines: @lines
  149. source: @source
  150. user_id: @user_id
  151. })
  152. .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.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  158. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @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.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  164. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @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 "getDocument", ->
  170. describe "successfully", ->
  171. beforeEach ->
  172. @body = JSON.stringify
  173. lines: @lines
  174. version: @version
  175. ops: @ops = ["mock-op-1", "mock-op-2"]
  176. ranges: @ranges = {"mock":"ranges"}
  177. @fromVersion = 2
  178. @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  179. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  180. it 'should get the document from the document updater', ->
  181. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}"
  182. @request.get.calledWith(url).should.equal true
  183. it "should call the callback with the lines and version", ->
  184. @callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true
  185. describe "when the document updater API returns an error", ->
  186. beforeEach ->
  187. @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  188. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  189. it "should return an error to the callback", ->
  190. @callback.calledWith(@error).should.equal true
  191. describe "when the document updater returns a failure error code", ->
  192. beforeEach ->
  193. @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  194. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  195. it "should return the callback with an error", ->
  196. @callback
  197. .calledWith(new Error("doc updater returned failure status code: 500"))
  198. .should.equal true
  199. describe "getProjectDocsIfMatch", ->
  200. beforeEach ->
  201. @project_state_hash = "1234567890abcdef"
  202. describe "successfully", ->
  203. beforeEach ->
  204. @doc0 =
  205. _id: @doc_id
  206. lines: @lines
  207. v: @version
  208. @docs = [ @doc0, @doc0, @doc0 ]
  209. @body = JSON.stringify @docs
  210. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  211. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  212. it 'should get the documenst from the document updater', ->
  213. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/get_and_flush_if_old?state=#{@project_state_hash}"
  214. @request.post.calledWith(url).should.equal true
  215. it "should call the callback with the documents", ->
  216. @callback.calledWithExactly(null, @docs).should.equal true
  217. describe "when the document updater API returns an error", ->
  218. beforeEach ->
  219. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  220. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  221. it "should return an error to the callback", ->
  222. @callback.calledWith(@error).should.equal true
  223. describe "when the document updater returns a conflict error code", ->
  224. beforeEach ->
  225. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
  226. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  227. it "should return the callback with no documents", ->
  228. @callback
  229. .alwaysCalledWithExactly()
  230. .should.equal true
  231. describe "clearProjectState", ->
  232. describe "successfully", ->
  233. beforeEach ->
  234. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200})
  235. @handler.clearProjectState @project_id, @callback
  236. it 'should clear the project state from the document updater', ->
  237. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/clearState"
  238. @request.post.calledWith(url).should.equal true
  239. it "should call the callback", ->
  240. @callback.calledWithExactly().should.equal true
  241. describe "when the document updater API returns an error", ->
  242. beforeEach ->
  243. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  244. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  245. it "should return an error to the callback", ->
  246. @callback.calledWith(@error).should.equal true
  247. describe "when the document updater returns a conflict error code", ->
  248. beforeEach ->
  249. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
  250. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  251. it "should return the callback with no documents", ->
  252. @callback
  253. .alwaysCalledWithExactly()
  254. .should.equal true
  255. describe "acceptChanges", ->
  256. beforeEach ->
  257. @change_id = "mock-change-id-1"
  258. describe "successfully", ->
  259. beforeEach ->
  260. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  261. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  262. it 'should accept the change in the document updater', ->
  263. req =
  264. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/change/accept"
  265. json:
  266. change_ids: [ @change_id ]
  267. @request.post.calledWith(req).should.equal true
  268. it "should call the callback", ->
  269. @callback.calledWith(null).should.equal true
  270. describe "when the document updater API returns an error", ->
  271. beforeEach ->
  272. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  273. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  274. it "should return an error to the callback", ->
  275. @callback.calledWith(@error).should.equal true
  276. describe "when the document updater returns a failure error code", ->
  277. beforeEach ->
  278. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  279. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  280. it "should return the callback with an error", ->
  281. @callback
  282. .calledWith(new Error("doc updater returned failure status code: 500"))
  283. .should.equal true
  284. describe "deleteThread", ->
  285. beforeEach ->
  286. @thread_id = "mock-thread-id-1"
  287. describe "successfully", ->
  288. beforeEach ->
  289. @request.del = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  290. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  291. it 'should delete the thread in the document updater', ->
  292. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/comment/#{@thread_id}"
  293. @request.del.calledWith(url).should.equal true
  294. it "should call the callback", ->
  295. @callback.calledWith(null).should.equal true
  296. describe "when the document updater API returns an error", ->
  297. beforeEach ->
  298. @request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  299. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  300. it "should return an error to the callback", ->
  301. @callback.calledWith(@error).should.equal true
  302. describe "when the document updater returns a failure error code", ->
  303. beforeEach ->
  304. @request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
  305. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  306. it "should return the callback with an error", ->
  307. @callback
  308. .calledWith(new Error("doc updater returned failure status code: 500"))
  309. .should.equal true
  310. describe "updateProjectStructure ", ->
  311. beforeEach ->
  312. @user_id = 1234
  313. @docIdA = new ObjectId()
  314. @docIdB = new ObjectId()
  315. @oldDocs = [
  316. { path: '/old_a', doc: _id: @docIdA }
  317. { path: '/old_b', doc: _id: @docIdB }
  318. ]
  319. # create new instances of the same ObjectIds so that == doens't pass
  320. @newDocs = [
  321. { path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
  322. { path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
  323. ]
  324. @oldFiles = []
  325. @newFiles = []
  326. describe "with project history disabled", ->
  327. beforeEach ->
  328. @settings.apis.project_history.enabled = false
  329. @request.post = sinon.stub()
  330. @handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
  331. it 'does not make a web request', ->
  332. @request.post.called.should.equal false
  333. it 'calls the callback', ->
  334. @callback.called.should.equal true
  335. describe "with project history enabled", ->
  336. beforeEach ->
  337. @settings.apis.project_history.enabled = true
  338. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
  339. @handler.updateProjectStructure @project_id, @user_id, @oldDocs, @newDocs, @oldFiles, @newFiles, @callback
  340. it 'should send the structure update to the document updater', ->
  341. docUpdates = [
  342. id: @docIdB,
  343. pathname: "/old_b"
  344. newPathname: "/new_b"
  345. ]
  346. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  347. @request.post
  348. .calledWith(url: url, json: {docUpdates, fileUpdates: [], userId: @user_id})
  349. .should.equal true
  350. it "should call the callback with no error", ->
  351. @callback.calledWith(null).should.equal true