FileStoreControllerTests.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/FileStoreController.js"
  7. SandboxedModule = require('sandboxed-module')
  8. describe "FileStoreController", ->
  9. beforeEach ->
  10. @FileStoreHandler =
  11. getFileStream: sinon.stub()
  12. @ProjectLocator =
  13. findElement: sinon.stub()
  14. @controller = SandboxedModule.require modulePath, requires:
  15. "settings-sharelatex": @settings
  16. "logger-sharelatex" : @logger = {log:sinon.stub(), err:sinon.stub()}
  17. "../Project/ProjectLocator": @ProjectLocator
  18. "./FileStoreHandler": @FileStoreHandler
  19. @stream = {}
  20. @project_id = "2k3j1lk3j21lk3j"
  21. @file_id = "12321kklj1lk3jk12"
  22. @req =
  23. params:
  24. Project_id: @project_id
  25. File_id: @file_id
  26. query: "query string here"
  27. @res =
  28. setHeader: sinon.stub()
  29. @file =
  30. name: "myfile.png"
  31. describe "getFile", ->
  32. beforeEach ->
  33. @FileStoreHandler.getFileStream.callsArgWith(3, null, @stream)
  34. @ProjectLocator.findElement.callsArgWith(1, null, @file)
  35. it "should call the file store handler with the project_id file_id and any query string", (done)->
  36. @stream.pipe = (des)=>
  37. @FileStoreHandler.getFileStream.calledWith(@req.params.Project_id, @req.params.File_id, @req.query).should.equal true
  38. done()
  39. @controller.getFile @req, @res
  40. it "should pipe to res", (done)->
  41. @stream.pipe = (des)=>
  42. des.should.equal @res
  43. done()
  44. @controller.getFile @req, @res
  45. it "should get the file from the db", (done)->
  46. @stream.pipe = (des)=>
  47. opts =
  48. project_id: @project_id
  49. element_id: @file_id
  50. type: "file"
  51. @ProjectLocator.findElement.calledWith(opts).should.equal true
  52. done()
  53. @controller.getFile @req, @res
  54. it "should set the Content-Disposition header", (done)->
  55. @stream.pipe = (des)=>
  56. @res.setHeader.calledWith("Content-Disposition", "attachment; filename=#{@file.name}").should.equal true
  57. done()
  58. @controller.getFile @req, @res