HttpControllerTests.js 27 KB

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