FileControllerTests.js 7.9 KB

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