HttpControllerTests.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. const sinon = require('sinon')
  2. const modulePath = '../../../../app/js/HttpController.js'
  3. const SandboxedModule = require('sandboxed-module')
  4. const Errors = require('../../../../app/js/Errors.js')
  5. describe('HttpController', function () {
  6. beforeEach(function () {
  7. this.HttpController = SandboxedModule.require(modulePath, {
  8. requires: {
  9. './DocumentManager': (this.DocumentManager = {}),
  10. './HistoryManager': (this.HistoryManager = {
  11. flushProjectChangesAsync: sinon.stub(),
  12. }),
  13. './ProjectHistoryRedisManager': (this.ProjectHistoryRedisManager = {}),
  14. './ProjectManager': (this.ProjectManager = {}),
  15. './DeleteQueueManager': (this.DeleteQueueManager = {}),
  16. './RedisManager': (this.RedisManager = {
  17. DOC_OPS_TTL: 42,
  18. }),
  19. './Metrics': (this.Metrics = {}),
  20. './Errors': Errors,
  21. './Utils': (this.Utils = {
  22. addTrackedDeletesToContent: sinon.stub().returnsArg(0),
  23. }),
  24. './HistoryConversions': (this.HistoryConversions = {
  25. toHistoryRanges: sinon.stub().returnsArg(0),
  26. }),
  27. '@overleaf/settings': { max_doc_length: 2 * 1024 * 1024 },
  28. },
  29. })
  30. this.Metrics.Timer = class Timer {}
  31. this.Metrics.Timer.prototype.done = sinon.stub()
  32. this.project_id = 'project-id-123'
  33. this.projectHistoryId = '123'
  34. this.doc_id = 'doc-id-123'
  35. this.source = 'editor'
  36. this.next = sinon.stub()
  37. this.res = {
  38. send: sinon.stub(),
  39. sendStatus: sinon.stub(),
  40. json: sinon.stub(),
  41. }
  42. })
  43. describe('getDoc', function () {
  44. beforeEach(function () {
  45. this.lines = ['one', 'two', 'three']
  46. this.ops = ['mock-op-1', 'mock-op-2']
  47. this.version = 42
  48. this.fromVersion = 42
  49. this.ranges = { changes: 'mock', comments: 'mock' }
  50. this.pathname = '/a/b/c'
  51. this.req = {
  52. params: {
  53. project_id: this.project_id,
  54. doc_id: this.doc_id,
  55. },
  56. query: {},
  57. body: {},
  58. }
  59. })
  60. describe('when the document exists and no recent ops are requested', function () {
  61. beforeEach(function () {
  62. this.DocumentManager.getDocAndRecentOpsWithLock = sinon
  63. .stub()
  64. .callsArgWith(
  65. 3,
  66. null,
  67. this.lines,
  68. this.version,
  69. [],
  70. this.ranges,
  71. this.pathname,
  72. this.projectHistoryId,
  73. 'sharejs-text-ot'
  74. )
  75. this.HttpController.getDoc(this.req, this.res, this.next)
  76. })
  77. it('should get the doc', function () {
  78. this.DocumentManager.getDocAndRecentOpsWithLock
  79. .calledWith(this.project_id, this.doc_id, -1)
  80. .should.equal(true)
  81. })
  82. it('should return the doc as JSON', function () {
  83. this.res.json.should.have.been.calledWith({
  84. id: this.doc_id,
  85. lines: this.lines,
  86. version: this.version,
  87. ops: [],
  88. ranges: this.ranges,
  89. pathname: this.pathname,
  90. ttlInS: 42,
  91. type: 'sharejs-text-ot',
  92. })
  93. })
  94. it('should log the request', function () {
  95. this.logger.debug
  96. .calledWith(
  97. {
  98. docId: this.doc_id,
  99. projectId: this.project_id,
  100. historyRanges: false,
  101. },
  102. 'getting doc via http'
  103. )
  104. .should.equal(true)
  105. })
  106. it('should time the request', function () {
  107. this.Metrics.Timer.prototype.done.called.should.equal(true)
  108. })
  109. })
  110. describe('when recent ops are requested', function () {
  111. beforeEach(function () {
  112. this.DocumentManager.getDocAndRecentOpsWithLock = sinon
  113. .stub()
  114. .callsArgWith(
  115. 3,
  116. null,
  117. this.lines,
  118. this.version,
  119. this.ops,
  120. this.ranges,
  121. this.pathname,
  122. this.projectHistoryId,
  123. 'sharejs-text-ot'
  124. )
  125. this.req.query = { fromVersion: `${this.fromVersion}` }
  126. this.HttpController.getDoc(this.req, this.res, this.next)
  127. })
  128. it('should get the doc', function () {
  129. this.DocumentManager.getDocAndRecentOpsWithLock
  130. .calledWith(this.project_id, this.doc_id, this.fromVersion)
  131. .should.equal(true)
  132. })
  133. it('should return the doc as JSON', function () {
  134. this.res.json.should.have.been.calledWith({
  135. id: this.doc_id,
  136. lines: this.lines,
  137. version: this.version,
  138. ops: this.ops,
  139. ranges: this.ranges,
  140. pathname: this.pathname,
  141. ttlInS: 42,
  142. type: 'sharejs-text-ot',
  143. })
  144. })
  145. it('should log the request', function () {
  146. this.logger.debug
  147. .calledWith(
  148. {
  149. docId: this.doc_id,
  150. projectId: this.project_id,
  151. historyRanges: false,
  152. },
  153. 'getting doc via http'
  154. )
  155. .should.equal(true)
  156. })
  157. it('should time the request', function () {
  158. this.Metrics.Timer.prototype.done.called.should.equal(true)
  159. })
  160. })
  161. describe('when historyRanges query param is true', function () {
  162. beforeEach(function () {
  163. this.DocumentManager.getDocAndRecentOpsWithLock = sinon
  164. .stub()
  165. .callsArgWith(
  166. 3,
  167. null,
  168. this.lines,
  169. this.version,
  170. [],
  171. this.ranges,
  172. this.pathname,
  173. this.projectHistoryId,
  174. 'sharejs-text-ot'
  175. )
  176. this.req.query = { historyRanges: 'true' }
  177. this.HttpController.getDoc(this.req, this.res, this.next)
  178. })
  179. it('should get the doc', function () {
  180. this.DocumentManager.getDocAndRecentOpsWithLock
  181. .calledWith(this.project_id, this.doc_id, -1)
  182. .should.equal(true)
  183. })
  184. it('should return the doc as JSON with history ranges processing', function () {
  185. this.res.json.should.have.been.calledWith({
  186. id: this.doc_id,
  187. lines: this.lines,
  188. version: this.version,
  189. ops: [],
  190. ranges: this.ranges,
  191. pathname: this.pathname,
  192. ttlInS: 42,
  193. type: 'sharejs-text-ot',
  194. })
  195. })
  196. it('should call addTrackedDeletesToContent for history ranges processing', function () {
  197. this.Utils.addTrackedDeletesToContent.called.should.equal(true)
  198. })
  199. it('should call toHistoryRanges for range conversion', function () {
  200. this.HistoryConversions.toHistoryRanges.called.should.equal(true)
  201. })
  202. it('should log the request with historyRanges: true', function () {
  203. this.logger.debug
  204. .calledWith(
  205. {
  206. docId: this.doc_id,
  207. projectId: this.project_id,
  208. historyRanges: true,
  209. },
  210. 'getting doc via http'
  211. )
  212. .should.equal(true)
  213. })
  214. it('should time the request', function () {
  215. this.Metrics.Timer.prototype.done.called.should.equal(true)
  216. })
  217. })
  218. describe('when the document does not exist', function () {
  219. beforeEach(function () {
  220. this.DocumentManager.getDocAndRecentOpsWithLock = sinon
  221. .stub()
  222. .callsArgWith(3, null, null, null)
  223. this.HttpController.getDoc(this.req, this.res, this.next)
  224. })
  225. it('should call next with NotFoundError', function () {
  226. this.next
  227. .calledWith(sinon.match.instanceOf(Errors.NotFoundError))
  228. .should.equal(true)
  229. })
  230. })
  231. describe('when an errors occurs', function () {
  232. beforeEach(function () {
  233. this.DocumentManager.getDocAndRecentOpsWithLock = sinon
  234. .stub()
  235. .callsArgWith(3, new Error('oops'), null, null)
  236. this.HttpController.getDoc(this.req, this.res, this.next)
  237. })
  238. it('should call next with the error', function () {
  239. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  240. })
  241. })
  242. })
  243. describe('getComment', function () {
  244. beforeEach(function () {
  245. this.ranges = {
  246. changes: 'mock',
  247. comments: [
  248. {
  249. id: 'comment-id-1',
  250. },
  251. {
  252. id: 'comment-id-2',
  253. },
  254. ],
  255. }
  256. this.req = {
  257. params: {
  258. project_id: this.project_id,
  259. doc_id: this.doc_id,
  260. comment_id: this.comment_id,
  261. },
  262. query: {},
  263. body: {},
  264. }
  265. })
  266. beforeEach(function () {
  267. this.DocumentManager.getCommentWithLock = sinon
  268. .stub()
  269. .callsArgWith(3, null, this.ranges.comments[0])
  270. this.HttpController.getComment(this.req, this.res, this.next)
  271. })
  272. it('should get the comment', function () {
  273. this.DocumentManager.getCommentWithLock
  274. .calledWith(this.project_id, this.doc_id, this.comment_id)
  275. .should.equal(true)
  276. })
  277. it('should return the comment as JSON', function () {
  278. this.res.json
  279. .calledWith({
  280. id: 'comment-id-1',
  281. })
  282. .should.equal(true)
  283. })
  284. it('should log the request', function () {
  285. this.logger.debug
  286. .calledWith(
  287. {
  288. projectId: this.project_id,
  289. docId: this.doc_id,
  290. commentId: this.comment_id,
  291. },
  292. 'getting comment via http'
  293. )
  294. .should.equal(true)
  295. })
  296. })
  297. describe('setDoc', function () {
  298. beforeEach(function () {
  299. this.lines = ['one', 'two', 'three']
  300. this.source = 'dropbox'
  301. this.user_id = 'user-id-123'
  302. this.req = {
  303. headers: {},
  304. params: {
  305. project_id: this.project_id,
  306. doc_id: this.doc_id,
  307. },
  308. query: {},
  309. body: {
  310. lines: this.lines,
  311. source: this.source,
  312. user_id: this.user_id,
  313. undoing: (this.undoing = true),
  314. },
  315. }
  316. })
  317. describe('successfully', function () {
  318. beforeEach(function () {
  319. this.DocumentManager.setDocWithLock = sinon
  320. .stub()
  321. .callsArgWith(7, null, { rev: '123' })
  322. this.HttpController.setDoc(this.req, this.res, this.next)
  323. })
  324. it('should set the doc', function () {
  325. this.DocumentManager.setDocWithLock
  326. .calledWith(
  327. this.project_id,
  328. this.doc_id,
  329. this.lines,
  330. this.source,
  331. this.user_id,
  332. this.undoing,
  333. true
  334. )
  335. .should.equal(true)
  336. })
  337. it('should return a json response with the document rev from web', function () {
  338. this.res.json.calledWithMatch({ rev: '123' }).should.equal(true)
  339. })
  340. it('should log the request', function () {
  341. this.logger.debug
  342. .calledWith(
  343. {
  344. docId: this.doc_id,
  345. projectId: this.project_id,
  346. lines: this.lines,
  347. source: this.source,
  348. userId: this.user_id,
  349. undoing: this.undoing,
  350. },
  351. 'setting doc via http'
  352. )
  353. .should.equal(true)
  354. })
  355. it('should time the request', function () {
  356. this.Metrics.Timer.prototype.done.called.should.equal(true)
  357. })
  358. })
  359. describe('when an errors occurs', function () {
  360. beforeEach(function () {
  361. this.DocumentManager.setDocWithLock = sinon
  362. .stub()
  363. .callsArgWith(7, new Error('oops'))
  364. this.HttpController.setDoc(this.req, this.res, this.next)
  365. })
  366. it('should call next with the error', function () {
  367. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  368. })
  369. })
  370. describe('when the payload is too large', function () {
  371. beforeEach(function () {
  372. const lines = []
  373. for (let _ = 0; _ <= 200000; _++) {
  374. lines.push('test test test')
  375. }
  376. this.req.body.lines = lines
  377. this.DocumentManager.setDocWithLock = sinon.stub().callsArgWith(6)
  378. this.HttpController.setDoc(this.req, this.res, this.next)
  379. })
  380. it('should send back a 406 response', function () {
  381. this.res.sendStatus.calledWith(406).should.equal(true)
  382. })
  383. it('should not call setDocWithLock', function () {
  384. this.DocumentManager.setDocWithLock.callCount.should.equal(0)
  385. })
  386. })
  387. })
  388. describe('flushProject', function () {
  389. beforeEach(function () {
  390. this.req = {
  391. params: {
  392. project_id: this.project_id,
  393. },
  394. query: {},
  395. body: {},
  396. }
  397. })
  398. describe('successfully', function () {
  399. beforeEach(function () {
  400. this.ProjectManager.flushProjectWithLocks = sinon.stub().callsArgWith(1)
  401. this.HttpController.flushProject(this.req, this.res, this.next)
  402. })
  403. it('should flush the project', function () {
  404. this.ProjectManager.flushProjectWithLocks
  405. .calledWith(this.project_id)
  406. .should.equal(true)
  407. })
  408. it('should return a successful No Content response', function () {
  409. this.res.sendStatus.calledWith(204).should.equal(true)
  410. })
  411. it('should log the request', function () {
  412. this.logger.debug
  413. .calledWith(
  414. { projectId: this.project_id },
  415. 'flushing project via http'
  416. )
  417. .should.equal(true)
  418. })
  419. it('should time the request', function () {
  420. this.Metrics.Timer.prototype.done.called.should.equal(true)
  421. })
  422. })
  423. describe('when an errors occurs', function () {
  424. beforeEach(function () {
  425. this.ProjectManager.flushProjectWithLocks = sinon
  426. .stub()
  427. .callsArgWith(1, new Error('oops'))
  428. this.HttpController.flushProject(this.req, this.res, this.next)
  429. })
  430. it('should call next with the error', function () {
  431. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  432. })
  433. })
  434. })
  435. describe('flushDocIfLoaded', function () {
  436. beforeEach(function () {
  437. this.lines = ['one', 'two', 'three']
  438. this.version = 42
  439. this.req = {
  440. params: {
  441. project_id: this.project_id,
  442. doc_id: this.doc_id,
  443. },
  444. query: {},
  445. body: {},
  446. }
  447. })
  448. describe('successfully', function () {
  449. beforeEach(function () {
  450. this.DocumentManager.flushDocIfLoadedWithLock = sinon
  451. .stub()
  452. .callsArgWith(2)
  453. this.HttpController.flushDocIfLoaded(this.req, this.res, this.next)
  454. })
  455. it('should flush the doc', function () {
  456. this.DocumentManager.flushDocIfLoadedWithLock
  457. .calledWith(this.project_id, this.doc_id)
  458. .should.equal(true)
  459. })
  460. it('should return a successful No Content response', function () {
  461. this.res.sendStatus.calledWith(204).should.equal(true)
  462. })
  463. it('should log the request', function () {
  464. this.logger.debug
  465. .calledWith(
  466. { docId: this.doc_id, projectId: this.project_id },
  467. 'flushing doc via http'
  468. )
  469. .should.equal(true)
  470. })
  471. it('should time the request', function () {
  472. this.Metrics.Timer.prototype.done.called.should.equal(true)
  473. })
  474. })
  475. describe('when an errors occurs', function () {
  476. beforeEach(function () {
  477. this.DocumentManager.flushDocIfLoadedWithLock = sinon
  478. .stub()
  479. .callsArgWith(2, new Error('oops'))
  480. this.HttpController.flushDocIfLoaded(this.req, this.res, this.next)
  481. })
  482. it('should call next with the error', function () {
  483. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  484. })
  485. })
  486. })
  487. describe('deleteDoc', function () {
  488. beforeEach(function () {
  489. this.req = {
  490. params: {
  491. project_id: this.project_id,
  492. doc_id: this.doc_id,
  493. },
  494. query: {},
  495. body: {},
  496. }
  497. })
  498. describe('successfully', function () {
  499. beforeEach(function () {
  500. this.DocumentManager.flushAndDeleteDocWithLock = sinon
  501. .stub()
  502. .callsArgWith(3)
  503. this.HttpController.deleteDoc(this.req, this.res, this.next)
  504. })
  505. it('should flush and delete the doc', function () {
  506. this.DocumentManager.flushAndDeleteDocWithLock
  507. .calledWith(this.project_id, this.doc_id, {
  508. ignoreFlushErrors: false,
  509. })
  510. .should.equal(true)
  511. })
  512. it('should flush project history', function () {
  513. this.HistoryManager.flushProjectChangesAsync
  514. .calledWithExactly(this.project_id)
  515. .should.equal(true)
  516. })
  517. it('should return a successful No Content response', function () {
  518. this.res.sendStatus.calledWith(204).should.equal(true)
  519. })
  520. it('should log the request', function () {
  521. this.logger.debug
  522. .calledWith(
  523. { docId: this.doc_id, projectId: this.project_id },
  524. 'deleting doc via http'
  525. )
  526. .should.equal(true)
  527. })
  528. it('should time the request', function () {
  529. this.Metrics.Timer.prototype.done.called.should.equal(true)
  530. })
  531. })
  532. describe('ignoring errors', function () {
  533. beforeEach(function () {
  534. this.req.query.ignore_flush_errors = 'true'
  535. this.DocumentManager.flushAndDeleteDocWithLock = sinon.stub().yields()
  536. this.HttpController.deleteDoc(this.req, this.res, this.next)
  537. })
  538. it('should delete the doc', function () {
  539. this.DocumentManager.flushAndDeleteDocWithLock
  540. .calledWith(this.project_id, this.doc_id, { ignoreFlushErrors: true })
  541. .should.equal(true)
  542. })
  543. it('should return a successful No Content response', function () {
  544. this.res.sendStatus.calledWith(204).should.equal(true)
  545. })
  546. })
  547. describe('when an errors occurs', function () {
  548. beforeEach(function () {
  549. this.DocumentManager.flushAndDeleteDocWithLock = sinon
  550. .stub()
  551. .callsArgWith(3, new Error('oops'))
  552. this.HttpController.deleteDoc(this.req, this.res, this.next)
  553. })
  554. it('should flush project history', function () {
  555. this.HistoryManager.flushProjectChangesAsync
  556. .calledWithExactly(this.project_id)
  557. .should.equal(true)
  558. })
  559. it('should call next with the error', function () {
  560. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  561. })
  562. })
  563. })
  564. describe('deleteProject', function () {
  565. beforeEach(function () {
  566. this.req = {
  567. params: {
  568. project_id: this.project_id,
  569. },
  570. query: {},
  571. body: {},
  572. }
  573. })
  574. describe('successfully', function () {
  575. beforeEach(function () {
  576. this.ProjectManager.flushAndDeleteProjectWithLocks = sinon
  577. .stub()
  578. .callsArgWith(2)
  579. this.HttpController.deleteProject(this.req, this.res, this.next)
  580. })
  581. it('should delete the project', function () {
  582. this.ProjectManager.flushAndDeleteProjectWithLocks
  583. .calledWith(this.project_id)
  584. .should.equal(true)
  585. })
  586. it('should return a successful No Content response', function () {
  587. this.res.sendStatus.calledWith(204).should.equal(true)
  588. })
  589. it('should log the request', function () {
  590. this.logger.debug
  591. .calledWith(
  592. { projectId: this.project_id },
  593. 'deleting project via http'
  594. )
  595. .should.equal(true)
  596. })
  597. it('should time the request', function () {
  598. this.Metrics.Timer.prototype.done.called.should.equal(true)
  599. })
  600. })
  601. describe('with the background=true option from realtime', function () {
  602. beforeEach(function () {
  603. this.ProjectManager.queueFlushAndDeleteProject = sinon
  604. .stub()
  605. .callsArgWith(1)
  606. this.req.query = { background: true, shutdown: true }
  607. this.HttpController.deleteProject(this.req, this.res, this.next)
  608. })
  609. it('should queue the flush and delete', function () {
  610. this.ProjectManager.queueFlushAndDeleteProject
  611. .calledWith(this.project_id)
  612. .should.equal(true)
  613. })
  614. })
  615. describe('when an errors occurs', function () {
  616. beforeEach(function () {
  617. this.ProjectManager.flushAndDeleteProjectWithLocks = sinon
  618. .stub()
  619. .callsArgWith(2, new Error('oops'))
  620. this.HttpController.deleteProject(this.req, this.res, this.next)
  621. })
  622. it('should call next with the error', function () {
  623. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  624. })
  625. })
  626. })
  627. describe('acceptChanges', function () {
  628. beforeEach(function () {
  629. this.req = {
  630. params: {
  631. project_id: this.project_id,
  632. doc_id: this.doc_id,
  633. change_id: (this.change_id = 'mock-change-od-1'),
  634. },
  635. query: {},
  636. body: {},
  637. }
  638. })
  639. describe('successfully with a single change', function () {
  640. beforeEach(function () {
  641. this.DocumentManager.acceptChangesWithLock = sinon
  642. .stub()
  643. .callsArgWith(3)
  644. this.HttpController.acceptChanges(this.req, this.res, this.next)
  645. })
  646. it('should accept the change', function () {
  647. this.DocumentManager.acceptChangesWithLock
  648. .calledWith(this.project_id, this.doc_id, [this.change_id])
  649. .should.equal(true)
  650. })
  651. it('should return a successful No Content response', function () {
  652. this.res.sendStatus.calledWith(204).should.equal(true)
  653. })
  654. it('should log the request', function () {
  655. this.logger.debug
  656. .calledWith(
  657. { projectId: this.project_id, docId: this.doc_id },
  658. 'accepting 1 changes via http'
  659. )
  660. .should.equal(true)
  661. })
  662. it('should time the request', function () {
  663. this.Metrics.Timer.prototype.done.called.should.equal(true)
  664. })
  665. })
  666. describe('succesfully with with multiple changes', function () {
  667. beforeEach(function () {
  668. this.change_ids = [
  669. 'mock-change-od-1',
  670. 'mock-change-od-2',
  671. 'mock-change-od-3',
  672. 'mock-change-od-4',
  673. ]
  674. this.req.body = { change_ids: this.change_ids }
  675. this.DocumentManager.acceptChangesWithLock = sinon
  676. .stub()
  677. .callsArgWith(3)
  678. this.HttpController.acceptChanges(this.req, this.res, this.next)
  679. })
  680. it('should accept the changes in the body payload', function () {
  681. this.DocumentManager.acceptChangesWithLock
  682. .calledWith(this.project_id, this.doc_id, this.change_ids)
  683. .should.equal(true)
  684. })
  685. it('should log the request with the correct number of changes', function () {
  686. this.logger.debug
  687. .calledWith(
  688. { projectId: this.project_id, docId: this.doc_id },
  689. `accepting ${this.change_ids.length} changes via http`
  690. )
  691. .should.equal(true)
  692. })
  693. })
  694. describe('when an errors occurs', function () {
  695. beforeEach(function () {
  696. this.DocumentManager.acceptChangesWithLock = sinon
  697. .stub()
  698. .callsArgWith(3, new Error('oops'))
  699. this.HttpController.acceptChanges(this.req, this.res, this.next)
  700. })
  701. it('should call next with the error', function () {
  702. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  703. })
  704. })
  705. })
  706. describe('resolveComment', function () {
  707. beforeEach(function () {
  708. this.user_id = 'user-id-123'
  709. this.req = {
  710. params: {
  711. project_id: this.project_id,
  712. doc_id: this.doc_id,
  713. comment_id: (this.comment_id = 'mock-comment-id'),
  714. },
  715. query: {},
  716. body: {
  717. user_id: this.user_id,
  718. },
  719. }
  720. this.resolved = true
  721. })
  722. describe('successfully', function () {
  723. beforeEach(function (done) {
  724. this.DocumentManager.updateCommentStateWithLock = sinon
  725. .stub()
  726. .callsArgWith(5)
  727. this.ProjectHistoryRedisManager.queueOps = sinon.stub()
  728. this.res.sendStatus.callsFake(() => done())
  729. this.HttpController.resolveComment(this.req, this.res, this.next)
  730. })
  731. it('should accept the change', function () {
  732. this.DocumentManager.updateCommentStateWithLock
  733. .calledWith(
  734. this.project_id,
  735. this.doc_id,
  736. this.comment_id,
  737. this.user_id,
  738. this.resolved
  739. )
  740. .should.equal(true)
  741. })
  742. it('should return a successful No Content response', function () {
  743. this.res.sendStatus.calledWith(204).should.equal(true)
  744. })
  745. it('should log the request', function () {
  746. this.logger.debug
  747. .calledWith(
  748. {
  749. projectId: this.project_id,
  750. docId: this.doc_id,
  751. commentId: this.comment_id,
  752. },
  753. 'resolving comment via http'
  754. )
  755. .should.equal(true)
  756. })
  757. })
  758. describe('when an errors occurs', function () {
  759. beforeEach(function () {
  760. this.DocumentManager.updateCommentStateWithLock = sinon
  761. .stub()
  762. .callsArgWith(5, new Error('oops'))
  763. this.HttpController.resolveComment(this.req, this.res, this.next)
  764. })
  765. it('should call next with the error', function () {
  766. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  767. })
  768. })
  769. })
  770. describe('reopenComment', function () {
  771. beforeEach(function () {
  772. this.user_id = 'user-id-123'
  773. this.req = {
  774. params: {
  775. project_id: this.project_id,
  776. doc_id: this.doc_id,
  777. comment_id: (this.comment_id = 'mock-comment-id'),
  778. },
  779. query: {},
  780. body: {
  781. user_id: this.user_id,
  782. },
  783. }
  784. this.resolved = false
  785. })
  786. describe('successfully', function () {
  787. beforeEach(function () {
  788. this.DocumentManager.updateCommentStateWithLock = sinon
  789. .stub()
  790. .callsArgWith(5)
  791. this.ProjectHistoryRedisManager.queueOps = sinon.stub()
  792. this.HttpController.reopenComment(this.req, this.res, this.next)
  793. })
  794. it('should accept the change', function () {
  795. this.DocumentManager.updateCommentStateWithLock
  796. .calledWith(
  797. this.project_id,
  798. this.doc_id,
  799. this.comment_id,
  800. this.user_id,
  801. this.resolved
  802. )
  803. .should.equal(true)
  804. })
  805. it('should return a successful No Content response', function () {
  806. this.res.sendStatus.calledWith(204).should.equal(true)
  807. })
  808. it('should log the request', function () {
  809. this.logger.debug
  810. .calledWith(
  811. {
  812. projectId: this.project_id,
  813. docId: this.doc_id,
  814. commentId: this.comment_id,
  815. },
  816. 'reopening comment via http'
  817. )
  818. .should.equal(true)
  819. })
  820. })
  821. describe('when an errors occurs', function () {
  822. beforeEach(function () {
  823. this.DocumentManager.updateCommentStateWithLock = sinon
  824. .stub()
  825. .callsArgWith(5, new Error('oops'))
  826. this.HttpController.reopenComment(this.req, this.res, this.next)
  827. })
  828. it('should call next with the error', function () {
  829. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  830. })
  831. })
  832. })
  833. describe('deleteComment', function () {
  834. beforeEach(function () {
  835. this.user_id = 'user-id-123'
  836. this.req = {
  837. params: {
  838. project_id: this.project_id,
  839. doc_id: this.doc_id,
  840. comment_id: (this.comment_id = 'mock-comment-id'),
  841. },
  842. query: {},
  843. body: {
  844. user_id: this.user_id,
  845. },
  846. }
  847. })
  848. describe('successfully', function () {
  849. beforeEach(function () {
  850. this.DocumentManager.deleteCommentWithLock = sinon
  851. .stub()
  852. .callsArgWith(4)
  853. this.ProjectHistoryRedisManager.queueOps = sinon.stub()
  854. this.HttpController.deleteComment(this.req, this.res, this.next)
  855. })
  856. it('should accept the change', function () {
  857. this.DocumentManager.deleteCommentWithLock
  858. .calledWith(
  859. this.project_id,
  860. this.doc_id,
  861. this.comment_id,
  862. this.user_id
  863. )
  864. .should.equal(true)
  865. })
  866. it('should return a successful No Content response', function () {
  867. this.res.sendStatus.calledWith(204).should.equal(true)
  868. })
  869. it('should log the request', function () {
  870. this.logger.debug
  871. .calledWith(
  872. {
  873. projectId: this.project_id,
  874. docId: this.doc_id,
  875. commentId: this.comment_id,
  876. },
  877. 'deleting comment via http'
  878. )
  879. .should.equal(true)
  880. })
  881. it('should time the request', function () {
  882. this.Metrics.Timer.prototype.done.called.should.equal(true)
  883. })
  884. })
  885. describe('when an errors occurs', function () {
  886. beforeEach(function () {
  887. this.DocumentManager.deleteCommentWithLock = sinon
  888. .stub()
  889. .callsArgWith(4, new Error('oops'))
  890. this.HttpController.deleteComment(this.req, this.res, this.next)
  891. })
  892. it('should call next with the error', function () {
  893. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  894. })
  895. })
  896. })
  897. describe('getProjectDocsAndFlushIfOld', function () {
  898. beforeEach(function () {
  899. this.state = '01234567890abcdef'
  900. this.docs = [
  901. { _id: '1234', lines: 'hello', v: 23 },
  902. { _id: '4567', lines: 'world', v: 45 },
  903. ]
  904. this.req = {
  905. params: {
  906. project_id: this.project_id,
  907. },
  908. query: {
  909. state: this.state,
  910. },
  911. body: {},
  912. }
  913. })
  914. describe('successfully', function () {
  915. beforeEach(function () {
  916. this.ProjectManager.getProjectDocsAndFlushIfOld = sinon
  917. .stub()
  918. .callsArgWith(3, null, this.docs)
  919. this.HttpController.getProjectDocsAndFlushIfOld(
  920. this.req,
  921. this.res,
  922. this.next
  923. )
  924. })
  925. it('should get docs from the project manager', function () {
  926. this.ProjectManager.getProjectDocsAndFlushIfOld
  927. .calledWith(this.project_id, this.state, {})
  928. .should.equal(true)
  929. })
  930. it('should return a successful response', function () {
  931. this.res.send.calledWith(this.docs).should.equal(true)
  932. })
  933. it('should log the request', function () {
  934. this.logger.debug
  935. .calledWith(
  936. { projectId: this.project_id, exclude: [] },
  937. 'getting docs via http'
  938. )
  939. .should.equal(true)
  940. })
  941. it('should log the response', function () {
  942. this.logger.debug
  943. .calledWith(
  944. { projectId: this.project_id, result: ['1234:23', '4567:45'] },
  945. 'got docs via http'
  946. )
  947. .should.equal(true)
  948. })
  949. it('should time the request', function () {
  950. this.Metrics.Timer.prototype.done.called.should.equal(true)
  951. })
  952. })
  953. describe('when there is a conflict', function () {
  954. beforeEach(function () {
  955. this.ProjectManager.getProjectDocsAndFlushIfOld = sinon
  956. .stub()
  957. .callsArgWith(
  958. 3,
  959. new Errors.ProjectStateChangedError('project state changed')
  960. )
  961. this.HttpController.getProjectDocsAndFlushIfOld(
  962. this.req,
  963. this.res,
  964. this.next
  965. )
  966. })
  967. it('should return an HTTP 409 Conflict response', function () {
  968. this.res.sendStatus.calledWith(409).should.equal(true)
  969. })
  970. })
  971. describe('when an error occurs', function () {
  972. beforeEach(function () {
  973. this.ProjectManager.getProjectDocsAndFlushIfOld = sinon
  974. .stub()
  975. .callsArgWith(3, new Error('oops'))
  976. this.HttpController.getProjectDocsAndFlushIfOld(
  977. this.req,
  978. this.res,
  979. this.next
  980. )
  981. })
  982. it('should call next with the error', function () {
  983. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  984. })
  985. })
  986. })
  987. describe('updateProject', function () {
  988. beforeEach(function () {
  989. this.projectHistoryId = 'history-id-123'
  990. this.userId = 'user-id-123'
  991. this.updates = [
  992. {
  993. type: 'rename-doc',
  994. id: 1,
  995. pathname: 'thesis.tex',
  996. newPathname: 'book.tex',
  997. },
  998. { type: 'add-doc', id: 2, pathname: 'article.tex', docLines: 'hello' },
  999. {
  1000. type: 'rename-file',
  1001. id: 3,
  1002. pathname: 'apple.png',
  1003. newPathname: 'banana.png',
  1004. },
  1005. { type: 'add-file', id: 4, url: 'filestore.example.com/4' },
  1006. ]
  1007. this.version = 1234567
  1008. this.req = {
  1009. query: {},
  1010. body: {
  1011. projectHistoryId: this.projectHistoryId,
  1012. userId: this.userId,
  1013. updates: this.updates,
  1014. version: this.version,
  1015. source: this.source,
  1016. },
  1017. params: {
  1018. project_id: this.project_id,
  1019. },
  1020. }
  1021. })
  1022. describe('successfully', function () {
  1023. beforeEach(function () {
  1024. this.ProjectManager.updateProjectWithLocks = sinon.stub().yields()
  1025. this.HttpController.updateProject(this.req, this.res, this.next)
  1026. })
  1027. it('should accept the change', function () {
  1028. this.ProjectManager.updateProjectWithLocks
  1029. .calledWith(
  1030. this.project_id,
  1031. this.projectHistoryId,
  1032. this.userId,
  1033. this.updates,
  1034. this.version,
  1035. this.source
  1036. )
  1037. .should.equal(true)
  1038. })
  1039. it('should return a successful No Content response', function () {
  1040. this.res.sendStatus.calledWith(204).should.equal(true)
  1041. })
  1042. it('should time the request', function () {
  1043. this.Metrics.Timer.prototype.done.called.should.equal(true)
  1044. })
  1045. })
  1046. describe('when an errors occurs', function () {
  1047. beforeEach(function () {
  1048. this.ProjectManager.updateProjectWithLocks = sinon
  1049. .stub()
  1050. .yields(new Error('oops'))
  1051. this.HttpController.updateProject(this.req, this.res, this.next)
  1052. })
  1053. it('should call next with the error', function () {
  1054. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  1055. })
  1056. })
  1057. })
  1058. describe('resyncProjectHistory', function () {
  1059. beforeEach(function () {
  1060. this.projectHistoryId = 'history-id-123'
  1061. this.docs = sinon.stub()
  1062. this.files = sinon.stub()
  1063. this.fileUpdates = sinon.stub()
  1064. this.req = {
  1065. query: {},
  1066. body: {
  1067. projectHistoryId: this.projectHistoryId,
  1068. docs: this.docs,
  1069. files: this.files,
  1070. },
  1071. params: {
  1072. project_id: this.project_id,
  1073. },
  1074. }
  1075. })
  1076. describe('successfully', function () {
  1077. beforeEach(function () {
  1078. this.HistoryManager.resyncProjectHistory = sinon.stub().callsArgWith(5)
  1079. this.HttpController.resyncProjectHistory(this.req, this.res, this.next)
  1080. })
  1081. it('should accept the change', function () {
  1082. this.HistoryManager.resyncProjectHistory
  1083. .calledWith(
  1084. this.project_id,
  1085. this.projectHistoryId,
  1086. this.docs,
  1087. this.files,
  1088. {}
  1089. )
  1090. .should.equal(true)
  1091. })
  1092. it('should return a successful No Content response', function () {
  1093. this.res.sendStatus.should.have.been.calledWith(204)
  1094. })
  1095. })
  1096. describe('when an errors occurs', function () {
  1097. beforeEach(function () {
  1098. this.HistoryManager.resyncProjectHistory = sinon
  1099. .stub()
  1100. .callsArgWith(5, new Error('oops'))
  1101. this.HttpController.resyncProjectHistory(this.req, this.res, this.next)
  1102. })
  1103. it('should call next with the error', function () {
  1104. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  1105. })
  1106. })
  1107. })
  1108. describe('appendToDoc', function () {
  1109. beforeEach(function () {
  1110. this.lines = ['one', 'two', 'three']
  1111. this.source = 'dropbox'
  1112. this.user_id = 'user-id-123'
  1113. this.req = {
  1114. headers: {},
  1115. params: {
  1116. project_id: this.project_id,
  1117. doc_id: this.doc_id,
  1118. },
  1119. query: {},
  1120. body: {
  1121. lines: this.lines,
  1122. source: this.source,
  1123. user_id: this.user_id,
  1124. undoing: (this.undoing = true),
  1125. },
  1126. }
  1127. })
  1128. describe('successfully', function () {
  1129. beforeEach(function () {
  1130. this.DocumentManager.appendToDocWithLock = sinon
  1131. .stub()
  1132. .callsArgWith(5, null, { rev: '123' })
  1133. this.HttpController.appendToDoc(this.req, this.res, this.next)
  1134. })
  1135. it('should append to the doc', function () {
  1136. this.DocumentManager.appendToDocWithLock
  1137. .calledWith(
  1138. this.project_id,
  1139. this.doc_id,
  1140. this.lines,
  1141. this.source,
  1142. this.user_id
  1143. )
  1144. .should.equal(true)
  1145. })
  1146. it('should return a json response with the document rev from web', function () {
  1147. this.res.json.calledWithMatch({ rev: '123' }).should.equal(true)
  1148. })
  1149. it('should log the request', function () {
  1150. this.logger.debug
  1151. .calledWith(
  1152. {
  1153. docId: this.doc_id,
  1154. projectId: this.project_id,
  1155. lines: this.lines,
  1156. source: this.source,
  1157. userId: this.user_id,
  1158. },
  1159. 'appending to doc via http'
  1160. )
  1161. .should.equal(true)
  1162. })
  1163. it('should time the request', function () {
  1164. this.Metrics.Timer.prototype.done.called.should.equal(true)
  1165. })
  1166. })
  1167. describe('when an errors occurs', function () {
  1168. beforeEach(function () {
  1169. this.DocumentManager.appendToDocWithLock = sinon
  1170. .stub()
  1171. .callsArgWith(5, new Error('oops'))
  1172. this.HttpController.appendToDoc(this.req, this.res, this.next)
  1173. })
  1174. it('should call next with the error', function () {
  1175. this.next.calledWith(sinon.match.instanceOf(Error)).should.equal(true)
  1176. })
  1177. })
  1178. describe('when the payload is too large', function () {
  1179. beforeEach(function () {
  1180. this.DocumentManager.appendToDocWithLock = sinon
  1181. .stub()
  1182. .callsArgWith(5, new Errors.FileTooLargeError())
  1183. this.HttpController.appendToDoc(this.req, this.res, this.next)
  1184. })
  1185. it('should send back a 422 response', function () {
  1186. this.res.sendStatus.calledWith(422).should.equal(true)
  1187. })
  1188. })
  1189. })
  1190. })