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

104 lines
3.3 KiB
JavaScript
Raw Normal View History

const sinon = require('sinon')
const chai = require('chai')
const { expect } = chai
const SandboxedModule = require('sandboxed-module')
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
const modulePath = '../../../app/js/FileConverter.js'
describe('FileConverter', function() {
2019-12-19 05:41:41 -05:00
let SafeExec, FileConverter
const sourcePath = '/data/wombat.eps'
const destPath = '/tmp/dest.png'
const format = 'png'
const errorMessage = 'guru meditation error'
const Settings = {
commands: {
convertCommandPrefix: []
}
}
beforeEach(function() {
2019-12-19 05:41:41 -05:00
SafeExec = {
promises: sinon.stub().resolves(destPath)
}
FileConverter = SandboxedModule.require(modulePath, {
requires: {
2019-12-19 05:41:41 -05:00
'./SafeExec': SafeExec,
'metrics-sharelatex': {
inc: sinon.stub(),
Timer: sinon.stub().returns({ done: sinon.stub() })
},
2019-12-19 05:41:41 -05:00
'settings-sharelatex': Settings
}
})
})
2014-02-14 11:39:05 -05:00
describe('convert', function() {
2019-12-19 05:41:41 -05:00
it('should convert the source to the requested format', async function() {
await FileConverter.promises.convert(sourcePath, format)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
expect(args).to.include(`${sourcePath}.${format}`)
})
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
it('should return the dest path', async function() {
const destPath = await FileConverter.promises.convert(sourcePath, format)
destPath.should.equal(`${sourcePath}.${format}`)
})
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
it('should wrap the error from convert', async function() {
SafeExec.promises.rejects(errorMessage)
try {
await FileConverter.promises.convert(sourcePath, format)
expect('error should have been thrown').not.to.exist
} catch (err) {
expect(err.name).to.equal('ConversionError')
expect(err.cause.toString()).to.equal(errorMessage)
}
})
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
it('should not accept an non approved format', async function() {
try {
await FileConverter.promises.convert(sourcePath, 'potato')
expect('error should have been thrown').not.to.exist
} catch (err) {
expect(err.name).to.equal('ConversionError')
}
})
it('should prefix the command with Settings.commands.convertCommandPrefix', async function() {
Settings.commands.convertCommandPrefix = ['nice']
await FileConverter.promises.convert(sourcePath, format)
})
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
it('should convert the file when called as a callback', function(done) {
FileConverter.convert(sourcePath, format, (err, destPath) => {
expect(err).not.to.exist
destPath.should.equal(`${sourcePath}.${format}`)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
expect(args).to.include(`${sourcePath}.${format}`)
done()
})
})
})
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
describe('thumbnail', function() {
it('should call converter resize with args', async function() {
await FileConverter.promises.thumbnail(sourcePath)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
})
})
2014-02-14 11:39:05 -05:00
2019-12-19 05:41:41 -05:00
describe('preview', function() {
it('should call converter resize with args', async function() {
await FileConverter.promises.preview(sourcePath)
const args = SafeExec.promises.args[0][0]
expect(args).to.include(`${sourcePath}[0]`)
})
})
})