FileControllerTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. assert = require("chai").assert
  2. sinon = require('sinon')
  3. chai = require('chai')
  4. should = chai.should()
  5. expect = chai.expect
  6. modulePath = "../../../app/js/FileController.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "FileController", ->
  9. beforeEach ->
  10. @PersistorManager =
  11. sendStream: sinon.stub()
  12. copyFile: sinon.stub()
  13. deleteFile:sinon.stub()
  14. @settings =
  15. s3:
  16. buckets:
  17. user_files:"user_files"
  18. @FileHandler =
  19. getFile: sinon.stub()
  20. deleteFile: sinon.stub()
  21. insertFile: sinon.stub()
  22. getDirectorySize: sinon.stub()
  23. @LocalFileWriter = {}
  24. @controller = SandboxedModule.require modulePath, requires:
  25. "./LocalFileWriter":@LocalFileWriter
  26. "./FileHandler": @FileHandler
  27. "./PersistorManager":@PersistorManager
  28. "./Errors": @Errors =
  29. NotFoundError: sinon.stub()
  30. "settings-sharelatex": @settings
  31. "metrics-sharelatex":
  32. inc:->
  33. "logger-sharelatex":
  34. log:->
  35. err:->
  36. @project_id = "project_id"
  37. @file_id = "file_id"
  38. @bucket = "user_files"
  39. @key = "#{@project_id}/#{@file_id}"
  40. @req =
  41. key:@key
  42. bucket:@bucket
  43. query:{}
  44. params:
  45. project_id:@project_id
  46. file_id:@file_id
  47. headers: {}
  48. @res =
  49. setHeader: ->
  50. @fileStream = {}
  51. describe "getFile", ->
  52. it "should pipe the stream", (done)->
  53. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  54. @fileStream.pipe = (res)=>
  55. res.should.equal @res
  56. done()
  57. @controller.getFile @req, @res
  58. it "should send a 200 if the cacheWarm param is true", (done)->
  59. @req.query.cacheWarm = true
  60. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  61. @res.send = (statusCode)=>
  62. statusCode.should.equal 200
  63. done()
  64. @controller.getFile @req, @res
  65. it "should send a 500 if there is a problem", (done)->
  66. @FileHandler.getFile.callsArgWith(3, "error")
  67. @res.send = (code)=>
  68. code.should.equal 500
  69. done()
  70. @controller.getFile @req, @res
  71. describe "with a 'Range' header set", ->
  72. beforeEach ->
  73. @req.headers.range = 'bytes=0-8'
  74. it "should pass 'start' and 'end' options to FileHandler", (done) ->
  75. @FileHandler.getFile.callsArgWith(3, null, @fileStream)
  76. @fileStream.pipe = (res)=>
  77. expect(@FileHandler.getFile.lastCall.args[2].start).to.equal 0
  78. expect(@FileHandler.getFile.lastCall.args[2].end).to.equal 8
  79. done()
  80. @controller.getFile @req, @res
  81. describe "insertFile", ->
  82. it "should send bucket name key and res to PersistorManager", (done)->
  83. @FileHandler.insertFile.callsArgWith(3)
  84. @res.send = =>
  85. @FileHandler.insertFile.calledWith(@bucket, @key, @req).should.equal true
  86. done()
  87. @controller.insertFile @req, @res
  88. describe "copyFile", ->
  89. beforeEach ->
  90. @oldFile_id = "old_file_id"
  91. @oldProject_id = "old_project_id"
  92. @req.body =
  93. source:
  94. project_id: @oldProject_id
  95. file_id: @oldFile_id
  96. it "should send bucket name and both keys to PersistorManager", (done)->
  97. @PersistorManager.copyFile.callsArgWith(3)
  98. @res.send = (code)=>
  99. code.should.equal 200
  100. @PersistorManager.copyFile.calledWith(@bucket, "#{@oldProject_id}/#{@oldFile_id}", @key).should.equal true
  101. done()
  102. @controller.copyFile @req, @res
  103. it "should send a 404 if the original file was not found", (done) ->
  104. @PersistorManager.copyFile.callsArgWith(3, new @Errors.NotFoundError())
  105. @res.send = (code)=>
  106. code.should.equal 404
  107. done()
  108. @controller.copyFile @req, @res
  109. it "should send a 500 if there was an error", (done)->
  110. @PersistorManager.copyFile.callsArgWith(3, "error")
  111. @res.send = (code)=>
  112. code.should.equal 500
  113. done()
  114. @controller.copyFile @req, @res
  115. describe "delete file", ->
  116. it "should tell the file handler", (done)->
  117. @FileHandler.deleteFile.callsArgWith(2)
  118. @res.send = (code)=>
  119. code.should.equal 204
  120. @FileHandler.deleteFile.calledWith(@bucket, @key).should.equal true
  121. done()
  122. @controller.deleteFile @req, @res
  123. it "should send a 500 if there was an error", (done)->
  124. @FileHandler.deleteFile.callsArgWith(2, "error")
  125. @res.send = (code)->
  126. code.should.equal 500
  127. done()
  128. @controller.deleteFile @req, @res
  129. describe "_get_range", ->
  130. it "should parse a valid Range header", (done) ->
  131. result = @controller._get_range('bytes=0-200')
  132. expect(result).to.not.equal null
  133. expect(result.start).to.equal 0
  134. expect(result.end).to.equal 200
  135. done()
  136. it "should return null for an invalid Range header", (done) ->
  137. result = @controller._get_range('wat')
  138. expect(result).to.equal null
  139. done()
  140. it "should return null for any type other than 'bytes'", (done) ->
  141. result = @controller._get_range('carrots=0-200')
  142. expect(result).to.equal null
  143. done()
  144. describe "directorySize", ->
  145. it "should return total directory size bytes", (done) ->
  146. @FileHandler.getDirectorySize.callsArgWith(2, null, 1024)
  147. @controller.directorySize @req, json:(result)=>
  148. expect(result['total bytes']).to.equal 1024
  149. done()
  150. it "should send a 500 if there was an error", (done)->
  151. @FileHandler.getDirectorySize.callsArgWith(2, "error")
  152. @res.send = (code)->
  153. code.should.equal 500
  154. done()
  155. @controller.directorySize @req, @res