HttpControllerTests.js 16 KB

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