FileStoreHandlerTests.coffee 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, {isSymbolicLink:=>@isSymlink})
  13. @writeStream =
  14. my:"writeStream"
  15. on: (type, cb)->
  16. if type == "end"
  17. cb()
  18. @readStream = {my:"readStream"}
  19. @request = sinon.stub()
  20. @settings = apis:{filestore:{url:"http//filestore.sharelatex.test"}}
  21. @handler = SandboxedModule.require modulePath, requires:
  22. "settings-sharelatex":@settings
  23. "request":@request
  24. "logger-sharelatex" : @logger = {log:sinon.stub(), err:sinon.stub()}
  25. "fs" : @fs
  26. @file_id = "file_id_here"
  27. @project_id = "1312312312"
  28. @fsPath = "uploads/myfile.eps"
  29. @handler._buildUrl = sinon.stub().returns("http://filestore.stubbedBuilder.com")
  30. describe "uploadFileFromDisk", ->
  31. beforeEach ->
  32. @request.returns(@writeStream)
  33. @isSymlink = false
  34. it "should create read stream", (done)->
  35. @fs.createReadStream.returns
  36. pipe:->
  37. on: (type, cb)->
  38. if type == "end"
  39. cb()
  40. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  41. @fs.createReadStream.calledWith(@fsPath).should.equal true
  42. done()
  43. it "should pipe the read stream to request", (done)->
  44. @request.returns(@writeStream)
  45. @fs.createReadStream.returns
  46. on: (type, cb)->
  47. if type == "end"
  48. cb()
  49. pipe:(o)=>
  50. @writeStream.should.equal o
  51. done()
  52. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  53. it "should pass the correct options to request", (done)->
  54. @fs.createReadStream.returns
  55. pipe:->
  56. on: (type, cb)->
  57. if type == "end"
  58. cb()
  59. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  60. @request.args[0][0].method.should.equal "post"
  61. @request.args[0][0].uri.should.equal @handler._buildUrl()
  62. done()
  63. it "builds the correct url", (done)->
  64. @fs.createReadStream.returns
  65. pipe:->
  66. on: (type, cb)->
  67. if type == "end"
  68. cb()
  69. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  70. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  71. done()
  72. describe "symlink", ->
  73. it "should not read file if it is symlink", (done)->
  74. @isSymlink = true
  75. @handler.uploadFileFromDisk @project_id, @file_id, @fsPath, =>
  76. @fs.createReadStream.called.should.equal false
  77. done()
  78. describe "deleteFile", ->
  79. it "should send a delete request to filestore api", (done)->
  80. @request.callsArgWith(1, null)
  81. @handler.deleteFile @project_id, @file_id, (err)=>
  82. assert.equal err, undefined
  83. @request.args[0][0].method.should.equal "delete"
  84. @request.args[0][0].uri.should.equal @handler._buildUrl()
  85. done()
  86. it "should return the error if there is one", (done)->
  87. error = "my error"
  88. @request.callsArgWith(1, error)
  89. @handler.deleteFile @project_id, @file_id, (err)=>
  90. assert.equal err, error
  91. done()
  92. it "builds the correct url", (done)->
  93. @request.callsArgWith(1, null)
  94. @handler.deleteFile @project_id, @file_id, (err)=>
  95. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  96. done()
  97. describe "getFileStream", ->
  98. beforeEach ->
  99. @request.returns(@readStream)
  100. it "should get the stream with the correct params", (done)->
  101. @handler.getFileStream @project_id, @file_id, {}, (err, stream)=>
  102. @request.args[0][0].method.should.equal "get"
  103. @request.args[0][0].uri.should.equal @handler._buildUrl()
  104. done()
  105. it "should get stream from request", (done)->
  106. @handler.getFileStream @project_id, @file_id, {}, (err, stream)=>
  107. stream.should.equal @readStream
  108. done()
  109. it "builds the correct url", (done)->
  110. @handler.getFileStream @project_id, @file_id, {}, (err, stream)=>
  111. @handler._buildUrl.calledWith(@project_id, @file_id).should.equal true
  112. done()
  113. describe "copyFile", ->
  114. beforeEach ->
  115. @newProject_id = "new project"
  116. @newFile_id = "new file id"
  117. it "should post json", (done)->
  118. @request.callsArgWith(1, null)
  119. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  120. @request.args[0][0].method.should.equal "put"
  121. @request.args[0][0].uri.should.equal @handler._buildUrl()
  122. @request.args[0][0].json.source.project_id.should.equal @project_id
  123. @request.args[0][0].json.source.file_id.should.equal @file_id
  124. done()
  125. it "builds the correct url", (done)->
  126. @request.callsArgWith(1, null)
  127. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, =>
  128. @handler._buildUrl.calledWith(@newProject_id, @newFile_id).should.equal true
  129. done()
  130. it "should return the err", (done)->
  131. error = "errrror"
  132. @request.callsArgWith(1, error)
  133. @handler.copyFile @project_id, @file_id, @newProject_id, @newFile_id, (err)=>
  134. err.should.equal error
  135. done()