HttpControllerTests.js 32 KB

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