2019-12-16 06:20:29 -05:00
|
|
|
const sinon = require('sinon')
|
|
|
|
const chai = require('chai')
|
|
|
|
const { expect } = chai
|
|
|
|
const modulePath = '../../../app/js/LocalFileWriter.js'
|
|
|
|
const SandboxedModule = require('sandboxed-module')
|
2020-07-07 08:49:54 -04:00
|
|
|
const { Errors } = require('@overleaf/object-persistor')
|
2019-12-18 10:40:30 -05:00
|
|
|
chai.use(require('sinon-chai'))
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('LocalFileWriter', function () {
|
2019-12-18 10:40:30 -05:00
|
|
|
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 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2019-12-18 10:40:30 -05:00
|
|
|
fs = {
|
|
|
|
createWriteStream: sinon.stub().returns(writeStream),
|
2021-07-13 07:04:46 -04:00
|
|
|
unlink: sinon.stub().yields(),
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
stream = {
|
2021-07-13 07:04:46 -04:00
|
|
|
pipeline: sinon.stub().yields(),
|
2019-12-16 06:20:29 -05:00
|
|
|
}
|
2019-12-18 10:40:30 -05:00
|
|
|
|
2020-07-07 08:49:54 -04:00
|
|
|
const ObjectPersistor = { Errors }
|
|
|
|
|
2019-12-18 10:40:30 -05:00
|
|
|
LocalFileWriter = SandboxedModule.require(modulePath, {
|
2019-12-16 06:20:29 -05:00
|
|
|
requires: {
|
2019-12-18 10:40:30 -05:00
|
|
|
fs,
|
|
|
|
stream,
|
2021-07-12 12:47:19 -04:00
|
|
|
'@overleaf/settings': settings,
|
2020-11-25 06:57:23 -05:00
|
|
|
'@overleaf/metrics': {
|
2019-12-16 06:28:24 -05:00
|
|
|
inc: sinon.stub(),
|
2021-07-13 07:04:46 -04:00
|
|
|
Timer: sinon.stub().returns({ done: sinon.stub() }),
|
2020-07-07 08:49:54 -04:00
|
|
|
},
|
2021-07-13 07:04:46 -04:00
|
|
|
'@overleaf/object-persistor': ObjectPersistor,
|
|
|
|
},
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('writeStream', function () {
|
|
|
|
it('writes the stream to the upload folder', function (done) {
|
2019-12-18 10:40:30 -05:00
|
|
|
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()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2020-01-06 09:43:23 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('when there is an error', function () {
|
2020-01-06 09:43:23 -05:00
|
|
|
const error = new Error('not enough ketchup')
|
2020-08-10 12:01:12 -04:00
|
|
|
beforeEach(function () {
|
2020-01-06 09:43:23 -05:00
|
|
|
stream.pipeline.yields(error)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should wrap the error', function () {
|
2021-07-13 07:04:46 -04:00
|
|
|
LocalFileWriter.writeStream(readStream, filename, err => {
|
2020-01-06 09:43:23 -05:00
|
|
|
expect(err).to.exist
|
|
|
|
expect(err.cause).to.equal(error)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should delete the temporary file', function () {
|
2020-01-06 09:43:23 -05:00
|
|
|
LocalFileWriter.writeStream(readStream, filename, () => {
|
|
|
|
expect(fs.unlink).to.have.been.calledWith(fsPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
describe('deleteFile', function () {
|
|
|
|
it('should unlink the file', function (done) {
|
2021-07-13 07:04:46 -04:00
|
|
|
LocalFileWriter.deleteFile(fsPath, err => {
|
2019-12-18 10:40:30 -05:00
|
|
|
expect(err).not.to.exist
|
|
|
|
expect(fs.unlink).to.have.been.calledWith(fsPath)
|
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not call unlink with an empty path', function (done) {
|
2021-07-13 07:04:46 -04:00
|
|
|
LocalFileWriter.deleteFile('', err => {
|
2020-01-06 09:43:23 -05:00
|
|
|
expect(err).not.to.exist
|
|
|
|
expect(fs.unlink).not.to.have.been.called
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
|
|
|
})
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should not throw a error if the file does not exist', function (done) {
|
2020-01-06 09:43:23 -05:00
|
|
|
const error = new Error('file not found')
|
|
|
|
error.code = 'ENOENT'
|
|
|
|
fs.unlink = sinon.stub().yields(error)
|
2021-07-13 07:04:46 -04:00
|
|
|
LocalFileWriter.deleteFile(fsPath, err => {
|
2019-12-18 10:40:30 -05:00
|
|
|
expect(err).not.to.exist
|
2020-01-06 09:43:23 -05:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
it('should wrap the error', function (done) {
|
2020-01-06 09:43:23 -05:00
|
|
|
const error = new Error('failed to reticulate splines')
|
|
|
|
fs.unlink = sinon.stub().yields(error)
|
2021-07-13 07:04:46 -04:00
|
|
|
LocalFileWriter.deleteFile(fsPath, err => {
|
2020-01-06 09:43:23 -05:00
|
|
|
expect(err).to.exist
|
|
|
|
expect(err.cause).to.equal(error)
|
2019-12-18 10:40:30 -05:00
|
|
|
done()
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|
2019-12-18 10:40:30 -05:00
|
|
|
})
|
|
|
|
})
|
2019-12-16 06:20:29 -05:00
|
|
|
})
|