FileStoreControllerTests.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. const { expect } = require('chai')
  2. const sinon = require('sinon')
  3. const SandboxedModule = require('sandboxed-module')
  4. const MODULE_PATH =
  5. '../../../../app/src/Features/FileStore/FileStoreController.js'
  6. describe('FileStoreController', function() {
  7. beforeEach(function() {
  8. this.FileStoreHandler = {
  9. getFileStream: sinon.stub(),
  10. getFileSize: sinon.stub()
  11. }
  12. this.ProjectLocator = { findElement: sinon.stub() }
  13. this.Errors = { NotFoundError: sinon.stub() }
  14. this.controller = SandboxedModule.require(MODULE_PATH, {
  15. globals: {
  16. console: console
  17. },
  18. requires: {
  19. 'settings-sharelatex': this.settings,
  20. 'logger-sharelatex': (this.logger = {
  21. log: sinon.stub(),
  22. err: sinon.stub()
  23. }),
  24. '../Project/ProjectLocator': this.ProjectLocator,
  25. '../Errors/Errors': this.Errors,
  26. './FileStoreHandler': this.FileStoreHandler
  27. }
  28. })
  29. this.stream = {}
  30. this.projectId = '2k3j1lk3j21lk3j'
  31. this.fileId = '12321kklj1lk3jk12'
  32. this.req = {
  33. params: {
  34. Project_id: this.projectId,
  35. File_id: this.fileId
  36. },
  37. query: 'query string here',
  38. get(key) {
  39. return undefined
  40. }
  41. }
  42. this.res = {
  43. set: sinon.stub().returnsThis(),
  44. setHeader: sinon.stub(),
  45. setContentDisposition: sinon.stub(),
  46. status: sinon.stub().returnsThis()
  47. }
  48. this.file = { name: 'myfile.png' }
  49. })
  50. describe('getFile', function() {
  51. beforeEach(function() {
  52. this.FileStoreHandler.getFileStream.callsArgWith(3, null, this.stream)
  53. this.ProjectLocator.findElement.callsArgWith(1, null, this.file)
  54. })
  55. it('should call the file store handler with the project_id file_id and any query string', function(done) {
  56. this.stream.pipe = des => {
  57. this.FileStoreHandler.getFileStream
  58. .calledWith(
  59. this.req.params.Project_id,
  60. this.req.params.File_id,
  61. this.req.query
  62. )
  63. .should.equal(true)
  64. done()
  65. }
  66. this.controller.getFile(this.req, this.res)
  67. })
  68. it('should pipe to res', function(done) {
  69. this.stream.pipe = des => {
  70. des.should.equal(this.res)
  71. done()
  72. }
  73. this.controller.getFile(this.req, this.res)
  74. })
  75. it('should get the file from the db', function(done) {
  76. this.stream.pipe = des => {
  77. const opts = {
  78. project_id: this.projectId,
  79. element_id: this.fileId,
  80. type: 'file'
  81. }
  82. this.ProjectLocator.findElement.calledWith(opts).should.equal(true)
  83. done()
  84. }
  85. this.controller.getFile(this.req, this.res)
  86. })
  87. it('should set the Content-Disposition header', function(done) {
  88. this.stream.pipe = des => {
  89. this.res.setContentDisposition
  90. .calledWith('attachment', { filename: this.file.name })
  91. .should.equal(true)
  92. done()
  93. }
  94. this.controller.getFile(this.req, this.res)
  95. })
  96. // Test behaviour around handling html files
  97. ;['.html', '.htm', '.xhtml'].forEach(extension => {
  98. describe(`with a '${extension}' file extension`, function() {
  99. beforeEach(function() {
  100. this.file.name = `bad${extension}`
  101. this.req.get = key => {
  102. if (key === 'User-Agent') {
  103. return 'A generic browser'
  104. }
  105. }
  106. })
  107. describe('from a non-ios browser', function() {
  108. it('should not set Content-Type', function(done) {
  109. this.stream.pipe = des => {
  110. this.res.setHeader
  111. .calledWith('Content-Type', 'text/plain')
  112. .should.equal(false)
  113. done()
  114. }
  115. this.controller.getFile(this.req, this.res)
  116. })
  117. })
  118. describe('from an iPhone', function() {
  119. beforeEach(function() {
  120. this.req.get = key => {
  121. if (key === 'User-Agent') {
  122. return 'An iPhone browser'
  123. }
  124. }
  125. })
  126. it("should set Content-Type to 'text/plain'", function(done) {
  127. this.stream.pipe = des => {
  128. this.res.setHeader
  129. .calledWith('Content-Type', 'text/plain')
  130. .should.equal(true)
  131. done()
  132. }
  133. this.controller.getFile(this.req, this.res)
  134. })
  135. })
  136. describe('from an iPad', function() {
  137. beforeEach(function() {
  138. this.req.get = key => {
  139. if (key === 'User-Agent') {
  140. return 'An iPad browser'
  141. }
  142. }
  143. })
  144. it("should set Content-Type to 'text/plain'", function(done) {
  145. this.stream.pipe = des => {
  146. this.res.setHeader
  147. .calledWith('Content-Type', 'text/plain')
  148. .should.equal(true)
  149. done()
  150. }
  151. this.controller.getFile(this.req, this.res)
  152. })
  153. })
  154. })
  155. })
  156. ;[
  157. // None of these should trigger the iOS/html logic
  158. ('x.html-is-rad',
  159. 'html.pdf',
  160. '.html-is-good-for-hidden-files',
  161. 'somefile')
  162. ].forEach(filename => {
  163. describe(`with filename as '${filename}'`, function() {
  164. beforeEach(function() {
  165. this.user_agent = 'A generic browser'
  166. this.file.name = filename
  167. this.req.get = key => {
  168. if (key === 'User-Agent') {
  169. return this.user_agent
  170. }
  171. }
  172. })
  173. ;[('iPhone', 'iPad', 'Firefox', 'Chrome')].forEach(browser => {
  174. describe(`downloaded from ${browser}`, function() {
  175. beforeEach(function() {
  176. this.user_agent = `Some ${browser} thing`
  177. })
  178. it('Should not set the Content-type', function(done) {
  179. this.stream.pipe = des => {
  180. this.res.setHeader
  181. .calledWith('Content-Type', 'text/plain')
  182. .should.equal(false)
  183. done()
  184. }
  185. this.controller.getFile(this.req, this.res)
  186. })
  187. })
  188. })
  189. })
  190. })
  191. })
  192. describe('getFileHead', function() {
  193. it('reports the file size', function(done) {
  194. const expectedFileSize = 99393
  195. this.FileStoreHandler.getFileSize.yields(
  196. new Error('getFileSize: unexpected arguments')
  197. )
  198. this.FileStoreHandler.getFileSize
  199. .withArgs(this.projectId, this.fileId)
  200. .yields(null, expectedFileSize)
  201. this.res.end = () => {
  202. expect(this.res.status.lastCall.args).to.deep.equal([200])
  203. expect(this.res.set.lastCall.args).to.deep.equal([
  204. 'Content-Length',
  205. expectedFileSize
  206. ])
  207. done()
  208. }
  209. this.controller.getFileHead(this.req, this.res)
  210. })
  211. it('returns 404 on NotFoundError', function(done) {
  212. this.FileStoreHandler.getFileSize.yields(new this.Errors.NotFoundError())
  213. this.res.end = () => {
  214. expect(this.res.status.lastCall.args).to.deep.equal([404])
  215. done()
  216. }
  217. this.controller.getFileHead(this.req, this.res)
  218. })
  219. it('returns 500 on error', function(done) {
  220. this.FileStoreHandler.getFileSize.yields(new Error('boom!'))
  221. this.res.end = () => {
  222. expect(this.res.status.lastCall.args).to.deep.equal([500])
  223. done()
  224. }
  225. this.controller.getFileHead(this.req, this.res)
  226. })
  227. })
  228. })