DocumentUpdaterHandlerTests.coffee 18 KB

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