FileStoreHandlerTests.coffee 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 == "response"
  20. cb({statusCode: 200})
  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. it 'should callback with null', (done) ->
  76. @fs.createReadStream.returns
  77. pipe:->
  78. on: (type, cb)->
  79. if type == "end"
  80. cb()
  81. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, (err) =>
  82. expect(err).to.not.exist
  83. done()
  84. describe "symlink", ->
  85. it "should not read file if it is symlink", (done)->
  86. @isSafeOnFileSystem = false
  87. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  88. @fs.createReadStream.called.should.equal false
  89. done()
  90. describe "when upload fails", ->
  91. beforeEach ->
  92. @writeStream.on = (type, cb) ->
  93. if type == "response"
  94. cb({statusCode: 500})
  95. it 'should callback with an error', (done) ->
  96. @fs.createReadStream.returns
  97. pipe:->
  98. on: (type, cb)->
  99. if type == "end"
  100. cb()
  101. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, (err) =>
  102. expect(err).to.exist
  103. expect(err).to.be.instanceof Error
  104. done()
  105. describe "deleteFile", ->
  106. it "should send a delete request to filestore api", (done)->
  107. @request.callsArgWith(1, null)
  108. @handler.deleteFile @project_id, @file_id, (err)=>
  109. assert.equal err, undefined
  110. @request.args[0][0].method.should.equal "delete"
  111. @request.args[0][0].uri.should.equal @handler._buildUrl()
  112. done()
  113. it "should return the error if there is one", (done)->
  114. error = "my error"
  115. @request.callsArgWith(1, error)
  116. @handler.deleteFile @project_id, @file_id, (err)=>
  117. assert.equal err, error
  118. done()
  119. it "builds the correct url", (done)->
  120. @request.callsArgWith(1, null)
  121. @handler.deleteFile @project_id, @file_id, (err)=>
  122. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  123. done()
  124. describe "getFileStream", ->
  125. beforeEach ->
  126. @query = {}
  127. @request.returns(@readStream)
  128. it "should get the stream with the correct params", (done)->
  129. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  130. @request.args[0][0].method.should.equal "get"
  131. @request.args[0][0].uri.should.equal @handler._buildUrl()
  132. done()
  133. it "should get stream from request", (done)->
  134. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  135. stream.should.equal @readStream
  136. done()
  137. it "builds the correct url", (done)->
  138. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  139. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  140. done()
  141. it "should add an error handler", (done) ->
  142. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  143. stream.on.calledWith("error").should.equal true
  144. done()
  145. describe 'when range is specified in query', ->
  146. beforeEach ->
  147. @query = {'range': '0-10'}
  148. it 'should add a range header', (done) ->
  149. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  150. @request.callCount.should.equal 1
  151. headers = @request.firstCall.args[0].headers
  152. expect(headers).to.have.keys('range')
  153. expect(headers['range']).to.equal 'bytes=0-10'
  154. done()
  155. describe 'when range is invalid', ->
  156. ['0-', '-100', 'one-two', 'nonsense'].forEach (r) =>
  157. beforeEach ->
  158. @query = {'range': "#{r}"}
  159. it "should not add a range header for '#{r}'", (done) ->
  160. @handler.getFileStream @project_id, @file_id, @query, (err, stream)=>
  161. @request.callCount.should.equal 1
  162. headers = @request.firstCall.args[0].headers
  163. expect(headers).to.not.have.keys('range')
  164. done()
  165. describe "copyFile", ->
  166. beforeEach ->
  167. @newProject_id = "new project"
  168. @newFile_id = "new file id"
  169. it "should post json", (done)->
  170. @request.callsArgWith(1, null)
  171. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  172. @request.args[0][0].method.should.equal "put"
  173. @request.args[0][0].uri.should.equal @handler._buildUrl()
  174. @request.args[0][0].json.source.project_id.should.equal @project_id
  175. @request.args[0][0].json.source.file_id.should.equal @file_id
  176. done()
  177. it "builds the correct url", (done)->
  178. @request.callsArgWith(1, null)
  179. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  180. @handler._buildUrl.calledWith(@newProject_id, @newFile_id).should.equal true
  181. done()
  182. it "should return the err", (done)->
  183. error = "errrror"
  184. @request.callsArgWith(1, error)
  185. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=>
  186. err.should.equal error
  187. done()