FileStoreHandlerTests.coffee 7.3 KB

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