FileStoreHandlerTests.coffee 7.3 KB

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