FileControllerTests.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 PersistorManager,
  9. FileHandler,
  10. LocalFileWriter,
  11. FileController,
  12. req,
  13. res,
  14. next,
  15. stream
  16. const settings = {
  17. s3: {
  18. buckets: {
  19. user_files: 'user_files',
  20. },
  21. },
  22. }
  23. const fileSize = 1234
  24. const fileStream = {
  25. destroy() {},
  26. }
  27. const projectId = 'projectId'
  28. const fileId = 'file_id'
  29. const bucket = 'user_files'
  30. const key = `${projectId}/${fileId}`
  31. const error = new Error('incorrect utensil')
  32. beforeEach(function () {
  33. PersistorManager = {
  34. sendStream: sinon.stub().yields(),
  35. copyObject: sinon.stub().resolves(),
  36. deleteObject: sinon.stub().yields(),
  37. }
  38. FileHandler = {
  39. getFile: sinon.stub().yields(null, fileStream),
  40. getFileSize: sinon.stub().yields(null, fileSize),
  41. deleteFile: sinon.stub().yields(),
  42. deleteProject: sinon.stub().yields(),
  43. insertFile: sinon.stub().yields(),
  44. getDirectorySize: sinon.stub().yields(null, fileSize),
  45. getRedirectUrl: sinon.stub().yields(null, null),
  46. }
  47. LocalFileWriter = {}
  48. stream = {
  49. pipeline: sinon.stub(),
  50. }
  51. FileController = SandboxedModule.require(modulePath, {
  52. requires: {
  53. './LocalFileWriter': LocalFileWriter,
  54. './FileHandler': FileHandler,
  55. './PersistorManager': PersistorManager,
  56. './Errors': Errors,
  57. stream,
  58. '@overleaf/settings': settings,
  59. '@overleaf/metrics': {
  60. inc() {},
  61. },
  62. },
  63. globals: { console },
  64. })
  65. req = {
  66. key,
  67. bucket,
  68. project_id: projectId,
  69. query: {},
  70. params: {
  71. project_id: projectId,
  72. file_id: fileId,
  73. },
  74. headers: {},
  75. requestLogger: {
  76. setMessage: sinon.stub(),
  77. addFields: sinon.stub(),
  78. },
  79. }
  80. res = {
  81. set: sinon.stub().returnsThis(),
  82. sendStatus: sinon.stub().returnsThis(),
  83. status: sinon.stub().returnsThis(),
  84. }
  85. next = sinon.stub()
  86. })
  87. describe('getFile', function () {
  88. it('should try and get a redirect url first', function () {
  89. FileController.getFile(req, res, next)
  90. expect(FileHandler.getRedirectUrl).to.have.been.calledWith(bucket, key)
  91. })
  92. it('should pipe the stream', function () {
  93. FileController.getFile(req, res, next)
  94. expect(stream.pipeline).to.have.been.calledWith(fileStream, res)
  95. })
  96. it('should send a 200 if the cacheWarm param is true', function (done) {
  97. req.query.cacheWarm = true
  98. res.sendStatus = statusCode => {
  99. statusCode.should.equal(200)
  100. done()
  101. }
  102. FileController.getFile(req, res, next)
  103. })
  104. it('should send an error if there is a problem', function () {
  105. FileHandler.getFile.yields(error)
  106. FileController.getFile(req, res, next)
  107. expect(next).to.have.been.calledWith(error)
  108. })
  109. describe('with a redirect url', function () {
  110. const redirectUrl = 'https://wombat.potato/giraffe'
  111. beforeEach(function () {
  112. FileHandler.getRedirectUrl.yields(null, redirectUrl)
  113. res.redirect = sinon.stub()
  114. })
  115. it('should redirect', function () {
  116. FileController.getFile(req, res, next)
  117. expect(res.redirect).to.have.been.calledWith(redirectUrl)
  118. })
  119. it('should not get a file stream', function () {
  120. FileController.getFile(req, res, next)
  121. expect(FileHandler.getFile).not.to.have.been.called
  122. })
  123. describe('when there is an error getting the redirect url', function () {
  124. beforeEach(function () {
  125. FileHandler.getRedirectUrl.yields(new Error('wombat herding error'))
  126. })
  127. it('should not redirect', function () {
  128. FileController.getFile(req, res, next)
  129. expect(res.redirect).not.to.have.been.called
  130. })
  131. it('should not return an error', function () {
  132. FileController.getFile(req, res, next)
  133. expect(next).not.to.have.been.called
  134. })
  135. it('should proxy the file', function () {
  136. FileController.getFile(req, res, next)
  137. expect(FileHandler.getFile).to.have.been.calledWith(bucket, key)
  138. })
  139. })
  140. })
  141. describe('with a range header', function () {
  142. let expectedOptions
  143. beforeEach(function () {
  144. expectedOptions = {
  145. bucket,
  146. key,
  147. format: undefined,
  148. style: undefined,
  149. }
  150. })
  151. it('should pass range options to FileHandler', function () {
  152. req.headers.range = 'bytes=0-8'
  153. expectedOptions.start = 0
  154. expectedOptions.end = 8
  155. FileController.getFile(req, res, next)
  156. expect(FileHandler.getFile).to.have.been.calledWith(
  157. bucket,
  158. key,
  159. expectedOptions
  160. )
  161. })
  162. it('should ignore an invalid range header', function () {
  163. req.headers.range = 'potato'
  164. FileController.getFile(req, res, next)
  165. expect(FileHandler.getFile).to.have.been.calledWith(
  166. bucket,
  167. key,
  168. expectedOptions
  169. )
  170. })
  171. it("should ignore any type other than 'bytes'", function () {
  172. req.headers.range = 'wombats=0-8'
  173. FileController.getFile(req, res, next)
  174. expect(FileHandler.getFile).to.have.been.calledWith(
  175. bucket,
  176. key,
  177. expectedOptions
  178. )
  179. })
  180. })
  181. })
  182. describe('getFileHead', function () {
  183. it('should return the file size in a Content-Length header', function (done) {
  184. res.end = () => {
  185. expect(res.status).to.have.been.calledWith(200)
  186. expect(res.set).to.have.been.calledWith('Content-Length', fileSize)
  187. done()
  188. }
  189. FileController.getFileHead(req, res, next)
  190. })
  191. it('should return a 404 is the file is not found', function (done) {
  192. FileHandler.getFileSize.yields(
  193. new Errors.NotFoundError({ message: 'not found', info: {} })
  194. )
  195. res.sendStatus = code => {
  196. expect(code).to.equal(404)
  197. done()
  198. }
  199. FileController.getFileHead(req, res, next)
  200. })
  201. it('should send an error on internal errors', function () {
  202. FileHandler.getFileSize.yields(error)
  203. FileController.getFileHead(req, res, next)
  204. expect(next).to.have.been.calledWith(error)
  205. })
  206. })
  207. describe('insertFile', function () {
  208. it('should send bucket name key and res to PersistorManager', function (done) {
  209. res.sendStatus = code => {
  210. expect(FileHandler.insertFile).to.have.been.calledWith(bucket, key, req)
  211. expect(code).to.equal(200)
  212. done()
  213. }
  214. FileController.insertFile(req, res, next)
  215. })
  216. })
  217. describe('copyFile', function () {
  218. const oldFileId = 'oldFileId'
  219. const oldProjectId = 'oldProjectid'
  220. const oldKey = `${oldProjectId}/${oldFileId}`
  221. beforeEach(function () {
  222. req.body = {
  223. source: {
  224. project_id: oldProjectId,
  225. file_id: oldFileId,
  226. },
  227. }
  228. })
  229. it('should send bucket name and both keys to PersistorManager', function (done) {
  230. res.sendStatus = code => {
  231. code.should.equal(200)
  232. expect(PersistorManager.copyObject).to.have.been.calledWith(
  233. bucket,
  234. oldKey,
  235. key
  236. )
  237. done()
  238. }
  239. FileController.copyFile(req, res, next)
  240. })
  241. it('should send a 404 if the original file was not found', function (done) {
  242. PersistorManager.copyObject.rejects(
  243. new Errors.NotFoundError({ message: 'not found', info: {} })
  244. )
  245. res.sendStatus = code => {
  246. code.should.equal(404)
  247. done()
  248. }
  249. FileController.copyFile(req, res, next)
  250. })
  251. it('should send an error if there was an error', function (done) {
  252. PersistorManager.copyObject.rejects(error)
  253. FileController.copyFile(req, res, err => {
  254. expect(err).to.equal(error)
  255. done()
  256. })
  257. })
  258. })
  259. describe('delete file', function () {
  260. it('should tell the file handler', function (done) {
  261. res.sendStatus = code => {
  262. code.should.equal(204)
  263. expect(FileHandler.deleteFile).to.have.been.calledWith(bucket, key)
  264. done()
  265. }
  266. FileController.deleteFile(req, res, next)
  267. })
  268. it('should send a 500 if there was an error', function () {
  269. FileHandler.deleteFile.yields(error)
  270. FileController.deleteFile(req, res, next)
  271. expect(next).to.have.been.calledWith(error)
  272. })
  273. })
  274. describe('delete project', function () {
  275. it('should tell the file handler', function (done) {
  276. res.sendStatus = code => {
  277. code.should.equal(204)
  278. expect(FileHandler.deleteProject).to.have.been.calledWith(bucket, key)
  279. done()
  280. }
  281. FileController.deleteProject(req, res, next)
  282. })
  283. it('should send a 500 if there was an error', function () {
  284. FileHandler.deleteProject.yields(error)
  285. FileController.deleteProject(req, res, next)
  286. expect(next).to.have.been.calledWith(error)
  287. })
  288. })
  289. describe('directorySize', function () {
  290. it('should return total directory size bytes', function (done) {
  291. FileController.directorySize(req, {
  292. json: result => {
  293. expect(result['total bytes']).to.equal(fileSize)
  294. done()
  295. },
  296. })
  297. })
  298. it('should send a 500 if there was an error', function () {
  299. FileHandler.getDirectorySize.yields(error)
  300. FileController.directorySize(req, res, next)
  301. expect(next).to.have.been.calledWith(error)
  302. })
  303. })
  304. })