FileControllerTests.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. const sinon = require('sinon')
  2. const chai = require('chai')
  3. const { expect } = chai
  4. const SandboxedModule = require('sandboxed-module')
  5. const Errors = require('../../../app/js/Errors')
  6. const modulePath = '../../../app/js/FileController.js'
  7. describe('FileController', function () {
  8. let FileHandler, LocalFileWriter, FileController, req, res, next, stream
  9. const settings = {
  10. s3: {
  11. buckets: {
  12. template_files: 'template_files',
  13. },
  14. },
  15. }
  16. const fileSize = 1234
  17. const fileStream = {
  18. destroy() {},
  19. }
  20. const projectId = 'projectId'
  21. const fileId = 'file_id'
  22. const bucket = 'template_files'
  23. const key = `${projectId}/${fileId}`
  24. const error = new Error('incorrect utensil')
  25. beforeEach(function () {
  26. FileHandler = {
  27. getFile: sinon.stub().yields(null, fileStream),
  28. getFileSize: sinon.stub().yields(null, fileSize),
  29. insertFile: sinon.stub().yields(),
  30. getRedirectUrl: sinon.stub().yields(null, null),
  31. }
  32. LocalFileWriter = {}
  33. stream = {
  34. pipeline: sinon.stub(),
  35. }
  36. FileController = SandboxedModule.require(modulePath, {
  37. requires: {
  38. './LocalFileWriter': LocalFileWriter,
  39. './FileHandler': FileHandler,
  40. './Errors': Errors,
  41. stream,
  42. '@overleaf/settings': settings,
  43. '@overleaf/metrics': {
  44. inc() {},
  45. },
  46. },
  47. globals: { console },
  48. })
  49. req = {
  50. key,
  51. bucket,
  52. project_id: projectId,
  53. query: {},
  54. params: {
  55. project_id: projectId,
  56. file_id: fileId,
  57. },
  58. headers: {},
  59. requestLogger: {
  60. setMessage: sinon.stub(),
  61. addFields: sinon.stub(),
  62. },
  63. }
  64. res = {
  65. set: sinon.stub().returnsThis(),
  66. sendStatus: sinon.stub().returnsThis(),
  67. status: sinon.stub().returnsThis(),
  68. }
  69. next = sinon.stub()
  70. })
  71. describe('getFile', function () {
  72. it('should try and get a redirect url first', function () {
  73. FileController.getFile(req, res, next)
  74. expect(FileHandler.getRedirectUrl).to.have.been.calledWith(bucket, key)
  75. })
  76. it('should pipe the stream', function () {
  77. FileController.getFile(req, res, next)
  78. expect(stream.pipeline).to.have.been.calledWith(fileStream, res)
  79. })
  80. it('should send a 200 if the cacheWarm param is true', function (done) {
  81. req.query.cacheWarm = true
  82. res.sendStatus = statusCode => {
  83. statusCode.should.equal(200)
  84. done()
  85. }
  86. FileController.getFile(req, res, next)
  87. })
  88. it('should send an error if there is a problem', function () {
  89. FileHandler.getFile.yields(error)
  90. FileController.getFile(req, res, next)
  91. expect(next).to.have.been.calledWith(error)
  92. })
  93. describe('with a redirect url', function () {
  94. const redirectUrl = 'https://wombat.potato/giraffe'
  95. beforeEach(function () {
  96. FileHandler.getRedirectUrl.yields(null, redirectUrl)
  97. res.redirect = sinon.stub()
  98. })
  99. it('should redirect', function () {
  100. FileController.getFile(req, res, next)
  101. expect(res.redirect).to.have.been.calledWith(redirectUrl)
  102. })
  103. it('should not get a file stream', function () {
  104. FileController.getFile(req, res, next)
  105. expect(FileHandler.getFile).not.to.have.been.called
  106. })
  107. describe('when there is an error getting the redirect url', function () {
  108. beforeEach(function () {
  109. FileHandler.getRedirectUrl.yields(new Error('wombat herding error'))
  110. })
  111. it('should not redirect', function () {
  112. FileController.getFile(req, res, next)
  113. expect(res.redirect).not.to.have.been.called
  114. })
  115. it('should not return an error', function () {
  116. FileController.getFile(req, res, next)
  117. expect(next).not.to.have.been.called
  118. })
  119. it('should proxy the file', function () {
  120. FileController.getFile(req, res, next)
  121. expect(FileHandler.getFile).to.have.been.calledWith(bucket, key)
  122. })
  123. })
  124. })
  125. describe('with a range header', function () {
  126. let expectedOptions
  127. beforeEach(function () {
  128. expectedOptions = {
  129. bucket,
  130. key,
  131. format: undefined,
  132. style: undefined,
  133. }
  134. })
  135. it('should pass range options to FileHandler', function () {
  136. req.headers.range = 'bytes=0-8'
  137. expectedOptions.start = 0
  138. expectedOptions.end = 8
  139. FileController.getFile(req, res, next)
  140. expect(FileHandler.getFile).to.have.been.calledWith(
  141. bucket,
  142. key,
  143. expectedOptions
  144. )
  145. })
  146. it('should ignore an invalid range header', function () {
  147. req.headers.range = 'potato'
  148. FileController.getFile(req, res, next)
  149. expect(FileHandler.getFile).to.have.been.calledWith(
  150. bucket,
  151. key,
  152. expectedOptions
  153. )
  154. })
  155. it("should ignore any type other than 'bytes'", function () {
  156. req.headers.range = 'wombats=0-8'
  157. FileController.getFile(req, res, next)
  158. expect(FileHandler.getFile).to.have.been.calledWith(
  159. bucket,
  160. key,
  161. expectedOptions
  162. )
  163. })
  164. })
  165. })
  166. describe('getFileHead', function () {
  167. it('should return the file size in a Content-Length header', function (done) {
  168. res.end = () => {
  169. expect(res.status).to.have.been.calledWith(200)
  170. expect(res.set).to.have.been.calledWith('Content-Length', fileSize)
  171. done()
  172. }
  173. FileController.getFileHead(req, res, next)
  174. })
  175. it('should return a 404 is the file is not found', function (done) {
  176. FileHandler.getFileSize.yields(
  177. new Errors.NotFoundError({ message: 'not found', info: {} })
  178. )
  179. res.sendStatus = code => {
  180. expect(code).to.equal(404)
  181. done()
  182. }
  183. FileController.getFileHead(req, res, next)
  184. })
  185. it('should send an error on internal errors', function () {
  186. FileHandler.getFileSize.yields(error)
  187. FileController.getFileHead(req, res, next)
  188. expect(next).to.have.been.calledWith(error)
  189. })
  190. })
  191. describe('insertFile', function () {
  192. it('should send bucket name key and res to FileHandler', function (done) {
  193. res.sendStatus = code => {
  194. expect(FileHandler.insertFile).to.have.been.calledWith(bucket, key, req)
  195. expect(code).to.equal(200)
  196. done()
  197. }
  198. FileController.insertFile(req, res, next)
  199. })
  200. })
  201. })