FileStoreHandlerTests.coffee 7.0 KB

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