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

112 lines
3.3 KiB
JavaScript
Raw Normal View History

const sinon = require('sinon')
const chai = require('chai')
const { expect } = chai
const modulePath = '../../../app/js/LocalFileWriter.js'
const SandboxedModule = require('sandboxed-module')
const { Errors } = require('@overleaf/object-persistor')
chai.use(require('sinon-chai'))
2014-02-14 16:39:05 +00:00
2020-08-10 16:01:12 +00:00
describe('LocalFileWriter', function () {
const writeStream = 'writeStream'
const readStream = 'readStream'
const settings = { path: { uploadFolder: '/uploads' } }
const fsPath = '/uploads/wombat'
const filename = 'wombat'
let stream, fs, LocalFileWriter
2020-08-10 16:01:12 +00:00
beforeEach(function () {
fs = {
createWriteStream: sinon.stub().returns(writeStream),
unlink: sinon.stub().yields()
}
stream = {
pipeline: sinon.stub().yields()
}
const ObjectPersistor = { Errors }
LocalFileWriter = SandboxedModule.require(modulePath, {
requires: {
fs,
stream,
'settings-sharelatex': settings,
'@overleaf/metrics': {
inc: sinon.stub(),
Timer: sinon.stub().returns({ done: sinon.stub() })
},
'@overleaf/object-persistor': ObjectPersistor
}
})
})
2014-02-14 16:39:05 +00:00
2020-08-10 16:01:12 +00:00
describe('writeStream', function () {
it('writes the stream to the upload folder', function (done) {
LocalFileWriter.writeStream(readStream, filename, (err, path) => {
expect(err).not.to.exist
expect(fs.createWriteStream).to.have.been.calledWith(fsPath)
expect(stream.pipeline).to.have.been.calledWith(readStream, writeStream)
expect(path).to.equal(fsPath)
done()
})
})
2020-08-10 16:01:12 +00:00
describe('when there is an error', function () {
const error = new Error('not enough ketchup')
2020-08-10 16:01:12 +00:00
beforeEach(function () {
stream.pipeline.yields(error)
})
2020-08-10 16:01:12 +00:00
it('should wrap the error', function () {
LocalFileWriter.writeStream(readStream, filename, (err) => {
expect(err).to.exist
expect(err.cause).to.equal(error)
})
})
2020-08-10 16:01:12 +00:00
it('should delete the temporary file', function () {
LocalFileWriter.writeStream(readStream, filename, () => {
expect(fs.unlink).to.have.been.calledWith(fsPath)
})
})
})
})
2014-02-14 16:39:05 +00:00
2020-08-10 16:01:12 +00:00
describe('deleteFile', function () {
it('should unlink the file', function (done) {
LocalFileWriter.deleteFile(fsPath, (err) => {
expect(err).not.to.exist
expect(fs.unlink).to.have.been.calledWith(fsPath)
done()
})
})
2014-02-14 16:39:05 +00:00
2020-08-10 16:01:12 +00:00
it('should not call unlink with an empty path', function (done) {
LocalFileWriter.deleteFile('', (err) => {
expect(err).not.to.exist
expect(fs.unlink).not.to.have.been.called
done()
})
})
2014-02-14 16:39:05 +00:00
2020-08-10 16:01:12 +00:00
it('should not throw a error if the file does not exist', function (done) {
const error = new Error('file not found')
error.code = 'ENOENT'
fs.unlink = sinon.stub().yields(error)
2020-08-10 16:01:12 +00:00
LocalFileWriter.deleteFile(fsPath, (err) => {
expect(err).not.to.exist
done()
})
})
2020-08-10 16:01:12 +00:00
it('should wrap the error', function (done) {
const error = new Error('failed to reticulate splines')
fs.unlink = sinon.stub().yields(error)
2020-08-10 16:01:12 +00:00
LocalFileWriter.deleteFile(fsPath, (err) => {
expect(err).to.exist
expect(err.cause).to.equal(error)
done()
})
})
})
})