FileStoreControllerTests.js 7.0 KB

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