| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const sinon = require('sinon')
- const chai = require('chai')
- const { expect } = chai
- const modulePath = '../../../app/js/LocalFileWriter.js'
- const SandboxedModule = require('sandboxed-module')
- chai.use(require('sinon-chai'))
- 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
- beforeEach(function() {
- fs = {
- createWriteStream: sinon.stub().returns(writeStream),
- unlink: sinon.stub().yields()
- }
- stream = {
- pipeline: sinon.stub().yields()
- }
- LocalFileWriter = SandboxedModule.require(modulePath, {
- requires: {
- fs,
- stream,
- 'logger-sharelatex': {
- log() {},
- err() {}
- },
- 'settings-sharelatex': settings,
- 'metrics-sharelatex': {
- inc: sinon.stub(),
- Timer: sinon.stub().returns({ done: sinon.stub() })
- }
- }
- })
- })
- 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()
- })
- })
- describe('when there is an error', function() {
- const error = new Error('not enough ketchup')
- beforeEach(function() {
- stream.pipeline.yields(error)
- })
- it('should wrap the error', function() {
- LocalFileWriter.writeStream(readStream, filename, err => {
- expect(err).to.exist
- expect(err.cause).to.equal(error)
- })
- })
- it('should delete the temporary file', function() {
- LocalFileWriter.writeStream(readStream, filename, () => {
- expect(fs.unlink).to.have.been.calledWith(fsPath)
- })
- })
- })
- })
- 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()
- })
- })
- 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()
- })
- })
- 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)
- LocalFileWriter.deleteFile(fsPath, err => {
- expect(err).not.to.exist
- done()
- })
- })
- it('should wrap the error', function(done) {
- const error = new Error('failed to reticulate splines')
- fs.unlink = sinon.stub().yields(error)
- LocalFileWriter.deleteFile(fsPath, err => {
- expect(err).to.exist
- expect(err.cause).to.equal(error)
- done()
- })
- })
- })
- })
|