FileStoreControllerTests.js 7.3 KB

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