DocumentUpdaterControllerTests.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import esmock from 'esmock'
  4. import MockResponse from '../helpers/MockResponse.js'
  5. const MODULE_PATH =
  6. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterController.mjs'
  7. describe('DocumentUpdaterController', function () {
  8. beforeEach(async function () {
  9. this.DocumentUpdaterHandler = {
  10. promises: {
  11. getDocument: sinon.stub(),
  12. },
  13. }
  14. this.ProjectLocator = {
  15. promises: {
  16. findElement: sinon.stub(),
  17. },
  18. }
  19. this.controller = await esmock.strict(MODULE_PATH, {
  20. '@overleaf/settings': this.settings,
  21. '../../../../app/src/Features/Project/ProjectLocator.js':
  22. this.ProjectLocator,
  23. '../../../../app/src/Features/DocumentUpdater/DocumentUpdaterHandler.js':
  24. this.DocumentUpdaterHandler,
  25. })
  26. this.projectId = '2k3j1lk3j21lk3j'
  27. this.fileId = '12321kklj1lk3jk12'
  28. this.req = {
  29. params: {
  30. Project_id: this.projectId,
  31. Doc_id: this.docId,
  32. },
  33. get(key) {
  34. return undefined
  35. },
  36. }
  37. this.lines = ['test', '', 'testing']
  38. this.res = new MockResponse()
  39. this.next = sinon.stub()
  40. this.doc = { name: 'myfile.tex' }
  41. })
  42. describe('getDoc', function () {
  43. beforeEach(function () {
  44. this.DocumentUpdaterHandler.promises.getDocument.resolves({
  45. lines: this.lines,
  46. })
  47. this.ProjectLocator.promises.findElement.resolves({
  48. element: this.doc,
  49. })
  50. this.res = new MockResponse()
  51. })
  52. it('should call the document updater handler with the project_id and doc_id', async function () {
  53. await this.controller.getDoc(this.req, this.res, this.next)
  54. expect(
  55. this.DocumentUpdaterHandler.promises.getDocument
  56. ).to.have.been.calledOnceWith(
  57. this.req.params.Project_id,
  58. this.req.params.Doc_id,
  59. -1
  60. )
  61. })
  62. it('should return the content', async function () {
  63. await this.controller.getDoc(this.req, this.res)
  64. expect(this.next).to.not.have.been.called
  65. expect(this.res.statusCode).to.equal(200)
  66. expect(this.res.body).to.equal('test\n\ntesting')
  67. })
  68. it('should find the doc in the project', async function () {
  69. await this.controller.getDoc(this.req, this.res)
  70. expect(
  71. this.ProjectLocator.promises.findElement
  72. ).to.have.been.calledOnceWith({
  73. project_id: this.projectId,
  74. element_id: this.docId,
  75. type: 'doc',
  76. })
  77. })
  78. it('should set the Content-Disposition header', async function () {
  79. await this.controller.getDoc(this.req, this.res)
  80. expect(this.res.setContentDisposition).to.have.been.calledWith(
  81. 'attachment',
  82. { filename: this.doc.name }
  83. )
  84. })
  85. })
  86. })