FileControllerTests.coffee 5.0 KB

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