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

129 lines
3 KiB
JavaScript
Raw Normal View History

/* eslint-disable
handle-callback-err,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2014-02-14 11:39:05 -05:00
const {
assert
} = require("chai");
const sinon = require('sinon');
const chai = require('chai');
const should = chai.should();
const {
expect
} = chai;
const modulePath = "../../../app/js/LocalFileWriter.js";
const SandboxedModule = require('sandboxed-module');
2014-02-14 11:39:05 -05:00
describe("LocalFileWriter", function() {
2014-02-14 11:39:05 -05:00
beforeEach(function() {
2014-02-14 11:39:05 -05:00
this.writeStream = {
on(type, cb){
if (type === "finish") {
return cb();
}
}
};
this.readStream =
{on() {}};
this.fs = {
createWriteStream : sinon.stub().returns(this.writeStream),
createReadStream: sinon.stub().returns(this.readStream),
2014-02-14 11:39:05 -05:00
unlink: sinon.stub()
};
this.settings = {
path: {
uploadFolder:"somewhere"
}
};
this.writer = SandboxedModule.require(modulePath, { requires: {
"fs": this.fs,
"logger-sharelatex": {
log() {},
err() {}
},
"settings-sharelatex":this.settings,
"metrics-sharelatex": {
inc() {},
Timer() {
return {done() {}};
}
}
}
}
);
return this.stubbedFsPath = "something/uploads/eio2k1j3";
});
2014-02-14 11:39:05 -05:00
describe("writeStrem", function() {
beforeEach(function() {
return this.writer._getPath = sinon.stub().returns(this.stubbedFsPath);
});
2014-02-14 11:39:05 -05:00
it("write the stream to ./uploads", function(done){
const stream = {
pipe: dest=> {
dest.should.equal(this.writeStream);
return done();
},
on() {}
};
return this.writer.writeStream(stream, null, ()=> {});
});
2014-02-14 11:39:05 -05:00
return it("should send the path in the callback", function(done){
const stream = {
pipe: dest=> {},
on(type, cb){
if (type === "end") {
return cb();
}
}
};
return this.writer.writeStream(stream, null, (err, fsPath)=> {
fsPath.should.equal(this.stubbedFsPath);
return done();
});
});
});
2014-02-14 11:39:05 -05:00
describe("getStream", function() {
it("should read the stream from the file ", function(done){
return this.writer.getStream(this.stubbedFsPath, (err, stream)=> {
this.fs.createReadStream.calledWith(this.stubbedFsPath).should.equal(true);
return done();
});
});
return it("should send the stream in the callback", function(done){
return this.writer.getStream(this.stubbedFsPath, (err, readStream)=> {
readStream.should.equal(this.readStream);
return done();
});
});
});
return describe("delete file", () => it("should unlink the file", function(done){
const error = "my error";
this.fs.unlink.callsArgWith(1, error);
return this.writer.deleteFile(this.stubbedFsPath, err=> {
this.fs.unlink.calledWith(this.stubbedFsPath).should.equal(true);
err.should.equal(error);
return done();
});
}));
});
2014-02-14 11:39:05 -05:00