DocumentUpdaterControllerTests.mjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.doc = { name: 'myfile.tex' }
  40. })
  41. describe('getDoc', function () {
  42. beforeEach(function () {
  43. this.DocumentUpdaterHandler.promises.getDocument.resolves({
  44. lines: this.lines,
  45. })
  46. this.ProjectLocator.promises.findElement.resolves({
  47. element: this.doc,
  48. })
  49. this.res = new MockResponse()
  50. })
  51. it('should call the document updater handler with the project_id and doc_id', async function () {
  52. await this.controller.promises.getDoc(this.req, this.res)
  53. expect(
  54. this.DocumentUpdaterHandler.promises.getDocument
  55. ).to.have.been.calledOnceWith(
  56. this.req.params.Project_id,
  57. this.req.params.Doc_id,
  58. -1
  59. )
  60. })
  61. it('should return the content', async function () {
  62. await this.controller.promises.getDoc(this.req, this.res)
  63. expect(this.res.statusCode).to.equal(200)
  64. expect(this.res.body).to.equal('test\n\ntesting')
  65. })
  66. it('should find the doc in the project', async function () {
  67. await this.controller.promises.getDoc(this.req, this.res)
  68. expect(
  69. this.ProjectLocator.promises.findElement
  70. ).to.have.been.calledOnceWith({
  71. project_id: this.projectId,
  72. element_id: this.docId,
  73. type: 'doc',
  74. })
  75. })
  76. it('should set the Content-Disposition header', async function () {
  77. await this.controller.promises.getDoc(this.req, this.res)
  78. expect(this.res.setContentDisposition).to.have.been.calledWith(
  79. 'attachment',
  80. { filename: this.doc.name }
  81. )
  82. })
  83. })
  84. })