2019-12-16 06:20:29 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-01-02 10:51:09 -05:00
|
|
|
const Errors = require('../../../app/js/Errors')
|
|
|
|
const modulePath = '../../../app/js/FileController.js'
|
2019-12-16 06:20:29 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('FileController', function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
let PersistorManager,
|
|
|
|
FileHandler,
|
|
|
|
LocalFileWriter,
|
|
|
|
FileController,
|
|
|
|
req,
|
|
|
|
res,
|
2020-01-07 16:19:26 -05:00
|
|
|
next,
|
2020-01-02 10:51:09 -05:00
|
|
|
stream
|
|
|
|
const settings = {
|
|
|
|
s3: {
|
|
|
|
buckets: {
|
2021-07-13 07:04:46 -04:00
|
|
|
user_files: 'user_files',
|
|
|
|
},
|
|
|
|
},
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
|
|
|
const fileSize = 1234
|
|
|
|
const fileStream = 'fileStream'
|
|
|
|
const projectId = 'projectId'
|
|
|
|
const fileId = 'file_id'
|
|
|
|
const bucket = 'user_files'
|
|
|
|
const key = `${projectId}/${fileId}`
|
2020-01-07 16:19:26 -05:00
|
|
|
const error = new Error('incorrect utensil')
|
2020-01-02 10:51:09 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
PersistorManager = {
|
|
|
|
sendStream: sinon.stub().yields(),
|
2020-07-07 08:49:54 -04:00
|
|
|
copyObject: sinon.stub().resolves(),
|
2021-07-13 07:04:46 -04:00
|
|
|
deleteObject: sinon.stub().yields(),
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
|
|
|
|
2020-01-02 10:51:09 -05:00
|
|
|
FileHandler = {
|
|
|
|
getFile: sinon.stub().yields(null, fileStream),
|
|
|
|
getFileSize: sinon.stub().yields(null, fileSize),
|
|
|
|
deleteFile: sinon.stub().yields(),
|
2020-03-14 10:56:29 -04:00
|
|
|
deleteProject: sinon.stub().yields(),
|
2020-01-02 10:51:09 -05:00
|
|
|
insertFile: sinon.stub().yields(),
|
2020-04-09 12:11:19 -04:00
|
|
|
getDirectorySize: sinon.stub().yields(null, fileSize),
|
2021-07-13 07:04:46 -04:00
|
|
|
getRedirectUrl: sinon.stub().yields(null, null),
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-02 10:51:09 -05:00
|
|
|
|
|
|
|
LocalFileWriter = {}
|
|
|
|
stream = {
|
2021-07-13 07:04:46 -04:00
|
|
|
pipeline: sinon.stub(),
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-02 10:51:09 -05:00
|
|
|
|
|
|
|
FileController = SandboxedModule.require(modulePath, {
|
2019-12-16 06:20:29 -05:00
|
|
|
requires: {
|
2020-01-02 10:51:09 -05:00
|
|
|
'./LocalFileWriter': LocalFileWriter,
|
|
|
|
'./FileHandler': FileHandler,
|
|
|
|
'./PersistorManager': PersistorManager,
|
|
|
|
'./Errors': Errors,
|
|
|
|
stream: stream,
|
2021-07-12 12:47:19 -04:00
|
|
|
'@overleaf/settings': settings,
|
2020-11-25 06:57:23 -05:00
|
|
|
'@overleaf/metrics': {
|
2021-07-13 07:04:46 -04:00
|
|
|
inc() {},
|
|
|
|
},
|
2020-01-02 10:51:09 -05:00
|
|
|
},
|
2021-07-13 07:04:46 -04:00
|
|
|
globals: { console },
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
2020-01-02 10:51:09 -05:00
|
|
|
|
|
|
|
req = {
|
|
|
|
key: key,
|
|
|
|
bucket: bucket,
|
2020-03-14 10:56:29 -04:00
|
|
|
project_id: projectId,
|
2019-12-16 06:20:29 -05:00
|
|
|
query: {},
|
|
|
|
params: {
|
2020-01-02 10:51:09 -05:00
|
|
|
project_id: projectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
file_id: fileId,
|
2019-12-16 06:20:29 -05:00
|
|
|
},
|
2020-01-10 12:08:47 -05:00
|
|
|
headers: {},
|
2020-01-14 07:02:39 -05:00
|
|
|
requestLogger: {
|
|
|
|
setMessage: sinon.stub(),
|
2021-07-13 07:04:46 -04:00
|
|
|
addFields: sinon.stub(),
|
|
|
|
},
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-02 10:51:09 -05:00
|
|
|
|
|
|
|
res = {
|
2019-12-16 06:20:29 -05:00
|
|
|
set: sinon.stub().returnsThis(),
|
2020-01-02 10:51:09 -05:00
|
|
|
sendStatus: sinon.stub().returnsThis(),
|
2021-07-13 07:04:46 -04:00
|
|
|
status: sinon.stub().returnsThis(),
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
|
|
|
|
next = sinon.stub()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('getFile', function () {
|
|
|
|
it('should try and get a redirect url first', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(FileHandler.getRedirectUrl).to.have.been.calledWith(bucket, key)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should pipe the stream', function () {
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFile(req, res, next)
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(stream.pipeline).to.have.been.calledWith(fileStream, res)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 200 if the cacheWarm param is true', function (done) {
|
2020-01-02 10:51:09 -05:00
|
|
|
req.query.cacheWarm = true
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = statusCode => {
|
2019-12-16 06:20:29 -05:00
|
|
|
statusCode.should.equal(200)
|
2020-01-02 10:51:09 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFile(req, res, next)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send an error if there is a problem', function () {
|
2020-01-07 16:19:26 -05:00
|
|
|
FileHandler.getFile.yields(error)
|
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(next).to.have.been.calledWith(error)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a redirect url', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
const redirectUrl = 'https://wombat.potato/giraffe'
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileHandler.getRedirectUrl.yields(null, redirectUrl)
|
|
|
|
res.redirect = sinon.stub()
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should redirect', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(res.redirect).to.have.been.calledWith(redirectUrl)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not get a file stream', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(FileHandler.getFile).not.to.have.been.called
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when there is an error getting the redirect url', function () {
|
|
|
|
beforeEach(function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileHandler.getRedirectUrl.yields(new Error('wombat herding error'))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not redirect', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(res.redirect).not.to.have.been.called
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not return an error', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(next).not.to.have.been.called
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should proxy the file', function () {
|
2020-04-09 12:11:19 -04:00
|
|
|
FileController.getFile(req, res, next)
|
|
|
|
expect(FileHandler.getFile).to.have.been.calledWith(bucket, key)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('with a range header', function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
let expectedOptions
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
expectedOptions = {
|
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
format: undefined,
|
2021-07-13 07:04:46 -04:00
|
|
|
style: undefined,
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should pass range options to FileHandler', function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
req.headers.range = 'bytes=0-8'
|
|
|
|
expectedOptions.start = 0
|
|
|
|
expectedOptions.end = 8
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFile(req, res, next)
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(FileHandler.getFile).to.have.been.calledWith(
|
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
expectedOptions
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should ignore an invalid range header', function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
req.headers.range = 'potato'
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFile(req, res, next)
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(FileHandler.getFile).to.have.been.calledWith(
|
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
expectedOptions
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it("should ignore any type other than 'bytes'", function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
req.headers.range = 'wombats=0-8'
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFile(req, res, next)
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(FileHandler.getFile).to.have.been.calledWith(
|
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
expectedOptions
|
|
|
|
)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('getFileHead', function () {
|
|
|
|
it('should return the file size in a Content-Length header', function (done) {
|
2020-01-02 10:51:09 -05:00
|
|
|
res.end = () => {
|
|
|
|
expect(res.status).to.have.been.calledWith(200)
|
|
|
|
expect(res.set).to.have.been.calledWith('Content-Length', fileSize)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFileHead(req, res, next)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should return a 404 is the file is not found', function (done) {
|
2020-07-07 08:49:54 -04:00
|
|
|
FileHandler.getFileSize.yields(
|
|
|
|
new Errors.NotFoundError({ message: 'not found', info: {} })
|
|
|
|
)
|
2019-12-16 06:20:29 -05:00
|
|
|
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = code => {
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(code).to.equal(404)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFileHead(req, res, next)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send an error on internal errors', function () {
|
2020-01-07 16:19:26 -05:00
|
|
|
FileHandler.getFileSize.yields(error)
|
2019-12-16 06:20:29 -05:00
|
|
|
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.getFileHead(req, res, next)
|
|
|
|
expect(next).to.have.been.calledWith(error)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('insertFile', function () {
|
|
|
|
it('should send bucket name key and res to PersistorManager', function (done) {
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = code => {
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(FileHandler.insertFile).to.have.been.calledWith(bucket, key, req)
|
|
|
|
expect(code).to.equal(200)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.insertFile(req, res, next)
|
2020-01-02 10:51:09 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('copyFile', function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
const oldFileId = 'oldFileId'
|
|
|
|
const oldProjectId = 'oldProjectid'
|
|
|
|
const oldKey = `${oldProjectId}/${oldFileId}`
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-01-02 10:51:09 -05:00
|
|
|
req.body = {
|
2019-12-16 06:20:29 -05:00
|
|
|
source: {
|
2020-01-02 10:51:09 -05:00
|
|
|
project_id: oldProjectId,
|
2021-07-13 07:04:46 -04:00
|
|
|
file_id: oldFileId,
|
|
|
|
},
|
2020-01-02 10:51:09 -05:00
|
|
|
}
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send bucket name and both keys to PersistorManager', function (done) {
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = code => {
|
2019-12-16 06:20:29 -05:00
|
|
|
code.should.equal(200)
|
2020-07-07 08:49:54 -04:00
|
|
|
expect(PersistorManager.copyObject).to.have.been.calledWith(
|
2020-01-02 10:51:09 -05:00
|
|
|
bucket,
|
|
|
|
oldKey,
|
|
|
|
key
|
|
|
|
)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.copyFile(req, res, next)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 404 if the original file was not found', function (done) {
|
2020-07-07 08:49:54 -04:00
|
|
|
PersistorManager.copyObject.rejects(
|
|
|
|
new Errors.NotFoundError({ message: 'not found', info: {} })
|
|
|
|
)
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = code => {
|
2019-12-16 06:20:29 -05:00
|
|
|
code.should.equal(404)
|
2020-01-02 10:51:09 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.copyFile(req, res, next)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send an error if there was an error', function (done) {
|
2020-07-07 08:49:54 -04:00
|
|
|
PersistorManager.copyObject.rejects(error)
|
2021-07-13 07:04:46 -04:00
|
|
|
FileController.copyFile(req, res, err => {
|
2020-07-07 08:49:54 -04:00
|
|
|
expect(err).to.equal(error)
|
|
|
|
done()
|
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('delete file', function () {
|
|
|
|
it('should tell the file handler', function (done) {
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = code => {
|
2019-12-16 06:20:29 -05:00
|
|
|
code.should.equal(204)
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(FileHandler.deleteFile).to.have.been.calledWith(bucket, key)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-01-07 16:19:26 -05:00
|
|
|
FileController.deleteFile(req, res, next)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 500 if there was an error', function () {
|
2020-01-07 16:19:26 -05:00
|
|
|
FileHandler.deleteFile.yields(error)
|
|
|
|
FileController.deleteFile(req, res, next)
|
|
|
|
expect(next).to.have.been.calledWith(error)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('delete project', function () {
|
|
|
|
it('should tell the file handler', function (done) {
|
2021-07-13 07:04:46 -04:00
|
|
|
res.sendStatus = code => {
|
2020-03-14 10:56:29 -04:00
|
|
|
code.should.equal(204)
|
2020-03-16 07:35:01 -04:00
|
|
|
expect(FileHandler.deleteProject).to.have.been.calledWith(bucket, key)
|
2020-03-14 10:56:29 -04:00
|
|
|
done()
|
|
|
|
}
|
|
|
|
FileController.deleteProject(req, res, next)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 500 if there was an error', function () {
|
2020-03-14 10:56:29 -04:00
|
|
|
FileHandler.deleteProject.yields(error)
|
|
|
|
FileController.deleteProject(req, res, next)
|
|
|
|
expect(next).to.have.been.calledWith(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('directorySize', function () {
|
|
|
|
it('should return total directory size bytes', function (done) {
|
2020-01-02 10:51:09 -05:00
|
|
|
FileController.directorySize(req, {
|
2021-07-13 07:04:46 -04:00
|
|
|
json: result => {
|
2020-01-02 10:51:09 -05:00
|
|
|
expect(result['total bytes']).to.equal(fileSize)
|
|
|
|
done()
|
2021-07-13 07:04:46 -04:00
|
|
|
},
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should send a 500 if there was an error', function () {
|
2020-01-07 16:19:26 -05:00
|
|
|
FileHandler.getDirectorySize.yields(error)
|
|
|
|
FileController.directorySize(req, res, next)
|
|
|
|
expect(next).to.have.been.calledWith(error)
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|