FileStoreHandlerTests.coffee 4.6 KB

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