FileStoreControllerTests.coffee 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. get: (key) -> undefined
  28. @res =
  29. setHeader: sinon.stub()
  30. @file =
  31. name: "myfile.png"
  32. describe "getFile", ->
  33. beforeEach ->
  34. @FileStoreHandler.getFileStream.callsArgWith(3, null, @stream)
  35. @ProjectLocator.findElement.callsArgWith(1, null, @file)
  36. it "should call the file store handler with the project_id file_id and any query string", (done)->
  37. @stream.pipe = (des)=>
  38. @FileStoreHandler.getFileStream.calledWith(@req.params.Project_id, @req.params.File_id, @req.query).should.equal true
  39. done()
  40. @controller.getFile @req, @res
  41. it "should pipe to res", (done)->
  42. @stream.pipe = (des)=>
  43. des.should.equal @res
  44. done()
  45. @controller.getFile @req, @res
  46. it "should get the file from the db", (done)->
  47. @stream.pipe = (des)=>
  48. opts =
  49. project_id: @project_id
  50. element_id: @file_id
  51. type: "file"
  52. @ProjectLocator.findElement.calledWith(opts).should.equal true
  53. done()
  54. @controller.getFile @req, @res
  55. it "should set the Content-Disposition header", (done)->
  56. @stream.pipe = (des)=>
  57. @res.setHeader.calledWith(
  58. "Content-Disposition", "attachment; filename=#{encodeURIComponent(@file.name)}"
  59. ).should.equal true
  60. done()
  61. @controller.getFile @req, @res
  62. # Test behaviour around handling html files
  63. ['.html', '.htm', '.xhtml'].forEach (extension) ->
  64. describe "with a '#{extension}' file extension", ->
  65. beforeEach ->
  66. @user_agent = 'A generic browser'
  67. @file.name = "bad#{extension}"
  68. @req.get = (key) =>
  69. if key == 'User-Agent'
  70. @user_agent
  71. describe "from a non-ios browser", ->
  72. it "should not set Content-Type", (done) ->
  73. @stream.pipe = (des) =>
  74. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal false
  75. done()
  76. @controller.getFile @req, @res
  77. describe "from an iPhone", ->
  78. beforeEach ->
  79. @user_agent = "An iPhone browser"
  80. it "should set Content-Type to 'text/plain'", (done) ->
  81. @stream.pipe = (des) =>
  82. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal true
  83. done()
  84. @controller.getFile @req, @res
  85. describe "from an iPad", ->
  86. beforeEach ->
  87. @user_agent = "An iPad browser"
  88. it "should set Content-Type to 'text/plain'", (done) ->
  89. @stream.pipe = (des) =>
  90. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal true
  91. done()
  92. @controller.getFile @req, @res
  93. # None of these should trigger the iOS/html logic
  94. ['x.html-is-rad', 'html.pdf', '.html-is-good-for-hidden-files', 'somefile'].forEach (filename) ->
  95. describe "with filename as '#{filename}'", ->
  96. beforeEach ->
  97. @user_agent = 'A generic browser'
  98. @file.name = filename
  99. @req.get = (key) =>
  100. if key == 'User-Agent'
  101. @user_agent
  102. ['iPhone', 'iPad', 'Firefox', 'Chrome'].forEach (browser) ->
  103. describe "downloaded from #{browser}", ->
  104. beforeEach ->
  105. @user_agent = "Some #{browser} thing"
  106. it 'Should not set the Content-type', (done) ->
  107. @stream.pipe = (des) =>
  108. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal false
  109. done()
  110. @controller.getFile @req, @res