FileStoreHandlerTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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"}
  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. @request.returns(@readStream)
  103. it "should get the stream with the correct params", (done)->
  104. @handler.getFileStream @project_id, @file_id, {}, (err, stream)=>
  105. @request.args[0][0].method.should.equal "get"
  106. @request.args[0][0].uri.should.equal @handler._buildUrl()
  107. done()
  108. it "should get stream from request", (done)->
  109. @handler.getFileStream @project_id, @file_id, {}, (err, stream)=>
  110. stream.should.equal @readStream
  111. done()
  112. it "builds the correct url", (done)->
  113. @handler.getFileStream @project_id, @file_id, {}, (err, stream)=>
  114. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  115. done()
  116. describe "copyFile", ->
  117. beforeEach ->
  118. @newProject_id = "new project"
  119. @newFile_id = "new file id"
  120. it "should post json", (done)->
  121. @request.callsArgWith(1, null)
  122. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  123. @request.args[0][0].method.should.equal "put"
  124. @request.args[0][0].uri.should.equal @handler._buildUrl()
  125. @request.args[0][0].json.source.project_id.should.equal @project_id
  126. @request.args[0][0].json.source.file_id.should.equal @file_id
  127. done()
  128. it "builds the correct url", (done)->
  129. @request.callsArgWith(1, null)
  130. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  131. @handler._buildUrl.calledWith(@newProject_id, @newFile_id).should.equal true
  132. done()
  133. it "should return the err", (done)->
  134. error = "errrror"
  135. @request.callsArgWith(1, error)
  136. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=>
  137. err.should.equal error
  138. done()