overleaf/services/filestore/test/unit/js/FileHandlerTests.js

399 lines
12 KiB
JavaScript
Raw Normal View History

const sinon = require('sinon')
const chai = require('chai')
const { expect } = chai
const modulePath = '../../../app/js/FileHandler.js'
const SandboxedModule = require('sandboxed-module')
const { ObjectId } = require('mongodb')
const { Errors } = require('@overleaf/object-persistor')
chai.use(require('sinon-chai'))
chai.use(require('chai-as-promised'))
2020-08-10 12:01:12 -04:00
describe('FileHandler', function () {
let PersistorManager,
LocalFileWriter,
FileConverter,
KeyBuilder,
ImageOptimiser,
FileHandler,
Settings,
fs
const bucket = 'my_bucket'
const key = `${ObjectId()}/${ObjectId()}`
const convertedFolderKey = `${ObjectId()}/${ObjectId()}`
const projectKey = `${ObjectId()}/`
const sourceStream = 'sourceStream'
const convertedKey = 'convertedKey'
const redirectUrl = 'https://wombat.potato/giraffe'
const readStream = {
stream: 'readStream',
2021-07-13 07:04:46 -04:00
on: sinon.stub(),
}
2020-08-10 12:01:12 -04:00
beforeEach(function () {
PersistorManager = {
getObjectStream: sinon.stub().resolves(sourceStream),
getRedirectUrl: sinon.stub().resolves(redirectUrl),
checkIfObjectExists: sinon.stub().resolves(),
deleteObject: sinon.stub().resolves(),
deleteDirectory: sinon.stub().resolves(),
sendStream: sinon.stub().resolves(),
insertFile: sinon.stub().resolves(),
sendFile: sinon.stub().resolves(),
2021-07-13 07:04:46 -04:00
directorySize: sinon.stub().resolves(),
}
LocalFileWriter = {
// the callback style is used for detached cleanup calls
deleteFile: sinon.stub().yields(),
promises: {
writeStream: sinon.stub().resolves(),
2021-07-13 07:04:46 -04:00
deleteFile: sinon.stub().resolves(),
},
}
FileConverter = {
promises: {
convert: sinon.stub().resolves(),
thumbnail: sinon.stub().resolves(),
2021-07-13 07:04:46 -04:00
preview: sinon.stub().resolves(),
},
}
KeyBuilder = {
addCachingToKey: sinon.stub().returns(convertedKey),
2021-07-13 07:04:46 -04:00
getConvertedFolderKey: sinon.stub().returns(convertedFolderKey),
}
ImageOptimiser = {
promises: {
2021-07-13 07:04:46 -04:00
compressPng: sinon.stub().resolves(),
},
}
Settings = {}
fs = {
2021-07-13 07:04:46 -04:00
createReadStream: sinon.stub().returns(readStream),
}
const ObjectPersistor = { Errors }
FileHandler = SandboxedModule.require(modulePath, {
requires: {
'./PersistorManager': PersistorManager,
'./LocalFileWriter': LocalFileWriter,
'./FileConverter': FileConverter,
'./KeyBuilder': KeyBuilder,
'./ImageOptimiser': ImageOptimiser,
'@overleaf/settings': Settings,
'@overleaf/object-persistor': ObjectPersistor,
fs,
},
2021-07-13 07:04:46 -04:00
globals: { console },
})
})
2020-08-10 12:01:12 -04:00
describe('insertFile', function () {
const stream = 'stream'
2020-08-10 12:01:12 -04:00
it('should send file to the filestore', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.insertFile(bucket, key, stream, err => {
expect(err).not.to.exist
expect(PersistorManager.sendStream).to.have.been.calledWith(
bucket,
key,
stream
)
done()
})
})
2020-08-10 12:01:12 -04:00
it('should not make a delete request for the convertedKey folder', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.insertFile(bucket, key, stream, err => {
expect(err).not.to.exist
expect(PersistorManager.deleteDirectory).not.to.have.been.called
done()
})
})
2020-08-10 12:01:12 -04:00
it('should accept templates-api key format', function (done) {
KeyBuilder.getConvertedFolderKey.returns(
'5ecba29f1a294e007d0bccb4/v/0/pdf'
)
2021-07-13 07:04:46 -04:00
FileHandler.insertFile(bucket, key, stream, err => {
expect(err).not.to.exist
done()
})
})
2020-08-10 12:01:12 -04:00
it('should throw an error when the key is in the wrong format', function (done) {
KeyBuilder.getConvertedFolderKey.returns('wombat')
2021-07-13 07:04:46 -04:00
FileHandler.insertFile(bucket, key, stream, err => {
expect(err).to.exist
done()
})
})
2020-08-10 12:01:12 -04:00
describe('when conversions are enabled', function () {
beforeEach(function () {
Settings.enableConversions = true
})
2020-08-10 12:01:12 -04:00
it('should delete the convertedKey folder', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.insertFile(bucket, key, stream, err => {
expect(err).not.to.exist
expect(PersistorManager.deleteDirectory).to.have.been.calledWith(
bucket,
convertedFolderKey
)
done()
})
})
})
})
2020-08-10 12:01:12 -04:00
describe('deleteFile', function () {
it('should tell the filestore manager to delete the file', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.deleteFile(bucket, key, err => {
expect(err).not.to.exist
expect(PersistorManager.deleteObject).to.have.been.calledWith(
bucket,
key
)
done()
})
})
2020-08-10 12:01:12 -04:00
it('should not tell the filestore manager to delete the cached folder', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.deleteFile(bucket, key, err => {
expect(err).not.to.exist
expect(PersistorManager.deleteDirectory).not.to.have.been.called
done()
})
})
2020-08-10 12:01:12 -04:00
it('should accept templates-api key format', function (done) {
KeyBuilder.getConvertedFolderKey.returns(
'5ecba29f1a294e007d0bccb4/v/0/pdf'
)
2021-07-13 07:04:46 -04:00
FileHandler.deleteFile(bucket, key, err => {
expect(err).not.to.exist
done()
})
})
2020-08-10 12:01:12 -04:00
it('should throw an error when the key is in the wrong format', function (done) {
KeyBuilder.getConvertedFolderKey.returns('wombat')
2021-07-13 07:04:46 -04:00
FileHandler.deleteFile(bucket, key, err => {
expect(err).to.exist
done()
})
})
2020-08-10 12:01:12 -04:00
describe('when conversions are enabled', function () {
beforeEach(function () {
Settings.enableConversions = true
})
2020-08-10 12:01:12 -04:00
it('should delete the convertedKey folder', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.deleteFile(bucket, key, err => {
expect(err).not.to.exist
expect(PersistorManager.deleteDirectory).to.have.been.calledWith(
bucket,
convertedFolderKey
)
done()
})
})
})
})
2020-08-10 12:01:12 -04:00
describe('deleteProject', function () {
it('should tell the filestore manager to delete the folder', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.deleteProject(bucket, projectKey, err => {
expect(err).not.to.exist
expect(PersistorManager.deleteDirectory).to.have.been.calledWith(
bucket,
projectKey
)
done()
})
})
2020-08-10 12:01:12 -04:00
it('should throw an error when the key is in the wrong format', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.deleteProject(bucket, 'wombat', err => {
expect(err).to.exist
done()
})
})
})
2020-08-10 12:01:12 -04:00
describe('getFile', function () {
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()
})
})
2020-08-10 12:01:12 -04:00
it('should pass options through to PersistorManager', function (done) {
const options = { start: 0, end: 8 }
2021-07-13 07:04:46 -04:00
FileHandler.getFile(bucket, key, options, err => {
expect(err).not.to.exist
expect(PersistorManager.getObjectStream).to.have.been.calledWith(
bucket,
key,
options
)
done()
})
})
2020-08-10 12:01:12 -04:00
describe('when a format is defined', function () {
let result
2020-08-10 12:01:12 -04:00
describe('when the file is not cached', function () {
beforeEach(function (done) {
FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
result = { err, stream }
done()
})
})
2020-08-10 12:01:12 -04:00
it('should convert the file', function () {
expect(FileConverter.promises.convert).to.have.been.called
})
2020-08-10 12:01:12 -04:00
it('should compress the converted file', function () {
expect(ImageOptimiser.promises.compressPng).to.have.been.called
})
2020-08-10 12:01:12 -04:00
it('should return the the converted stream', function () {
expect(result.err).not.to.exist
expect(result.stream).to.equal(readStream)
expect(PersistorManager.getObjectStream).to.have.been.calledWith(
bucket,
key
)
})
})
2020-08-10 12:01:12 -04:00
describe('when the file is cached', function () {
beforeEach(function (done) {
PersistorManager.checkIfObjectExists = sinon.stub().resolves(true)
FileHandler.getFile(bucket, key, { format: 'png' }, (err, stream) => {
result = { err, stream }
done()
})
})
2020-08-10 12:01:12 -04:00
it('should not convert the file', function () {
expect(FileConverter.promises.convert).not.to.have.been.called
})
2020-08-10 12:01:12 -04:00
it('should not compress the converted file again', function () {
expect(ImageOptimiser.promises.compressPng).not.to.have.been.called
})
2020-08-10 12:01:12 -04:00
it('should return the cached stream', function () {
expect(result.err).not.to.exist
expect(result.stream).to.equal(sourceStream)
expect(PersistorManager.getObjectStream).to.have.been.calledWith(
bucket,
convertedKey
)
})
})
})
2020-08-10 12:01:12 -04:00
describe('when a style is defined', function () {
it('generates a thumbnail when requested', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.getFile(bucket, key, { style: 'thumbnail' }, err => {
expect(err).not.to.exist
expect(FileConverter.promises.thumbnail).to.have.been.called
expect(FileConverter.promises.preview).not.to.have.been.called
done()
})
})
2020-08-10 12:01:12 -04:00
it('generates a preview when requested', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.getFile(bucket, key, { style: 'preview' }, err => {
expect(err).not.to.exist
expect(FileConverter.promises.thumbnail).not.to.have.been.called
expect(FileConverter.promises.preview).to.have.been.called
done()
})
})
})
})
2020-08-10 12:01:12 -04:00
describe('getRedirectUrl', function () {
beforeEach(function () {
Settings.filestore = {
allowRedirects: true,
stores: {
2021-07-13 07:04:46 -04:00
userFiles: bucket,
},
}
})
2020-08-10 12:01:12 -04:00
it('should return a redirect url', function (done) {
FileHandler.getRedirectUrl(bucket, key, (err, url) => {
expect(err).not.to.exist
expect(url).to.equal(redirectUrl)
done()
})
})
2020-08-10 12:01:12 -04:00
it('should call the persistor to get a redirect url', function (done) {
FileHandler.getRedirectUrl(bucket, key, () => {
expect(PersistorManager.getRedirectUrl).to.have.been.calledWith(
bucket,
key
)
done()
})
})
2020-08-10 12:01:12 -04:00
it('should return null if options are supplied', function (done) {
FileHandler.getRedirectUrl(
bucket,
key,
{ start: 100, end: 200 },
(err, url) => {
expect(err).not.to.exist
expect(url).to.be.null
done()
}
)
})
2020-08-10 12:01:12 -04:00
it('should return null if the bucket is not one of the defined ones', function (done) {
FileHandler.getRedirectUrl('a_different_bucket', key, (err, url) => {
expect(err).not.to.exist
expect(url).to.be.null
done()
})
})
2020-08-10 12:01:12 -04:00
it('should return null if redirects are not enabled', function (done) {
Settings.filestore.allowRedirects = false
FileHandler.getRedirectUrl(bucket, key, (err, url) => {
expect(err).not.to.exist
expect(url).to.be.null
done()
})
})
})
2020-08-10 12:01:12 -04:00
describe('getDirectorySize', function () {
it('should call the filestore manager to get directory size', function (done) {
2021-07-13 07:04:46 -04:00
FileHandler.getDirectorySize(bucket, key, err => {
expect(err).not.to.exist
expect(PersistorManager.directorySize).to.have.been.calledWith(
bucket,
key
)
done()
})
})
})
})