FileStoreControllerTests.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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("Content-Disposition", "attachment; filename=#{@file.name}").should.equal true
  58. done()
  59. @controller.getFile @req, @res
  60. # Test behaviour around handling html files
  61. ['.html', '.htm', '.xhtml'].forEach (extension) ->
  62. describe "with a '#{extension}' file extension", ->
  63. beforeEach ->
  64. @user_agent = 'A generic browser'
  65. @file.name = "bad#{extension}"
  66. @req.get = (key) =>
  67. if key == 'User-Agent'
  68. @user_agent
  69. describe "from a non-ios browser", ->
  70. it "should not set Content-Type", (done) ->
  71. @stream.pipe = (des) =>
  72. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal false
  73. done()
  74. @controller.getFile @req, @res
  75. describe "from an iPhone", ->
  76. beforeEach ->
  77. @user_agent = "An iPhone browser"
  78. it "should set Content-Type to 'text/plain'", (done) ->
  79. @stream.pipe = (des) =>
  80. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal true
  81. done()
  82. @controller.getFile @req, @res
  83. describe "from an iPad", ->
  84. beforeEach ->
  85. @user_agent = "An iPad browser"
  86. it "should set Content-Type to 'text/plain'", (done) ->
  87. @stream.pipe = (des) =>
  88. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal true
  89. done()
  90. @controller.getFile @req, @res
  91. # None of these should trigger the iOS/html logic
  92. ['x.html-is-rad', 'html.pdf', '.html-is-good-for-hidden-files', 'somefile'].forEach (filename) ->
  93. describe "with filename as '#{filename}'", ->
  94. beforeEach ->
  95. @user_agent = 'A generic browser'
  96. @file.name = filename
  97. @req.get = (key) =>
  98. if key == 'User-Agent'
  99. @user_agent
  100. ['iPhone', 'iPad', 'Firefox', 'Chrome'].forEach (browser) ->
  101. describe "downloaded from #{browser}", ->
  102. beforeEach ->
  103. @user_agent = "Some #{browser} thing"
  104. it 'Should not set the Content-type', (done) ->
  105. @stream.pipe = (des) =>
  106. @res.setHeader.calledWith("Content-Type", "text/plain").should.equal false
  107. done()
  108. @controller.getFile @req, @res