2019-06-20 07:17:55 -04:00
|
|
|
const { expect } = require('chai')
|
2019-05-29 05:21:06 -04:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-11-27 08:11:12 -05:00
|
|
|
const Errors = require('../../../../app/src/Features/Errors/Errors')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-20 07:17:55 -04:00
|
|
|
const MODULE_PATH =
|
|
|
|
'../../../../app/src/Features/FileStore/FileStoreController.js'
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('FileStoreController', function () {
|
|
|
|
beforeEach(function () {
|
2019-06-20 07:17:55 -04:00
|
|
|
this.FileStoreHandler = {
|
|
|
|
getFileStream: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
getFileSize: sinon.stub(),
|
2019-06-20 07:17:55 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
this.ProjectLocator = { findElement: sinon.stub() }
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller = SandboxedModule.require(MODULE_PATH, {
|
2019-05-29 05:21:06 -04:00
|
|
|
requires: {
|
|
|
|
'settings-sharelatex': this.settings,
|
|
|
|
'../Project/ProjectLocator': this.ProjectLocator,
|
2021-04-27 03:52:58 -04:00
|
|
|
'./FileStoreHandler': this.FileStoreHandler,
|
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
this.stream = {}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.projectId = '2k3j1lk3j21lk3j'
|
|
|
|
this.fileId = '12321kklj1lk3jk12'
|
2019-05-29 05:21:06 -04:00
|
|
|
this.req = {
|
|
|
|
params: {
|
2019-06-20 07:17:55 -04:00
|
|
|
Project_id: this.projectId,
|
2021-04-27 03:52:58 -04:00
|
|
|
File_id: this.fileId,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
query: 'query string here',
|
|
|
|
get(key) {
|
|
|
|
return undefined
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.res = {
|
2019-06-20 07:17:55 -04:00
|
|
|
set: sinon.stub().returnsThis(),
|
2019-05-29 05:21:06 -04:00
|
|
|
setHeader: sinon.stub(),
|
2019-06-20 07:17:55 -04:00
|
|
|
setContentDisposition: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
status: sinon.stub().returnsThis(),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.file = { name: 'myfile.png' }
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getFile', function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.FileStoreHandler.getFileStream.callsArgWith(3, null, this.stream)
|
2019-06-20 07:17:55 -04:00
|
|
|
this.ProjectLocator.findElement.callsArgWith(1, null, this.file)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should call the file store handler with the project_id file_id and any query string', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
this.FileStoreHandler.getFileStream
|
|
|
|
.calledWith(
|
|
|
|
this.req.params.Project_id,
|
|
|
|
this.req.params.File_id,
|
|
|
|
this.req.query
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should pipe to res', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
des.should.equal(this.res)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should get the file from the db', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
const opts = {
|
2019-06-20 07:17:55 -04:00
|
|
|
project_id: this.projectId,
|
|
|
|
element_id: this.fileId,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'file',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
this.ProjectLocator.findElement.calledWith(opts).should.equal(true)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('should set the Content-Disposition header', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
this.res.setContentDisposition
|
|
|
|
.calledWith('attachment', { filename: this.file.name })
|
|
|
|
.should.equal(true)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// Test behaviour around handling html files
|
2019-06-20 07:17:55 -04:00
|
|
|
;['.html', '.htm', '.xhtml'].forEach(extension => {
|
2021-04-14 09:17:21 -04:00
|
|
|
describe(`with a '${extension}' file extension`, function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.file.name = `bad${extension}`
|
2019-06-20 07:17:55 -04:00
|
|
|
this.req.get = key => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (key === 'User-Agent') {
|
|
|
|
return 'A generic browser'
|
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('from a non-ios browser', function () {
|
|
|
|
it('should not set Content-Type', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
this.res.setHeader
|
|
|
|
.calledWith('Content-Type', 'text/plain')
|
|
|
|
.should.equal(false)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
|
|
|
})
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('from an iPhone', function () {
|
|
|
|
beforeEach(function () {
|
2019-06-20 07:17:55 -04:00
|
|
|
this.req.get = key => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (key === 'User-Agent') {
|
|
|
|
return 'An iPhone browser'
|
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("should set Content-Type to 'text/plain'", function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
this.res.setHeader
|
|
|
|
.calledWith('Content-Type', 'text/plain')
|
|
|
|
.should.equal(true)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('from an iPad', function () {
|
|
|
|
beforeEach(function () {
|
2019-06-20 07:17:55 -04:00
|
|
|
this.req.get = key => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (key === 'User-Agent') {
|
|
|
|
return 'An iPad browser'
|
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it("should set Content-Type to 'text/plain'", function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
this.res.setHeader
|
|
|
|
.calledWith('Content-Type', 'text/plain')
|
|
|
|
.should.equal(true)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-06-20 07:17:55 -04:00
|
|
|
})
|
|
|
|
;[
|
|
|
|
// None of these should trigger the iOS/html logic
|
|
|
|
('x.html-is-rad',
|
2019-05-29 05:21:06 -04:00
|
|
|
'html.pdf',
|
|
|
|
'.html-is-good-for-hidden-files',
|
2021-04-27 03:52:58 -04:00
|
|
|
'somefile'),
|
2019-06-20 07:17:55 -04:00
|
|
|
].forEach(filename => {
|
2021-04-14 09:17:21 -04:00
|
|
|
describe(`with filename as '${filename}'`, function () {
|
|
|
|
beforeEach(function () {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.user_agent = 'A generic browser'
|
|
|
|
this.file.name = filename
|
2019-06-20 07:17:55 -04:00
|
|
|
this.req.get = key => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (key === 'User-Agent') {
|
|
|
|
return this.user_agent
|
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-06-20 07:17:55 -04:00
|
|
|
;[('iPhone', 'iPad', 'Firefox', 'Chrome')].forEach(browser => {
|
2021-04-14 09:17:21 -04:00
|
|
|
describe(`downloaded from ${browser}`, function () {
|
|
|
|
beforeEach(function () {
|
2019-06-20 07:17:55 -04:00
|
|
|
this.user_agent = `Some ${browser} thing`
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('Should not set the Content-type', function (done) {
|
2019-05-29 05:21:06 -04:00
|
|
|
this.stream.pipe = des => {
|
|
|
|
this.res.setHeader
|
|
|
|
.calledWith('Content-Type', 'text/plain')
|
|
|
|
.should.equal(false)
|
2019-06-20 07:17:55 -04:00
|
|
|
done()
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-20 07:17:55 -04:00
|
|
|
this.controller.getFile(this.req, this.res)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
2019-06-20 07:17:55 -04:00
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
2019-06-20 07:17:55 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
describe('getFileHead', function () {
|
|
|
|
it('reports the file size', function (done) {
|
2019-06-20 07:17:55 -04:00
|
|
|
const expectedFileSize = 99393
|
|
|
|
this.FileStoreHandler.getFileSize.yields(
|
|
|
|
new Error('getFileSize: unexpected arguments')
|
|
|
|
)
|
|
|
|
this.FileStoreHandler.getFileSize
|
|
|
|
.withArgs(this.projectId, this.fileId)
|
|
|
|
.yields(null, expectedFileSize)
|
|
|
|
|
|
|
|
this.res.end = () => {
|
|
|
|
expect(this.res.status.lastCall.args).to.deep.equal([200])
|
|
|
|
expect(this.res.set.lastCall.args).to.deep.equal([
|
|
|
|
'Content-Length',
|
2021-04-27 03:52:58 -04:00
|
|
|
expectedFileSize,
|
2019-06-20 07:17:55 -04:00
|
|
|
])
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.controller.getFileHead(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns 404 on NotFoundError', function (done) {
|
2020-11-27 08:11:12 -05:00
|
|
|
this.FileStoreHandler.getFileSize.yields(new Errors.NotFoundError())
|
2019-06-20 07:17:55 -04:00
|
|
|
|
|
|
|
this.res.end = () => {
|
|
|
|
expect(this.res.status.lastCall.args).to.deep.equal([404])
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.controller.getFileHead(this.req, this.res)
|
|
|
|
})
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
it('returns 500 on error', function (done) {
|
2019-06-20 07:17:55 -04:00
|
|
|
this.FileStoreHandler.getFileSize.yields(new Error('boom!'))
|
|
|
|
|
|
|
|
this.res.end = () => {
|
|
|
|
expect(this.res.status.lastCall.args).to.deep.equal([500])
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
|
|
|
|
this.controller.getFileHead(this.req, this.res)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|