Prettier: convert test/unit decaffeinated files to Prettier format

This commit is contained in:
Simon Detheridge 2019-12-16 11:20:29 +00:00
parent ec60f778e6
commit eae7d28495
13 changed files with 2642 additions and 2129 deletions

View file

@ -11,351 +11,499 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const sinon = require('sinon'); const sinon = require('sinon')
const chai = require('chai'); const chai = require('chai')
const should = chai.should(); const should = chai.should()
const { const { expect } = chai
expect
} = chai;
const modulePath = "../../../app/js/AWSSDKPersistorManager.js"; const modulePath = '../../../app/js/AWSSDKPersistorManager.js'
const SandboxedModule = require('sandboxed-module'); const SandboxedModule = require('sandboxed-module')
describe("AWSSDKPersistorManager", function() { describe('AWSSDKPersistorManager', function() {
beforeEach(function() { beforeEach(function() {
this.settings = { this.settings = {
filestore: { filestore: {
backend: "aws-sdk" backend: 'aws-sdk'
} }
}; }
this.s3 = { this.s3 = {
upload: sinon.stub(), upload: sinon.stub(),
getObject: sinon.stub(), getObject: sinon.stub(),
copyObject: sinon.stub(), copyObject: sinon.stub(),
deleteObject: sinon.stub(), deleteObject: sinon.stub(),
listObjects: sinon.stub(), listObjects: sinon.stub(),
deleteObjects: sinon.stub(), deleteObjects: sinon.stub(),
headObject: sinon.stub() headObject: sinon.stub()
}; }
this.awssdk = this.awssdk = { S3: sinon.stub().returns(this.s3) }
{S3: sinon.stub().returns(this.s3)};
this.requires = { this.requires = {
"aws-sdk": this.awssdk, 'aws-sdk': this.awssdk,
"settings-sharelatex": this.settings, 'settings-sharelatex': this.settings,
"logger-sharelatex": { 'logger-sharelatex': {
log() {}, log() {},
err() {} err() {}
}, },
"fs": (this.fs = fs: (this.fs = { createReadStream: sinon.stub() }),
{createReadStream: sinon.stub()}), './Errors': (this.Errors = { NotFoundError: sinon.stub() })
"./Errors": (this.Errors = }
{NotFoundError: sinon.stub()}) this.key = 'my/key'
}; this.bucketName = 'my-bucket'
this.key = "my/key"; this.error = 'my error'
this.bucketName = "my-bucket"; return (this.AWSSDKPersistorManager = SandboxedModule.require(modulePath, {
this.error = "my error"; requires: this.requires
return this.AWSSDKPersistorManager = SandboxedModule.require(modulePath, {requires: this.requires}); }))
}); })
describe("sendFile", function() { describe('sendFile', function() {
beforeEach(function() { beforeEach(function() {
this.stream = {}; this.stream = {}
this.fsPath = "/usr/local/some/file"; this.fsPath = '/usr/local/some/file'
return this.fs.createReadStream.returns(this.stream); return this.fs.createReadStream.returns(this.stream)
}); })
it("should put the file with s3.upload", function(done) { it('should put the file with s3.upload', function(done) {
this.s3.upload.callsArgWith(1); this.s3.upload.callsArgWith(1)
return this.AWSSDKPersistorManager.sendFile(this.bucketName, this.key, this.fsPath, err => { return this.AWSSDKPersistorManager.sendFile(
expect(err).to.not.be.ok; this.bucketName,
expect(this.s3.upload.calledOnce, "called only once").to.be.true; this.key,
expect((this.s3.upload.calledWith({Bucket: this.bucketName, Key: this.key, Body: this.stream})) this.fsPath,
, "called with correct arguments").to.be.true; err => {
return done(); expect(err).to.not.be.ok
}); expect(this.s3.upload.calledOnce, 'called only once').to.be.true
}); expect(
this.s3.upload.calledWith({
Bucket: this.bucketName,
Key: this.key,
Body: this.stream
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
return it("should dispatch the error from s3.upload", function(done) { return it('should dispatch the error from s3.upload', function(done) {
this.s3.upload.callsArgWith(1, this.error); this.s3.upload.callsArgWith(1, this.error)
return this.AWSSDKPersistorManager.sendFile(this.bucketName, this.key, this.fsPath, err => { return this.AWSSDKPersistorManager.sendFile(
expect(err).to.equal(this.error); this.bucketName,
return done(); this.key,
}); this.fsPath,
}); err => {
}); expect(err).to.equal(this.error)
return done()
}
)
})
})
describe('sendStream', function() {
beforeEach(function() {
return (this.stream = {})
})
describe("sendStream", function() { it('should put the file with s3.upload', function(done) {
beforeEach(function() { this.s3.upload.callsArgWith(1)
return this.stream = {};}); return this.AWSSDKPersistorManager.sendStream(
this.bucketName,
this.key,
this.stream,
err => {
expect(err).to.not.be.ok
expect(this.s3.upload.calledOnce, 'called only once').to.be.true
expect(
this.s3.upload.calledWith({
Bucket: this.bucketName,
Key: this.key,
Body: this.stream
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
it("should put the file with s3.upload", function(done) { return it('should dispatch the error from s3.upload', function(done) {
this.s3.upload.callsArgWith(1); this.s3.upload.callsArgWith(1, this.error)
return this.AWSSDKPersistorManager.sendStream(this.bucketName, this.key, this.stream, err => { return this.AWSSDKPersistorManager.sendStream(
expect(err).to.not.be.ok; this.bucketName,
expect(this.s3.upload.calledOnce, "called only once").to.be.true; this.key,
expect((this.s3.upload.calledWith({Bucket: this.bucketName, Key: this.key, Body: this.stream})), this.stream,
"called with correct arguments").to.be.true; err => {
return done(); expect(err).to.equal(this.error)
}); return done()
}); }
)
})
})
return it("should dispatch the error from s3.upload", function(done) { describe('getFileStream', function() {
this.s3.upload.callsArgWith(1, this.error); beforeEach(function() {
return this.AWSSDKPersistorManager.sendStream(this.bucketName, this.key, this.stream, err => { this.opts = {}
expect(err).to.equal(this.error); this.stream = {}
return done(); this.read_stream = { on: (this.read_stream_on = sinon.stub()) }
}); this.object = { createReadStream: sinon.stub().returns(this.read_stream) }
}); return this.s3.getObject.returns(this.object)
}); })
describe("getFileStream", function() { it('should return a stream from s3.getObject', function(done) {
beforeEach(function() { this.read_stream_on.withArgs('readable').callsArgWith(1)
this.opts = {};
this.stream = {};
this.read_stream =
{on: (this.read_stream_on = sinon.stub())};
this.object =
{createReadStream: sinon.stub().returns(this.read_stream)};
return this.s3.getObject.returns(this.object);
});
it("should return a stream from s3.getObject", function(done) { return this.AWSSDKPersistorManager.getFileStream(
this.read_stream_on.withArgs('readable').callsArgWith(1); this.bucketName,
this.key,
this.opts,
(err, stream) => {
expect(this.read_stream_on.calledTwice)
expect(err).to.not.be.ok
expect(stream, 'returned the stream').to.equal(this.read_stream)
expect(
this.s3.getObject.calledWith({
Bucket: this.bucketName,
Key: this.key
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
return this.AWSSDKPersistorManager.getFileStream(this.bucketName, this.key, this.opts, (err, stream) => { describe('with start and end options', function() {
expect(this.read_stream_on.calledTwice); beforeEach(function() {
expect(err).to.not.be.ok; return (this.opts = {
expect(stream, "returned the stream").to.equal(this.read_stream); start: 0,
expect((this.s3.getObject.calledWith({Bucket: this.bucketName, Key: this.key})), end: 8
"called with correct arguments").to.be.true; })
return done(); })
}); return it('should pass headers to the s3.GetObject', function(done) {
}); this.read_stream_on.withArgs('readable').callsArgWith(1)
this.AWSSDKPersistorManager.getFileStream(
this.bucketName,
this.key,
this.opts,
(err, stream) => {
return expect(
this.s3.getObject.calledWith({
Bucket: this.bucketName,
Key: this.key,
Range: 'bytes=0-8'
}),
'called with correct arguments'
).to.be.true
}
)
return done()
})
})
describe("with start and end options", function() { return describe('error conditions', function() {
beforeEach(function() { describe("when the file doesn't exist", function() {
return this.opts = { beforeEach(function() {
start: 0, this.error = new Error()
end: 8 return (this.error.code = 'NoSuchKey')
}; })
}); return it('should produce a NotFoundError', function(done) {
return it("should pass headers to the s3.GetObject", function(done) { this.read_stream_on.withArgs('error').callsArgWith(1, this.error)
this.read_stream_on.withArgs('readable').callsArgWith(1); return this.AWSSDKPersistorManager.getFileStream(
this.AWSSDKPersistorManager.getFileStream(this.bucketName, this.key, this.opts, (err, stream) => { this.bucketName,
return expect((this.s3.getObject.calledWith({Bucket: this.bucketName, Key: this.key, Range: 'bytes=0-8'})), this.key,
"called with correct arguments").to.be.true; this.opts,
}); (err, stream) => {
return done(); expect(stream).to.not.be.ok
}); expect(err).to.be.ok
}); expect(
err instanceof this.Errors.NotFoundError,
'error is a correct instance'
).to.equal(true)
return done()
}
)
})
})
return describe("error conditions", function() { return describe('when there is some other error', function() {
describe("when the file doesn't exist", function() { beforeEach(function() {
beforeEach(function() { return (this.error = new Error())
this.error = new Error(); })
return this.error.code = 'NoSuchKey'; return it('should dispatch the error from s3 object stream', function(done) {
}); this.read_stream_on.withArgs('error').callsArgWith(1, this.error)
return it("should produce a NotFoundError", function(done) { return this.AWSSDKPersistorManager.getFileStream(
this.read_stream_on.withArgs('error').callsArgWith(1, this.error); this.bucketName,
return this.AWSSDKPersistorManager.getFileStream(this.bucketName, this.key, this.opts, (err, stream) => { this.key,
expect(stream).to.not.be.ok; this.opts,
expect(err).to.be.ok; (err, stream) => {
expect(err instanceof this.Errors.NotFoundError, "error is a correct instance").to.equal(true); expect(stream).to.not.be.ok
return done(); expect(err).to.be.ok
}); expect(err).to.equal(this.error)
}); return done()
}); }
)
})
})
})
})
return describe("when there is some other error", function() { describe('copyFile', function() {
beforeEach(function() { beforeEach(function() {
return this.error = new Error(); this.destKey = 'some/key'
}); return (this.stream = {})
return it("should dispatch the error from s3 object stream", function(done) { })
this.read_stream_on.withArgs('error').callsArgWith(1, this.error);
return this.AWSSDKPersistorManager.getFileStream(this.bucketName, this.key, this.opts, (err, stream) => {
expect(stream).to.not.be.ok;
expect(err).to.be.ok;
expect(err).to.equal(this.error);
return done();
});
});
});
});
});
describe("copyFile", function() { it('should copy the file with s3.copyObject', function(done) {
beforeEach(function() { this.s3.copyObject.callsArgWith(1)
this.destKey = "some/key"; return this.AWSSDKPersistorManager.copyFile(
return this.stream = {};}); this.bucketName,
this.key,
this.destKey,
err => {
expect(err).to.not.be.ok
expect(this.s3.copyObject.calledOnce, 'called only once').to.be.true
expect(
this.s3.copyObject.calledWith({
Bucket: this.bucketName,
Key: this.destKey,
CopySource: this.bucketName + '/' + this.key
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
it("should copy the file with s3.copyObject", function(done) { return it('should dispatch the error from s3.copyObject', function(done) {
this.s3.copyObject.callsArgWith(1); this.s3.copyObject.callsArgWith(1, this.error)
return this.AWSSDKPersistorManager.copyFile(this.bucketName, this.key, this.destKey, err => { return this.AWSSDKPersistorManager.copyFile(
expect(err).to.not.be.ok; this.bucketName,
expect(this.s3.copyObject.calledOnce, "called only once").to.be.true; this.key,
expect((this.s3.copyObject.calledWith({Bucket: this.bucketName, Key: this.destKey, CopySource: this.bucketName + '/' + this.key})), this.destKey,
"called with correct arguments").to.be.true; err => {
return done(); expect(err).to.equal(this.error)
}); return done()
}); }
)
})
})
return it("should dispatch the error from s3.copyObject", function(done) { describe('deleteFile', function() {
this.s3.copyObject.callsArgWith(1, this.error); it('should delete the file with s3.deleteObject', function(done) {
return this.AWSSDKPersistorManager.copyFile(this.bucketName, this.key, this.destKey, err => { this.s3.deleteObject.callsArgWith(1)
expect(err).to.equal(this.error); return this.AWSSDKPersistorManager.deleteFile(
return done(); this.bucketName,
}); this.key,
}); err => {
}); expect(err).to.not.be.ok
expect(this.s3.deleteObject.calledOnce, 'called only once').to.be.true
expect(
this.s3.deleteObject.calledWith({
Bucket: this.bucketName,
Key: this.key
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
describe("deleteFile", function() { return it('should dispatch the error from s3.deleteObject', function(done) {
it("should delete the file with s3.deleteObject", function(done) { this.s3.deleteObject.callsArgWith(1, this.error)
this.s3.deleteObject.callsArgWith(1); return this.AWSSDKPersistorManager.deleteFile(
return this.AWSSDKPersistorManager.deleteFile(this.bucketName, this.key, err => { this.bucketName,
expect(err).to.not.be.ok; this.key,
expect(this.s3.deleteObject.calledOnce, "called only once").to.be.true; err => {
expect((this.s3.deleteObject.calledWith({Bucket: this.bucketName, Key: this.key})), expect(err).to.equal(this.error)
"called with correct arguments").to.be.true; return done()
return done(); }
}); )
}); })
})
return it("should dispatch the error from s3.deleteObject", function(done) { describe('deleteDirectory', function() {
this.s3.deleteObject.callsArgWith(1, this.error); it('should list the directory content using s3.listObjects', function(done) {
return this.AWSSDKPersistorManager.deleteFile(this.bucketName, this.key, err => { this.s3.listObjects.callsArgWith(1, null, { Contents: [] })
expect(err).to.equal(this.error); return this.AWSSDKPersistorManager.deleteDirectory(
return done(); this.bucketName,
}); this.key,
}); err => {
}); expect(err).to.not.be.ok
expect(this.s3.listObjects.calledOnce, 'called only once').to.be.true
expect(
this.s3.listObjects.calledWith({
Bucket: this.bucketName,
Prefix: this.key
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
describe("deleteDirectory", function() { it('should dispatch the error from s3.listObjects', function(done) {
this.s3.listObjects.callsArgWith(1, this.error)
return this.AWSSDKPersistorManager.deleteDirectory(
this.bucketName,
this.key,
err => {
expect(err).to.equal(this.error)
return done()
}
)
})
it("should list the directory content using s3.listObjects", function(done) { return describe('with directory content', function() {
this.s3.listObjects.callsArgWith(1, null, {Contents: []}); beforeEach(function() {
return this.AWSSDKPersistorManager.deleteDirectory(this.bucketName, this.key, err => { return (this.fileList = [{ Key: 'foo' }, { Key: 'bar', Key: 'baz' }])
expect(err).to.not.be.ok; })
expect(this.s3.listObjects.calledOnce, "called only once").to.be.true;
expect((this.s3.listObjects.calledWith({Bucket: this.bucketName, Prefix: this.key})),
"called with correct arguments").to.be.true;
return done();
});
});
it("should dispatch the error from s3.listObjects", function(done) { it('should forward the file keys to s3.deleteObjects', function(done) {
this.s3.listObjects.callsArgWith(1, this.error); this.s3.listObjects.callsArgWith(1, null, { Contents: this.fileList })
return this.AWSSDKPersistorManager.deleteDirectory(this.bucketName, this.key, err => { this.s3.deleteObjects.callsArgWith(1)
expect(err).to.equal(this.error); return this.AWSSDKPersistorManager.deleteDirectory(
return done(); this.bucketName,
}); this.key,
}); err => {
expect(err).to.not.be.ok
expect(this.s3.deleteObjects.calledOnce, 'called only once').to.be
.true
expect(
this.s3.deleteObjects.calledWith({
Bucket: this.bucketName,
Delete: {
Quiet: true,
Objects: this.fileList
}
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
return describe("with directory content", function() { return it('should dispatch the error from s3.deleteObjects', function(done) {
beforeEach(function() { this.s3.listObjects.callsArgWith(1, null, { Contents: this.fileList })
return this.fileList = [ this.s3.deleteObjects.callsArgWith(1, this.error)
{Key: 'foo'} return this.AWSSDKPersistorManager.deleteDirectory(
, { Key: 'bar' this.bucketName,
, Key: 'baz' this.key,
} err => {
];}); expect(err).to.equal(this.error)
return done()
}
)
})
})
})
it("should forward the file keys to s3.deleteObjects", function(done) { describe('checkIfFileExists', function() {
this.s3.listObjects.callsArgWith(1, null, {Contents: this.fileList}); it('should check for the file with s3.headObject', function(done) {
this.s3.deleteObjects.callsArgWith(1); this.s3.headObject.callsArgWith(1, null, {})
return this.AWSSDKPersistorManager.deleteDirectory(this.bucketName, this.key, err => { return this.AWSSDKPersistorManager.checkIfFileExists(
expect(err).to.not.be.ok; this.bucketName,
expect(this.s3.deleteObjects.calledOnce, "called only once").to.be.true; this.key,
expect((this.s3.deleteObjects.calledWith({ (err, exists) => {
Bucket: this.bucketName, expect(err).to.not.be.ok
Delete: { expect(this.s3.headObject.calledOnce, 'called only once').to.be.true
Quiet: true, expect(
Objects: this.fileList this.s3.headObject.calledWith({
}})), Bucket: this.bucketName,
"called with correct arguments").to.be.true; Key: this.key
return done(); }),
}); 'called with correct arguments'
}); ).to.be.true
return done()
}
)
})
return it("should dispatch the error from s3.deleteObjects", function(done) { it('should return false on an inexistant file', function(done) {
this.s3.listObjects.callsArgWith(1, null, {Contents: this.fileList}); this.s3.headObject.callsArgWith(1, null, {})
this.s3.deleteObjects.callsArgWith(1, this.error); return this.AWSSDKPersistorManager.checkIfFileExists(
return this.AWSSDKPersistorManager.deleteDirectory(this.bucketName, this.key, err => { this.bucketName,
expect(err).to.equal(this.error); this.key,
return done(); (err, exists) => {
}); expect(exists).to.be.false
}); return done()
}); }
}); )
})
it('should return true on an existing file', function(done) {
this.s3.headObject.callsArgWith(1, null, { ETag: 'etag' })
return this.AWSSDKPersistorManager.checkIfFileExists(
this.bucketName,
this.key,
(err, exists) => {
expect(exists).to.be.true
return done()
}
)
})
describe("checkIfFileExists", function() { return it('should dispatch the error from s3.headObject', function(done) {
this.s3.headObject.callsArgWith(1, this.error)
return this.AWSSDKPersistorManager.checkIfFileExists(
this.bucketName,
this.key,
(err, exists) => {
expect(err).to.equal(this.error)
return done()
}
)
})
})
it("should check for the file with s3.headObject", function(done) { return describe('directorySize', function() {
this.s3.headObject.callsArgWith(1, null, {}); it('should list the directory content using s3.listObjects', function(done) {
return this.AWSSDKPersistorManager.checkIfFileExists(this.bucketName, this.key, (err, exists) => { this.s3.listObjects.callsArgWith(1, null, { Contents: [] })
expect(err).to.not.be.ok; return this.AWSSDKPersistorManager.directorySize(
expect(this.s3.headObject.calledOnce, "called only once").to.be.true; this.bucketName,
expect((this.s3.headObject.calledWith({Bucket: this.bucketName, Key: this.key})), this.key,
"called with correct arguments").to.be.true; err => {
return done(); expect(err).to.not.be.ok
}); expect(this.s3.listObjects.calledOnce, 'called only once').to.be.true
}); expect(
this.s3.listObjects.calledWith({
Bucket: this.bucketName,
Prefix: this.key
}),
'called with correct arguments'
).to.be.true
return done()
}
)
})
it("should return false on an inexistant file", function(done) { it('should dispatch the error from s3.listObjects', function(done) {
this.s3.headObject.callsArgWith(1, null, {}); this.s3.listObjects.callsArgWith(1, this.error)
return this.AWSSDKPersistorManager.checkIfFileExists(this.bucketName, this.key, (err, exists) => { return this.AWSSDKPersistorManager.directorySize(
expect(exists).to.be.false; this.bucketName,
return done(); this.key,
}); err => {
}); expect(err).to.equal(this.error)
return done()
}
)
})
it("should return true on an existing file", function(done) { return it('should sum directory files sizes', function(done) {
this.s3.headObject.callsArgWith(1, null, {ETag: "etag"}); this.s3.listObjects.callsArgWith(1, null, {
return this.AWSSDKPersistorManager.checkIfFileExists(this.bucketName, this.key, (err, exists) => { Contents: [{ Size: 1024 }, { Size: 2048 }]
expect(exists).to.be.true; })
return done(); return this.AWSSDKPersistorManager.directorySize(
}); this.bucketName,
}); this.key,
(err, size) => {
return it("should dispatch the error from s3.headObject", function(done) { expect(size).to.equal(3072)
this.s3.headObject.callsArgWith(1, this.error); return done()
return this.AWSSDKPersistorManager.checkIfFileExists(this.bucketName, this.key, (err, exists) => { }
expect(err).to.equal(this.error); )
return done(); })
}); })
}); })
});
return describe("directorySize", function() {
it("should list the directory content using s3.listObjects", function(done) {
this.s3.listObjects.callsArgWith(1, null, {Contents: []});
return this.AWSSDKPersistorManager.directorySize(this.bucketName, this.key, err => {
expect(err).to.not.be.ok;
expect(this.s3.listObjects.calledOnce, "called only once").to.be.true;
expect((this.s3.listObjects.calledWith({Bucket: this.bucketName, Prefix: this.key})),
"called with correct arguments").to.be.true;
return done();
});
});
it("should dispatch the error from s3.listObjects", function(done) {
this.s3.listObjects.callsArgWith(1, this.error);
return this.AWSSDKPersistorManager.directorySize(this.bucketName, this.key, err => {
expect(err).to.equal(this.error);
return done();
});
});
return it("should sum directory files sizes", function(done) {
this.s3.listObjects.callsArgWith(1, null, {Contents: [ { Size: 1024 }, { Size: 2048 }]});
return this.AWSSDKPersistorManager.directorySize(this.bucketName, this.key, (err, size) => {
expect(size).to.equal(3072);
return done();
});
});
});
});

View file

@ -9,98 +9,92 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/BucketController.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/BucketController.js";
const SandboxedModule = require('sandboxed-module');
describe("BucketController", function() { describe('BucketController', function() {
beforeEach(function() {
this.PersistorManager = {
sendStream: sinon.stub(),
copyFile: sinon.stub(),
deleteFile: sinon.stub()
}
beforeEach(function() { this.settings = {
this.PersistorManager = { s3: {
sendStream: sinon.stub(), buckets: {
copyFile: sinon.stub(), user_files: 'user_files'
deleteFile:sinon.stub() }
}; },
filestore: {
backend: 's3',
s3: {
secret: 'secret',
key: 'this_key'
}
}
}
this.settings = { this.FileHandler = {
s3: { getFile: sinon.stub(),
buckets: { deleteFile: sinon.stub(),
user_files:"user_files" insertFile: sinon.stub(),
} getDirectorySize: sinon.stub()
}, }
filestore: { this.LocalFileWriter = {}
backend: "s3", this.controller = SandboxedModule.require(modulePath, {
s3: { requires: {
secret: "secret", './LocalFileWriter': this.LocalFileWriter,
key: "this_key" './FileHandler': this.FileHandler,
} './PersistorManager': this.PersistorManager,
} 'settings-sharelatex': this.settings,
}; 'metrics-sharelatex': {
inc() {}
},
'logger-sharelatex': {
log() {},
err() {}
}
}
})
this.project_id = 'project_id'
this.file_id = 'file_id'
this.bucket = 'user_files'
this.key = `${this.project_id}/${this.file_id}`
this.req = {
query: {},
params: {
bucket: this.bucket,
0: this.key
},
headers: {}
}
this.res = { setHeader() {} }
return (this.fileStream = {})
})
this.FileHandler = { return describe('getFile', function() {
getFile: sinon.stub(), it('should pipe the stream', function(done) {
deleteFile: sinon.stub(), this.FileHandler.getFile.callsArgWith(3, null, this.fileStream)
insertFile: sinon.stub(), this.fileStream.pipe = res => {
getDirectorySize: sinon.stub() res.should.equal(this.res)
}; return done()
this.LocalFileWriter = {}; }
this.controller = SandboxedModule.require(modulePath, { requires: { return this.controller.getFile(this.req, this.res)
"./LocalFileWriter":this.LocalFileWriter, })
"./FileHandler": this.FileHandler,
"./PersistorManager":this.PersistorManager,
"settings-sharelatex": this.settings,
"metrics-sharelatex": {
inc() {}
},
"logger-sharelatex": {
log() {},
err() {}
}
}
}
);
this.project_id = "project_id";
this.file_id = "file_id";
this.bucket = "user_files";
this.key = `${this.project_id}/${this.file_id}`;
this.req = {
query:{},
params: {
bucket: this.bucket,
0: this.key
},
headers: {}
};
this.res =
{setHeader() {}};
return this.fileStream = {};});
return describe("getFile", function() { return it('should send a 500 if there is a problem', function(done) {
this.FileHandler.getFile.callsArgWith(3, 'error')
it("should pipe the stream", function(done){ this.res.send = code => {
this.FileHandler.getFile.callsArgWith(3, null, this.fileStream); code.should.equal(500)
this.fileStream.pipe = res=> { return done()
res.should.equal(this.res); }
return done(); return this.controller.getFile(this.req, this.res)
}; })
return this.controller.getFile(this.req, this.res); })
}); })
return it("should send a 500 if there is a problem", function(done){
this.FileHandler.getFile.callsArgWith(3, "error");
this.res.send = code=> {
code.should.equal(500);
return done();
};
return this.controller.getFile(this.req, this.res);
});
});
});

View file

@ -11,369 +11,492 @@
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const { should } = chai
const chai = require('chai'); const { expect } = chai
const { const modulePath = '../../../app/js/FSPersistorManager.js'
should const SandboxedModule = require('sandboxed-module')
} = chai; const fs = require('fs')
const { const response = require('response')
expect
} = chai;
const modulePath = "../../../app/js/FSPersistorManager.js";
const SandboxedModule = require('sandboxed-module');
const fs = require("fs");
const response = require("response");
describe("FSPersistorManagerTests", function() {
describe('FSPersistorManagerTests', function() {
beforeEach(function() { beforeEach(function() {
this.Fs = { this.Fs = {
rename:sinon.stub(), rename: sinon.stub(),
createReadStream:sinon.stub(), createReadStream: sinon.stub(),
createWriteStream:sinon.stub(), createWriteStream: sinon.stub(),
unlink:sinon.stub(), unlink: sinon.stub(),
rmdir:sinon.stub(), rmdir: sinon.stub(),
exists:sinon.stub(), exists: sinon.stub(),
readdir:sinon.stub(), readdir: sinon.stub(),
open:sinon.stub(), open: sinon.stub(),
openSync:sinon.stub(), openSync: sinon.stub(),
fstatSync:sinon.stub(), fstatSync: sinon.stub(),
closeSync:sinon.stub(), closeSync: sinon.stub(),
stat:sinon.stub() stat: sinon.stub()
}; }
this.Rimraf = sinon.stub(); this.Rimraf = sinon.stub()
this.LocalFileWriter = { this.LocalFileWriter = {
writeStream: sinon.stub(), writeStream: sinon.stub(),
deleteFile: sinon.stub() deleteFile: sinon.stub()
}; }
this.requires = { this.requires = {
"./LocalFileWriter":this.LocalFileWriter, './LocalFileWriter': this.LocalFileWriter,
"fs":this.Fs, fs: this.Fs,
"logger-sharelatex": { 'logger-sharelatex': {
log() {}, log() {},
err() {} err() {}
}, },
"response":response, response: response,
"rimraf":this.Rimraf, rimraf: this.Rimraf,
"./Errors": (this.Errors = './Errors': (this.Errors = { NotFoundError: sinon.stub() })
{NotFoundError: sinon.stub()}) }
}; this.location = '/tmp'
this.location = "/tmp"; this.name1 = '530f2407e7ef165704000007/530f838b46d9a9e859000008'
this.name1 = "530f2407e7ef165704000007/530f838b46d9a9e859000008"; this.name1Filtered = '530f2407e7ef165704000007_530f838b46d9a9e859000008'
this.name1Filtered ="530f2407e7ef165704000007_530f838b46d9a9e859000008"; this.name2 = 'second_file'
this.name2 = "second_file"; this.error = 'error_message'
this.error = "error_message"; return (this.FSPersistorManager = SandboxedModule.require(modulePath, {
return this.FSPersistorManager = SandboxedModule.require(modulePath, {requires: this.requires}); requires: this.requires
}); }))
})
describe("sendFile", function() { describe('sendFile', function() {
beforeEach(function() { beforeEach(function() {
return this.Fs.createReadStream = sinon.stub().returns({ return (this.Fs.createReadStream = sinon.stub().returns({
on() {}, on() {},
pipe() {} pipe() {}
}); }))
}); })
it("should copy the file", function(done) { it('should copy the file', function(done) {
this.Fs.createWriteStream =sinon.stub().returns({ this.Fs.createWriteStream = sinon.stub().returns({
on(event, handler) { on(event, handler) {
if (event === 'finish') { return process.nextTick(handler); } if (event === 'finish') {
return process.nextTick(handler)
}
} }
}); })
return this.FSPersistorManager.sendFile(this.location, this.name1, this.name2, err=> { return this.FSPersistorManager.sendFile(
this.Fs.createReadStream.calledWith(this.name2).should.equal(true); this.location,
this.Fs.createWriteStream.calledWith(`${this.location}/${this.name1Filtered}` ).should.equal(true); this.name1,
return done(); this.name2,
}); err => {
}); this.Fs.createReadStream.calledWith(this.name2).should.equal(true)
this.Fs.createWriteStream
.calledWith(`${this.location}/${this.name1Filtered}`)
.should.equal(true)
return done()
}
)
})
return it("should return an error if the file cannot be stored", function(done) { return it('should return an error if the file cannot be stored', function(done) {
this.Fs.createWriteStream =sinon.stub().returns({ this.Fs.createWriteStream = sinon.stub().returns({
on: (event, handler) => { on: (event, handler) => {
if (event === 'error') { if (event === 'error') {
return process.nextTick(() => { return process.nextTick(() => {
return handler(this.error); return handler(this.error)
}); })
}
} }
} })
}); return this.FSPersistorManager.sendFile(
return this.FSPersistorManager.sendFile(this.location, this.name1, this.name2, err=> { this.location,
this.Fs.createReadStream.calledWith(this.name2).should.equal(true); this.name1,
this.Fs.createWriteStream.calledWith(`${this.location}/${this.name1Filtered}` ).should.equal(true); this.name2,
err.should.equal(this.error); err => {
return done(); this.Fs.createReadStream.calledWith(this.name2).should.equal(true)
}); this.Fs.createWriteStream
}); .calledWith(`${this.location}/${this.name1Filtered}`)
}); .should.equal(true)
err.should.equal(this.error)
return done()
}
)
})
})
describe("sendStream", function() { describe('sendStream', function() {
beforeEach(function() { beforeEach(function() {
this.FSPersistorManager.sendFile = sinon.stub().callsArgWith(3); this.FSPersistorManager.sendFile = sinon.stub().callsArgWith(3)
this.LocalFileWriter.writeStream.callsArgWith(2, null, this.name1); this.LocalFileWriter.writeStream.callsArgWith(2, null, this.name1)
this.LocalFileWriter.deleteFile.callsArg(1); this.LocalFileWriter.deleteFile.callsArg(1)
return this.SourceStream = return (this.SourceStream = { on() {} })
{on() {}}; })
});
it("should sent stream to LocalFileWriter", function(done){ it('should sent stream to LocalFileWriter', function(done) {
return this.FSPersistorManager.sendStream(this.location, this.name1, this.SourceStream, () => { return this.FSPersistorManager.sendStream(
this.LocalFileWriter.writeStream.calledWith(this.SourceStream).should.equal(true); this.location,
return done(); this.name1,
}); this.SourceStream,
}); () => {
this.LocalFileWriter.writeStream
.calledWith(this.SourceStream)
.should.equal(true)
return done()
}
)
})
it("should return the error from LocalFileWriter", function(done){ it('should return the error from LocalFileWriter', function(done) {
this.LocalFileWriter.writeStream.callsArgWith(2, this.error); this.LocalFileWriter.writeStream.callsArgWith(2, this.error)
return this.FSPersistorManager.sendStream(this.location, this.name1, this.SourceStream, err=> { return this.FSPersistorManager.sendStream(
err.should.equal(this.error); this.location,
return done(); this.name1,
}); this.SourceStream,
}); err => {
err.should.equal(this.error)
return done()
}
)
})
return it("should send the file to the filestore", function(done){ return it('should send the file to the filestore', function(done) {
this.LocalFileWriter.writeStream.callsArgWith(2); this.LocalFileWriter.writeStream.callsArgWith(2)
return this.FSPersistorManager.sendStream(this.location, this.name1, this.SourceStream, err=> { return this.FSPersistorManager.sendStream(
this.FSPersistorManager.sendFile.called.should.equal(true); this.location,
return done(); this.name1,
}); this.SourceStream,
}); err => {
}); this.FSPersistorManager.sendFile.called.should.equal(true)
return done()
}
)
})
})
describe("getFileStream", function() { describe('getFileStream', function() {
beforeEach(function() { beforeEach(function() {
return this.opts = {};}); return (this.opts = {})
})
it("should use correct file location", function(done) { it('should use correct file location', function(done) {
this.FSPersistorManager.getFileStream(this.location, this.name1, this.opts, (err,res) => {}); this.FSPersistorManager.getFileStream(
this.Fs.open.calledWith(`${this.location}/${this.name1Filtered}`).should.equal(true); this.location,
return done(); this.name1,
}); this.opts,
(err, res) => {}
describe("with start and end options", function() { )
this.Fs.open
.calledWith(`${this.location}/${this.name1Filtered}`)
.should.equal(true)
return done()
})
describe('with start and end options', function() {
beforeEach(function() { beforeEach(function() {
this.fd = 2019; this.fd = 2019
this.opts_in = {start: 0, end: 8}; this.opts_in = { start: 0, end: 8 }
this.opts = {start: 0, end: 8, fd: this.fd}; this.opts = { start: 0, end: 8, fd: this.fd }
return this.Fs.open.callsArgWith(2, null, this.fd); return this.Fs.open.callsArgWith(2, null, this.fd)
}); })
return it('should pass the options to createReadStream', function(done) { return it('should pass the options to createReadStream', function(done) {
this.FSPersistorManager.getFileStream(this.location, this.name1, this.opts_in, (err,res)=> {}); this.FSPersistorManager.getFileStream(
this.Fs.createReadStream.calledWith(null, this.opts).should.equal(true); this.location,
return done(); this.name1,
}); this.opts_in,
}); (err, res) => {}
)
return describe("error conditions", function() { this.Fs.createReadStream.calledWith(null, this.opts).should.equal(true)
return done()
describe("when the file does not exist", function() { })
})
return describe('error conditions', function() {
describe('when the file does not exist', function() {
beforeEach(function() { beforeEach(function() {
this.fakeCode = 'ENOENT'; this.fakeCode = 'ENOENT'
const err = new Error(); const err = new Error()
err.code = this.fakeCode; err.code = this.fakeCode
return this.Fs.open.callsArgWith(2, err, null); return this.Fs.open.callsArgWith(2, err, null)
}); })
return it("should give a NotFoundError", function(done) { return it('should give a NotFoundError', function(done) {
return this.FSPersistorManager.getFileStream(this.location, this.name1, this.opts, (err,res)=> { return this.FSPersistorManager.getFileStream(
expect(res).to.equal(null); this.location,
expect(err).to.not.equal(null); this.name1,
expect(err instanceof this.Errors.NotFoundError).to.equal(true); this.opts,
return done(); (err, res) => {
}); expect(res).to.equal(null)
}); expect(err).to.not.equal(null)
}); expect(err instanceof this.Errors.NotFoundError).to.equal(true)
return done()
return describe("when some other error happens", function() { }
)
})
})
return describe('when some other error happens', function() {
beforeEach(function() { beforeEach(function() {
this.fakeCode = 'SOMETHINGHORRIBLE'; this.fakeCode = 'SOMETHINGHORRIBLE'
const err = new Error(); const err = new Error()
err.code = this.fakeCode; err.code = this.fakeCode
return this.Fs.open.callsArgWith(2, err, null); return this.Fs.open.callsArgWith(2, err, null)
}); })
return it("should give an Error", function(done) { return it('should give an Error', function(done) {
return this.FSPersistorManager.getFileStream(this.location, this.name1, this.opts, (err,res)=> { return this.FSPersistorManager.getFileStream(
expect(res).to.equal(null); this.location,
expect(err).to.not.equal(null); this.name1,
expect(err instanceof Error).to.equal(true); this.opts,
return done(); (err, res) => {
}); expect(res).to.equal(null)
}); expect(err).to.not.equal(null)
}); expect(err instanceof Error).to.equal(true)
}); return done()
}); }
)
})
})
})
})
describe("getFileSize", function() { describe('getFileSize', function() {
it("should return the file size", function(done) { it('should return the file size', function(done) {
const expectedFileSize = 75382; const expectedFileSize = 75382
this.Fs.stat.yields(new Error("fs.stat got unexpected arguments")); this.Fs.stat.yields(new Error('fs.stat got unexpected arguments'))
this.Fs.stat.withArgs(`${this.location}/${this.name1Filtered}`) this.Fs.stat
.yields(null, { size: expectedFileSize }); .withArgs(`${this.location}/${this.name1Filtered}`)
.yields(null, { size: expectedFileSize })
return this.FSPersistorManager.getFileSize(this.location, this.name1, (err, fileSize) => { return this.FSPersistorManager.getFileSize(
if (err != null) { this.location,
return done(err); this.name1,
(err, fileSize) => {
if (err != null) {
return done(err)
}
expect(fileSize).to.equal(expectedFileSize)
return done()
} }
expect(fileSize).to.equal(expectedFileSize); )
return done(); })
});
});
it("should throw a NotFoundError if the file does not exist", function(done) { it('should throw a NotFoundError if the file does not exist', function(done) {
const error = new Error(); const error = new Error()
error.code = "ENOENT"; error.code = 'ENOENT'
this.Fs.stat.yields(error); this.Fs.stat.yields(error)
return this.FSPersistorManager.getFileSize(this.location, this.name1, (err, fileSize) => { return this.FSPersistorManager.getFileSize(
expect(err).to.be.instanceof(this.Errors.NotFoundError); this.location,
return done(); this.name1,
}); (err, fileSize) => {
}); expect(err).to.be.instanceof(this.Errors.NotFoundError)
return done()
}
)
})
return it("should rethrow any other error", function(done) { return it('should rethrow any other error', function(done) {
const error = new Error(); const error = new Error()
this.Fs.stat.yields(error); this.Fs.stat.yields(error)
return this.FSPersistorManager.getFileSize(this.location, this.name1, (err, fileSize) => { return this.FSPersistorManager.getFileSize(
expect(err).to.equal(error); this.location,
return done(); this.name1,
}); (err, fileSize) => {
}); expect(err).to.equal(error)
}); return done()
}
)
})
})
describe("copyFile", function() { describe('copyFile', function() {
beforeEach(function() { beforeEach(function() {
this.ReadStream= { this.ReadStream = {
on() {}, on() {},
pipe:sinon.stub() pipe: sinon.stub()
}; }
this.WriteStream= this.WriteStream = { on() {} }
{on() {}}; this.Fs.createReadStream.returns(this.ReadStream)
this.Fs.createReadStream.returns(this.ReadStream); return this.Fs.createWriteStream.returns(this.WriteStream)
return this.Fs.createWriteStream.returns(this.WriteStream); })
});
it("Should open the source for reading", function(done) { it('Should open the source for reading', function(done) {
this.FSPersistorManager.copyFile(this.location, this.name1, this.name2, function() {}); this.FSPersistorManager.copyFile(
this.Fs.createReadStream.calledWith(`${this.location}/${this.name1Filtered}`).should.equal(true); this.location,
return done(); this.name1,
}); this.name2,
function() {}
)
this.Fs.createReadStream
.calledWith(`${this.location}/${this.name1Filtered}`)
.should.equal(true)
return done()
})
it("Should open the target for writing", function(done) { it('Should open the target for writing', function(done) {
this.FSPersistorManager.copyFile(this.location, this.name1, this.name2, function() {}); this.FSPersistorManager.copyFile(
this.Fs.createWriteStream.calledWith(`${this.location}/${this.name2}`).should.equal(true); this.location,
return done(); this.name1,
}); this.name2,
function() {}
)
this.Fs.createWriteStream
.calledWith(`${this.location}/${this.name2}`)
.should.equal(true)
return done()
})
return it("Should pipe the source to the target", function(done) { return it('Should pipe the source to the target', function(done) {
this.FSPersistorManager.copyFile(this.location, this.name1, this.name2, function() {}); this.FSPersistorManager.copyFile(
this.ReadStream.pipe.calledWith(this.WriteStream).should.equal(true); this.location,
return done(); this.name1,
}); this.name2,
}); function() {}
)
this.ReadStream.pipe.calledWith(this.WriteStream).should.equal(true)
return done()
})
})
describe("deleteFile", function() { describe('deleteFile', function() {
beforeEach(function() { beforeEach(function() {
return this.Fs.unlink.callsArgWith(1,this.error); return this.Fs.unlink.callsArgWith(1, this.error)
}); })
it("Should call unlink with correct options", function(done) { it('Should call unlink with correct options', function(done) {
return this.FSPersistorManager.deleteFile(this.location, this.name1, err => { return this.FSPersistorManager.deleteFile(
this.Fs.unlink.calledWith(`${this.location}/${this.name1Filtered}`).should.equal(true); this.location,
return done(); this.name1,
}); err => {
}); this.Fs.unlink
.calledWith(`${this.location}/${this.name1Filtered}`)
.should.equal(true)
return done()
}
)
})
return it("Should propogate the error", function(done) { return it('Should propogate the error', function(done) {
return this.FSPersistorManager.deleteFile(this.location, this.name1, err => { return this.FSPersistorManager.deleteFile(
err.should.equal(this.error); this.location,
return done(); this.name1,
}); err => {
}); err.should.equal(this.error)
}); return done()
}
)
})
})
describe('deleteDirectory', function() {
describe("deleteDirectory", function() {
beforeEach(function() { beforeEach(function() {
return this.Rimraf.callsArgWith(1,this.error); return this.Rimraf.callsArgWith(1, this.error)
}); })
it("Should call rmdir(rimraf) with correct options", function(done) { it('Should call rmdir(rimraf) with correct options', function(done) {
return this.FSPersistorManager.deleteDirectory(this.location, this.name1, err => { return this.FSPersistorManager.deleteDirectory(
this.Rimraf.calledWith(`${this.location}/${this.name1Filtered}`).should.equal(true); this.location,
return done(); this.name1,
}); err => {
}); this.Rimraf.calledWith(
`${this.location}/${this.name1Filtered}`
).should.equal(true)
return done()
}
)
})
return it("Should propogate the error", function(done) { return it('Should propogate the error', function(done) {
return this.FSPersistorManager.deleteDirectory(this.location, this.name1, err => { return this.FSPersistorManager.deleteDirectory(
err.should.equal(this.error); this.location,
return done(); this.name1,
}); err => {
}); err.should.equal(this.error)
}); return done()
}
)
})
})
describe("checkIfFileExists", function() { describe('checkIfFileExists', function() {
beforeEach(function() { beforeEach(function() {
return this.Fs.exists.callsArgWith(1,true); return this.Fs.exists.callsArgWith(1, true)
}); })
it("Should call exists with correct options", function(done) { it('Should call exists with correct options', function(done) {
return this.FSPersistorManager.checkIfFileExists(this.location, this.name1, exists => { return this.FSPersistorManager.checkIfFileExists(
this.Fs.exists.calledWith(`${this.location}/${this.name1Filtered}`).should.equal(true); this.location,
return done(); this.name1,
}); exists => {
}); this.Fs.exists
.calledWith(`${this.location}/${this.name1Filtered}`)
.should.equal(true)
return done()
}
)
})
// fs.exists simply returns false on any error, so... // fs.exists simply returns false on any error, so...
it("should not return an error", function(done) { it('should not return an error', function(done) {
return this.FSPersistorManager.checkIfFileExists(this.location, this.name1, (err,exists) => { return this.FSPersistorManager.checkIfFileExists(
expect(err).to.be.null; this.location,
return done(); this.name1,
}); (err, exists) => {
}); expect(err).to.be.null
return done()
}
)
})
it("Should return true for existing files", function(done) { it('Should return true for existing files', function(done) {
this.Fs.exists.callsArgWith(1,true); this.Fs.exists.callsArgWith(1, true)
return this.FSPersistorManager.checkIfFileExists(this.location, this.name1, (err,exists) => { return this.FSPersistorManager.checkIfFileExists(
exists.should.be.true; this.location,
return done(); this.name1,
}); (err, exists) => {
}); exists.should.be.true
return done()
}
)
})
return it("Should return false for non-existing files", function(done) { return it('Should return false for non-existing files', function(done) {
this.Fs.exists.callsArgWith(1,false); this.Fs.exists.callsArgWith(1, false)
return this.FSPersistorManager.checkIfFileExists(this.location, this.name1, (err,exists) => { return this.FSPersistorManager.checkIfFileExists(
exists.should.be.false; this.location,
return done(); this.name1,
}); (err, exists) => {
}); exists.should.be.false
}); return done()
}
)
})
})
return describe("directorySize", function() { return describe('directorySize', function() {
it('should propogate the error', function(done) {
this.Fs.readdir.callsArgWith(1, this.error)
return this.FSPersistorManager.directorySize(
this.location,
this.name1,
(err, totalsize) => {
err.should.equal(this.error)
return done()
}
)
})
it("should propogate the error", function(done) { return it('should sum directory files size', function(done) {
this.Fs.readdir.callsArgWith(1, this.error); this.Fs.readdir.callsArgWith(1, null, [
return this.FSPersistorManager.directorySize(this.location, this.name1, (err, totalsize) => { { file1: 'file1' },
err.should.equal(this.error); { file2: 'file2' }
return done(); ])
}); this.Fs.fstatSync.returns({ size: 1024 })
}); return this.FSPersistorManager.directorySize(
this.location,
return it("should sum directory files size", function(done) { this.name1,
this.Fs.readdir.callsArgWith(1, null, [ {'file1': 'file1'}, {'file2': 'file2'} ]); (err, totalsize) => {
this.Fs.fstatSync.returns({size : 1024}); expect(totalsize).to.equal(2048)
return this.FSPersistorManager.directorySize(this.location, this.name1, (err, totalsize) => { return done()
expect(totalsize).to.equal(2048); }
return done(); )
}); })
}); })
}); })
});

View file

@ -9,281 +9,289 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/FileController.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/FileController.js";
const SandboxedModule = require('sandboxed-module');
describe("FileController", function() { describe('FileController', function() {
beforeEach(function() {
this.PersistorManager = {
sendStream: sinon.stub(),
copyFile: sinon.stub(),
deleteFile: sinon.stub()
}
beforeEach(function() { this.settings = {
this.PersistorManager = { s3: {
sendStream: sinon.stub(), buckets: {
copyFile: sinon.stub(), user_files: 'user_files'
deleteFile:sinon.stub() }
}; }
}
this.FileHandler = {
getFile: sinon.stub(),
getFileSize: sinon.stub(),
deleteFile: sinon.stub(),
insertFile: sinon.stub(),
getDirectorySize: sinon.stub()
}
this.LocalFileWriter = {}
this.controller = SandboxedModule.require(modulePath, {
requires: {
'./LocalFileWriter': this.LocalFileWriter,
'./FileHandler': this.FileHandler,
'./PersistorManager': this.PersistorManager,
'./Errors': (this.Errors = { NotFoundError: sinon.stub() }),
'settings-sharelatex': this.settings,
'metrics-sharelatex': {
inc() {}
},
'logger-sharelatex': {
log() {},
err() {}
}
}
})
this.project_id = 'project_id'
this.file_id = 'file_id'
this.bucket = 'user_files'
this.key = `${this.project_id}/${this.file_id}`
this.req = {
key: this.key,
bucket: this.bucket,
query: {},
params: {
project_id: this.project_id,
file_id: this.file_id
},
headers: {}
}
this.res = {
set: sinon.stub().returnsThis(),
status: sinon.stub().returnsThis()
}
return (this.fileStream = {})
})
this.settings = { describe('getFile', function() {
s3: { it('should pipe the stream', function(done) {
buckets: { this.FileHandler.getFile.callsArgWith(3, null, this.fileStream)
user_files:"user_files" this.fileStream.pipe = res => {
} res.should.equal(this.res)
} return done()
}; }
this.FileHandler = { return this.controller.getFile(this.req, this.res)
getFile: sinon.stub(), })
getFileSize: sinon.stub(),
deleteFile: sinon.stub(),
insertFile: sinon.stub(),
getDirectorySize: sinon.stub()
};
this.LocalFileWriter = {};
this.controller = SandboxedModule.require(modulePath, { requires: {
"./LocalFileWriter":this.LocalFileWriter,
"./FileHandler": this.FileHandler,
"./PersistorManager":this.PersistorManager,
"./Errors": (this.Errors =
{NotFoundError: sinon.stub()}),
"settings-sharelatex": this.settings,
"metrics-sharelatex": {
inc() {}
},
"logger-sharelatex": {
log() {},
err() {}
}
}
}
);
this.project_id = "project_id";
this.file_id = "file_id";
this.bucket = "user_files";
this.key = `${this.project_id}/${this.file_id}`;
this.req = {
key:this.key,
bucket:this.bucket,
query:{},
params: {
project_id:this.project_id,
file_id:this.file_id
},
headers: {}
};
this.res = {
set: sinon.stub().returnsThis(),
status: sinon.stub().returnsThis()
};
return this.fileStream = {};});
describe("getFile", function() { it('should send a 200 if the cacheWarm param is true', function(done) {
this.req.query.cacheWarm = true
this.FileHandler.getFile.callsArgWith(3, null, this.fileStream)
this.res.send = statusCode => {
statusCode.should.equal(200)
return done()
}
return this.controller.getFile(this.req, this.res)
})
it("should pipe the stream", function(done){ it('should send a 500 if there is a problem', function(done) {
this.FileHandler.getFile.callsArgWith(3, null, this.fileStream); this.FileHandler.getFile.callsArgWith(3, 'error')
this.fileStream.pipe = res=> { this.res.send = code => {
res.should.equal(this.res); code.should.equal(500)
return done(); return done()
}; }
return this.controller.getFile(this.req, this.res); return this.controller.getFile(this.req, this.res)
}); })
it("should send a 200 if the cacheWarm param is true", function(done){ return describe("with a 'Range' header set", function() {
this.req.query.cacheWarm = true; beforeEach(function() {
this.FileHandler.getFile.callsArgWith(3, null, this.fileStream); return (this.req.headers.range = 'bytes=0-8')
this.res.send = statusCode=> { })
statusCode.should.equal(200);
return done();
};
return this.controller.getFile(this.req, this.res);
});
it("should send a 500 if there is a problem", function(done){ return it("should pass 'start' and 'end' options to FileHandler", function(done) {
this.FileHandler.getFile.callsArgWith(3, "error"); this.FileHandler.getFile.callsArgWith(3, null, this.fileStream)
this.res.send = code=> { this.fileStream.pipe = res => {
code.should.equal(500); expect(this.FileHandler.getFile.lastCall.args[2].start).to.equal(0)
return done(); expect(this.FileHandler.getFile.lastCall.args[2].end).to.equal(8)
}; return done()
return this.controller.getFile(this.req, this.res); }
}); return this.controller.getFile(this.req, this.res)
})
})
})
return describe("with a 'Range' header set", function() { describe('getFileHead', function() {
it('should return the file size in a Content-Length header', function(done) {
const expectedFileSize = 84921
this.FileHandler.getFileSize.yields(
new Error('FileHandler.getFileSize: unexpected arguments')
)
this.FileHandler.getFileSize
.withArgs(this.bucket, this.key)
.yields(null, expectedFileSize)
beforeEach(function() { this.res.end = () => {
return this.req.headers.range = 'bytes=0-8'; expect(this.res.status.lastCall.args[0]).to.equal(200)
}); expect(
this.res.set.calledWith('Content-Length', expectedFileSize)
).to.equal(true)
return done()
}
return it("should pass 'start' and 'end' options to FileHandler", function(done) { return this.controller.getFileHead(this.req, this.res)
this.FileHandler.getFile.callsArgWith(3, null, this.fileStream); })
this.fileStream.pipe = res=> {
expect(this.FileHandler.getFile.lastCall.args[2].start).to.equal(0);
expect(this.FileHandler.getFile.lastCall.args[2].end).to.equal(8);
return done();
};
return this.controller.getFile(this.req, this.res);
});
});
});
describe("getFileHead", function() { it('should return a 404 is the file is not found', function(done) {
it("should return the file size in a Content-Length header", function(done) { this.FileHandler.getFileSize.yields(new this.Errors.NotFoundError())
const expectedFileSize = 84921;
this.FileHandler.getFileSize.yields(
new Error("FileHandler.getFileSize: unexpected arguments")
);
this.FileHandler.getFileSize.withArgs(this.bucket, this.key).yields(null, expectedFileSize);
this.res.end = () => { this.res.end = () => {
expect(this.res.status.lastCall.args[0]).to.equal(200); expect(this.res.status.lastCall.args[0]).to.equal(404)
expect(this.res.set.calledWith("Content-Length", expectedFileSize)).to.equal(true); return done()
return done(); }
};
return this.controller.getFileHead(this.req, this.res); return this.controller.getFileHead(this.req, this.res)
}); })
it("should return a 404 is the file is not found", function(done) { return it('should return a 500 on internal errors', function(done) {
this.FileHandler.getFileSize.yields(new this.Errors.NotFoundError()); this.FileHandler.getFileSize.yields(new Error())
this.res.end = () => { this.res.end = () => {
expect(this.res.status.lastCall.args[0]).to.equal(404); expect(this.res.status.lastCall.args[0]).to.equal(500)
return done(); return done()
}; }
return this.controller.getFileHead(this.req, this.res); return this.controller.getFileHead(this.req, this.res)
}); })
})
return it("should return a 500 on internal errors", function(done) { describe('insertFile', () =>
this.FileHandler.getFileSize.yields(new Error()); it('should send bucket name key and res to PersistorManager', function(done) {
this.FileHandler.insertFile.callsArgWith(3)
this.res.send = () => {
this.FileHandler.insertFile
.calledWith(this.bucket, this.key, this.req)
.should.equal(true)
return done()
}
return this.controller.insertFile(this.req, this.res)
}))
this.res.end = () => { describe('copyFile', function() {
expect(this.res.status.lastCall.args[0]).to.equal(500); beforeEach(function() {
return done(); this.oldFile_id = 'old_file_id'
}; this.oldProject_id = 'old_project_id'
return (this.req.body = {
source: {
project_id: this.oldProject_id,
file_id: this.oldFile_id
}
})
})
return this.controller.getFileHead(this.req, this.res); it('should send bucket name and both keys to PersistorManager', function(done) {
}); this.PersistorManager.copyFile.callsArgWith(3)
}); this.res.send = code => {
code.should.equal(200)
this.PersistorManager.copyFile
.calledWith(
this.bucket,
`${this.oldProject_id}/${this.oldFile_id}`,
this.key
)
.should.equal(true)
return done()
}
return this.controller.copyFile(this.req, this.res)
})
describe("insertFile", () => it("should send bucket name key and res to PersistorManager", function(done){ it('should send a 404 if the original file was not found', function(done) {
this.FileHandler.insertFile.callsArgWith(3); this.PersistorManager.copyFile.callsArgWith(
this.res.send = () => { 3,
this.FileHandler.insertFile.calledWith(this.bucket, this.key, this.req).should.equal(true); new this.Errors.NotFoundError()
return done(); )
}; this.res.send = code => {
return this.controller.insertFile(this.req, this.res); code.should.equal(404)
})); return done()
}
return this.controller.copyFile(this.req, this.res)
})
describe("copyFile", function() { return it('should send a 500 if there was an error', function(done) {
beforeEach(function() { this.PersistorManager.copyFile.callsArgWith(3, 'error')
this.oldFile_id = "old_file_id"; this.res.send = code => {
this.oldProject_id = "old_project_id"; code.should.equal(500)
return this.req.body = { return done()
source: { }
project_id: this.oldProject_id, return this.controller.copyFile(this.req, this.res)
file_id: this.oldFile_id })
} })
};
});
it("should send bucket name and both keys to PersistorManager", function(done){ describe('delete file', function() {
this.PersistorManager.copyFile.callsArgWith(3); it('should tell the file handler', function(done) {
this.res.send = code=> { this.FileHandler.deleteFile.callsArgWith(2)
code.should.equal(200); this.res.send = code => {
this.PersistorManager.copyFile.calledWith(this.bucket, `${this.oldProject_id}/${this.oldFile_id}`, this.key).should.equal(true); code.should.equal(204)
return done(); this.FileHandler.deleteFile
}; .calledWith(this.bucket, this.key)
return this.controller.copyFile(this.req, this.res); .should.equal(true)
}); return done()
}
return this.controller.deleteFile(this.req, this.res)
})
it("should send a 404 if the original file was not found", function(done) { return it('should send a 500 if there was an error', function(done) {
this.PersistorManager.copyFile.callsArgWith(3, new this.Errors.NotFoundError()); this.FileHandler.deleteFile.callsArgWith(2, 'error')
this.res.send = code=> { this.res.send = function(code) {
code.should.equal(404); code.should.equal(500)
return done(); return done()
}; }
return this.controller.copyFile(this.req, this.res); return this.controller.deleteFile(this.req, this.res)
}); })
})
return it("should send a 500 if there was an error", function(done){ describe('_get_range', function() {
this.PersistorManager.copyFile.callsArgWith(3, "error"); it('should parse a valid Range header', function(done) {
this.res.send = code=> { const result = this.controller._get_range('bytes=0-200')
code.should.equal(500); expect(result).to.not.equal(null)
return done(); expect(result.start).to.equal(0)
}; expect(result.end).to.equal(200)
return this.controller.copyFile(this.req, this.res); return done()
}); })
});
describe("delete file", function() { it('should return null for an invalid Range header', function(done) {
const result = this.controller._get_range('wat')
expect(result).to.equal(null)
return done()
})
it("should tell the file handler", function(done){ return it("should return null for any type other than 'bytes'", function(done) {
this.FileHandler.deleteFile.callsArgWith(2); const result = this.controller._get_range('carrots=0-200')
this.res.send = code=> { expect(result).to.equal(null)
code.should.equal(204); return done()
this.FileHandler.deleteFile.calledWith(this.bucket, this.key).should.equal(true); })
return done(); })
};
return this.controller.deleteFile(this.req, this.res);
});
return it("should send a 500 if there was an error", function(done){ return describe('directorySize', function() {
this.FileHandler.deleteFile.callsArgWith(2, "error"); it('should return total directory size bytes', function(done) {
this.res.send = function(code){ this.FileHandler.getDirectorySize.callsArgWith(2, null, 1024)
code.should.equal(500); return this.controller.directorySize(this.req, {
return done(); json: result => {
}; expect(result['total bytes']).to.equal(1024)
return this.controller.deleteFile(this.req, this.res); return done()
}); }
}); })
})
describe("_get_range", function() { return it('should send a 500 if there was an error', function(done) {
this.FileHandler.getDirectorySize.callsArgWith(2, 'error')
it("should parse a valid Range header", function(done) { this.res.send = function(code) {
const result = this.controller._get_range('bytes=0-200'); code.should.equal(500)
expect(result).to.not.equal(null); return done()
expect(result.start).to.equal(0); }
expect(result.end).to.equal(200); return this.controller.directorySize(this.req, this.res)
return done(); })
}); })
})
it("should return null for an invalid Range header", function(done) {
const result = this.controller._get_range('wat');
expect(result).to.equal(null);
return done();
});
return it("should return null for any type other than 'bytes'", function(done) {
const result = this.controller._get_range('carrots=0-200');
expect(result).to.equal(null);
return done();
});
});
return describe("directorySize", function() {
it("should return total directory size bytes", function(done) {
this.FileHandler.getDirectorySize.callsArgWith(2, null, 1024);
return this.controller.directorySize(this.req, { json:result=> {
expect(result['total bytes']).to.equal(1024);
return done();
}
}
);
});
return it("should send a 500 if there was an error", function(done){
this.FileHandler.getDirectorySize.callsArgWith(2, "error");
this.res.send = function(code){
code.should.equal(500);
return done();
};
return this.controller.directorySize(this.req, this.res);
});
});
});

View file

@ -10,110 +10,110 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/FileConverter.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/FileConverter.js";
const SandboxedModule = require('sandboxed-module');
describe("FileConverter", function() { describe('FileConverter', function() {
beforeEach(function() {
this.safe_exec = sinon.stub()
this.converter = SandboxedModule.require(modulePath, {
requires: {
'./SafeExec': this.safe_exec,
'logger-sharelatex': {
log() {},
err() {}
},
'metrics-sharelatex': {
inc() {},
Timer() {
return { done() {} }
}
},
'settings-sharelatex': (this.Settings = {
commands: {
convertCommandPrefix: []
}
})
}
})
beforeEach(function() { this.sourcePath = '/this/path/here.eps'
this.format = 'png'
return (this.error = 'Error')
})
this.safe_exec = sinon.stub(); describe('convert', function() {
this.converter = SandboxedModule.require(modulePath, { requires: { it('should convert the source to the requested format', function(done) {
"./SafeExec": this.safe_exec, this.safe_exec.callsArgWith(2)
"logger-sharelatex": { return this.converter.convert(this.sourcePath, this.format, err => {
log() {}, const args = this.safe_exec.args[0][0]
err() {} args.indexOf(`${this.sourcePath}[0]`).should.not.equal(-1)
}, args.indexOf(`${this.sourcePath}.${this.format}`).should.not.equal(-1)
"metrics-sharelatex": { return done()
inc() {}, })
Timer() { })
return {done() {}};
}
},
"settings-sharelatex": (this.Settings = {
commands: {
convertCommandPrefix: []
}
})
}
});
this.sourcePath = "/this/path/here.eps"; it('should return the dest path', function(done) {
this.format = "png"; this.safe_exec.callsArgWith(2)
return this.error = "Error"; return this.converter.convert(
}); this.sourcePath,
this.format,
(err, destPath) => {
destPath.should.equal(`${this.sourcePath}.${this.format}`)
return done()
}
)
})
describe("convert", function() { it('should return the error from convert', function(done) {
this.safe_exec.callsArgWith(2, this.error)
return this.converter.convert(this.sourcePath, this.format, err => {
err.should.equal(this.error)
return done()
})
})
it("should convert the source to the requested format", function(done){ it('should not accapt an non aproved format', function(done) {
this.safe_exec.callsArgWith(2); this.safe_exec.callsArgWith(2)
return this.converter.convert(this.sourcePath, this.format, err=> { return this.converter.convert(this.sourcePath, 'ahhhhh', err => {
const args = this.safe_exec.args[0][0]; expect(err).to.exist
args.indexOf(`${this.sourcePath}[0]`).should.not.equal(-1); return done()
args.indexOf(`${this.sourcePath}.${this.format}`).should.not.equal(-1); })
return done(); })
});
});
it("should return the dest path", function(done){ return it('should prefix the command with Settings.commands.convertCommandPrefix', function(done) {
this.safe_exec.callsArgWith(2); this.safe_exec.callsArgWith(2)
return this.converter.convert(this.sourcePath, this.format, (err, destPath)=> { this.Settings.commands.convertCommandPrefix = ['nice']
destPath.should.equal(`${this.sourcePath}.${this.format}`); return this.converter.convert(this.sourcePath, this.format, err => {
return done(); const command = this.safe_exec.args[0][0]
}); command[0].should.equal('nice')
}); return done()
})
})
})
it("should return the error from convert", function(done){ describe('thumbnail', () =>
this.safe_exec.callsArgWith(2, this.error); it('should call converter resize with args', function(done) {
return this.converter.convert(this.sourcePath, this.format, err=> { this.safe_exec.callsArgWith(2)
err.should.equal(this.error); return this.converter.thumbnail(this.sourcePath, err => {
return done(); const args = this.safe_exec.args[0][0]
}); args.indexOf(`${this.sourcePath}[0]`).should.not.equal(-1)
}); return done()
})
}))
it("should not accapt an non aproved format", function(done){ return describe('preview', () =>
this.safe_exec.callsArgWith(2); it('should call converter resize with args', function(done) {
return this.converter.convert(this.sourcePath, "ahhhhh", err=> { this.safe_exec.callsArgWith(2)
expect(err).to.exist; return this.converter.preview(this.sourcePath, err => {
return done(); const args = this.safe_exec.args[0][0]
}); args.indexOf(`${this.sourcePath}[0]`).should.not.equal(-1)
}); return done()
})
return it("should prefix the command with Settings.commands.convertCommandPrefix", function(done) { }))
this.safe_exec.callsArgWith(2); })
this.Settings.commands.convertCommandPrefix = ["nice"];
return this.converter.convert(this.sourcePath, this.format, err=> {
const command = this.safe_exec.args[0][0];
command[0].should.equal("nice");
return done();
});
});
});
describe("thumbnail", () => it("should call converter resize with args", function(done){
this.safe_exec.callsArgWith(2);
return this.converter.thumbnail(this.sourcePath, err=> {
const args = this.safe_exec.args[0][0];
args.indexOf(`${this.sourcePath}[0]`).should.not.equal(-1);
return done();
});
}));
return describe("preview", () => it("should call converter resize with args", function(done){
this.safe_exec.callsArgWith(2);
return this.converter.preview(this.sourcePath, err=> {
const args = this.safe_exec.args[0][0];
args.indexOf(`${this.sourcePath}[0]`).should.not.equal(-1);
return done();
});
}));
});

View file

@ -10,273 +10,359 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/FileHandler.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/FileHandler.js";
const SandboxedModule = require('sandboxed-module');
describe("FileHandler", function() { describe('FileHandler', function() {
beforeEach(function() {
this.settings = {
s3: {
buckets: {
user_files: 'user_files'
}
}
}
this.PersistorManager = {
getFileStream: sinon.stub(),
checkIfFileExists: sinon.stub(),
deleteFile: sinon.stub(),
deleteDirectory: sinon.stub(),
sendStream: sinon.stub(),
insertFile: sinon.stub(),
directorySize: sinon.stub()
}
this.LocalFileWriter = {
writeStream: sinon.stub(),
getStream: sinon.stub(),
deleteFile: sinon.stub()
}
this.FileConverter = {
convert: sinon.stub(),
thumbnail: sinon.stub(),
preview: sinon.stub()
}
this.keyBuilder = {
addCachingToKey: sinon.stub(),
getConvertedFolderKey: sinon.stub()
}
this.ImageOptimiser = { compressPng: sinon.stub() }
this.handler = SandboxedModule.require(modulePath, {
requires: {
'settings-sharelatex': this.settings,
'./PersistorManager': this.PersistorManager,
'./LocalFileWriter': this.LocalFileWriter,
'./FileConverter': this.FileConverter,
'./KeyBuilder': this.keyBuilder,
'./ImageOptimiser': this.ImageOptimiser,
'logger-sharelatex': {
log() {},
err() {}
}
}
})
this.bucket = 'my_bucket'
this.key = 'key/here'
this.stubbedPath = '/var/somewhere/path'
this.format = 'png'
return (this.formattedStubbedPath = `${this.stubbedPath}.${this.format}`)
})
beforeEach(function() { describe('insertFile', function() {
this.settings = { beforeEach(function() {
s3: { this.stream = {}
buckets: { this.PersistorManager.deleteDirectory.callsArgWith(2)
user_files:"user_files" return this.PersistorManager.sendStream.callsArgWith(3)
} })
}
};
this.PersistorManager = {
getFileStream: sinon.stub(),
checkIfFileExists: sinon.stub(),
deleteFile: sinon.stub(),
deleteDirectory: sinon.stub(),
sendStream: sinon.stub(),
insertFile: sinon.stub(),
directorySize: sinon.stub()
};
this.LocalFileWriter = {
writeStream: sinon.stub(),
getStream: sinon.stub(),
deleteFile: sinon.stub()
};
this.FileConverter = {
convert: sinon.stub(),
thumbnail: sinon.stub(),
preview: sinon.stub()
};
this.keyBuilder = {
addCachingToKey: sinon.stub(),
getConvertedFolderKey: sinon.stub()
};
this.ImageOptimiser =
{compressPng: sinon.stub()};
this.handler = SandboxedModule.require(modulePath, { requires: {
"settings-sharelatex": this.settings,
"./PersistorManager":this.PersistorManager,
"./LocalFileWriter":this.LocalFileWriter,
"./FileConverter":this.FileConverter,
"./KeyBuilder": this.keyBuilder,
"./ImageOptimiser":this.ImageOptimiser,
"logger-sharelatex": {
log() {},
err() {}
}
}
}
);
this.bucket = "my_bucket";
this.key = "key/here";
this.stubbedPath = "/var/somewhere/path";
this.format = "png";
return this.formattedStubbedPath = `${this.stubbedPath}.${this.format}`;
});
describe("insertFile", function() { it('should send file to the filestore', function(done) {
beforeEach(function() { return this.handler.insertFile(this.bucket, this.key, this.stream, () => {
this.stream = {}; this.PersistorManager.sendStream
this.PersistorManager.deleteDirectory.callsArgWith(2); .calledWith(this.bucket, this.key, this.stream)
return this.PersistorManager.sendStream.callsArgWith(3); .should.equal(true)
}); return done()
})
})
it("should send file to the filestore", function(done){ return it('should delete the convetedKey folder', function(done) {
return this.handler.insertFile(this.bucket, this.key, this.stream, () => { this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey)
this.PersistorManager.sendStream.calledWith(this.bucket, this.key, this.stream).should.equal(true); return this.handler.insertFile(this.bucket, this.key, this.stream, () => {
return done(); this.PersistorManager.deleteDirectory
}); .calledWith(this.bucket, this.stubbedConvetedKey)
}); .should.equal(true)
return done()
})
})
})
return it("should delete the convetedKey folder", function(done){ describe('deleteFile', function() {
this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey); beforeEach(function() {
return this.handler.insertFile(this.bucket, this.key, this.stream, () => { this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey)
this.PersistorManager.deleteDirectory.calledWith(this.bucket, this.stubbedConvetedKey).should.equal(true); this.PersistorManager.deleteFile.callsArgWith(2)
return done(); return this.PersistorManager.deleteDirectory.callsArgWith(2)
}); })
});
});
describe("deleteFile", function() { it('should tell the filestore manager to delete the file', function(done) {
beforeEach(function() { return this.handler.deleteFile(this.bucket, this.key, () => {
this.keyBuilder.getConvertedFolderKey.returns(this.stubbedConvetedKey); this.PersistorManager.deleteFile
this.PersistorManager.deleteFile.callsArgWith(2); .calledWith(this.bucket, this.key)
return this.PersistorManager.deleteDirectory.callsArgWith(2); .should.equal(true)
}); return done()
})
})
it("should tell the filestore manager to delete the file", function(done){ return it('should tell the filestore manager to delete the cached foler', function(done) {
return this.handler.deleteFile(this.bucket, this.key, () => { return this.handler.deleteFile(this.bucket, this.key, () => {
this.PersistorManager.deleteFile.calledWith(this.bucket, this.key).should.equal(true); this.PersistorManager.deleteDirectory
return done(); .calledWith(this.bucket, this.stubbedConvetedKey)
}); .should.equal(true)
}); return done()
})
})
})
return it("should tell the filestore manager to delete the cached foler", function(done){ describe('getFile', function() {
return this.handler.deleteFile(this.bucket, this.key, () => { beforeEach(function() {
this.PersistorManager.deleteDirectory.calledWith(this.bucket, this.stubbedConvetedKey).should.equal(true); this.handler._getStandardFile = sinon.stub().callsArgWith(3)
return done(); return (this.handler._getConvertedFile = sinon.stub().callsArgWith(3))
}); })
});
});
describe("getFile", function() { it('should call _getStandardFile if no format or style are defined', function(done) {
beforeEach(function() { return this.handler.getFile(this.bucket, this.key, null, () => {
this.handler._getStandardFile = sinon.stub().callsArgWith(3); this.handler._getStandardFile.called.should.equal(true)
return this.handler._getConvertedFile = sinon.stub().callsArgWith(3); this.handler._getConvertedFile.called.should.equal(false)
}); return done()
})
})
it("should call _getStandardFile if no format or style are defined", function(done){ it('should pass options to _getStandardFile', function(done) {
const options = { start: 0, end: 8 }
return this.handler.getFile(this.bucket, this.key, options, () => {
expect(this.handler._getStandardFile.lastCall.args[2].start).to.equal(0)
expect(this.handler._getStandardFile.lastCall.args[2].end).to.equal(8)
return done()
})
})
return this.handler.getFile(this.bucket, this.key, null, () => { return it('should call _getConvertedFile if a format is defined', function(done) {
this.handler._getStandardFile.called.should.equal(true); return this.handler.getFile(
this.handler._getConvertedFile.called.should.equal(false); this.bucket,
return done(); this.key,
}); { format: 'png' },
}); () => {
this.handler._getStandardFile.called.should.equal(false)
this.handler._getConvertedFile.called.should.equal(true)
return done()
}
)
})
})
it("should pass options to _getStandardFile", function(done) { describe('_getStandardFile', function() {
const options = {start: 0, end: 8}; beforeEach(function() {
return this.handler.getFile(this.bucket, this.key, options, () => { this.fileStream = { on() {} }
expect(this.handler._getStandardFile.lastCall.args[2].start).to.equal(0); return this.PersistorManager.getFileStream.callsArgWith(
expect(this.handler._getStandardFile.lastCall.args[2].end).to.equal(8); 3,
return done(); 'err',
}); this.fileStream
}); )
})
return it("should call _getConvertedFile if a format is defined", function(done){ it('should get the stream', function(done) {
return this.handler.getFile(this.bucket, this.key, {format:"png"}, () => { return this.handler.getFile(this.bucket, this.key, null, () => {
this.handler._getStandardFile.called.should.equal(false); this.PersistorManager.getFileStream
this.handler._getConvertedFile.called.should.equal(true); .calledWith(this.bucket, this.key)
return done(); .should.equal(true)
}); return done()
}); })
}); })
describe("_getStandardFile", function() { it('should return the stream and error', function(done) {
return this.handler.getFile(
this.bucket,
this.key,
null,
(err, stream) => {
err.should.equal('err')
stream.should.equal(this.fileStream)
return done()
}
)
})
beforeEach(function() { return it('should pass options to PersistorManager', function(done) {
this.fileStream = {on() {}}; return this.handler.getFile(
return this.PersistorManager.getFileStream.callsArgWith(3, "err", this.fileStream); this.bucket,
}); this.key,
{ start: 0, end: 8 },
() => {
expect(
this.PersistorManager.getFileStream.lastCall.args[2].start
).to.equal(0)
expect(
this.PersistorManager.getFileStream.lastCall.args[2].end
).to.equal(8)
return done()
}
)
})
})
it("should get the stream", function(done){ describe('_getConvertedFile', function() {
return this.handler.getFile(this.bucket, this.key, null, () => { it('should getFileStream if it does exists', function(done) {
this.PersistorManager.getFileStream.calledWith(this.bucket, this.key).should.equal(true); this.PersistorManager.checkIfFileExists.callsArgWith(2, null, true)
return done(); this.PersistorManager.getFileStream.callsArgWith(3)
}); return this.handler._getConvertedFile(this.bucket, this.key, {}, () => {
}); this.PersistorManager.getFileStream
.calledWith(this.bucket)
.should.equal(true)
return done()
})
})
it("should return the stream and error", function(done){ return it('should call _getConvertedFileAndCache if it does exists', function(done) {
return this.handler.getFile(this.bucket, this.key, null, (err, stream)=> { this.PersistorManager.checkIfFileExists.callsArgWith(2, null, false)
err.should.equal("err"); this.handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4)
stream.should.equal(this.fileStream); return this.handler._getConvertedFile(this.bucket, this.key, {}, () => {
return done(); this.handler._getConvertedFileAndCache
}); .calledWith(this.bucket, this.key)
}); .should.equal(true)
return done()
})
})
})
return it("should pass options to PersistorManager", function(done) { describe('_getConvertedFileAndCache', () =>
return this.handler.getFile(this.bucket, this.key, {start: 0, end: 8}, () => { it('should _convertFile ', function(done) {
expect(this.PersistorManager.getFileStream.lastCall.args[2].start).to.equal(0); this.stubbedStream = { something: 'here' }
expect(this.PersistorManager.getFileStream.lastCall.args[2].end).to.equal(8); this.localStream = {
return done(); on() {}
}); }
}); this.PersistorManager.sendFile = sinon.stub().callsArgWith(3)
}); this.LocalFileWriter.getStream = sinon
.stub()
.callsArgWith(1, null, this.localStream)
this.convetedKey = this.key + 'converted'
this.handler._convertFile = sinon
.stub()
.callsArgWith(3, null, this.stubbedPath)
this.ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
return this.handler._getConvertedFileAndCache(
this.bucket,
this.key,
this.convetedKey,
{},
(err, fsStream) => {
this.handler._convertFile.called.should.equal(true)
this.PersistorManager.sendFile
.calledWith(this.bucket, this.convetedKey, this.stubbedPath)
.should.equal(true)
this.ImageOptimiser.compressPng
.calledWith(this.stubbedPath)
.should.equal(true)
this.LocalFileWriter.getStream
.calledWith(this.stubbedPath)
.should.equal(true)
fsStream.should.equal(this.localStream)
return done()
}
)
}))
describe('_convertFile', function() {
beforeEach(function() {
this.FileConverter.convert.callsArgWith(
2,
null,
this.formattedStubbedPath
)
this.FileConverter.thumbnail.callsArgWith(
1,
null,
this.formattedStubbedPath
)
this.FileConverter.preview.callsArgWith(
1,
null,
this.formattedStubbedPath
)
this.handler._writeS3FileToDisk = sinon
.stub()
.callsArgWith(3, null, this.stubbedPath)
return this.LocalFileWriter.deleteFile.callsArgWith(1)
})
describe("_getConvertedFile", function() { it('should call thumbnail on the writer path if style was thumbnail was specified', function(done) {
return this.handler._convertFile(
this.bucket,
this.key,
{ style: 'thumbnail' },
(err, path) => {
path.should.equal(this.formattedStubbedPath)
this.FileConverter.thumbnail
.calledWith(this.stubbedPath)
.should.equal(true)
this.LocalFileWriter.deleteFile
.calledWith(this.stubbedPath)
.should.equal(true)
return done()
}
)
})
it("should getFileStream if it does exists", function(done){ it('should call preview on the writer path if style was preview was specified', function(done) {
this.PersistorManager.checkIfFileExists.callsArgWith(2, null, true); return this.handler._convertFile(
this.PersistorManager.getFileStream.callsArgWith(3); this.bucket,
return this.handler._getConvertedFile(this.bucket, this.key, {}, () => { this.key,
this.PersistorManager.getFileStream.calledWith(this.bucket).should.equal(true); { style: 'preview' },
return done(); (err, path) => {
}); path.should.equal(this.formattedStubbedPath)
}); this.FileConverter.preview
.calledWith(this.stubbedPath)
.should.equal(true)
this.LocalFileWriter.deleteFile
.calledWith(this.stubbedPath)
.should.equal(true)
return done()
}
)
})
return it("should call _getConvertedFileAndCache if it does exists", function(done){ return it('should call convert on the writer path if a format was specified', function(done) {
this.PersistorManager.checkIfFileExists.callsArgWith(2, null, false); return this.handler._convertFile(
this.handler._getConvertedFileAndCache = sinon.stub().callsArgWith(4); this.bucket,
return this.handler._getConvertedFile(this.bucket, this.key, {}, () => { this.key,
this.handler._getConvertedFileAndCache.calledWith(this.bucket, this.key).should.equal(true); { format: this.format },
return done(); (err, path) => {
}); path.should.equal(this.formattedStubbedPath)
}); this.FileConverter.convert
}); .calledWith(this.stubbedPath, this.format)
.should.equal(true)
this.LocalFileWriter.deleteFile
.calledWith(this.stubbedPath)
.should.equal(true)
return done()
}
)
})
})
describe("_getConvertedFileAndCache", () => it("should _convertFile ", function(done){ return describe('getDirectorySize', function() {
this.stubbedStream = {"something":"here"}; beforeEach(function() {
this.localStream = { return this.PersistorManager.directorySize.callsArgWith(2)
on() {} })
};
this.PersistorManager.sendFile = sinon.stub().callsArgWith(3);
this.LocalFileWriter.getStream = sinon.stub().callsArgWith(1, null, this.localStream);
this.convetedKey = this.key+"converted";
this.handler._convertFile = sinon.stub().callsArgWith(3, null, this.stubbedPath);
this.ImageOptimiser.compressPng = sinon.stub().callsArgWith(1);
return this.handler._getConvertedFileAndCache(this.bucket, this.key, this.convetedKey, {}, (err, fsStream)=> {
this.handler._convertFile.called.should.equal(true);
this.PersistorManager.sendFile.calledWith(this.bucket, this.convetedKey, this.stubbedPath).should.equal(true);
this.ImageOptimiser.compressPng.calledWith(this.stubbedPath).should.equal(true);
this.LocalFileWriter.getStream.calledWith(this.stubbedPath).should.equal(true);
fsStream.should.equal(this.localStream);
return done();
});
}));
describe("_convertFile", function() { return it('should call the filestore manager to get directory size', function(done) {
beforeEach(function() { return this.handler.getDirectorySize(this.bucket, this.key, () => {
this.FileConverter.convert.callsArgWith(2, null, this.formattedStubbedPath); this.PersistorManager.directorySize
this.FileConverter.thumbnail.callsArgWith(1, null, this.formattedStubbedPath); .calledWith(this.bucket, this.key)
this.FileConverter.preview.callsArgWith(1, null, this.formattedStubbedPath); .should.equal(true)
this.handler._writeS3FileToDisk = sinon.stub().callsArgWith(3, null, this.stubbedPath); return done()
return this.LocalFileWriter.deleteFile.callsArgWith(1); })
}); })
})
it("should call thumbnail on the writer path if style was thumbnail was specified", function(done){ })
return this.handler._convertFile(this.bucket, this.key, {style:"thumbnail"}, (err, path)=> {
path.should.equal(this.formattedStubbedPath);
this.FileConverter.thumbnail.calledWith(this.stubbedPath).should.equal(true);
this.LocalFileWriter.deleteFile.calledWith(this.stubbedPath).should.equal(true);
return done();
});
});
it("should call preview on the writer path if style was preview was specified", function(done){
return this.handler._convertFile(this.bucket, this.key, {style:"preview"}, (err, path)=> {
path.should.equal(this.formattedStubbedPath);
this.FileConverter.preview.calledWith(this.stubbedPath).should.equal(true);
this.LocalFileWriter.deleteFile.calledWith(this.stubbedPath).should.equal(true);
return done();
});
});
return it("should call convert on the writer path if a format was specified", function(done){
return this.handler._convertFile(this.bucket, this.key, {format:this.format}, (err, path)=> {
path.should.equal(this.formattedStubbedPath);
this.FileConverter.convert.calledWith(this.stubbedPath, this.format).should.equal(true);
this.LocalFileWriter.deleteFile.calledWith(this.stubbedPath).should.equal(true);
return done();
});
});
});
return describe("getDirectorySize", function() {
beforeEach(function() {
return this.PersistorManager.directorySize.callsArgWith(2);
});
return it("should call the filestore manager to get directory size", function(done){
return this.handler.getDirectorySize(this.bucket, this.key, () => {
this.PersistorManager.directorySize.calledWith(this.bucket, this.key).should.equal(true);
return done();
});
});
});
});

View file

@ -10,82 +10,72 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/ImageOptimiser.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/ImageOptimiser.js";
const SandboxedModule = require('sandboxed-module');
describe("ImageOptimiser", function() { describe('ImageOptimiser', function() {
beforeEach(function() {
this.child_process = { exec: sinon.stub() }
this.settings = { enableConversions: true }
this.optimiser = SandboxedModule.require(modulePath, {
requires: {
child_process: this.child_process,
'logger-sharelatex': {
log() {},
err() {},
warn() {}
},
'settings-sharelatex': this.settings
}
})
beforeEach(function() { this.sourcePath = '/this/path/here.eps'
this.child_process = return (this.error = 'Error')
{exec : sinon.stub()}; })
this.settings =
{enableConversions:true};
this.optimiser = SandboxedModule.require(modulePath, { requires: {
'child_process': this.child_process,
"logger-sharelatex": {
log() {},
err() {},
warn() {}
},
"settings-sharelatex": this.settings
}
}
);
this.sourcePath = "/this/path/here.eps"; describe('compressPng', function() {
return this.error = "Error"; it('convert the file', function(done) {
}); this.child_process.exec.callsArgWith(2)
return this.optimiser.compressPng(this.sourcePath, err => {
const args = this.child_process.exec.args[0][0]
args.should.equal(`optipng ${this.sourcePath}`)
return done()
})
})
describe("compressPng", function() { return it('should return the error', function(done) {
this.child_process.exec.callsArgWith(2, this.error)
return this.optimiser.compressPng(this.sourcePath, err => {
err.should.equal(this.error)
return done()
})
})
})
it("convert the file", function(done){ describe('when enableConversions is disabled', () =>
this.child_process.exec.callsArgWith(2); it('should produce an error', function(done) {
return this.optimiser.compressPng(this.sourcePath, err=> { this.settings.enableConversions = false
const args = this.child_process.exec.args[0][0]; this.child_process.exec.callsArgWith(2)
args.should.equal(`optipng ${this.sourcePath}`); return this.optimiser.compressPng(this.sourcePath, err => {
return done(); this.child_process.exec.called.should.equal(false)
}); expect(err).to.exist
}); return done()
})
}))
return describe('when optimiser is sigkilled', () =>
return it("should return the error", function(done){ it('should not produce an error', function(done) {
this.child_process.exec.callsArgWith(2, this.error); this.error = new Error('woops')
return this.optimiser.compressPng(this.sourcePath, err=> { this.error.signal = 'SIGKILL'
err.should.equal(this.error); this.child_process.exec.callsArgWith(2, this.error)
return done(); return this.optimiser.compressPng(this.sourcePath, err => {
}); expect(err).to.equal(null)
}); return done()
}); })
}))
describe('when enableConversions is disabled', () => it('should produce an error', function(done) { })
this.settings.enableConversions = false;
this.child_process.exec.callsArgWith(2);
return this.optimiser.compressPng(this.sourcePath, err=> {
this.child_process.exec.called.should.equal(false);
expect(err).to.exist;
return done();
});
}));
return describe('when optimiser is sigkilled', () => it('should not produce an error', function(done) {
this.error = new Error('woops');
this.error.signal = 'SIGKILL';
this.child_process.exec.callsArgWith(2, this.error);
return this.optimiser.compressPng(this.sourcePath, err=> {
expect(err).to.equal(null);
return done();
});
}));
});

View file

@ -10,56 +10,49 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/KeyBuilder.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/KeyBuilder.js";
const SandboxedModule = require('sandboxed-module');
describe("LocalFileWriter", function() { describe('LocalFileWriter', function() {
beforeEach(function() {
this.keyBuilder = SandboxedModule.require(modulePath, {
requires: {
'logger-sharelatex': {
log() {},
err() {}
}
}
})
return (this.key = '123/456')
})
beforeEach(function() { return describe('cachedKey', function() {
it('should add the fomat on', function() {
const opts = { format: 'png' }
const newKey = this.keyBuilder.addCachingToKey(this.key, opts)
return newKey.should.equal(`${this.key}-converted-cache/format-png`)
})
this.keyBuilder = SandboxedModule.require(modulePath, { requires: { it('should add the style on', function() {
"logger-sharelatex": { const opts = { style: 'thumbnail' }
log() {}, const newKey = this.keyBuilder.addCachingToKey(this.key, opts)
err() {} return newKey.should.equal(`${this.key}-converted-cache/style-thumbnail`)
} })
}
}
);
return this.key = "123/456";
});
return describe("cachedKey", function() {
it("should add the fomat on", function() { return it('should add format on first', function() {
const opts = const opts = {
{format: "png"}; style: 'thumbnail',
const newKey = this.keyBuilder.addCachingToKey(this.key, opts); format: 'png'
return newKey.should.equal(`${this.key}-converted-cache/format-png`); }
}); const newKey = this.keyBuilder.addCachingToKey(this.key, opts)
return newKey.should.equal(
it("should add the style on", function() { `${this.key}-converted-cache/format-png-style-thumbnail`
const opts = )
{style: "thumbnail"}; })
const newKey = this.keyBuilder.addCachingToKey(this.key, opts); })
return newKey.should.equal(`${this.key}-converted-cache/style-thumbnail`); })
});
return it("should add format on first", function() {
const opts = {
style: "thumbnail",
format: "png"
};
const newKey = this.keyBuilder.addCachingToKey(this.key, opts);
return newKey.should.equal(`${this.key}-converted-cache/format-png-style-thumbnail`);
});
});
});

View file

@ -11,118 +11,112 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/LocalFileWriter.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/LocalFileWriter.js";
const SandboxedModule = require('sandboxed-module');
describe("LocalFileWriter", function() { describe('LocalFileWriter', function() {
beforeEach(function() {
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),
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() {} }
}
}
}
})
beforeEach(function() { return (this.stubbedFsPath = 'something/uploads/eio2k1j3')
})
this.writeStream = { describe('writeStrem', function() {
on(type, cb){ beforeEach(function() {
if (type === "finish") { return (this.writer._getPath = sinon.stub().returns(this.stubbedFsPath))
return cb(); })
}
}
};
this.readStream =
{on() {}};
this.fs = {
createWriteStream : sinon.stub().returns(this.writeStream),
createReadStream: sinon.stub().returns(this.readStream),
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"; 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, () => {})
})
describe("writeStrem", function() { return it('should send the path in the callback', function(done) {
beforeEach(function() { const stream = {
return this.writer._getPath = sinon.stub().returns(this.stubbedFsPath); 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()
})
})
})
it("write the stream to ./uploads", function(done){ describe('getStream', function() {
const stream = { it('should read the stream from the file ', function(done) {
pipe: dest=> { return this.writer.getStream(this.stubbedFsPath, (err, stream) => {
dest.should.equal(this.writeStream); this.fs.createReadStream
return done(); .calledWith(this.stubbedFsPath)
}, .should.equal(true)
on() {} return done()
}; })
return this.writer.writeStream(stream, null, ()=> {}); })
});
return it("should send the path in the callback", function(done){ return it('should send the stream in the callback', function(done) {
const stream = { return this.writer.getStream(this.stubbedFsPath, (err, readStream) => {
pipe: dest=> {}, readStream.should.equal(this.readStream)
on(type, cb){ return done()
if (type === "end") { })
return cb(); })
} })
}
};
return this.writer.writeStream(stream, null, (err, fsPath)=> {
fsPath.should.equal(this.stubbedFsPath);
return done();
});
});
});
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();
});
}));
});
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()
})
}))
})

View file

@ -9,129 +9,129 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const logger = require("logger-sharelatex"); const logger = require('logger-sharelatex')
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/PersistorManager.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/PersistorManager.js";
const SandboxedModule = require('sandboxed-module');
describe('PersistorManagerTests', function() {
beforeEach(function() {
return (this.S3PersistorManager = {
getFileStream: sinon.stub(),
checkIfFileExists: sinon.stub(),
deleteFile: sinon.stub(),
deleteDirectory: sinon.stub(),
sendStream: sinon.stub(),
insertFile: sinon.stub()
})
})
describe("PersistorManagerTests", function() { describe('test s3 mixin', function() {
beforeEach(function() {
beforeEach(function() { this.settings = {
return this.S3PersistorManager = { filestore: {
getFileStream: sinon.stub(), backend: 's3'
checkIfFileExists: sinon.stub(),
deleteFile: sinon.stub(),
deleteDirectory: sinon.stub(),
sendStream: sinon.stub(),
insertFile: sinon.stub()
};
});
describe("test s3 mixin", function() {
beforeEach(function() {
this.settings = {
filestore: {
backend: "s3"
}
};
this.requires = {
"./S3PersistorManager": this.S3PersistorManager,
"settings-sharelatex": this.settings,
"logger-sharelatex": {
log() {},
err() {}
}
};
return this.PersistorManager = SandboxedModule.require(modulePath, {requires: this.requires});
});
it("should load getFileStream", function(done) {
this.PersistorManager.should.respondTo("getFileStream");
this.PersistorManager.getFileStream();
this.S3PersistorManager.getFileStream.calledOnce.should.equal(true);
return done();
});
it("should load checkIfFileExists", function(done) {
this.PersistorManager.checkIfFileExists();
this.S3PersistorManager.checkIfFileExists.calledOnce.should.equal(true);
return done();
});
it("should load deleteFile", function(done) {
this.PersistorManager.deleteFile();
this.S3PersistorManager.deleteFile.calledOnce.should.equal(true);
return done();
});
it("should load deleteDirectory", function(done) {
this.PersistorManager.deleteDirectory();
this.S3PersistorManager.deleteDirectory.calledOnce.should.equal(true);
return done();
});
it("should load sendStream", function(done) {
this.PersistorManager.sendStream();
this.S3PersistorManager.sendStream.calledOnce.should.equal(true);
return done();
});
return it("should load insertFile", function(done) {
this.PersistorManager.insertFile();
this.S3PersistorManager.insertFile.calledOnce.should.equal(true);
return done();
});
});
describe("test unspecified mixins", () => it("should load s3 when no wrapper specified", function(done) {
this.settings = {filestore:{}};
this.requires = {
"./S3PersistorManager": this.S3PersistorManager,
"settings-sharelatex": this.settings,
"logger-sharelatex": {
log() {},
err() {}
}
};
this.PersistorManager = SandboxedModule.require(modulePath, {requires: this.requires});
this.PersistorManager.should.respondTo("getFileStream");
this.PersistorManager.getFileStream();
this.S3PersistorManager.getFileStream.calledOnce.should.equal(true);
return done();
}));
return describe("test invalid mixins", () => it("should not load an invalid wrapper", function(done) {
this.settings = {
filestore: {
backend:"magic"
}
};
this.requires = {
"./S3PersistorManager": this.S3PersistorManager,
"settings-sharelatex": this.settings,
"logger-sharelatex": {
log() {},
err() {}
}
};
this.fsWrapper=null;
try {
this.PersistorManager=SandboxedModule.require(modulePath, {requires: this.requires});
} catch (error) {
assert.equal("Unknown filestore backend: magic",error.message);
} }
assert.isNull(this.fsWrapper); }
return done(); this.requires = {
})); './S3PersistorManager': this.S3PersistorManager,
}); 'settings-sharelatex': this.settings,
'logger-sharelatex': {
log() {},
err() {}
}
}
return (this.PersistorManager = SandboxedModule.require(modulePath, {
requires: this.requires
}))
})
it('should load getFileStream', function(done) {
this.PersistorManager.should.respondTo('getFileStream')
this.PersistorManager.getFileStream()
this.S3PersistorManager.getFileStream.calledOnce.should.equal(true)
return done()
})
it('should load checkIfFileExists', function(done) {
this.PersistorManager.checkIfFileExists()
this.S3PersistorManager.checkIfFileExists.calledOnce.should.equal(true)
return done()
})
it('should load deleteFile', function(done) {
this.PersistorManager.deleteFile()
this.S3PersistorManager.deleteFile.calledOnce.should.equal(true)
return done()
})
it('should load deleteDirectory', function(done) {
this.PersistorManager.deleteDirectory()
this.S3PersistorManager.deleteDirectory.calledOnce.should.equal(true)
return done()
})
it('should load sendStream', function(done) {
this.PersistorManager.sendStream()
this.S3PersistorManager.sendStream.calledOnce.should.equal(true)
return done()
})
return it('should load insertFile', function(done) {
this.PersistorManager.insertFile()
this.S3PersistorManager.insertFile.calledOnce.should.equal(true)
return done()
})
})
describe('test unspecified mixins', () =>
it('should load s3 when no wrapper specified', function(done) {
this.settings = { filestore: {} }
this.requires = {
'./S3PersistorManager': this.S3PersistorManager,
'settings-sharelatex': this.settings,
'logger-sharelatex': {
log() {},
err() {}
}
}
this.PersistorManager = SandboxedModule.require(modulePath, {
requires: this.requires
})
this.PersistorManager.should.respondTo('getFileStream')
this.PersistorManager.getFileStream()
this.S3PersistorManager.getFileStream.calledOnce.should.equal(true)
return done()
}))
return describe('test invalid mixins', () =>
it('should not load an invalid wrapper', function(done) {
this.settings = {
filestore: {
backend: 'magic'
}
}
this.requires = {
'./S3PersistorManager': this.S3PersistorManager,
'settings-sharelatex': this.settings,
'logger-sharelatex': {
log() {},
err() {}
}
}
this.fsWrapper = null
try {
this.PersistorManager = SandboxedModule.require(modulePath, {
requires: this.requires
})
} catch (error) {
assert.equal('Unknown filestore backend: magic', error.message)
}
assert.isNull(this.fsWrapper)
return done()
}))
})

File diff suppressed because it is too large Load diff

View file

@ -9,73 +9,87 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/SafeExec.js'
const { const SandboxedModule = require('sandboxed-module')
expect
} = chai;
const modulePath = "../../../app/js/SafeExec.js";
const SandboxedModule = require('sandboxed-module');
describe("SafeExec", function() { describe('SafeExec', function() {
beforeEach(function() {
this.settings = { enableConversions: true }
this.safe_exec = SandboxedModule.require(modulePath, {
requires: {
'logger-sharelatex': {
log() {},
err() {}
},
'settings-sharelatex': this.settings
}
})
return (this.options = { timeout: 10 * 1000, killSignal: 'SIGTERM' })
})
beforeEach(function() { return describe('safe_exec', function() {
this.settings = it('should execute a valid command', function(done) {
{enableConversions:true}; return this.safe_exec(
this.safe_exec = SandboxedModule.require(modulePath, { requires: { ['/bin/echo', 'hello'],
"logger-sharelatex": { this.options,
log() {}, (err, stdout, stderr) => {
err() {} stdout.should.equal('hello\n')
}, should.not.exist(err)
"settings-sharelatex": this.settings return done()
} }
} )
); })
return this.options = {timeout: 10*1000, killSignal: "SIGTERM" };});
return describe("safe_exec", function() { it('should error when conversions are disabled', function(done) {
this.settings.enableConversions = false
return this.safe_exec(
['/bin/echo', 'hello'],
this.options,
(err, stdout, stderr) => {
expect(err).to.exist
return done()
}
)
})
it("should execute a valid command", function(done) { it('should execute a command with non-zero exit status', function(done) {
return this.safe_exec(["/bin/echo", "hello"], this.options, (err, stdout, stderr) => { return this.safe_exec(
stdout.should.equal("hello\n"); ['/usr/bin/env', 'false'],
should.not.exist(err); this.options,
return done(); (err, stdout, stderr) => {
}); stdout.should.equal('')
}); stderr.should.equal('')
err.message.should.equal('exit status 1')
return done()
}
)
})
it("should error when conversions are disabled", function(done) { it('should handle an invalid command', function(done) {
this.settings.enableConversions = false; return this.safe_exec(
return this.safe_exec(["/bin/echo", "hello"], this.options, (err, stdout, stderr) => { ['/bin/foobar'],
expect(err).to.exist; this.options,
return done(); (err, stdout, stderr) => {
}); err.code.should.equal('ENOENT')
}); return done()
}
)
})
it("should execute a command with non-zero exit status", function(done) { return it('should handle a command that runs too long', function(done) {
return this.safe_exec(["/usr/bin/env", "false"], this.options, (err, stdout, stderr) => { return this.safe_exec(
stdout.should.equal(""); ['/bin/sleep', '10'],
stderr.should.equal(""); { timeout: 500, killSignal: 'SIGTERM' },
err.message.should.equal("exit status 1"); (err, stdout, stderr) => {
return done(); err.should.equal('SIGTERM')
}); return done()
}); }
)
it("should handle an invalid command", function(done) { })
return this.safe_exec(["/bin/foobar"], this.options, (err, stdout, stderr) => { })
err.code.should.equal("ENOENT"); })
return done();
});
});
return it("should handle a command that runs too long", function(done) {
return this.safe_exec(["/bin/sleep", "10"], {timeout: 500, killSignal: "SIGTERM"}, (err, stdout, stderr) => {
err.should.equal("SIGTERM");
return done();
});
});
});
});

View file

@ -9,27 +9,25 @@
* DS102: Remove unnecessary code created because of implicit returns * DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/BucketController.js'
const {
expect
} = chai;
const modulePath = "../../../app/js/BucketController.js";
describe("Settings", () => describe("s3", () => it("should use JSONified env var if present", function(done){ describe('Settings', () =>
const s3_settings = { describe('s3', () =>
it('should use JSONified env var if present', function(done) {
const s3_settings = {
bucket1: { bucket1: {
auth_key: 'bucket1_key', auth_key: 'bucket1_key',
auth_secret: 'bucket1_secret' auth_secret: 'bucket1_secret'
} }
}; }
process.env.S3_BUCKET_CREDENTIALS = JSON.stringify(s3_settings); process.env.S3_BUCKET_CREDENTIALS = JSON.stringify(s3_settings)
const settings = require("settings-sharelatex"); const settings = require('settings-sharelatex')
expect(settings.filestore.s3BucketCreds).to.deep.equal(s3_settings); expect(settings.filestore.s3BucketCreds).to.deep.equal(s3_settings)
return done(); return done()
}))); })))