HttpControllerTests.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. const SandboxedModule = require('sandboxed-module')
  2. const sinon = require('sinon')
  3. const { assert, expect } = require('chai')
  4. const modulePath = require('node:path').join(
  5. __dirname,
  6. '../../../app/js/HttpController'
  7. )
  8. const { ObjectId } = require('mongodb-legacy')
  9. const Errors = require('../../../app/js/Errors')
  10. describe('HttpController', function () {
  11. beforeEach(function () {
  12. const settings = {
  13. max_doc_length: 2 * 1024 * 1024,
  14. }
  15. this.DocArchiveManager = {
  16. unArchiveAllDocs: sinon.stub().returns(),
  17. }
  18. this.DocManager = {}
  19. this.HttpController = SandboxedModule.require(modulePath, {
  20. requires: {
  21. './DocManager': this.DocManager,
  22. './DocArchiveManager': this.DocArchiveManager,
  23. '@overleaf/settings': settings,
  24. './HealthChecker': {},
  25. './Errors': Errors,
  26. },
  27. })
  28. this.res = {
  29. send: sinon.stub(),
  30. sendStatus: sinon.stub(),
  31. json: sinon.stub(),
  32. setHeader: sinon.stub(),
  33. }
  34. this.res.status = sinon.stub().returns(this.res)
  35. this.req = { query: {} }
  36. this.next = sinon.stub()
  37. this.projectId = 'mock-project-id'
  38. this.docId = 'mock-doc-id'
  39. this.doc = {
  40. _id: this.docId,
  41. lines: ['mock', 'lines', ' here', '', '', ' spaces '],
  42. version: 42,
  43. rev: 5,
  44. }
  45. this.deletedDoc = {
  46. deleted: true,
  47. _id: this.docId,
  48. lines: ['mock', 'lines', ' here', '', '', ' spaces '],
  49. version: 42,
  50. rev: 5,
  51. }
  52. })
  53. describe('getDoc', function () {
  54. describe('without deleted docs', function () {
  55. beforeEach(async function () {
  56. this.req.params = {
  57. project_id: this.projectId,
  58. doc_id: this.docId,
  59. }
  60. this.DocManager.getFullDoc = sinon.stub().resolves(this.doc)
  61. await this.HttpController.getDoc(this.req, this.res, this.next)
  62. })
  63. it('should get the document with the version (including deleted)', function () {
  64. this.DocManager.getFullDoc
  65. .calledWith(this.projectId, this.docId)
  66. .should.equal(true)
  67. })
  68. it('should return the doc as JSON', function () {
  69. this.res.json
  70. .calledWith({
  71. _id: this.docId,
  72. lines: this.doc.lines,
  73. rev: this.doc.rev,
  74. version: this.doc.version,
  75. })
  76. .should.equal(true)
  77. })
  78. })
  79. describe('which is deleted', function () {
  80. beforeEach(function () {
  81. this.req.params = {
  82. project_id: this.projectId,
  83. doc_id: this.docId,
  84. }
  85. this.DocManager.getFullDoc = sinon.stub().resolves(this.deletedDoc)
  86. })
  87. it('should get the doc from the doc manager', async function () {
  88. await this.HttpController.getDoc(this.req, this.res, this.next)
  89. this.DocManager.getFullDoc
  90. .calledWith(this.projectId, this.docId)
  91. .should.equal(true)
  92. })
  93. it('should return 404 if the query string delete is not set ', async function () {
  94. await this.HttpController.getDoc(this.req, this.res, this.next)
  95. this.res.sendStatus.calledWith(404).should.equal(true)
  96. })
  97. it('should return the doc as JSON if include_deleted is set to true', async function () {
  98. this.req.query.include_deleted = 'true'
  99. await this.HttpController.getDoc(this.req, this.res, this.next)
  100. this.res.json
  101. .calledWith({
  102. _id: this.docId,
  103. lines: this.doc.lines,
  104. rev: this.doc.rev,
  105. deleted: true,
  106. version: this.doc.version,
  107. })
  108. .should.equal(true)
  109. })
  110. })
  111. })
  112. describe('getRawDoc', function () {
  113. beforeEach(async function () {
  114. this.req.params = {
  115. project_id: this.projectId,
  116. doc_id: this.docId,
  117. }
  118. this.DocManager.getDocLines = sinon
  119. .stub()
  120. .resolves(this.doc.lines.join('\n'))
  121. await this.HttpController.getRawDoc(this.req, this.res, this.next)
  122. })
  123. it('should get the document without the version', function () {
  124. this.DocManager.getDocLines
  125. .calledWith(this.projectId, this.docId)
  126. .should.equal(true)
  127. })
  128. it('should set the content type header', function () {
  129. this.res.setHeader
  130. .calledWith('content-type', 'text/plain')
  131. .should.equal(true)
  132. })
  133. it('should send the raw version of the doc', function () {
  134. assert.deepEqual(
  135. this.res.send.args[0][0],
  136. `${this.doc.lines[0]}\n${this.doc.lines[1]}\n${this.doc.lines[2]}\n${this.doc.lines[3]}\n${this.doc.lines[4]}\n${this.doc.lines[5]}`
  137. )
  138. })
  139. })
  140. describe('getAllDocs', function () {
  141. describe('normally', function () {
  142. beforeEach(async function () {
  143. this.req.params = { project_id: this.projectId }
  144. this.docs = [
  145. {
  146. _id: new ObjectId(),
  147. lines: ['mock', 'lines', 'one'],
  148. rev: 2,
  149. },
  150. {
  151. _id: new ObjectId(),
  152. lines: ['mock', 'lines', 'two'],
  153. rev: 4,
  154. },
  155. ]
  156. this.DocManager.getAllNonDeletedDocs = sinon.stub().resolves(this.docs)
  157. await this.HttpController.getAllDocs(this.req, this.res, this.next)
  158. })
  159. it('should get all the (non-deleted) docs', function () {
  160. this.DocManager.getAllNonDeletedDocs
  161. .calledWith(this.projectId, { lines: true, rev: true })
  162. .should.equal(true)
  163. })
  164. it('should return the doc as JSON', function () {
  165. this.res.json
  166. .calledWith([
  167. {
  168. _id: this.docs[0]._id.toString(),
  169. lines: this.docs[0].lines,
  170. rev: this.docs[0].rev,
  171. },
  172. {
  173. _id: this.docs[1]._id.toString(),
  174. lines: this.docs[1].lines,
  175. rev: this.docs[1].rev,
  176. },
  177. ])
  178. .should.equal(true)
  179. })
  180. })
  181. describe('with null lines', function () {
  182. beforeEach(async function () {
  183. this.req.params = { project_id: this.projectId }
  184. this.docs = [
  185. {
  186. _id: new ObjectId(),
  187. lines: null,
  188. rev: 2,
  189. },
  190. {
  191. _id: new ObjectId(),
  192. lines: ['mock', 'lines', 'two'],
  193. rev: 4,
  194. },
  195. ]
  196. this.DocManager.getAllNonDeletedDocs = sinon.stub().resolves(this.docs)
  197. await this.HttpController.getAllDocs(this.req, this.res, this.next)
  198. })
  199. it('should return the doc with fallback lines', function () {
  200. this.res.json
  201. .calledWith([
  202. {
  203. _id: this.docs[0]._id.toString(),
  204. lines: [],
  205. rev: this.docs[0].rev,
  206. },
  207. {
  208. _id: this.docs[1]._id.toString(),
  209. lines: this.docs[1].lines,
  210. rev: this.docs[1].rev,
  211. },
  212. ])
  213. .should.equal(true)
  214. })
  215. })
  216. describe('with a null doc', function () {
  217. beforeEach(async function () {
  218. this.req.params = { project_id: this.projectId }
  219. this.docs = [
  220. {
  221. _id: new ObjectId(),
  222. lines: ['mock', 'lines', 'one'],
  223. rev: 2,
  224. },
  225. null,
  226. {
  227. _id: new ObjectId(),
  228. lines: ['mock', 'lines', 'two'],
  229. rev: 4,
  230. },
  231. ]
  232. this.DocManager.getAllNonDeletedDocs = sinon.stub().resolves(this.docs)
  233. await this.HttpController.getAllDocs(this.req, this.res, this.next)
  234. })
  235. it('should return the non null docs as JSON', function () {
  236. this.res.json
  237. .calledWith([
  238. {
  239. _id: this.docs[0]._id.toString(),
  240. lines: this.docs[0].lines,
  241. rev: this.docs[0].rev,
  242. },
  243. {
  244. _id: this.docs[2]._id.toString(),
  245. lines: this.docs[2].lines,
  246. rev: this.docs[2].rev,
  247. },
  248. ])
  249. .should.equal(true)
  250. })
  251. it('should log out an error', function () {
  252. this.logger.error
  253. .calledWith(
  254. {
  255. err: sinon.match.has('message', 'null doc'),
  256. projectId: this.projectId,
  257. },
  258. 'encountered null doc'
  259. )
  260. .should.equal(true)
  261. })
  262. })
  263. })
  264. describe('getAllRanges', function () {
  265. describe('normally', function () {
  266. beforeEach(async function () {
  267. this.req.params = { project_id: this.projectId }
  268. this.docs = [
  269. {
  270. _id: new ObjectId(),
  271. ranges: { mock_ranges: 'one' },
  272. },
  273. {
  274. _id: new ObjectId(),
  275. ranges: { mock_ranges: 'two' },
  276. },
  277. ]
  278. this.DocManager.getAllNonDeletedDocs = sinon.stub().resolves(this.docs)
  279. await this.HttpController.getAllRanges(this.req, this.res, this.next)
  280. })
  281. it('should get all the (non-deleted) doc ranges', function () {
  282. this.DocManager.getAllNonDeletedDocs
  283. .calledWith(this.projectId, { ranges: true })
  284. .should.equal(true)
  285. })
  286. it('should return the doc as JSON', function () {
  287. this.res.json
  288. .calledWith([
  289. {
  290. _id: this.docs[0]._id.toString(),
  291. ranges: this.docs[0].ranges,
  292. },
  293. {
  294. _id: this.docs[1]._id.toString(),
  295. ranges: this.docs[1].ranges,
  296. },
  297. ])
  298. .should.equal(true)
  299. })
  300. })
  301. })
  302. describe('updateDoc', function () {
  303. beforeEach(function () {
  304. this.req.params = {
  305. project_id: this.projectId,
  306. doc_id: this.docId,
  307. }
  308. })
  309. describe('when the doc lines exist and were updated', function () {
  310. beforeEach(async function () {
  311. this.req.body = {
  312. lines: (this.lines = ['hello', 'world']),
  313. version: (this.version = 42),
  314. ranges: (this.ranges = { changes: 'mock' }),
  315. }
  316. this.rev = 5
  317. this.DocManager.updateDoc = sinon
  318. .stub()
  319. .resolves({ modified: true, rev: this.rev })
  320. await this.HttpController.updateDoc(this.req, this.res, this.next)
  321. })
  322. it('should update the document', function () {
  323. this.DocManager.updateDoc
  324. .calledWith(
  325. this.projectId,
  326. this.docId,
  327. this.lines,
  328. this.version,
  329. this.ranges
  330. )
  331. .should.equal(true)
  332. })
  333. it('should return a modified status', function () {
  334. this.res.json
  335. .calledWith({ modified: true, rev: this.rev })
  336. .should.equal(true)
  337. })
  338. })
  339. describe('when the doc lines exist and were not updated', function () {
  340. beforeEach(async function () {
  341. this.req.body = {
  342. lines: (this.lines = ['hello', 'world']),
  343. version: (this.version = 42),
  344. ranges: {},
  345. }
  346. this.rev = 5
  347. this.DocManager.updateDoc = sinon
  348. .stub()
  349. .resolves({ modified: false, rev: this.rev })
  350. await this.HttpController.updateDoc(this.req, this.res, this.next)
  351. })
  352. it('should return a modified status', function () {
  353. this.res.json
  354. .calledWith({ modified: false, rev: this.rev })
  355. .should.equal(true)
  356. })
  357. })
  358. describe('when the doc lines are not provided', function () {
  359. beforeEach(async function () {
  360. this.req.body = { version: 42, ranges: {} }
  361. this.DocManager.updateDoc = sinon
  362. .stub()
  363. .resolves({ modified: false, rev: 0 })
  364. await this.HttpController.updateDoc(this.req, this.res, this.next)
  365. })
  366. it('should not update the document', function () {
  367. this.DocManager.updateDoc.called.should.equal(false)
  368. })
  369. it('should return a 400 (bad request) response', function () {
  370. this.res.sendStatus.calledWith(400).should.equal(true)
  371. })
  372. })
  373. describe('when the doc version are not provided', function () {
  374. beforeEach(async function () {
  375. this.req.body = { version: 42, lines: ['hello world'] }
  376. this.DocManager.updateDoc = sinon
  377. .stub()
  378. .resolves({ modified: false, rev: 0 })
  379. await this.HttpController.updateDoc(this.req, this.res, this.next)
  380. })
  381. it('should not update the document', function () {
  382. this.DocManager.updateDoc.called.should.equal(false)
  383. })
  384. it('should return a 400 (bad request) response', function () {
  385. this.res.sendStatus.calledWith(400).should.equal(true)
  386. })
  387. })
  388. describe('when the doc ranges is not provided', function () {
  389. beforeEach(async function () {
  390. this.req.body = { lines: ['foo'], version: 42 }
  391. this.DocManager.updateDoc = sinon
  392. .stub()
  393. .resolves({ modified: false, rev: 0 })
  394. await this.HttpController.updateDoc(this.req, this.res, this.next)
  395. })
  396. it('should not update the document', function () {
  397. this.DocManager.updateDoc.called.should.equal(false)
  398. })
  399. it('should return a 400 (bad request) response', function () {
  400. this.res.sendStatus.calledWith(400).should.equal(true)
  401. })
  402. })
  403. describe('when the doc body is too large', function () {
  404. beforeEach(async function () {
  405. this.req.body = {
  406. lines: (this.lines = Array(2049).fill('a'.repeat(1024))),
  407. version: (this.version = 42),
  408. ranges: (this.ranges = { changes: 'mock' }),
  409. }
  410. this.DocManager.updateDoc = sinon
  411. .stub()
  412. .resolves({ modified: false, rev: 0 })
  413. await this.HttpController.updateDoc(this.req, this.res, this.next)
  414. })
  415. it('should not update the document', function () {
  416. this.DocManager.updateDoc.called.should.equal(false)
  417. })
  418. it('should return a 413 (too large) response', function () {
  419. sinon.assert.calledWith(this.res.status, 413)
  420. })
  421. it('should report that the document body is too large', function () {
  422. sinon.assert.calledWith(this.res.send, 'document body too large')
  423. })
  424. })
  425. })
  426. describe('patchDoc', function () {
  427. beforeEach(async function () {
  428. this.req.params = {
  429. project_id: this.projectId,
  430. doc_id: this.docId,
  431. }
  432. this.req.body = { name: 'foo.tex' }
  433. this.DocManager.patchDoc = sinon.stub().resolves()
  434. await this.HttpController.patchDoc(this.req, this.res, this.next)
  435. })
  436. it('should delete the document', function () {
  437. expect(this.DocManager.patchDoc).to.have.been.calledWith(
  438. this.projectId,
  439. this.docId
  440. )
  441. })
  442. it('should return a 204 (No Content)', function () {
  443. expect(this.res.sendStatus).to.have.been.calledWith(204)
  444. })
  445. describe('with an invalid payload', function () {
  446. beforeEach(async function () {
  447. this.req.body = { cannot: 'happen' }
  448. this.DocManager.patchDoc = sinon.stub().resolves()
  449. await this.HttpController.patchDoc(this.req, this.res, this.next)
  450. })
  451. it('should log a message', function () {
  452. expect(this.logger.fatal).to.have.been.calledWith(
  453. { field: 'cannot' },
  454. 'joi validation for pathDoc is broken'
  455. )
  456. })
  457. it('should not pass the invalid field along', function () {
  458. expect(this.DocManager.patchDoc).to.have.been.calledWith(
  459. this.projectId,
  460. this.docId,
  461. {}
  462. )
  463. })
  464. })
  465. })
  466. describe('archiveAllDocs', function () {
  467. beforeEach(async function () {
  468. this.req.params = { project_id: this.projectId }
  469. this.DocArchiveManager.archiveAllDocs = sinon.stub().resolves()
  470. await this.HttpController.archiveAllDocs(this.req, this.res, this.next)
  471. })
  472. it('should archive the project', function () {
  473. this.DocArchiveManager.archiveAllDocs
  474. .calledWith(this.projectId)
  475. .should.equal(true)
  476. })
  477. it('should return a 204 (No Content)', function () {
  478. this.res.sendStatus.calledWith(204).should.equal(true)
  479. })
  480. })
  481. describe('unArchiveAllDocs', function () {
  482. beforeEach(function () {
  483. this.req.params = { project_id: this.projectId }
  484. })
  485. describe('on success', function () {
  486. beforeEach(async function () {
  487. await this.HttpController.unArchiveAllDocs(
  488. this.req,
  489. this.res,
  490. this.next
  491. )
  492. })
  493. it('returns a 200', function () {
  494. expect(this.res.sendStatus).to.have.been.calledWith(200)
  495. })
  496. })
  497. describe("when the archived rev doesn't match", function () {
  498. beforeEach(async function () {
  499. this.DocArchiveManager.unArchiveAllDocs.rejects(
  500. new Errors.DocRevValueError('bad rev')
  501. )
  502. await this.HttpController.unArchiveAllDocs(
  503. this.req,
  504. this.res,
  505. this.next
  506. )
  507. })
  508. it('returns a 409', function () {
  509. expect(this.res.sendStatus).to.have.been.calledWith(409)
  510. })
  511. })
  512. })
  513. describe('destroyProject', function () {
  514. beforeEach(async function () {
  515. this.req.params = { project_id: this.projectId }
  516. this.DocArchiveManager.destroyProject = sinon.stub().resolves()
  517. await this.HttpController.destroyProject(this.req, this.res, this.next)
  518. })
  519. it('should destroy the docs', function () {
  520. sinon.assert.calledWith(
  521. this.DocArchiveManager.destroyProject,
  522. this.projectId
  523. )
  524. })
  525. it('should return 204', function () {
  526. sinon.assert.calledWith(this.res.sendStatus, 204)
  527. })
  528. })
  529. })