FileStoreHandlerTests.coffee 4.5 KB

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