DocumentUpdaterHandlerTests.js 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. const sinon = require('sinon')
  2. const SandboxedModule = require('sandboxed-module')
  3. const path = require('path')
  4. const { ObjectId } = require('mongodb')
  5. const modulePath = path.join(
  6. __dirname,
  7. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler'
  8. )
  9. describe('DocumentUpdaterHandler', function () {
  10. beforeEach(function () {
  11. this.project_id = 'project-id-923'
  12. this.projectHistoryId = 'ol-project-id-1'
  13. this.doc_id = 'doc-id-394'
  14. this.lines = ['one', 'two', 'three']
  15. this.version = 42
  16. this.user_id = 'mock-user-id-123'
  17. this.project = { _id: this.project_id }
  18. this.request = sinon.stub()
  19. this.projectEntityHandler = {}
  20. this.settings = {
  21. apis: {
  22. documentupdater: {
  23. url: 'http://document_updater.example.com',
  24. },
  25. project_history: {
  26. url: 'http://project_history.example.com',
  27. },
  28. },
  29. }
  30. this.source = 'dropbox'
  31. this.callback = sinon.stub()
  32. this.handler = SandboxedModule.require(modulePath, {
  33. requires: {
  34. request: {
  35. defaults: () => {
  36. return this.request
  37. },
  38. },
  39. '@overleaf/settings': this.settings,
  40. '../Project/ProjectEntityHandler': this.projectEntityHandler,
  41. '../../models/Project': {
  42. Project: (this.Project = {}),
  43. },
  44. '../../Features/Project/ProjectLocator': {},
  45. '@overleaf/metrics': {
  46. Timer: class {
  47. done() {}
  48. },
  49. },
  50. },
  51. })
  52. })
  53. describe('flushProjectToMongo', function () {
  54. describe('successfully', function () {
  55. beforeEach(function () {
  56. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  57. this.handler.flushProjectToMongo(this.project_id, this.callback)
  58. })
  59. it('should flush the document from the document updater', function () {
  60. this.request
  61. .calledWithMatch({
  62. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/flush`,
  63. method: 'POST',
  64. })
  65. .should.equal(true)
  66. })
  67. it('should call the callback with no error', function () {
  68. this.callback.calledWith(null).should.equal(true)
  69. })
  70. })
  71. describe('when the document updater API returns an error', function () {
  72. beforeEach(function () {
  73. this.request.callsArgWith(
  74. 1,
  75. new Error('something went wrong'),
  76. null,
  77. null
  78. )
  79. this.handler.flushProjectToMongo(this.project_id, this.callback)
  80. })
  81. it('should return an error to the callback', function () {
  82. this.callback
  83. .calledWith(sinon.match.instanceOf(Error))
  84. .should.equal(true)
  85. })
  86. })
  87. describe('when the document updater returns a failure error code', function () {
  88. beforeEach(function () {
  89. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  90. this.handler.flushProjectToMongo(this.project_id, this.callback)
  91. })
  92. it('should return the callback with an error', function () {
  93. this.callback
  94. .calledWith(
  95. sinon.match
  96. .instanceOf(Error)
  97. .and(
  98. sinon.match.has(
  99. 'message',
  100. 'document updater returned a failure status code: 500'
  101. )
  102. )
  103. )
  104. .should.equal(true)
  105. })
  106. })
  107. })
  108. describe('flushProjectToMongoAndDelete', function () {
  109. describe('successfully', function () {
  110. beforeEach(function () {
  111. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  112. this.handler.flushProjectToMongoAndDelete(
  113. this.project_id,
  114. this.callback
  115. )
  116. })
  117. it('should delete the project from the document updater', function () {
  118. this.request
  119. .calledWithMatch({
  120. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}`,
  121. method: 'DELETE',
  122. })
  123. .should.equal(true)
  124. })
  125. it('should call the callback with no error', function () {
  126. this.callback.calledWith(null).should.equal(true)
  127. })
  128. })
  129. describe('when the document updater API returns an error', function () {
  130. beforeEach(function () {
  131. this.request.callsArgWith(
  132. 1,
  133. new Error('something went wrong'),
  134. null,
  135. null
  136. )
  137. this.handler.flushProjectToMongoAndDelete(
  138. this.project_id,
  139. this.callback
  140. )
  141. })
  142. it('should return an error to the callback', function () {
  143. this.callback
  144. .calledWith(sinon.match.instanceOf(Error))
  145. .should.equal(true)
  146. })
  147. })
  148. describe('when the document updater returns a failure error code', function () {
  149. beforeEach(function () {
  150. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  151. this.handler.flushProjectToMongoAndDelete(
  152. this.project_id,
  153. this.callback
  154. )
  155. })
  156. it('should return the callback with an error', function () {
  157. this.callback
  158. .calledWith(
  159. sinon.match
  160. .instanceOf(Error)
  161. .and(
  162. sinon.match.has(
  163. 'message',
  164. 'document updater returned a failure status code: 500'
  165. )
  166. )
  167. )
  168. .should.equal(true)
  169. })
  170. })
  171. })
  172. describe('flushDocToMongo', function () {
  173. describe('successfully', function () {
  174. beforeEach(function () {
  175. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  176. this.handler.flushDocToMongo(
  177. this.project_id,
  178. this.doc_id,
  179. this.callback
  180. )
  181. })
  182. it('should flush the document from the document updater', function () {
  183. this.request
  184. .calledWithMatch({
  185. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}/flush`,
  186. method: 'POST',
  187. })
  188. .should.equal(true)
  189. })
  190. it('should call the callback with no error', function () {
  191. this.callback.calledWith(null).should.equal(true)
  192. })
  193. })
  194. describe('when the document updater API returns an error', function () {
  195. beforeEach(function () {
  196. this.request.callsArgWith(
  197. 1,
  198. new Error('something went wrong'),
  199. null,
  200. null
  201. )
  202. this.handler.flushDocToMongo(
  203. this.project_id,
  204. this.doc_id,
  205. this.callback
  206. )
  207. })
  208. it('should return an error to the callback', function () {
  209. this.callback
  210. .calledWith(sinon.match.instanceOf(Error))
  211. .should.equal(true)
  212. })
  213. })
  214. describe('when the document updater returns a failure error code', function () {
  215. beforeEach(function () {
  216. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  217. this.handler.flushDocToMongo(
  218. this.project_id,
  219. this.doc_id,
  220. this.callback
  221. )
  222. })
  223. it('should return the callback with an error', function () {
  224. this.callback
  225. .calledWith(
  226. sinon.match
  227. .instanceOf(Error)
  228. .and(
  229. sinon.match.has(
  230. 'message',
  231. 'document updater returned a failure status code: 500'
  232. )
  233. )
  234. )
  235. .should.equal(true)
  236. })
  237. })
  238. })
  239. describe('deleteDoc', function () {
  240. describe('successfully', function () {
  241. beforeEach(function () {
  242. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  243. this.handler.deleteDoc(this.project_id, this.doc_id, this.callback)
  244. })
  245. it('should delete the document from the document updater', function () {
  246. this.request
  247. .calledWithMatch({
  248. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`,
  249. method: 'DELETE',
  250. })
  251. .should.equal(true)
  252. })
  253. it('should call the callback with no error', function () {
  254. this.callback.calledWith(null).should.equal(true)
  255. })
  256. })
  257. describe('when the document updater API returns an error', function () {
  258. beforeEach(function () {
  259. this.request.callsArgWith(
  260. 1,
  261. new Error('something went wrong'),
  262. null,
  263. null
  264. )
  265. this.handler.deleteDoc(this.project_id, this.doc_id, this.callback)
  266. })
  267. it('should return an error to the callback', function () {
  268. this.callback
  269. .calledWith(sinon.match.instanceOf(Error))
  270. .should.equal(true)
  271. })
  272. })
  273. describe('when the document updater returns a failure error code', function () {
  274. beforeEach(function () {
  275. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  276. this.handler.deleteDoc(this.project_id, this.doc_id, this.callback)
  277. })
  278. it('should return the callback with an error', function () {
  279. this.callback
  280. .calledWith(
  281. sinon.match
  282. .instanceOf(Error)
  283. .and(
  284. sinon.match.has(
  285. 'message',
  286. 'document updater returned a failure status code: 500'
  287. )
  288. )
  289. )
  290. .should.equal(true)
  291. })
  292. })
  293. describe("with 'ignoreFlushErrors' option", function () {
  294. beforeEach(function () {
  295. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  296. })
  297. it('when option is true, should send a `ignore_flush_errors=true` URL query to document-updater', function () {
  298. this.handler.deleteDoc(
  299. this.project_id,
  300. this.doc_id,
  301. true,
  302. this.callback
  303. )
  304. this.request
  305. .calledWithMatch({
  306. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}?ignore_flush_errors=true`,
  307. method: 'DELETE',
  308. })
  309. .should.equal(true)
  310. })
  311. it("when option is false, shouldn't send any URL query to document-updater", function () {
  312. this.handler.deleteDoc(
  313. this.project_id,
  314. this.doc_id,
  315. false,
  316. this.callback
  317. )
  318. this.request
  319. .calledWithMatch({
  320. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`,
  321. method: 'DELETE',
  322. })
  323. .should.equal(true)
  324. })
  325. })
  326. })
  327. describe('setDocument', function () {
  328. describe('successfully', function () {
  329. beforeEach(function () {
  330. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  331. this.handler.setDocument(
  332. this.project_id,
  333. this.doc_id,
  334. this.user_id,
  335. this.lines,
  336. this.source,
  337. this.callback
  338. )
  339. })
  340. it('should set the document in the document updater', function () {
  341. this.request
  342. .calledWith({
  343. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}`,
  344. json: {
  345. lines: this.lines,
  346. source: this.source,
  347. user_id: this.user_id,
  348. },
  349. method: 'POST',
  350. timeout: 30 * 1000,
  351. })
  352. .should.equal(true)
  353. })
  354. it('should call the callback with no error', function () {
  355. this.callback.calledWith(null).should.equal(true)
  356. })
  357. })
  358. describe('when the document updater API returns an error', function () {
  359. beforeEach(function () {
  360. this.request.callsArgWith(
  361. 1,
  362. new Error('something went wrong'),
  363. null,
  364. null
  365. )
  366. this.handler.setDocument(
  367. this.project_id,
  368. this.doc_id,
  369. this.user_id,
  370. this.lines,
  371. this.source,
  372. this.callback
  373. )
  374. })
  375. it('should return an error to the callback', function () {
  376. this.callback
  377. .calledWith(sinon.match.instanceOf(Error))
  378. .should.equal(true)
  379. })
  380. })
  381. describe('when the document updater returns a failure error code', function () {
  382. beforeEach(function () {
  383. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  384. this.handler.setDocument(
  385. this.project_id,
  386. this.doc_id,
  387. this.user_id,
  388. this.lines,
  389. this.source,
  390. this.callback
  391. )
  392. })
  393. it('should return the callback with an error', function () {
  394. this.callback
  395. .calledWith(
  396. sinon.match
  397. .instanceOf(Error)
  398. .and(
  399. sinon.match.has(
  400. 'message',
  401. 'document updater returned a failure status code: 500'
  402. )
  403. )
  404. )
  405. .should.equal(true)
  406. })
  407. })
  408. })
  409. describe('getDocument', function () {
  410. describe('successfully', function () {
  411. beforeEach(function () {
  412. this.body = {
  413. lines: this.lines,
  414. version: this.version,
  415. ops: (this.ops = ['mock-op-1', 'mock-op-2']),
  416. ranges: (this.ranges = { mock: 'ranges' }),
  417. }
  418. this.fromVersion = 2
  419. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  420. this.handler.getDocument(
  421. this.project_id,
  422. this.doc_id,
  423. this.fromVersion,
  424. this.callback
  425. )
  426. })
  427. it('should get the document from the document updater', function () {
  428. this.request
  429. .calledWith({
  430. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}?fromVersion=${this.fromVersion}`,
  431. method: 'GET',
  432. json: true,
  433. timeout: 30 * 1000,
  434. })
  435. .should.equal(true)
  436. })
  437. it('should call the callback with the lines and version', function () {
  438. this.callback
  439. .calledWith(null, this.lines, this.version, this.ranges, this.ops)
  440. .should.equal(true)
  441. })
  442. })
  443. describe('when the document updater API returns an error', function () {
  444. beforeEach(function () {
  445. this.request.callsArgWith(
  446. 1,
  447. new Error('something went wrong'),
  448. null,
  449. null
  450. )
  451. this.handler.getDocument(
  452. this.project_id,
  453. this.doc_id,
  454. this.fromVersion,
  455. this.callback
  456. )
  457. })
  458. it('should return an error to the callback', function () {
  459. this.callback
  460. .calledWith(sinon.match.instanceOf(Error))
  461. .should.equal(true)
  462. })
  463. })
  464. describe('when the document updater returns a failure error code', function () {
  465. beforeEach(function () {
  466. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  467. this.handler.getDocument(
  468. this.project_id,
  469. this.doc_id,
  470. this.fromVersion,
  471. this.callback
  472. )
  473. })
  474. it('should return the callback with an error', function () {
  475. this.callback
  476. .calledWith(
  477. sinon.match
  478. .instanceOf(Error)
  479. .and(
  480. sinon.match.has(
  481. 'message',
  482. 'document updater returned a failure status code: 500'
  483. )
  484. )
  485. )
  486. .should.equal(true)
  487. })
  488. })
  489. })
  490. describe('getProjectDocsIfMatch', function () {
  491. beforeEach(function () {
  492. this.project_state_hash = '1234567890abcdef'
  493. })
  494. describe('successfully', function () {
  495. beforeEach(function () {
  496. this.doc0 = {
  497. _id: this.doc_id,
  498. lines: this.lines,
  499. v: this.version,
  500. }
  501. this.docs = [this.doc0, this.doc0, this.doc0]
  502. this.body = JSON.stringify(this.docs)
  503. this.request.post = sinon
  504. .stub()
  505. .callsArgWith(1, null, { statusCode: 200 }, this.body)
  506. this.handler.getProjectDocsIfMatch(
  507. this.project_id,
  508. this.project_state_hash,
  509. this.callback
  510. )
  511. })
  512. it('should get the documents from the document updater', function () {
  513. const url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}/get_and_flush_if_old?state=${this.project_state_hash}`
  514. this.request.post.calledWith(url).should.equal(true)
  515. })
  516. it('should call the callback with the documents', function () {
  517. this.callback.calledWithExactly(null, this.docs).should.equal(true)
  518. })
  519. })
  520. describe('when the document updater API returns an error', function () {
  521. beforeEach(function () {
  522. this.request.post = sinon
  523. .stub()
  524. .callsArgWith(1, new Error('something went wrong'), null, null)
  525. this.handler.getProjectDocsIfMatch(
  526. this.project_id,
  527. this.project_state_hash,
  528. this.callback
  529. )
  530. })
  531. it('should return an error to the callback', function () {
  532. this.callback
  533. .calledWith(sinon.match.instanceOf(Error))
  534. .should.equal(true)
  535. })
  536. })
  537. describe('when the document updater returns a conflict error code', function () {
  538. beforeEach(function () {
  539. this.request.post = sinon
  540. .stub()
  541. .callsArgWith(1, null, { statusCode: 409 }, 'Conflict')
  542. this.handler.getProjectDocsIfMatch(
  543. this.project_id,
  544. this.project_state_hash,
  545. this.callback
  546. )
  547. })
  548. it('should return the callback with no documents', function () {
  549. this.callback.alwaysCalledWithExactly().should.equal(true)
  550. })
  551. })
  552. })
  553. describe('clearProjectState', function () {
  554. describe('successfully', function () {
  555. beforeEach(function () {
  556. this.request.callsArgWith(1, null, { statusCode: 200 })
  557. this.handler.clearProjectState(this.project_id, this.callback)
  558. })
  559. it('should clear the project state from the document updater', function () {
  560. this.request
  561. .calledWithMatch({
  562. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/clearState`,
  563. method: 'POST',
  564. })
  565. .should.equal(true)
  566. })
  567. it('should call the callback', function () {
  568. this.callback.calledWith(null).should.equal(true)
  569. })
  570. })
  571. describe('when the document updater API returns an error', function () {
  572. beforeEach(function () {
  573. this.request.callsArgWith(
  574. 1,
  575. new Error('something went wrong'),
  576. null,
  577. null
  578. )
  579. this.handler.clearProjectState(this.project_id, this.callback)
  580. })
  581. it('should return an error to the callback', function () {
  582. this.callback
  583. .calledWith(sinon.match.instanceOf(Error))
  584. .should.equal(true)
  585. })
  586. })
  587. describe('when the document updater returns an error code', function () {
  588. beforeEach(function () {
  589. this.request.callsArgWith(1, null, { statusCode: 500 }, null)
  590. this.handler.clearProjectState(this.project_id, this.callback)
  591. })
  592. it('should return the callback with no documents', function () {
  593. this.callback
  594. .calledWith(
  595. sinon.match
  596. .instanceOf(Error)
  597. .and(
  598. sinon.match.has(
  599. 'message',
  600. 'document updater returned a failure status code: 500'
  601. )
  602. )
  603. )
  604. .should.equal(true)
  605. })
  606. })
  607. })
  608. describe('acceptChanges', function () {
  609. beforeEach(function () {
  610. this.change_id = 'mock-change-id-1'
  611. })
  612. describe('successfully', function () {
  613. beforeEach(function () {
  614. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  615. this.handler.acceptChanges(
  616. this.project_id,
  617. this.doc_id,
  618. [this.change_id],
  619. this.callback
  620. )
  621. })
  622. it('should accept the change in the document updater', function () {
  623. this.request
  624. .calledWith({
  625. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}/change/accept`,
  626. json: {
  627. change_ids: [this.change_id],
  628. },
  629. method: 'POST',
  630. timeout: 30 * 1000,
  631. })
  632. .should.equal(true)
  633. })
  634. it('should call the callback', function () {
  635. this.callback.calledWith(null).should.equal(true)
  636. })
  637. })
  638. describe('when the document updater API returns an error', function () {
  639. beforeEach(function () {
  640. this.request.callsArgWith(
  641. 1,
  642. new Error('something went wrong'),
  643. null,
  644. null
  645. )
  646. this.handler.acceptChanges(
  647. this.project_id,
  648. this.doc_id,
  649. [this.change_id],
  650. this.callback
  651. )
  652. })
  653. it('should return an error to the callback', function () {
  654. this.callback
  655. .calledWith(sinon.match.instanceOf(Error))
  656. .should.equal(true)
  657. })
  658. })
  659. describe('when the document updater returns a failure error code', function () {
  660. beforeEach(function () {
  661. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  662. this.handler.acceptChanges(
  663. this.project_id,
  664. this.doc_id,
  665. [this.change_id],
  666. this.callback
  667. )
  668. })
  669. it('should return the callback with an error', function () {
  670. this.callback
  671. .calledWith(
  672. sinon.match
  673. .instanceOf(Error)
  674. .and(
  675. sinon.match.has(
  676. 'message',
  677. 'document updater returned a failure status code: 500'
  678. )
  679. )
  680. )
  681. .should.equal(true)
  682. })
  683. })
  684. })
  685. describe('deleteThread', function () {
  686. beforeEach(function () {
  687. this.thread_id = 'mock-thread-id-1'
  688. })
  689. describe('successfully', function () {
  690. beforeEach(function () {
  691. this.request.callsArgWith(1, null, { statusCode: 200 }, this.body)
  692. this.handler.deleteThread(
  693. this.project_id,
  694. this.doc_id,
  695. this.thread_id,
  696. this.callback
  697. )
  698. })
  699. it('should delete the thread in the document updater', function () {
  700. this.request
  701. .calledWithMatch({
  702. url: `${this.settings.apis.documentupdater.url}/project/${this.project_id}/doc/${this.doc_id}/comment/${this.thread_id}`,
  703. method: 'DELETE',
  704. })
  705. .should.equal(true)
  706. })
  707. it('should call the callback', function () {
  708. this.callback.calledWith(null).should.equal(true)
  709. })
  710. })
  711. describe('when the document updater API returns an error', function () {
  712. beforeEach(function () {
  713. this.request.callsArgWith(
  714. 1,
  715. new Error('something went wrong'),
  716. null,
  717. null
  718. )
  719. this.handler.deleteThread(
  720. this.project_id,
  721. this.doc_id,
  722. this.thread_id,
  723. this.callback
  724. )
  725. })
  726. it('should return an error to the callback', function () {
  727. this.callback
  728. .calledWith(sinon.match.instanceOf(Error))
  729. .should.equal(true)
  730. })
  731. })
  732. describe('when the document updater returns a failure error code', function () {
  733. beforeEach(function () {
  734. this.request.callsArgWith(1, null, { statusCode: 500 }, '')
  735. this.handler.deleteThread(
  736. this.project_id,
  737. this.doc_id,
  738. this.thread_id,
  739. this.callback
  740. )
  741. })
  742. it('should return the callback with an error', function () {
  743. this.callback
  744. .calledWith(
  745. sinon.match
  746. .instanceOf(Error)
  747. .and(
  748. sinon.match.has(
  749. 'message',
  750. 'document updater returned a failure status code: 500'
  751. )
  752. )
  753. )
  754. .should.equal(true)
  755. })
  756. })
  757. })
  758. describe('updateProjectStructure ', function () {
  759. beforeEach(function () {
  760. this.user_id = 1234
  761. this.version = 999
  762. })
  763. describe('with project history disabled', function () {
  764. beforeEach(function () {
  765. this.settings.apis.project_history.sendProjectStructureOps = false
  766. this.handler.updateProjectStructure(
  767. this.project_id,
  768. this.projectHistoryId,
  769. this.user_id,
  770. {},
  771. this.source,
  772. this.callback
  773. )
  774. })
  775. it('does not make a web request', function () {
  776. this.request.called.should.equal(false)
  777. })
  778. it('calls the callback', function () {
  779. this.callback.called.should.equal(true)
  780. })
  781. })
  782. describe('with project history enabled', function () {
  783. beforeEach(function () {
  784. this.settings.apis.project_history.sendProjectStructureOps = true
  785. this.url = `${this.settings.apis.documentupdater.url}/project/${this.project_id}`
  786. this.request.callsArgWith(1, null, { statusCode: 204 }, '')
  787. })
  788. describe('when an entity has changed name', function () {
  789. it('should send the structure update to the document updater', function (done) {
  790. this.docIdA = new ObjectId()
  791. this.docIdB = new ObjectId()
  792. this.changes = {
  793. oldDocs: [
  794. { path: '/old_a', doc: { _id: this.docIdA } },
  795. { path: '/old_b', doc: { _id: this.docIdB } },
  796. ],
  797. // create new instances of the same ObjectIds so that == doesn't pass
  798. newDocs: [
  799. {
  800. path: '/old_a',
  801. doc: { _id: new ObjectId(this.docIdA.toString()) },
  802. },
  803. {
  804. path: '/new_b',
  805. doc: { _id: new ObjectId(this.docIdB.toString()) },
  806. },
  807. ],
  808. newProject: { version: this.version },
  809. }
  810. const updates = [
  811. {
  812. type: 'rename-doc',
  813. id: this.docIdB.toString(),
  814. pathname: '/old_b',
  815. newPathname: '/new_b',
  816. },
  817. ]
  818. this.handler.updateProjectStructure(
  819. this.project_id,
  820. this.projectHistoryId,
  821. this.user_id,
  822. this.changes,
  823. this.source,
  824. () => {
  825. this.request
  826. .calledWith({
  827. url: this.url,
  828. method: 'POST',
  829. json: {
  830. updates,
  831. userId: this.user_id,
  832. version: this.version,
  833. projectHistoryId: this.projectHistoryId,
  834. source: this.source,
  835. },
  836. timeout: 30 * 1000,
  837. })
  838. .should.equal(true)
  839. done()
  840. }
  841. )
  842. })
  843. })
  844. describe('when a doc has been added', function () {
  845. it('should send the structure update to the document updater', function (done) {
  846. this.docId = new ObjectId()
  847. this.changes = {
  848. newDocs: [
  849. { path: '/foo', docLines: 'a\nb', doc: { _id: this.docId } },
  850. ],
  851. newProject: { version: this.version },
  852. }
  853. const updates = [
  854. {
  855. type: 'add-doc',
  856. id: this.docId.toString(),
  857. pathname: '/foo',
  858. docLines: 'a\nb',
  859. url: undefined,
  860. hash: undefined,
  861. },
  862. ]
  863. this.handler.updateProjectStructure(
  864. this.project_id,
  865. this.projectHistoryId,
  866. this.user_id,
  867. this.changes,
  868. this.source,
  869. () => {
  870. this.request
  871. .calledWith({
  872. url: this.url,
  873. method: 'POST',
  874. json: {
  875. updates,
  876. userId: this.user_id,
  877. version: this.version,
  878. projectHistoryId: this.projectHistoryId,
  879. source: this.source,
  880. },
  881. timeout: 30 * 1000,
  882. })
  883. .should.equal(true)
  884. done()
  885. }
  886. )
  887. })
  888. })
  889. describe('when a file has been added', function () {
  890. it('should send the structure update to the document updater', function (done) {
  891. this.fileId = new ObjectId()
  892. this.changes = {
  893. newFiles: [
  894. {
  895. path: '/bar',
  896. url: 'filestore.example.com/file',
  897. file: { _id: this.fileId, hash: '12345' },
  898. },
  899. ],
  900. newProject: { version: this.version },
  901. }
  902. const updates = [
  903. {
  904. type: 'add-file',
  905. id: this.fileId.toString(),
  906. pathname: '/bar',
  907. url: 'filestore.example.com/file',
  908. docLines: undefined,
  909. hash: '12345',
  910. },
  911. ]
  912. this.handler.updateProjectStructure(
  913. this.project_id,
  914. this.projectHistoryId,
  915. this.user_id,
  916. this.changes,
  917. this.source,
  918. () => {
  919. this.request
  920. .calledWith({
  921. url: this.url,
  922. method: 'POST',
  923. json: {
  924. updates,
  925. userId: this.user_id,
  926. version: this.version,
  927. projectHistoryId: this.projectHistoryId,
  928. source: this.source,
  929. },
  930. timeout: 30 * 1000,
  931. })
  932. .should.equal(true)
  933. done()
  934. }
  935. )
  936. })
  937. })
  938. describe('when an entity has been deleted', function () {
  939. it('should end the structure update to the document updater', function (done) {
  940. this.docId = new ObjectId()
  941. this.changes = {
  942. oldDocs: [
  943. { path: '/foo', docLines: 'a\nb', doc: { _id: this.docId } },
  944. ],
  945. newProject: { version: this.version },
  946. }
  947. const updates = [
  948. {
  949. type: 'rename-doc',
  950. id: this.docId.toString(),
  951. pathname: '/foo',
  952. newPathname: '',
  953. },
  954. ]
  955. this.handler.updateProjectStructure(
  956. this.project_id,
  957. this.projectHistoryId,
  958. this.user_id,
  959. this.changes,
  960. this.source,
  961. () => {
  962. this.request
  963. .calledWith({
  964. url: this.url,
  965. method: 'POST',
  966. json: {
  967. updates,
  968. userId: this.user_id,
  969. version: this.version,
  970. projectHistoryId: this.projectHistoryId,
  971. source: this.source,
  972. },
  973. timeout: 30 * 1000,
  974. })
  975. .should.equal(true)
  976. done()
  977. }
  978. )
  979. })
  980. })
  981. describe('when a file is converted to a doc', function () {
  982. it('should send the delete first', function (done) {
  983. this.docId = new ObjectId()
  984. this.fileId = new ObjectId()
  985. this.changes = {
  986. oldFiles: [
  987. {
  988. path: '/foo.doc',
  989. url: 'filestore.example.com/file',
  990. file: { _id: this.fileId },
  991. },
  992. ],
  993. newDocs: [
  994. {
  995. path: '/foo.doc',
  996. docLines: 'hello there',
  997. doc: { _id: this.docId },
  998. },
  999. ],
  1000. newProject: { version: this.version },
  1001. }
  1002. const updates = [
  1003. {
  1004. type: 'rename-file',
  1005. id: this.fileId.toString(),
  1006. pathname: '/foo.doc',
  1007. newPathname: '',
  1008. },
  1009. {
  1010. type: 'add-doc',
  1011. id: this.docId.toString(),
  1012. pathname: '/foo.doc',
  1013. docLines: 'hello there',
  1014. url: undefined,
  1015. hash: undefined,
  1016. },
  1017. ]
  1018. this.handler.updateProjectStructure(
  1019. this.project_id,
  1020. this.projectHistoryId,
  1021. this.user_id,
  1022. this.changes,
  1023. this.source,
  1024. () => {
  1025. this.request.should.have.been.calledWith({
  1026. url: this.url,
  1027. method: 'POST',
  1028. json: {
  1029. updates,
  1030. userId: this.user_id,
  1031. version: this.version,
  1032. projectHistoryId: this.projectHistoryId,
  1033. source: this.source,
  1034. },
  1035. timeout: 30 * 1000,
  1036. })
  1037. done()
  1038. }
  1039. )
  1040. })
  1041. })
  1042. describe('when the project version is missing', function () {
  1043. it('should call the callback with an error', function () {
  1044. this.docId = new ObjectId()
  1045. this.changes = {
  1046. oldDocs: [
  1047. { path: '/foo', docLines: 'a\nb', doc: { _id: this.docId } },
  1048. ],
  1049. }
  1050. this.handler.updateProjectStructure(
  1051. this.project_id,
  1052. this.projectHistoryId,
  1053. this.user_id,
  1054. this.changes,
  1055. this.source,
  1056. this.callback
  1057. )
  1058. this.callback
  1059. .calledWith(sinon.match.instanceOf(Error))
  1060. .should.equal(true)
  1061. const firstCallArgs = this.callback.args[0]
  1062. firstCallArgs[0].message.should.equal(
  1063. 'did not receive project version in changes'
  1064. )
  1065. })
  1066. })
  1067. })
  1068. })
  1069. })