2019-12-16 06:20:29 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const modulePath = '../../../app/js/FileHandler.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2020-02-23 17:34:40 -05:00
|
|
|
chai.use(require('sinon-chai'))
|
|
|
|
chai.use(require('chai-as-promised'))
|
|
|
|
|
2019-12-16 06:20:29 -05:00
|
|
|
describe('FileHandler', function() {
|
2019-12-18 10:40:30 -05:00
|
|
|
let PersistorManager,
|
|
|
|
LocalFileWriter,
|
|
|
|
FileConverter,
|
|
|
|
KeyBuilder,
|
|
|
|
ImageOptimiser,
|
|
|
|
FileHandler,
|
|
|
|
fs
|
|
|
|
const settings = {
|
|
|
|
s3: {
|
|
|
|
buckets: {
|
|
|
|
user_files: 'user_files'
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const bucket = 'my_bucket'
|
|
|
|
const key = 'key/here'
|
|
|
|
const convertedFolderKey = 'convertedFolder'
|
|
|
|
const sourceStream = 'sourceStream'
|
|
|
|
const convertedKey = 'convertedKey'
|
|
|
|
const readStream = {
|
|
|
|
stream: 'readStream',
|
|
|
|
on: sinon.stub()
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
PersistorManager = {
|
2020-02-23 17:34:40 -05:00
|
|
|
promises: {
|
|
|
|
getFileStream: sinon.stub().resolves(sourceStream),
|
|
|
|
checkIfFileExists: sinon.stub().resolves(),
|
|
|
|
deleteFile: sinon.stub().resolves(),
|
|
|
|
deleteDirectory: sinon.stub().resolves(),
|
|
|
|
sendStream: sinon.stub().resolves(),
|
|
|
|
insertFile: sinon.stub().resolves(),
|
|
|
|
sendFile: sinon.stub().resolves(),
|
|
|
|
directorySize: sinon.stub().resolves()
|
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
}
|
|
|
|
LocalFileWriter = {
|
2020-02-23 17:34:40 -05:00
|
|
|
// the callback style is used for detached cleanup calls
|
|
|
|
deleteFile: sinon.stub().yields(),
|
|
|
|
promises: {
|
|
|
|
writeStream: sinon.stub().resolves(),
|
|
|
|
deleteFile: sinon.stub().resolves()
|
|
|
|
}
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
FileConverter = {
|
2020-02-23 17:34:40 -05:00
|
|
|
promises: {
|
|
|
|
convert: sinon.stub().resolves(),
|
|
|
|
thumbnail: sinon.stub().resolves(),
|
|
|
|
preview: sinon.stub().resolves()
|
|
|
|
}
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
KeyBuilder = {
|
|
|
|
addCachingToKey: sinon.stub().returns(convertedKey),
|
|
|
|
getConvertedFolderKey: sinon.stub().returns(convertedFolderKey)
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2020-02-23 17:34:40 -05:00
|
|
|
ImageOptimiser = {
|
|
|
|
promises: {
|
|
|
|
compressPng: sinon.stub().resolves()
|
|
|
|
}
|
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
fs = {
|
|
|
|
createReadStream: sinon.stub().returns(readStream)
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
|
|
|
|
FileHandler = SandboxedModule.require(modulePath, {
|
2019-12-16 06:20:29 -05:00
|
|
|
requires: {
|
2019-12-18 10:40:30 -05:00
|
|
|
'settings-sharelatex': settings,
|
|
|
|
'./PersistorManager': PersistorManager,
|
|
|
|
'./LocalFileWriter': LocalFileWriter,
|
|
|
|
'./FileConverter': FileConverter,
|
|
|
|
'./KeyBuilder': KeyBuilder,
|
|
|
|
'./ImageOptimiser': ImageOptimiser,
|
2020-01-07 16:19:26 -05:00
|
|
|
fs: fs
|
2019-12-18 10:40:30 -05:00
|
|
|
},
|
|
|
|
globals: { console }
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-16 06:20:29 -05:00
|
|
|
describe('insertFile', function() {
|
2019-12-18 10:40:30 -05:00
|
|
|
const stream = 'stream'
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-16 06:20:29 -05:00
|
|
|
it('should send file to the filestore', function(done) {
|
2019-12-18 10:40:30 -05:00
|
|
|
FileHandler.insertFile(bucket, key, stream, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(PersistorManager.promises.sendStream).to.have.been.calledWith(
|
2019-12-18 10:40:30 -05:00
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
stream
|
|
|
|
)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
it('should delete the convertedKey folder', function(done) {
|
|
|
|
FileHandler.insertFile(bucket, key, stream, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(
|
|
|
|
PersistorManager.promises.deleteDirectory
|
|
|
|
).to.have.been.calledWith(bucket, convertedFolderKey)
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-16 06:20:29 -05:00
|
|
|
describe('deleteFile', function() {
|
|
|
|
it('should tell the filestore manager to delete the file', function(done) {
|
2019-12-18 10:40:30 -05:00
|
|
|
FileHandler.deleteFile(bucket, key, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(PersistorManager.promises.deleteFile).to.have.been.calledWith(
|
|
|
|
bucket,
|
|
|
|
key
|
|
|
|
)
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
it('should tell the filestore manager to delete the cached folder', function(done) {
|
|
|
|
FileHandler.deleteFile(bucket, key, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(
|
|
|
|
PersistorManager.promises.deleteDirectory
|
|
|
|
).to.have.been.calledWith(bucket, convertedFolderKey)
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-16 06:20:29 -05:00
|
|
|
describe('getFile', function() {
|
2019-12-18 10:40:30 -05:00
|
|
|
it('should return the source stream no format or style are defined', function(done) {
|
|
|
|
FileHandler.getFile(bucket, key, null, (err, stream) => {
|
|
|
|
expect(err).not.to.exist
|
|
|
|
expect(stream).to.equal(sourceStream)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
it('should pass options through to PersistorManager', function(done) {
|
2019-12-16 06:20:29 -05:00
|
|
|
const options = { start: 0, end: 8 }
|
2019-12-18 10:40:30 -05:00
|
|
|
FileHandler.getFile(bucket, key, options, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(PersistorManager.promises.getFileStream).to.have.been.calledWith(
|
2019-12-18 10:40:30 -05:00
|
|
|
bucket,
|
|
|
|
key,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
describe('when a format is defined', function() {
|
|
|
|
let result
|
|
|
|
|
|
|
|
describe('when the file is not cached', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
|
|
|
|
result = { err, stream }
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should convert the file', function() {
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(FileConverter.promises.convert).to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should compress the converted file', function() {
|
|
|
|
expect(ImageOptimiser.promises.compressPng).to.have.been.called
|
2019-12-18 10:40:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the the converted stream', function() {
|
|
|
|
expect(result.err).not.to.exist
|
|
|
|
expect(result.stream).to.equal(readStream)
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(
|
|
|
|
PersistorManager.promises.getFileStream
|
|
|
|
).to.have.been.calledWith(bucket, key)
|
2019-12-18 10:40:30 -05:00
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
describe('when the file is cached', function() {
|
|
|
|
beforeEach(function(done) {
|
2020-02-23 17:34:40 -05:00
|
|
|
PersistorManager.promises.checkIfFileExists = sinon
|
|
|
|
.stub()
|
|
|
|
.resolves(true)
|
2019-12-18 10:40:30 -05:00
|
|
|
FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
|
|
|
|
result = { err, stream }
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not convert the file', function() {
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(FileConverter.promises.convert).not.to.have.been.called
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not compress the converted file again', function() {
|
|
|
|
expect(ImageOptimiser.promises.compressPng).not.to.have.been.called
|
2019-12-18 10:40:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the cached stream', function() {
|
|
|
|
expect(result.err).not.to.exist
|
|
|
|
expect(result.stream).to.equal(sourceStream)
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(
|
|
|
|
PersistorManager.promises.getFileStream
|
|
|
|
).to.have.been.calledWith(bucket, convertedKey)
|
2019-12-18 10:40:30 -05:00
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
describe('when a style is defined', function() {
|
|
|
|
it('generates a thumbnail when requested', function(done) {
|
|
|
|
FileHandler.getFile(bucket, key, { style: 'thumbnail' }, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(FileConverter.promises.thumbnail).to.have.been.called
|
|
|
|
expect(FileConverter.promises.preview).not.to.have.been.called
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
it('generates a preview when requested', function(done) {
|
|
|
|
FileHandler.getFile(bucket, key, { style: 'preview' }, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(FileConverter.promises.thumbnail).not.to.have.been.called
|
|
|
|
expect(FileConverter.promises.preview).to.have.been.called
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:22 -05:00
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
describe('getDirectorySize', function() {
|
|
|
|
it('should call the filestore manager to get directory size', function(done) {
|
|
|
|
FileHandler.getDirectorySize(bucket, key, err => {
|
|
|
|
expect(err).not.to.exist
|
2020-02-23 17:34:40 -05:00
|
|
|
expect(PersistorManager.promises.directorySize).to.have.been.calledWith(
|
2019-12-18 10:40:30 -05:00
|
|
|
bucket,
|
|
|
|
key
|
|
|
|
)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|