FileControllerTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 = 'fileStream'
  25. const projectId = 'projectId'
  26. const fileId = 'file_id'
  27. const bucket = 'user_files'
  28. const key = `${projectId}/${fileId}`
  29. const error = new Error('incorrect utensil')
  30. beforeEach(function() {
  31. PersistorManager = {
  32. sendStream: sinon.stub().yields(),
  33. copyFile: sinon.stub().yields(),
  34. deleteFile: sinon.stub().yields()
  35. }
  36. FileHandler = {
  37. getFile: sinon.stub().yields(null, fileStream),
  38. getFileSize: sinon.stub().yields(null, fileSize),
  39. deleteFile: sinon.stub().yields(),
  40. insertFile: sinon.stub().yields(),
  41. getDirectorySize: sinon.stub().yields(null, fileSize)
  42. }
  43. LocalFileWriter = {}
  44. stream = {
  45. pipeline: sinon.stub()
  46. }
  47. FileController = SandboxedModule.require(modulePath, {
  48. requires: {
  49. './LocalFileWriter': LocalFileWriter,
  50. './FileHandler': FileHandler,
  51. './PersistorManager': PersistorManager,
  52. './Errors': Errors,
  53. stream: stream,
  54. 'settings-sharelatex': settings,
  55. 'metrics-sharelatex': {
  56. inc() {}
  57. }
  58. },
  59. globals: { console }
  60. })
  61. req = {
  62. key: key,
  63. bucket: bucket,
  64. query: {},
  65. params: {
  66. project_id: projectId,
  67. file_id: fileId
  68. },
  69. headers: {},
  70. requestLogger: {
  71. setMessage: sinon.stub(),
  72. addFields: sinon.stub()
  73. }
  74. }
  75. res = {
  76. set: sinon.stub().returnsThis(),
  77. sendStatus: sinon.stub().returnsThis(),
  78. status: sinon.stub().returnsThis()
  79. }
  80. next = sinon.stub()
  81. })
  82. describe('getFile', function() {
  83. it('should pipe the stream', function() {
  84. FileController.getFile(req, res, next)
  85. expect(stream.pipeline).to.have.been.calledWith(fileStream, res)
  86. })
  87. it('should send a 200 if the cacheWarm param is true', function(done) {
  88. req.query.cacheWarm = true
  89. res.sendStatus = statusCode => {
  90. statusCode.should.equal(200)
  91. done()
  92. }
  93. FileController.getFile(req, res, next)
  94. })
  95. it('should send an error if there is a problem', function() {
  96. FileHandler.getFile.yields(error)
  97. FileController.getFile(req, res, next)
  98. expect(next).to.have.been.calledWith(error)
  99. })
  100. describe('with a range header', function() {
  101. let expectedOptions
  102. beforeEach(function() {
  103. expectedOptions = {
  104. bucket,
  105. key,
  106. format: undefined,
  107. style: undefined
  108. }
  109. })
  110. it('should pass range options to FileHandler', function() {
  111. req.headers.range = 'bytes=0-8'
  112. expectedOptions.start = 0
  113. expectedOptions.end = 8
  114. FileController.getFile(req, res, next)
  115. expect(FileHandler.getFile).to.have.been.calledWith(
  116. bucket,
  117. key,
  118. expectedOptions
  119. )
  120. })
  121. it('should ignore an invalid range header', function() {
  122. req.headers.range = 'potato'
  123. FileController.getFile(req, res, next)
  124. expect(FileHandler.getFile).to.have.been.calledWith(
  125. bucket,
  126. key,
  127. expectedOptions
  128. )
  129. })
  130. it("should ignore any type other than 'bytes'", function() {
  131. req.headers.range = 'wombats=0-8'
  132. FileController.getFile(req, res, next)
  133. expect(FileHandler.getFile).to.have.been.calledWith(
  134. bucket,
  135. key,
  136. expectedOptions
  137. )
  138. })
  139. })
  140. })
  141. describe('getFileHead', function() {
  142. it('should return the file size in a Content-Length header', function(done) {
  143. res.end = () => {
  144. expect(res.status).to.have.been.calledWith(200)
  145. expect(res.set).to.have.been.calledWith('Content-Length', fileSize)
  146. done()
  147. }
  148. FileController.getFileHead(req, res, next)
  149. })
  150. it('should return a 404 is the file is not found', function(done) {
  151. FileHandler.getFileSize.yields(new Errors.NotFoundError())
  152. res.sendStatus = code => {
  153. expect(code).to.equal(404)
  154. done()
  155. }
  156. FileController.getFileHead(req, res, next)
  157. })
  158. it('should send an error on internal errors', function() {
  159. FileHandler.getFileSize.yields(error)
  160. FileController.getFileHead(req, res, next)
  161. expect(next).to.have.been.calledWith(error)
  162. })
  163. })
  164. describe('insertFile', function() {
  165. it('should send bucket name key and res to PersistorManager', function(done) {
  166. res.sendStatus = code => {
  167. expect(FileHandler.insertFile).to.have.been.calledWith(bucket, key, req)
  168. expect(code).to.equal(200)
  169. done()
  170. }
  171. FileController.insertFile(req, res, next)
  172. })
  173. })
  174. describe('copyFile', function() {
  175. const oldFileId = 'oldFileId'
  176. const oldProjectId = 'oldProjectid'
  177. const oldKey = `${oldProjectId}/${oldFileId}`
  178. beforeEach(function() {
  179. req.body = {
  180. source: {
  181. project_id: oldProjectId,
  182. file_id: oldFileId
  183. }
  184. }
  185. })
  186. it('should send bucket name and both keys to PersistorManager', function(done) {
  187. res.sendStatus = code => {
  188. code.should.equal(200)
  189. expect(PersistorManager.copyFile).to.have.been.calledWith(
  190. bucket,
  191. oldKey,
  192. key
  193. )
  194. done()
  195. }
  196. FileController.copyFile(req, res, next)
  197. })
  198. it('should send a 404 if the original file was not found', function(done) {
  199. PersistorManager.copyFile.yields(new Errors.NotFoundError())
  200. res.sendStatus = code => {
  201. code.should.equal(404)
  202. done()
  203. }
  204. FileController.copyFile(req, res, next)
  205. })
  206. it('should send an error if there was an error', function() {
  207. PersistorManager.copyFile.yields(error)
  208. FileController.copyFile(req, res, next)
  209. expect(next).to.have.been.calledWith(error)
  210. })
  211. })
  212. describe('delete file', function() {
  213. it('should tell the file handler', function(done) {
  214. res.sendStatus = code => {
  215. code.should.equal(204)
  216. expect(FileHandler.deleteFile).to.have.been.calledWith(bucket, key)
  217. done()
  218. }
  219. FileController.deleteFile(req, res, next)
  220. })
  221. it('should send a 500 if there was an error', function() {
  222. FileHandler.deleteFile.yields(error)
  223. FileController.deleteFile(req, res, next)
  224. expect(next).to.have.been.calledWith(error)
  225. })
  226. })
  227. describe('directorySize', function() {
  228. it('should return total directory size bytes', function(done) {
  229. FileController.directorySize(req, {
  230. json: result => {
  231. expect(result['total bytes']).to.equal(fileSize)
  232. done()
  233. }
  234. })
  235. })
  236. it('should send a 500 if there was an error', function() {
  237. FileHandler.getDirectorySize.yields(error)
  238. FileController.directorySize(req, res, next)
  239. expect(next).to.have.been.calledWith(error)
  240. })
  241. })
  242. })