FileStoreHandlerTests.coffee 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/Features/FileStore/FileStoreHandler.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "FileStoreHandler", ->
  9. beforeEach ->
  10. @fs =
  11. createReadStream : sinon.stub()
  12. lstat: sinon.stub().callsArgWith(1, null, {
  13. isFile:=> @isSafeOnFileSystem
  14. isDirectory:-> return false
  15. })
  16. @writeStream =
  17. my:"writeStream"
  18. on: (type, cb)->
  19. if type == "end"
  20. cb()
  21. @readStream = {my:"readStream", on: sinon.stub()}
  22. @request = sinon.stub()
  23. @settings = apis:{filestore:{url:"http//filestore.sharelatex.test"}}
  24. @handler = SandboxedModule.require modulePath, requires:
  25. "settings-sharelatex":@settings
  26. "request":@request
  27. "logger-sharelatex" : @logger = {log:sinon.stub(), err:sinon.stub()}
  28. "fs" : @fs
  29. @file_id = "file_id_here"
  30. @project_id = "1312312312"
  31. @fsPath = "uploads/myfile.eps"
  32. @handler._buildUrl = sinon.stub().returns("http://filestore.stubbedBuilder.com")
  33. describe "uploadFileFromDisk", ->
  34. beforeEach ->
  35. @request.returns(@writeStream)
  36. @isSafeOnFileSystem = true
  37. it "should create read stream", (done)->
  38. @fs.createReadStream.returns
  39. pipe:->
  40. on: (type, cb)->
  41. if type == "end"
  42. cb()
  43. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  44. @fs.createReadStream.calledWith(@fsPath).should.equal true
  45. done()
  46. it "should pipe the read stream to request", (done)->
  47. @request.returns(@writeStream)
  48. @fs.createReadStream.returns
  49. on: (type, cb)->
  50. if type == "end"
  51. cb()
  52. pipe:(o)=>
  53. @writeStream.should.equal o
  54. done()
  55. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  56. it "should pass the correct options to request", (done)->
  57. @fs.createReadStream.returns
  58. pipe:->
  59. on: (type, cb)->
  60. if type == "end"
  61. cb()
  62. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  63. @request.args[0][0].method.should.equal "post"
  64. @request.args[0][0].uri.should.equal @handler._buildUrl()
  65. done()
  66. it "builds the correct url", (done)->
  67. @fs.createReadStream.returns
  68. pipe:->
  69. on: (type, cb)->
  70. if type == "end"
  71. cb()
  72. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  73. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  74. done()
  75. describe "symlink", ->
  76. it "should not read file if it is symlink", (done)->
  77. @isSafeOnFileSystem = false
  78. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  79. @fs.createReadStream.called.should.equal false
  80. done()
  81. describe "deleteFile", ->
  82. it "should send a delete request to filestore api", (done)->
  83. @request.callsArgWith(1, null)
  84. @handler.deleteFile @project_id, @file_id, (err)=>
  85. assert.equal err, undefined
  86. @request.args[0][0].method.should.equal "delete"
  87. @request.args[0][0].uri.should.equal @handler._buildUrl()
  88. done()
  89. it "should return the error if there is one", (done)->
  90. error = "my error"
  91. @request.callsArgWith(1, error)
  92. @handler.deleteFile @project_id, @file_id, (err)=>
  93. assert.equal err, error
  94. done()
  95. it "builds the correct url", (done)->
  96. @request.callsArgWith(1, null)
  97. @handler.deleteFile @project_id, @file_id, (err)=>
  98. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  99. done()
  100. describe "getFileStream", ->
  101. beforeEach ->
  102. @query = {}
  103. @request.returns(@readStream)
  104. it "should get the stream with the correct params", (done)->
  105. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  106. @request.args[0][0].method.should.equal "get"
  107. @request.args[0][0].uri.should.equal @handler._buildUrl()
  108. done()
  109. it "should get stream from request", (done)->
  110. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  111. stream.should.equal @readStream
  112. done()
  113. it "builds the correct url", (done)->
  114. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  115. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  116. done()
  117. it "should add an error handler", (done) ->
  118. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  119. stream.on.calledWith("error").should.equal true
  120. done()
  121. describe 'when range is specified in query', ->
  122. beforeEach ->
  123. @query = {'range': '0-10'}
  124. it 'should add a range header', (done) ->
  125. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  126. @request.callCount.should.equal 1
  127. headers = @request.firstCall.args[0].headers
  128. expect(headers).to.have.keys('range')
  129. expect(headers['range']).to.equal 'bytes=0-10'
  130. done()
  131. describe 'when range is invalid', ->
  132. ['0-', '-100', 'one-two', 'nonsense'].forEach (r) =>
  133. beforeEach ->
  134. @query = {'range': "#{r}"}
  135. it "should not add a range header for '#{r}'", (done) ->
  136. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  137. @request.callCount.should.equal 1
  138. headers = @request.firstCall.args[0].headers
  139. expect(headers).to.not.have.keys('range')
  140. done()
  141. describe "copyFile", ->
  142. beforeEach ->
  143. @newProject_id = "new project"
  144. @newFile_id = "new file id"
  145. it "should post json", (done)->
  146. @request.callsArgWith(1, null)
  147. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  148. @request.args[0][0].method.should.equal "put"
  149. @request.args[0][0].uri.should.equal @handler._buildUrl()
  150. @request.args[0][0].json.source.project_id.should.equal @project_id
  151. @request.args[0][0].json.source.file_id.should.equal @file_id
  152. done()
  153. it "builds the correct url", (done)->
  154. @request.callsArgWith(1, null)
  155. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  156. @handler._buildUrl.calledWith(@newProject_id, @newFile_id).should.equal true
  157. done()
  158. it "should return the err", (done)->
  159. error = "errrror"
  160. @request.callsArgWith(1, error)
  161. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=>
  162. err.should.equal error
  163. done()