HttpControllerTests.js 28 KB

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