DocumentUpdaterHandlerTests.coffee 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. @projectHistoryId = "ol-project-id-1"
  15. @doc_id = "doc-id-394"
  16. @lines = ["one", "two", "three"]
  17. @version = 42
  18. @user_id = "mock-user-id-123"
  19. @project =
  20. _id: @project_id
  21. @request = sinon.stub()
  22. @projectEntityHandler = {}
  23. @settings =
  24. apis:
  25. documentupdater:
  26. url : "http://document_updater.example.com"
  27. project_history:
  28. url: "http://project_history.example.com"
  29. @callback = sinon.stub()
  30. @handler = SandboxedModule.require modulePath, requires:
  31. 'request': defaults:=> return @request
  32. 'settings-sharelatex':@settings
  33. 'logger-sharelatex':{log:(->), error:(->)}
  34. '../Project/ProjectEntityHandler':@projectEntityHandler
  35. "../../models/Project": Project: @Project={}
  36. '../../Features/Project/ProjectLocator':{}
  37. "metrics-sharelatex":
  38. Timer:->
  39. done:->
  40. describe 'flushProjectToMongo', ->
  41. describe "successfully", ->
  42. beforeEach ->
  43. @request.callsArgWith(1, null, {statusCode: 204}, "")
  44. @handler.flushProjectToMongo @project_id, @callback
  45. it 'should flush the document from the document updater', ->
  46. @request.calledWithMatch(
  47. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/flush"
  48. method: "POST"
  49. ).should.equal true
  50. it "should call the callback with no error", ->
  51. @callback.calledWith(null).should.equal true
  52. describe "when the document updater API returns an error", ->
  53. beforeEach ->
  54. @request.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  55. @handler.flushProjectToMongo @project_id, @callback
  56. it "should return an error to the callback", ->
  57. @callback.calledWith(@error).should.equal true
  58. describe "when the document updater returns a failure error code", ->
  59. beforeEach ->
  60. @request.callsArgWith(1, null, { statusCode: 500 }, "")
  61. @handler.flushProjectToMongo @project_id, @callback
  62. it "should return the callback with an error", ->
  63. @callback
  64. .calledWith(new Error("doc updater returned failure status code: 500"))
  65. .should.equal true
  66. describe 'flushProjectToMongoAndDelete', ->
  67. describe "successfully", ->
  68. beforeEach ->
  69. @request.callsArgWith(1, null, {statusCode: 204}, "")
  70. @handler.flushProjectToMongoAndDelete @project_id, @callback
  71. it 'should delete the project from the document updater', ->
  72. @request.calledWithMatch(
  73. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  74. method: "DELETE"
  75. ).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.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  81. @handler.flushProjectToMongoAndDelete @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.callsArgWith(1, null, { statusCode: 500 }, "")
  87. @handler.flushProjectToMongoAndDelete @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 'flushDocToMongo', ->
  93. describe "successfully", ->
  94. beforeEach ->
  95. @request.callsArgWith(1, null, {statusCode: 204}, "")
  96. @handler.flushDocToMongo @project_id, @doc_id, @callback
  97. it 'should flush the document from the document updater', ->
  98. @request.calledWithMatch(
  99. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/flush"
  100. method: "POST"
  101. ).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.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  107. @handler.flushDocToMongo @project_id, @doc_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.callsArgWith(1, null, { statusCode: 500 }, "")
  113. @handler.flushDocToMongo @project_id, @doc_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 "deleteDoc", ->
  119. describe "successfully", ->
  120. beforeEach ->
  121. @request.callsArgWith(1, null, {statusCode: 204}, "")
  122. @handler.deleteDoc @project_id, @doc_id, @callback
  123. it 'should delete the document from the document updater', ->
  124. @request.calledWithMatch(
  125. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  126. method: "DELETE"
  127. ).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.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  133. @handler.deleteDoc @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.callsArgWith(1, null, { statusCode: 500 }, "")
  139. @handler.deleteDoc @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 "setDocument", ->
  145. beforeEach ->
  146. @source = "dropbox"
  147. describe "successfully", ->
  148. beforeEach ->
  149. @request.callsArgWith(1, null, {statusCode: 204}, "")
  150. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  151. it 'should set the document in the document updater', ->
  152. @request.calledWith(
  153. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}"
  154. json:
  155. lines: @lines
  156. source: @source
  157. user_id: @user_id
  158. method: "POST"
  159. ).should.equal true
  160. it "should call the callback with no error", ->
  161. @callback.calledWith(null).should.equal true
  162. describe "when the document updater API returns an error", ->
  163. beforeEach ->
  164. @request.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  165. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  166. it "should return an error to the callback", ->
  167. @callback.calledWith(@error).should.equal true
  168. describe "when the document updater returns a failure error code", ->
  169. beforeEach ->
  170. @request.callsArgWith(1, null, { statusCode: 500 }, "")
  171. @handler.setDocument @project_id, @doc_id, @user_id, @lines, @source, @callback
  172. it "should return the callback with an error", ->
  173. @callback
  174. .calledWith(new Error("doc updater returned failure status code: 500"))
  175. .should.equal true
  176. describe "getDocument", ->
  177. describe "successfully", ->
  178. beforeEach ->
  179. @body =
  180. lines: @lines
  181. version: @version
  182. ops: @ops = ["mock-op-1", "mock-op-2"]
  183. ranges: @ranges = {"mock":"ranges"}
  184. @fromVersion = 2
  185. @request.callsArgWith(1, null, {statusCode: 200}, @body)
  186. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  187. it 'should get the document from the document updater', ->
  188. @request.calledWith(
  189. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}?fromVersion=#{@fromVersion}"
  190. method: "GET"
  191. json: true
  192. ).should.equal true
  193. it "should call the callback with the lines and version", ->
  194. @callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true
  195. describe "when the document updater API returns an error", ->
  196. beforeEach ->
  197. @request.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  198. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  199. it "should return an error to the callback", ->
  200. @callback.calledWith(@error).should.equal true
  201. describe "when the document updater returns a failure error code", ->
  202. beforeEach ->
  203. @request.callsArgWith(1, null, { statusCode: 500 }, "")
  204. @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
  205. it "should return the callback with an error", ->
  206. @callback
  207. .calledWith(new Error("doc updater returned failure status code: 500"))
  208. .should.equal true
  209. describe "getProjectDocsIfMatch", ->
  210. beforeEach ->
  211. @project_state_hash = "1234567890abcdef"
  212. describe "successfully", ->
  213. beforeEach ->
  214. @doc0 =
  215. _id: @doc_id
  216. lines: @lines
  217. v: @version
  218. @docs = [ @doc0, @doc0, @doc0 ]
  219. @body = JSON.stringify @docs
  220. @request.post = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
  221. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  222. it 'should get the documents from the document updater', ->
  223. url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/get_and_flush_if_old?state=#{@project_state_hash}"
  224. @request.post.calledWith(url).should.equal true
  225. it "should call the callback with the documents", ->
  226. @callback.calledWithExactly(null, @docs).should.equal true
  227. describe "when the document updater API returns an error", ->
  228. beforeEach ->
  229. @request.post = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
  230. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  231. it "should return an error to the callback", ->
  232. @callback.calledWith(@error).should.equal true
  233. describe "when the document updater returns a conflict error code", ->
  234. beforeEach ->
  235. @request.post = sinon.stub().callsArgWith(1, null, { statusCode: 409 }, "Conflict")
  236. @handler.getProjectDocsIfMatch @project_id, @project_state_hash, @callback
  237. it "should return the callback with no documents", ->
  238. @callback
  239. .alwaysCalledWithExactly()
  240. .should.equal true
  241. describe "clearProjectState", ->
  242. describe "successfully", ->
  243. beforeEach ->
  244. @request.callsArgWith(1, null, {statusCode: 200})
  245. @handler.clearProjectState @project_id, @callback
  246. it 'should clear the project state from the document updater', ->
  247. @request.calledWithMatch(
  248. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/clearState"
  249. method: "POST"
  250. ).should.equal true
  251. it "should call the callback", ->
  252. @callback.calledWith(null).should.equal true
  253. describe "when the document updater API returns an error", ->
  254. beforeEach ->
  255. @request.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  256. @handler.clearProjectState @project_id, @callback
  257. it "should return an error to the callback", ->
  258. @callback.calledWith(@error).should.equal true
  259. describe "when the document updater returns an error code", ->
  260. beforeEach ->
  261. @request.callsArgWith(1, null, { statusCode: 500 }, null)
  262. @handler.clearProjectState @project_id, @callback
  263. it "should return the callback with no documents", ->
  264. @callback
  265. .calledWith(new Error("doc updater returned failure status code: 500"))
  266. .should.equal true
  267. describe "acceptChanges", ->
  268. beforeEach ->
  269. @change_id = "mock-change-id-1"
  270. describe "successfully", ->
  271. beforeEach ->
  272. @request.callsArgWith(1, null, {statusCode: 200}, @body)
  273. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  274. it 'should accept the change in the document updater', ->
  275. @request.calledWith(
  276. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/change/accept"
  277. json:
  278. change_ids: [ @change_id ]
  279. method: "POST"
  280. ).should.equal true
  281. it "should call the callback", ->
  282. @callback.calledWith(null).should.equal true
  283. describe "when the document updater API returns an error", ->
  284. beforeEach ->
  285. @request.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  286. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  287. it "should return an error to the callback", ->
  288. @callback.calledWith(@error).should.equal true
  289. describe "when the document updater returns a failure error code", ->
  290. beforeEach ->
  291. @request.callsArgWith(1, null, { statusCode: 500 }, "")
  292. @handler.acceptChanges @project_id, @doc_id, [ @change_id ], @callback
  293. it "should return the callback with an error", ->
  294. @callback
  295. .calledWith(new Error("doc updater returned failure status code: 500"))
  296. .should.equal true
  297. describe "deleteThread", ->
  298. beforeEach ->
  299. @thread_id = "mock-thread-id-1"
  300. describe "successfully", ->
  301. beforeEach ->
  302. @request.callsArgWith(1, null, {statusCode: 200}, @body)
  303. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  304. it 'should delete the thread in the document updater', ->
  305. @request.calledWithMatch(
  306. url: "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}/comment/#{@thread_id}"
  307. method: "DELETE"
  308. ).should.equal true
  309. it "should call the callback", ->
  310. @callback.calledWith(null).should.equal true
  311. describe "when the document updater API returns an error", ->
  312. beforeEach ->
  313. @request.callsArgWith(1, @error = new Error("something went wrong"), null, null)
  314. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  315. it "should return an error to the callback", ->
  316. @callback.calledWith(@error).should.equal true
  317. describe "when the document updater returns a failure error code", ->
  318. beforeEach ->
  319. @request.callsArgWith(1, null, { statusCode: 500 }, "")
  320. @handler.deleteThread @project_id, @doc_id, @thread_id, @callback
  321. it "should return the callback with an error", ->
  322. @callback
  323. .calledWith(new Error("doc updater returned failure status code: 500"))
  324. .should.equal true
  325. describe "updateProjectStructure ", ->
  326. beforeEach ->
  327. @user_id = 1234
  328. @version = 999
  329. @Project.findOne = sinon.stub().callsArgWith(2,null, {_id: @project_id, version:@version})
  330. describe "with project history disabled", ->
  331. beforeEach ->
  332. @settings.apis.project_history.sendProjectStructureOps = false
  333. @handler.updateProjectStructure @project_id, @projectHistoryId, @user_id, {}, @callback
  334. it 'does not make a web request', ->
  335. @request.called.should.equal false
  336. it 'calls the callback', ->
  337. @callback.called.should.equal true
  338. describe "with project history enabled", ->
  339. beforeEach ->
  340. @settings.apis.project_history.sendProjectStructureOps = true
  341. @url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
  342. @request.callsArgWith(1, null, {statusCode: 204}, "")
  343. describe "when an entity has changed name", ->
  344. it 'should send the structure update to the document updater', (done) ->
  345. @docIdA = new ObjectId()
  346. @docIdB = new ObjectId()
  347. @changes = {
  348. oldDocs: [
  349. { path: '/old_a', doc: _id: @docIdA }
  350. { path: '/old_b', doc: _id: @docIdB }
  351. ]
  352. # create new instances of the same ObjectIds so that == doesn't pass
  353. newDocs: [
  354. { path: '/old_a', doc: _id: new ObjectId(@docIdA.toString()) }
  355. { path: '/new_b', doc: _id: new ObjectId(@docIdB.toString()) }
  356. ]
  357. }
  358. docUpdates = [
  359. id: @docIdB.toString(),
  360. pathname: "/old_b"
  361. newPathname: "/new_b"
  362. ]
  363. @handler.updateProjectStructure @project_id, @projectHistoryId, @user_id, @changes, () =>
  364. @request.calledWith(
  365. url: @url,
  366. method: "POST"
  367. json: {docUpdates, fileUpdates: [], userId: @user_id, @version, @projectHistoryId}
  368. )
  369. .should.equal true
  370. done()
  371. describe "when a doc has been added", ->
  372. it 'should send the structure update to the document updater', (done) ->
  373. @docId = new ObjectId()
  374. @changes = newDocs: [
  375. { path: '/foo', docLines: 'a\nb', doc: _id: @docId }
  376. ]
  377. docUpdates = [
  378. id: @docId.toString(),
  379. pathname: "/foo"
  380. docLines: 'a\nb'
  381. url: undefined
  382. ]
  383. @handler.updateProjectStructure @project_id, @projectHistoryId, @user_id, @changes, () =>
  384. @request.calledWith(
  385. url: @url
  386. method: "POST"
  387. json: {docUpdates, fileUpdates: [], userId: @user_id, @version, @projectHistoryId}
  388. ).should.equal true
  389. done()
  390. describe "when a file has been added", ->
  391. it 'should send the structure update to the document updater', (done) ->
  392. @fileId = new ObjectId()
  393. @changes = newFiles: [
  394. { path: '/bar', url: 'filestore.example.com/file', file: _id: @fileId }
  395. ]
  396. fileUpdates = [
  397. id: @fileId.toString(),
  398. pathname: "/bar"
  399. url: 'filestore.example.com/file'
  400. docLines: undefined
  401. ]
  402. @handler.updateProjectStructure @project_id, @projectHistoryId, @user_id, @changes, () =>
  403. @request.calledWith(
  404. url: @url
  405. method: "POST"
  406. json: {docUpdates: [], fileUpdates, userId: @user_id, @version, @projectHistoryId}
  407. ).should.equal true
  408. done()
  409. describe "when an entity has been deleted", ->
  410. it 'should end the structure update to the document updater', (done) ->
  411. @docId = new ObjectId()
  412. @changes = oldDocs: [
  413. { path: '/foo', docLines: 'a\nb', doc: _id: @docId }
  414. ]
  415. docUpdates = [
  416. id: @docId.toString(),
  417. pathname: '/foo',
  418. newPathname: ''
  419. ]
  420. @handler.updateProjectStructure @project_id, @projectHistoryId, @user_id, @changes, () =>
  421. @request.calledWith(
  422. url: @url
  423. method: "POST"
  424. json: {docUpdates, fileUpdates: [], userId: @user_id, @version, @projectHistoryId}
  425. ).should.equal true
  426. done()