FileStoreControllerTests.mjs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { expect } from 'chai'
  2. import sinon from 'sinon'
  3. import esmock from 'esmock'
  4. import Errors from '../../../../app/src/Features/Errors/Errors.js'
  5. import MockResponse from '../helpers/MockResponse.js'
  6. const MODULE_PATH =
  7. '../../../../app/src/Features/FileStore/FileStoreController.mjs'
  8. const expectedFileHeaders = {
  9. 'Cache-Control': 'private, max-age=3600',
  10. }
  11. describe('FileStoreController', function () {
  12. beforeEach(async function () {
  13. this.FileStoreHandler = {
  14. getFileStream: sinon.stub(),
  15. getFileSize: sinon.stub(),
  16. }
  17. this.ProjectLocator = { findElement: sinon.stub() }
  18. this.controller = await esmock.strict(MODULE_PATH, {
  19. '@overleaf/settings': this.settings,
  20. '../../../../app/src/Features/Project/ProjectLocator':
  21. this.ProjectLocator,
  22. '../../../../app/src/Features/FileStore/FileStoreHandler':
  23. this.FileStoreHandler,
  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. it('should return a 404 when not found', function (done) {
  88. this.res.callback = () => {
  89. expect(this.res.statusCode).to.equal(404)
  90. done()
  91. }
  92. this.ProjectLocator.findElement.yields(new Errors.NotFoundError())
  93. this.controller.getFile(this.req, this.res)
  94. })
  95. // Test behaviour around handling html files
  96. ;['.html', '.htm', '.xhtml'].forEach(extension => {
  97. describe(`with a '${extension}' file extension`, function () {
  98. beforeEach(function () {
  99. this.file.name = `bad${extension}`
  100. this.req.get = key => {
  101. if (key === 'User-Agent') {
  102. return 'A generic browser'
  103. }
  104. }
  105. })
  106. describe('from a non-ios browser', function () {
  107. it('should not set Content-Type', function (done) {
  108. this.stream.pipe = des => {
  109. this.res.headers.should.deep.equal({
  110. ...expectedFileHeaders,
  111. })
  112. done()
  113. }
  114. this.controller.getFile(this.req, this.res)
  115. })
  116. })
  117. describe('from an iPhone', function () {
  118. beforeEach(function () {
  119. this.req.get = key => {
  120. if (key === 'User-Agent') {
  121. return 'An iPhone browser'
  122. }
  123. }
  124. })
  125. it("should set Content-Type to 'text/plain'", function (done) {
  126. this.stream.pipe = des => {
  127. this.res.headers.should.deep.equal({
  128. ...expectedFileHeaders,
  129. 'Content-Type': 'text/plain; charset=utf-8',
  130. 'X-Content-Type-Options': 'nosniff',
  131. })
  132. done()
  133. }
  134. this.controller.getFile(this.req, this.res)
  135. })
  136. })
  137. describe('from an iPad', function () {
  138. beforeEach(function () {
  139. this.req.get = key => {
  140. if (key === 'User-Agent') {
  141. return 'An iPad browser'
  142. }
  143. }
  144. })
  145. it("should set Content-Type to 'text/plain'", function (done) {
  146. this.stream.pipe = des => {
  147. this.res.headers.should.deep.equal({
  148. ...expectedFileHeaders,
  149. 'Content-Type': 'text/plain; charset=utf-8',
  150. 'X-Content-Type-Options': 'nosniff',
  151. })
  152. done()
  153. }
  154. this.controller.getFile(this.req, this.res)
  155. })
  156. })
  157. })
  158. })
  159. ;[
  160. // None of these should trigger the iOS/html logic
  161. 'x.html-is-rad',
  162. 'html.pdf',
  163. '.html-is-good-for-hidden-files',
  164. 'somefile',
  165. ].forEach(filename => {
  166. describe(`with filename as '${filename}'`, function () {
  167. beforeEach(function () {
  168. this.user_agent = 'A generic browser'
  169. this.file.name = filename
  170. this.req.get = key => {
  171. if (key === 'User-Agent') {
  172. return this.user_agent
  173. }
  174. }
  175. })
  176. ;['iPhone', 'iPad', 'Firefox', 'Chrome'].forEach(browser => {
  177. describe(`downloaded from ${browser}`, function () {
  178. beforeEach(function () {
  179. this.user_agent = `Some ${browser} thing`
  180. })
  181. it('Should not set the Content-type', function (done) {
  182. this.stream.pipe = des => {
  183. this.res.headers.should.deep.equal({
  184. ...expectedFileHeaders,
  185. })
  186. done()
  187. }
  188. this.controller.getFile(this.req, this.res)
  189. })
  190. })
  191. })
  192. })
  193. })
  194. })
  195. describe('getFileHead', function () {
  196. it('reports the file size', function (done) {
  197. const expectedFileSize = 99393
  198. this.FileStoreHandler.getFileSize.yields(
  199. new Error('getFileSize: unexpected arguments')
  200. )
  201. this.FileStoreHandler.getFileSize
  202. .withArgs(this.projectId, this.fileId)
  203. .yields(null, expectedFileSize)
  204. this.res.end = () => {
  205. expect(this.res.status.lastCall.args).to.deep.equal([200])
  206. expect(this.res.header.lastCall.args).to.deep.equal([
  207. 'Content-Length',
  208. expectedFileSize,
  209. ])
  210. done()
  211. }
  212. this.controller.getFileHead(this.req, this.res)
  213. })
  214. it('returns 404 on NotFoundError', function (done) {
  215. this.FileStoreHandler.getFileSize.yields(new Errors.NotFoundError())
  216. this.res.end = () => {
  217. expect(this.res.status.lastCall.args).to.deep.equal([404])
  218. done()
  219. }
  220. this.controller.getFileHead(this.req, this.res)
  221. })
  222. it('returns 500 on error', function (done) {
  223. this.FileStoreHandler.getFileSize.yields(new Error('boom!'))
  224. this.res.end = () => {
  225. expect(this.res.status.lastCall.args).to.deep.equal([500])
  226. done()
  227. }
  228. this.controller.getFileHead(this.req, this.res)
  229. })
  230. })
  231. })