diff --git a/src/connector/passive.js b/src/connector/passive.js index 36677e36..f191f21a 100644 --- a/src/connector/passive.js +++ b/src/connector/passive.js @@ -89,7 +89,8 @@ class Passive extends Connector { this.server.options.pasv_range.split('-').map(v => v ? parseInt(v) : v) : [this.server.options.pasv_range]; return findPort(min, max); - } else return undefined; + } + throw new errors.ConnectorError('Invalid pasv_range'); } } diff --git a/src/index.js b/src/index.js index cecbb6d3..f1751546 100644 --- a/src/index.js +++ b/src/index.js @@ -95,7 +95,7 @@ class FtpServer { setupTLS(_tls) { if (!tls) return false; - return _.assign(_tls, { + return _.assign({}, _tls, { cert: _tls.cert ? fs.readFileSync(_tls.cert) : undefined, key: _tls.key ? fs.readFileSync(_tls.key) : undefined, ca: _tls.ca ? Array.isArray(_tls.ca) ? _tls.ca.map(_ca => fs.readFileSync(_ca)) : [fs.readFileSync(_tls.ca)] : undefined diff --git a/test/commands/registration/abor.spec.js b/test/commands/registration/abor.spec.js index 97aae3b4..2c3bdee5 100644 --- a/test/commands/registration/abor.spec.js +++ b/test/commands/registration/abor.spec.js @@ -25,29 +25,25 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful | no active connection', done => { + it('// successful | no active connection', () => { mockClient.connector.waitForConnection.restore(); sandbox.stub(mockClient.connector, 'waitForConnection').rejects(); - cmdFn() + return cmdFn() .then(() => { expect(mockClient.connector.waitForConnection.callCount).to.equal(1); expect(mockClient.connector.end.callCount).to.equal(0); expect(mockClient.reply.args[0][0]).to.equal(226); - done(); - }) - .catch(done); + }); }); - it('// successful | active connection', done => { - cmdFn() + it('// successful | active connection', () => { + return cmdFn() .then(() => { expect(mockClient.connector.waitForConnection.callCount).to.equal(1); expect(mockClient.connector.end.callCount).to.equal(1); expect(mockClient.reply.args[0][0]).to.equal(426); expect(mockClient.reply.args[1][0]).to.equal(226); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/allo.spec.js b/test/commands/registration/allo.spec.js index 8c9f1eff..0e59b95b 100644 --- a/test/commands/registration/allo.spec.js +++ b/test/commands/registration/allo.spec.js @@ -19,12 +19,10 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn() + it('// successful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(202); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/auth.spec.js b/test/commands/registration/auth.spec.js index 798cdfcf..9401b062 100644 --- a/test/commands/registration/auth.spec.js +++ b/test/commands/registration/auth.spec.js @@ -22,31 +22,25 @@ describe(CMD, function () { sandbox.restore(); }); - it('TLS // supported', done => { - cmdFn({command: { arg: 'TLS', directive: CMD}}) + it('TLS // supported', () => { + return cmdFn({command: { arg: 'TLS', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(234); expect(mockClient.secure).to.equal(true); - done(); - }) - .catch(done); + }); }); - it('SSL // not supported', done => { - cmdFn({command: { arg: 'SSL', directive: CMD}}) + it('SSL // not supported', () => { + return cmdFn({command: { arg: 'SSL', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(504); - done(); - }) - .catch(done); + }); }); - it('bad // bad', done => { - cmdFn({command: { arg: 'bad', directive: CMD}}) + it('bad // bad', () => { + return cmdFn({command: { arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(504); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/cdup.spec.js b/test/commands/registration/cdup.spec.js index 87205aea..1b5a12fe 100644 --- a/test/commands/registration/cdup.spec.js +++ b/test/commands/registration/cdup.spec.js @@ -25,13 +25,11 @@ describe(CMD, function () { sandbox.restore(); }); - it('.. // successful', done => { - cmdFn({log, command: {directive: CMD}}) + it('.. // successful', () => { + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(250); expect(mockClient.fs.chdir.args[0][0]).to.equal('..'); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/cwd.spec.js b/test/commands/registration/cwd.spec.js index 6ed6d2b3..73ed1233 100644 --- a/test/commands/registration/cwd.spec.js +++ b/test/commands/registration/cwd.spec.js @@ -23,63 +23,56 @@ describe(CMD, function () { }); describe('// check', function () { - it('fails on no fs', done => { + it('fails on no fs', () => { const badMockClient = { reply: () => {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('fails on no fs chdir command', done => { + it('fails on no fs chdir command', () => { const badMockClient = { reply: () => {}, fs: {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); }); - it('test // successful', done => { - cmdFn({log, command: { arg: 'test', directive: CMD}}) + it('test // successful', () => { + return cmdFn({log, command: { arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(250); expect(mockClient.fs.chdir.args[0][0]).to.equal('test'); - done(); - }) - .catch(done); + }); }); - it('test // successful', done => { + it('test // successful', () => { mockClient.fs.chdir.restore(); sandbox.stub(mockClient.fs, 'chdir').resolves('/test'); - cmdFn({log, command: { arg: 'test', directive: CMD}}) + + return cmdFn({log, command: { arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(250); expect(mockClient.fs.chdir.args[0][0]).to.equal('test'); - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { + it('bad // unsuccessful', () => { mockClient.fs.chdir.restore(); sandbox.stub(mockClient.fs, 'chdir').rejects(new Error('Bad')); - cmdFn({log, command: { arg: 'bad', directive: CMD}}) + return cmdFn({log, command: { arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); expect(mockClient.fs.chdir.args[0][0]).to.equal('bad'); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/dele.spec.js b/test/commands/registration/dele.spec.js index 2317f868..0a4130f9 100644 --- a/test/commands/registration/dele.spec.js +++ b/test/commands/registration/dele.spec.js @@ -23,51 +23,45 @@ describe(CMD, function () { }); describe('// check', function () { - it('fails on no fs', done => { + it('fails on no fs', () => { const badMockClient = { reply: () => {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('fails on no fs delete command', done => { + it('fails on no fs delete command', () => { const badMockClient = { reply: () => {}, fs: {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); }); - it('test // successful', done => { - cmdFn({log, command: { arg: 'test', directive: CMD}}) + it('test // successful', () => { + return cmdFn({log, command: { arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(250); expect(mockClient.fs.delete.args[0][0]).to.equal('test'); - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { + it('bad // unsuccessful', () => { mockClient.fs.delete.restore(); sandbox.stub(mockClient.fs, 'delete').rejects(new Error('Bad')); - cmdFn({log, command: { arg: 'bad', directive: CMD}}) + return cmdFn({log, command: { arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); expect(mockClient.fs.delete.args[0][0]).to.equal('bad'); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/help.spec.js b/test/commands/registration/help.spec.js index 061a10a3..2e83e3a7 100644 --- a/test/commands/registration/help.spec.js +++ b/test/commands/registration/help.spec.js @@ -19,39 +19,31 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn({command: { directive: CMD }}) + it('// successful', () => { + return cmdFn({command: { directive: CMD }}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(211); - done(); - }) - .catch(done); + }); }); - it('help // successful', done => { - cmdFn({command: { arg: 'help', directive: CMD}}) + it('help // successful', () => { + return cmdFn({command: { arg: 'help', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(214); - done(); - }) - .catch(done); + }); }); - it('help // successful', done => { - cmdFn({command: { arg: 'allo', directive: CMD}}) + it('allo // successful', () => { + return cmdFn({command: { arg: 'allo', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(214); - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { - cmdFn({command: { arg: 'bad', directive: CMD}}) + it('bad // unsuccessful', () => { + return cmdFn({command: { arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(502); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/list.spec.js b/test/commands/registration/list.spec.js index fc1a79dc..9da7f99f 100644 --- a/test/commands/registration/list.spec.js +++ b/test/commands/registration/list.spec.js @@ -87,33 +87,31 @@ describe(CMD, function () { }); describe('// check', function () { - it('fails on no fs', done => { + it('fails on no fs', () => { const badMockClient = { reply: () => {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('fails on no fs list command', done => { + it('fails on no fs list command', () => { const badMockClient = { reply: () => {}, fs: {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); }); - it('. // successful', done => { - cmdFn({log, command: {directive: CMD}}) + it('. // successful', () => { + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(150); expect(mockClient.reply.args[1].length).to.equal(3); @@ -121,12 +119,10 @@ describe(CMD, function () { expect(mockClient.reply.args[1][1]).to.have.property('message'); expect(mockClient.reply.args[1][1]).to.have.property('socket'); expect(mockClient.reply.args[2][0]).to.equal(226); - done(); - }) - .catch(done); + }); }); - it('testfile.txt // successful', done => { + it('testfile.txt // successful', () => { mockClient.fs.get.restore(); sandbox.stub(mockClient.fs, 'get').resolves({ name: 'testfile.txt', @@ -147,7 +143,7 @@ describe(CMD, function () { isDirectory: () => false }); - cmdFn({log, command: {directive: CMD, arg: 'testfile.txt'}}) + return cmdFn({log, command: {directive: CMD, arg: 'testfile.txt'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(150); expect(mockClient.reply.args[1].length).to.equal(2); @@ -155,31 +151,25 @@ describe(CMD, function () { expect(mockClient.reply.args[1][1]).to.have.property('message'); expect(mockClient.reply.args[1][1]).to.have.property('socket'); expect(mockClient.reply.args[2][0]).to.equal(226); - done(); - }) - .catch(done); + }); }); - it('. // unsuccessful', done => { + it('. // unsuccessful', () => { mockClient.fs.list.restore(); sandbox.stub(mockClient.fs, 'list').rejects(new Error()); - cmdFn({log, command: {directive: CMD}}) + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(451); - done(); - }) - .catch(done); + }); }); - it('. // unsuccessful (timeout)', done => { + it('. // unsuccessful (timeout)', () => { sandbox.stub(mockClient.connector, 'waitForConnection').returns(when.reject(new when.TimeoutError())); - cmdFn({log, command: {directive: CMD}}) + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(425); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/mdtm.spec.js b/test/commands/registration/mdtm.spec.js index 431734a3..a7ef52f4 100644 --- a/test/commands/registration/mdtm.spec.js +++ b/test/commands/registration/mdtm.spec.js @@ -23,50 +23,44 @@ describe(CMD, function () { }); describe('// check', function () { - it('fails on no fs', done => { + it('fails on no fs', () => { const badMockClient = { reply: () => {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('fails on no fs get command', done => { + it('fails on no fs get command', () => { const badMockClient = { reply: () => {}, fs: {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); }); - it('. // successful', done => { - cmdFn({log, command: {directive: CMD}}) + it('. // successful', () => { + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(213); //expect(mockClient.reply.args[0][1]).to.equal('20111010172411.000'); - done(); - }) - .catch(done); + }); }); - it('. // unsuccessful', done => { + it('. // unsuccessful', () => { mockClient.fs.get.restore(); sandbox.stub(mockClient.fs, 'get').rejects(new Error()); - cmdFn({log, command: {directive: CMD}}) + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/mkd.spec.js b/test/commands/registration/mkd.spec.js index 5375e3ac..6849498a 100644 --- a/test/commands/registration/mkd.spec.js +++ b/test/commands/registration/mkd.spec.js @@ -23,63 +23,56 @@ describe(CMD, function () { }); describe('// check', function () { - it('fails on no fs', done => { + it('fails on no fs', () => { const badMockClient = { reply: () => {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('fails on no fs mkdir command', done => { + it('fails on no fs mkdir command', () => { const badMockClient = { reply: () => {}, fs: {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); }); - it('test // successful', done => { - cmdFn({log, command: {arg: 'test', directive: CMD}}) + it('test // successful', () => { + return cmdFn({log, command: {arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(257); expect(mockClient.fs.mkdir.args[0][0]).to.equal('test'); - done(); - }) - .catch(done); + }); }); - it('test // successful', done => { + it('test // successful', () => { mockClient.fs.mkdir.restore(); sandbox.stub(mockClient.fs, 'mkdir').resolves('test'); - cmdFn({log, command: {arg: 'test', directive: CMD}}) + + return cmdFn({log, command: {arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(257); expect(mockClient.fs.mkdir.args[0][0]).to.equal('test'); - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { + it('bad // unsuccessful', () => { mockClient.fs.mkdir.restore(); sandbox.stub(mockClient.fs, 'mkdir').rejects(new Error('Bad')); - cmdFn({log, command: {arg: 'bad', directive: CMD}}) + return cmdFn({log, command: {arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); expect(mockClient.fs.mkdir.args[0][0]).to.equal('bad'); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/mode.spec.js b/test/commands/registration/mode.spec.js index 4ee7e292..3296dfa5 100644 --- a/test/commands/registration/mode.spec.js +++ b/test/commands/registration/mode.spec.js @@ -19,21 +19,17 @@ describe(CMD, function () { sandbox.restore(); }); - it('S // successful', done => { - cmdFn({command: {arg: 'S'}}) + it('S // successful', () => { + return cmdFn({command: {arg: 'S'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - done(); - }) - .catch(done); + }); }); - it('Q // unsuccessful', done => { - cmdFn({command: {arg: 'Q'}}) + it('Q // unsuccessful', () => { + return cmdFn({command: {arg: 'Q'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(504); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/nlst.spec.js b/test/commands/registration/nlst.spec.js index b3907a97..812c7dd8 100644 --- a/test/commands/registration/nlst.spec.js +++ b/test/commands/registration/nlst.spec.js @@ -86,8 +86,8 @@ describe(CMD, function () { sandbox.restore(); }); - it('. // successful', done => { - cmdFn({log, command: {directive: CMD}}) + it('. // successful', () => { + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(150); expect(mockClient.reply.args[1].length).to.equal(3); @@ -95,12 +95,10 @@ describe(CMD, function () { expect(mockClient.reply.args[1][1]).to.have.property('message'); expect(mockClient.reply.args[1][1]).to.have.property('socket'); expect(mockClient.reply.args[2][0]).to.equal(226); - done(); - }) - .catch(done); + }); }); - it('testfile.txt // successful', done => { + it('testfile.txt // successful', () => { mockClient.fs.get.restore(); sandbox.stub(mockClient.fs, 'get').resolves({ name: 'testfile.txt', @@ -121,7 +119,7 @@ describe(CMD, function () { isDirectory: () => false }); - cmdFn({log, command: {directive: CMD, arg: 'testfile.txt'}}) + return cmdFn({log, command: {directive: CMD, arg: 'testfile.txt'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(150); expect(mockClient.reply.args[1].length).to.equal(2); @@ -129,8 +127,6 @@ describe(CMD, function () { expect(mockClient.reply.args[1][1]).to.have.property('message'); expect(mockClient.reply.args[1][1]).to.have.property('socket'); expect(mockClient.reply.args[2][0]).to.equal(226); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/noop.spec.js b/test/commands/registration/noop.spec.js index 19f1c682..152a082e 100644 --- a/test/commands/registration/noop.spec.js +++ b/test/commands/registration/noop.spec.js @@ -19,12 +19,10 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn() + it('// successful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/opts.spec.js b/test/commands/registration/opts.spec.js index e3efba02..6b60dd2e 100644 --- a/test/commands/registration/opts.spec.js +++ b/test/commands/registration/opts.spec.js @@ -19,12 +19,10 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn() + it('// successful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(501); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/pass.spec.js b/test/commands/registration/pass.spec.js index af97ed1c..b1d53230 100644 --- a/test/commands/registration/pass.spec.js +++ b/test/commands/registration/pass.spec.js @@ -24,61 +24,51 @@ describe(CMD, function () { sandbox.restore(); }); - it('pass // successful', done => { - cmdFn({log, command: {arg: 'pass', directive: CMD}}) + it('pass // successful', () => { + return cmdFn({log, command: {arg: 'pass', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(230); expect(mockClient.login.args[0]).to.eql(['anonymous', 'pass']); - done(); - }) - .catch(done); + }); }); - it('// successful (already authenticated)', done => { + it('// successful (already authenticated)', () => { mockClient.server.options.anonymous = true; mockClient.authenticated = true; - cmdFn({log, command: {directive: CMD}}) + return cmdFn({log, command: {directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(202); expect(mockClient.login.callCount).to.equal(0); mockClient.server.options.anonymous = false; mockClient.authenticated = false; - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { + it('bad // unsuccessful', () => { mockClient.login.restore(); sandbox.stub(mockClient, 'login').rejects('bad'); - cmdFn({log, command: {arg: 'bad', directive: CMD}}) + return cmdFn({log, command: {arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(530); - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { + it('bad // unsuccessful', () => { mockClient.login.restore(); sandbox.stub(mockClient, 'login').rejects({}); - cmdFn({log, command: {arg: 'bad', directive: CMD}}) + return cmdFn({log, command: {arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(530); - done(); - }) - .catch(done); + }); }); - it('bad // unsuccessful', done => { + it('bad // unsuccessful', () => { delete mockClient.username; - cmdFn({log, command: {arg: 'bad', directive: CMD}}) + return cmdFn({log, command: {arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(503); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/pbsz.spec.js b/test/commands/registration/pbsz.spec.js index 1bd1d7b8..ce508269 100644 --- a/test/commands/registration/pbsz.spec.js +++ b/test/commands/registration/pbsz.spec.js @@ -20,38 +20,32 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful', done => { - cmdFn() + it('// unsuccessful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(202); - done(); - }) - .catch(done); + }); }); - it('// successful', done => { + it('// successful', () => { mockClient.secure = true; mockClient.server._tls = {}; - cmdFn({command: {arg: '0'}}) + return cmdFn({command: {arg: '0'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); expect(mockClient.bufferSize).to.equal(0); - done(); - }) - .catch(done); + }); }); - it('// successful', done => { + it('// successful', () => { mockClient.secure = true; mockClient.server._tls = {}; - cmdFn({command: {arg: '10'}}) + return cmdFn({command: {arg: '10'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); expect(mockClient.bufferSize).to.equal(10); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/port.spec.js b/test/commands/registration/port.spec.js index 09d51d97..fb880d52 100644 --- a/test/commands/registration/port.spec.js +++ b/test/commands/registration/port.spec.js @@ -22,33 +22,27 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful | no argument', done => { - cmdFn() + it('// unsuccessful | no argument', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(425); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | invalid argument', done => { - cmdFn({ command: { arg: '1,2,3,4,5' } }) + it('// unsuccessful | invalid argument', () => { + return cmdFn({ command: { arg: '1,2,3,4,5' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(425); - done(); - }) - .catch(done); + }); }); - it('// successful', done => { - cmdFn({ command: { arg: '192,168,0,100,137,214' } }) + it('// successful', () => { + return cmdFn({ command: { arg: '192,168,0,100,137,214' } }) .then(() => { const [ip, port] = ActiveConnector.prototype.setupConnection.args[0]; expect(mockClient.reply.args[0][0]).to.equal(200); expect(ip).to.equal('192.168.0.100'); expect(port).to.equal(35286); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/prot.spec.js b/test/commands/registration/prot.spec.js index 97b93766..50cc5453 100644 --- a/test/commands/registration/prot.spec.js +++ b/test/commands/registration/prot.spec.js @@ -20,56 +20,46 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful', done => { - cmdFn() + it('// unsuccessful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(202); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful - no bufferSize', done => { + it('// unsuccessful - no bufferSize', () => { mockClient.server._tls = {}; mockClient.secure = true; - cmdFn({command: {arg: 'P'}}) + return cmdFn({command: {arg: 'P'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(503); - done(); - }) - .catch(done); + }); }); - it('// successful', done => { + it('// successful', () => { mockClient.bufferSize = 0; mockClient.secure = true; - cmdFn({command: {arg: 'p'}}) + return cmdFn({command: {arg: 'p'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful - unsupported', done => { + it('// unsuccessful - unsupported', () => { mockClient.secure = true; - cmdFn({command: {arg: 'C'}}) + return cmdFn({command: {arg: 'C'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(536); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful - unknown', done => { + it('// unsuccessful - unknown', () => { mockClient.secure = true; - cmdFn({command: {arg: 'QQ'}}) + return cmdFn({command: {arg: 'QQ'}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(504); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/pwd.spec.js b/test/commands/registration/pwd.spec.js index 2e64e68b..8f95126b 100644 --- a/test/commands/registration/pwd.spec.js +++ b/test/commands/registration/pwd.spec.js @@ -23,61 +23,53 @@ describe(CMD, function () { }); describe('// check', function () { - it('fails on no fs', done => { + it('fails on no fs', () => { const badMockClient = { reply: () => {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('fails on no fs currentDirectory command', done => { + it('fails on no fs currentDirectory command', () => { const badMockClient = { reply: () => {}, fs: {} }; const badCmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(badMockClient); sandbox.stub(badMockClient, 'reply').resolves(); - badCmdFn() + + return badCmdFn() .then(() => { expect(badMockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); }); - it('// successful', done => { - cmdFn({log, command: { arg: 'test', directive: CMD}}) + it('// successful', () => { + return cmdFn({log, command: { arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(257); - done(); - }) - .catch(done); + }); }); - it('// successful', done => { + it('// successful', () => { mockClient.fs.currentDirectory.restore(); sandbox.stub(mockClient.fs, 'currentDirectory').resolves('/test'); - cmdFn({log, command: {arg: 'test', directive: CMD}}) + return cmdFn({log, command: {arg: 'test', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(257); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful', done => { + it('// unsuccessful', () => { mockClient.fs.currentDirectory.restore(); sandbox.stub(mockClient.fs, 'currentDirectory').rejects(new Error('Bad')); - cmdFn({log, command: {arg: 'bad', directive: CMD}}) + return cmdFn({log, command: {arg: 'bad', directive: CMD}}) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/quit.spec.js b/test/commands/registration/quit.spec.js index e453aa52..a6a696fa 100644 --- a/test/commands/registration/quit.spec.js +++ b/test/commands/registration/quit.spec.js @@ -18,12 +18,10 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn() + it('// successful', () => { + return cmdFn() .then(() => { expect(mockClient.close.callCount).to.equal(1); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/retr.spec.js b/test/commands/registration/retr.spec.js new file mode 100644 index 00000000..ff22d801 --- /dev/null +++ b/test/commands/registration/retr.spec.js @@ -0,0 +1,75 @@ +const when = require('when'); +const bunyan = require('bunyan'); +const {expect} = require('chai'); +const sinon = require('sinon'); + +const CMD = 'RETR'; +describe(CMD, function () { + let sandbox; + let log = bunyan.createLogger({name: CMD}); + const mockClient = { + commandSocket: { + pause: () => {}, + resume: () => {} + }, + reply: () => when.resolve(), + connector: { + waitForConnection: () => when.resolve({ + resume: () => {} + }), + end: () => {} + } + }; + const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient); + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + + mockClient.fs = { + read: () => {} + }; + + sandbox.spy(mockClient, 'reply'); + }); + afterEach(() => sandbox.restore()); + + it('// unsuccessful | no file system', () => { + delete mockClient.fs; + + return cmdFn() + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(550); + }); + }); + + it('// unsuccessful | file system does not have functions', () => { + mockClient.fs = {}; + + return cmdFn() + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(402); + }); + }); + + it('// unsuccessful | connector times out', () => { + sandbox.stub(mockClient.connector, 'waitForConnection').callsFake(function () { + return when.reject(new when.TimeoutError()); + }); + + return cmdFn({log, command: {arg: 'test.txt'} }) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(425); + }); + }); + + it('// unsuccessful | connector errors out', () => { + sandbox.stub(mockClient.connector, 'waitForConnection').callsFake(function () { + return when.reject(new Error('test')); + }); + + return cmdFn({log, command: {arg: 'test.txt'} }) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(551); + }); + }); +}); diff --git a/test/commands/registration/rnfr.spec.js b/test/commands/registration/rnfr.spec.js index 0c51dc52..00ae371a 100644 --- a/test/commands/registration/rnfr.spec.js +++ b/test/commands/registration/rnfr.spec.js @@ -24,47 +24,39 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful | no file system', done => { + it('// unsuccessful | no file system', () => { delete mockClient.fs; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file system does not have functions', done => { + it('// unsuccessful | file system does not have functions', () => { mockClient.fs = {}; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); - it('test // unsuccessful | file get fails', done => { + it('test // unsuccessful | file get fails', () => { mockClient.fs.get.restore(); sandbox.stub(mockClient.fs, 'get').rejects(new Error('test')); - cmdFn({ log: mockLog, command: { arg: 'test' } }) + return cmdFn({ log: mockLog, command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('test // successful', done => { - cmdFn({ log: mockLog, command: { arg: 'test' } }) + it('test // successful', () => { + return cmdFn({ log: mockLog, command: { arg: 'test' } }) .then(() => { expect(mockClient.fs.get.args[0][0]).to.equal('test'); expect(mockClient.reply.args[0][0]).to.equal(350); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/rnto.spec.js b/test/commands/registration/rnto.spec.js index 84aff483..bd569388 100644 --- a/test/commands/registration/rnto.spec.js +++ b/test/commands/registration/rnto.spec.js @@ -25,58 +25,48 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful | no renameFrom set', done => { + it('// unsuccessful | no renameFrom set', () => { delete mockClient.renameFrom; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(503); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | no file system', done => { + it('// unsuccessful | no file system', () => { delete mockClient.fs; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file system does not have functions', done => { + it('// unsuccessful | file system does not have functions', () => { mockClient.fs = {}; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); - it('new // unsuccessful | rename fails', done => { + it('new // unsuccessful | rename fails', () => { mockClient.fs.rename.restore(); sandbox.stub(mockClient.fs, 'rename').rejects(new Error('test')); - cmdFn({ log: mockLog, command: { arg: 'new' } }) + return cmdFn({ log: mockLog, command: { arg: 'new' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('new // successful', done => { - cmdFn({ command: { arg: 'new' } }) + it('new // successful', () => { + return cmdFn({ command: { arg: 'new' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(250); expect(mockClient.fs.rename.args[0]).to.eql(['test', 'new']); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/size.spec.js b/test/commands/registration/size.spec.js index 733c8d89..f692ca98 100644 --- a/test/commands/registration/size.spec.js +++ b/test/commands/registration/size.spec.js @@ -22,45 +22,37 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful | no file system', done => { + it('// unsuccessful | no file system', () => { delete mockClient.fs; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file system does not have functions', done => { + it('// unsuccessful | file system does not have functions', () => { mockClient.fs = {}; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file get fails', done => { + it('// unsuccessful | file get fails', () => { sandbox.stub(mockClient.fs, 'get').rejects(new Error('test')); - cmdFn({ log: mockLog, command: { arg: 'test' } }) + return cmdFn({ log: mockLog, command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('// successful', done => { - cmdFn({ command: { arg: 'test' } }) + it('// successful', () => { + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(213); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/stat.spec.js b/test/commands/registration/stat.spec.js index 5693f8bc..e69ef17e 100644 --- a/test/commands/registration/stat.spec.js +++ b/test/commands/registration/stat.spec.js @@ -23,49 +23,41 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn() + it('// successful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(211); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | no file system', done => { + it('// unsuccessful | no file system', () => { delete mockClient.fs; - cmdFn({ command: { arg: 'test' } }) + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file system does not have functions', done => { + it('// unsuccessful | file system does not have functions', () => { mockClient.fs = {}; - cmdFn({ command: { arg: 'test' } }) + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file get fails', done => { + it('// unsuccessful | file get fails', () => { sandbox.stub(mockClient.fs, 'get').rejects(new Error('test')); - cmdFn({ log: mockLog, command: { arg: 'test' } }) + return cmdFn({ log: mockLog, command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(450); - done(); - }) - .catch(done); + }); }); - it('// successful | file', done => { + it('// successful | file', () => { sandbox.stub(mockClient.fs, 'get').returns({ name: 'test_file', dev: 2114, @@ -85,15 +77,13 @@ describe(CMD, function () { isDirectory: () => false }); - cmdFn({ command: { arg: 'test' } }) + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(212); - done(); - }) - .catch(done); + }); }); - it('// successful | directory', done => { + it('// successful | directory', () => { sandbox.stub(mockClient.fs, 'list').returns([{ name: 'test_file', dev: 2114, @@ -132,11 +122,9 @@ describe(CMD, function () { isDirectory: () => true }); - cmdFn({ command: { arg: 'test' } }) + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(213); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/stor.spec.js b/test/commands/registration/stor.spec.js new file mode 100644 index 00000000..e8c7df17 --- /dev/null +++ b/test/commands/registration/stor.spec.js @@ -0,0 +1,75 @@ +const when = require('when'); +const bunyan = require('bunyan'); +const {expect} = require('chai'); +const sinon = require('sinon'); + +const CMD = 'STOR'; +describe(CMD, function () { + let sandbox; + let log = bunyan.createLogger({name: CMD}); + const mockClient = { + commandSocket: { + pause: () => {}, + resume: () => {} + }, + reply: () => when.resolve(), + connector: { + waitForConnection: () => when.resolve({ + resume: () => {} + }), + end: () => {} + } + }; + const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient); + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + + mockClient.fs = { + write: () => {} + }; + + sandbox.spy(mockClient, 'reply'); + }); + afterEach(() => sandbox.restore()); + + it('// unsuccessful | no file system', () => { + delete mockClient.fs; + + return cmdFn() + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(550); + }); + }); + + it('// unsuccessful | file system does not have functions', () => { + mockClient.fs = {}; + + return cmdFn() + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(402); + }); + }); + + it('// unsuccessful | connector times out', () => { + sandbox.stub(mockClient.connector, 'waitForConnection').callsFake(function () { + return when.reject(new when.TimeoutError()); + }); + + return cmdFn({log, command: {arg: 'test.txt'} }) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(425); + }); + }); + + it('// unsuccessful | connector errors out', () => { + sandbox.stub(mockClient.connector, 'waitForConnection').callsFake(function () { + return when.reject(new Error('test')); + }); + + return cmdFn({log, command: {arg: 'test.txt'} }) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(550); + }); + }); +}); diff --git a/test/commands/registration/stou.spec.js b/test/commands/registration/stou.spec.js index 4337dc59..113b9a2b 100644 --- a/test/commands/registration/stou.spec.js +++ b/test/commands/registration/stou.spec.js @@ -30,54 +30,46 @@ describe(CMD, function () { sandbox.restore(); }); - it('// unsuccessful | no file system', done => { + it('// unsuccessful | no file system', () => { delete mockClient.fs; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(550); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful | file system does not have functions', done => { + it('// unsuccessful | file system does not have functions', () => { mockClient.fs = {}; - cmdFn() + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(402); - done(); - }) - .catch(done); + }); }); - it('// successful | given name is unique', done => { + it('// successful | given name is unique', () => { mockClient.fs.get.restore(); sandbox.stub(mockClient.fs, 'get').rejects({}); - cmdFn({ command: { arg: 'good' } }) + return cmdFn({ command: { arg: 'good' } }) .then(() => { const call = stor.handler.call.args[0][1]; expect(call).to.have.property('command'); expect(call.command).to.have.property('arg'); expect(call.command.arg).to.eql('good'); expect(mockClient.fs.getUniqueName.callCount).to.equal(0); - done(); - }) - .catch(done); + }); }); - it('// successful | generates unique name', done => { - cmdFn({ command: { arg: 'bad' } }) + it('// successful | generates unique name', () => { + return cmdFn({ command: { arg: 'bad' } }) .then(() => { const call = stor.handler.call.args[0][1]; expect(call).to.have.property('command'); expect(call.command).to.have.property('arg'); expect(call.command.arg).to.eql('4'); expect(mockClient.fs.getUniqueName.callCount).to.equal(1); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/stru.spec.js b/test/commands/registration/stru.spec.js index db553549..ebd12911 100644 --- a/test/commands/registration/stru.spec.js +++ b/test/commands/registration/stru.spec.js @@ -19,21 +19,17 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn({command: { arg: 'F' } }) + it('// successful', () => { + return cmdFn({command: { arg: 'F' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); - done(); - }) - .catch(done); + }); }); - it('// unsuccessful', done => { - cmdFn({command: { arg: 'X' } }) + it('// unsuccessful', () => { + return cmdFn({command: { arg: 'X' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(504); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/syst.spec.js b/test/commands/registration/syst.spec.js index 2caf763c..e7bde9bc 100644 --- a/test/commands/registration/syst.spec.js +++ b/test/commands/registration/syst.spec.js @@ -19,12 +19,10 @@ describe(CMD, function () { sandbox.restore(); }); - it('// successful', done => { - cmdFn() + it('// successful', () => { + return cmdFn() .then(() => { expect(mockClient.reply.args[0][0]).to.equal(215); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/type.spec.js b/test/commands/registration/type.spec.js index 6f60bb30..0f5541d2 100644 --- a/test/commands/registration/type.spec.js +++ b/test/commands/registration/type.spec.js @@ -20,43 +20,35 @@ describe(CMD, function () { sandbox.restore(); }); - it('A // successful', done => { - cmdFn({ command: { arg: 'A' } }) + it('A // successful', () => { + return cmdFn({ command: { arg: 'A' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); expect(mockClient.encoding).to.equal('utf8'); - done(); - }) - .catch(done); + }); }); - it('I // successful', done => { - cmdFn({ command: { arg: 'I' } }) + it('I // successful', () => { + return cmdFn({ command: { arg: 'I' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); expect(mockClient.encoding).to.equal('binary'); - done(); - }) - .catch(done); + }); }); - it('L // successful', done => { - cmdFn({ command: { arg: 'L' } }) + it('L // successful', () => { + return cmdFn({ command: { arg: 'L' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(200); expect(mockClient.encoding).to.equal('binary'); - done(); - }) - .catch(done); + }); }); - it('X // successful', done => { - cmdFn({ command: { arg: 'X' } }) + it('X // successful', () => { + return cmdFn({ command: { arg: 'X' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(501); expect(mockClient.encoding).to.equal(null); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/commands/registration/user.spec.js b/test/commands/registration/user.spec.js index 0662acfa..11dc3634 100644 --- a/test/commands/registration/user.spec.js +++ b/test/commands/registration/user.spec.js @@ -28,70 +28,80 @@ describe(CMD, function () { sandbox.restore(); }); - it('test // successful | prompt for password', done => { - cmdFn({ command: { arg: 'test' } }) + it('test // successful | prompt for password', () => { + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(331); - done(); - }) - .catch(done); + }); }); - it('test // successful | anonymous login', done => { + it('test // successful | anonymous login', () => { mockClient.server.options = {anonymous: true}; - cmdFn({ command: { arg: 'anonymous' } }) + return cmdFn({ command: { arg: 'anonymous' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(230); expect(mockClient.login.callCount).to.equal(1); - done(); - }) - .catch(done); + }); }); - it('test // unsuccessful | no username provided', done => { - cmdFn({ command: { } }) + it('test // unsuccessful | no username provided', () => { + return cmdFn({ command: { } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(501); expect(mockClient.login.callCount).to.equal(0); - done(); - }) - .catch(done); + }); }); - it('test // unsuccessful | already set username', done => { + it('test // unsuccessful | already set username', () => { mockClient.username = 'test'; - cmdFn({ command: { arg: 'test' } }) + return cmdFn({ command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(530); expect(mockClient.login.callCount).to.equal(0); - done(); - }) - .catch(done); + }); }); - it('test // successful | regular login if anonymous is true', done => { + it('test // successful | regular login if anonymous is true', () => { mockClient.server.options = {anonymous: true}; - cmdFn({ log: mockLog, command: { arg: 'test' } }) + return cmdFn({ log: mockLog, command: { arg: 'test' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(331); expect(mockClient.login.callCount).to.equal(0); - done(); - }) - .catch(done); + }); }); - it('test // successful | anonymous login with set username', done => { + it('test // successful | anonymous login with set username', () => { mockClient.server.options = {anonymous: 'sillyrabbit'}; - cmdFn({ log: mockLog, command: { arg: 'sillyrabbit' } }) + return cmdFn({ log: mockLog, command: { arg: 'sillyrabbit' } }) .then(() => { expect(mockClient.reply.args[0][0]).to.equal(230); expect(mockClient.login.callCount).to.equal(1); - done(); - }) - .catch(done); + }); + }); + + it('test // unsuccessful | anonymous login fails', () => { + mockClient.server.options = {anonymous: true}; + mockClient.login.restore(); + sandbox.stub(mockClient, 'login').rejects(new Error('test')); + + return cmdFn({ log: mockLog, command: { arg: 'anonymous' } }) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(530); + expect(mockClient.login.callCount).to.equal(1); + }); + }); + + it('test // successful | does not login if already authenticated', () => { + mockClient.authenticated = true; + + return cmdFn({ log: mockLog, command: { arg: 'sillyrabbit' } }) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(230); + expect(mockClient.login.callCount).to.equal(0); + }); }); }); diff --git a/test/connector/active.spec.js b/test/connector/active.spec.js index 4fa2913b..b25a3e05 100644 --- a/test/connector/active.spec.js +++ b/test/connector/active.spec.js @@ -3,6 +3,7 @@ const {expect} = require('chai'); const sinon = require('sinon'); const net = require('net'); +const tls = require('tls'); const ActiveConnector = require('../../src/connector/active'); const findPort = require('../../src/helpers/find-port'); @@ -33,34 +34,49 @@ describe('Connector - Active //', function () { server.close(done); }); - it('sets up a connection', function (done) { - active.setupConnection('127.0.0.1', PORT) + it('sets up a connection', function () { + return active.setupConnection('127.0.0.1', PORT) .then(() => { expect(active.dataSocket).to.exist; - done(); - }) - .catch(done); + }); }); - it('destroys existing connection, then sets up a connection', function (done) { + it('destroys existing connection, then sets up a connection', function () { const destroyFnSpy = sandbox.spy(active.dataSocket, 'destroy'); - active.setupConnection('127.0.0.1', PORT) + return active.setupConnection('127.0.0.1', PORT) .then(() => { expect(destroyFnSpy.callCount).to.equal(1); expect(active.dataSocket).to.exist; - done(); + }); + }); + + it('waits for connection', function () { + return active.setupConnection('127.0.0.1', PORT) + .then(() => { + expect(active.dataSocket).to.exist; + return active.waitForConnection(); }) - .catch(done); + .then(dataSocket => { + expect(dataSocket.connected).to.equal(true); + expect(dataSocket instanceof net.Socket).to.equal(true); + expect(dataSocket instanceof tls.TLSSocket).to.equal(false); + }); }); - it('waits for connection', function (done) { - active.setupConnection('127.0.0.1', PORT) + it('upgrades to a secure connection', function () { + mockConnection.secure = true; + mockConnection.server = { _tls: {} }; + + return active.setupConnection('127.0.0.1', PORT) .then(() => { expect(active.dataSocket).to.exist; return active.waitForConnection(); }) - .then(() => done()) - .catch(done); + .then(dataSocket => { + expect(dataSocket.connected).to.equal(true); + expect(dataSocket instanceof net.Socket).to.equal(true); + expect(dataSocket instanceof tls.TLSSocket).to.equal(true); + }); }); }); diff --git a/test/connector/passive.spec.js b/test/connector/passive.spec.js index 116f0976..783805bb 100644 --- a/test/connector/passive.spec.js +++ b/test/connector/passive.spec.js @@ -20,6 +20,10 @@ describe('Connector - Passive //', function () { }; let sandbox; + function shouldNotResolve() { + throw new Error('Should not resolve'); + } + before(() => { passive = new PassiveConnector(mockConnection); }); @@ -36,45 +40,49 @@ describe('Connector - Passive //', function () { sandbox.restore(); }); - it('cannot wait for connection with no server', function (done) { - passive.waitForConnection() - .then(() => done('should not happen')) + it('cannot wait for connection with no server', function () { + return passive.waitForConnection() + .then(shouldNotResolve) + .catch(err => { + expect(err.name).to.equal('ConnectorError'); + }); + }); + + it('no pasv range provided', function () { + delete mockConnection.server.options.pasv_range; + + return passive.setupServer() + .then(shouldNotResolve) .catch(err => { expect(err.name).to.equal('ConnectorError'); - done(); }); }); - it('has invalid pasv range', function (done) { + it('has invalid pasv range', function () { mockConnection.server.options.pasv_range = -1; - passive.setupServer() - .then(() => done('should not happen')) + return passive.setupServer() + .then(shouldNotResolve) .catch(err => { expect(err.name).to.equal('RangeError'); - done(); }); }); - it('sets up a server', function (done) { - passive.setupServer() + it('sets up a server', function () { + return passive.setupServer() .then(() => { expect(passive.dataServer).to.exist; - done(); - }) - .catch(done); + }); }); - it('destroys existing server, then sets up a server', function (done) { + it('destroys existing server, then sets up a server', function () { const closeFnSpy = sandbox.spy(passive.dataServer, 'close'); - passive.setupServer() + return passive.setupServer() .then(() => { expect(closeFnSpy.callCount).to.equal(1); expect(passive.dataServer).to.exist; - done(); - }) - .catch(done); + }); }); it('refuses connection with different remote address', function (done) { @@ -97,8 +105,8 @@ describe('Connector - Passive //', function () { .catch(done); }); - it('accepts connection', function (done) { - passive.setupServer() + it('accepts connection', function () { + return passive.setupServer() .then(() => { expect(passive.dataServer).to.exist; @@ -109,8 +117,6 @@ describe('Connector - Passive //', function () { .then(() => { expect(passive.dataSocket).to.exist; passive.end(); - done(); - }) - .catch(done); + }); }); }); diff --git a/test/helpers/escape-path.spec.js b/test/helpers/escape-path.spec.js index d91cf40a..802d99d2 100644 --- a/test/helpers/escape-path.spec.js +++ b/test/helpers/escape-path.spec.js @@ -2,10 +2,9 @@ const {expect} = require('chai'); const escapePath = require('../../src/helpers/escape-path'); describe('helpers // escape-path', function () { - it('escapes quotes', done => { + it('escapes quotes', () => { const string = '"test"'; const escapedString = escapePath(string); expect(escapedString).to.equal('""test""'); - done(); }); }); diff --git a/test/helpers/find-port.spec.js b/test/helpers/find-port.spec.js index cc894db6..567b5b88 100644 --- a/test/helpers/find-port.spec.js +++ b/test/helpers/find-port.spec.js @@ -17,22 +17,19 @@ describe('helpers // find-port', function () { sandbox.restore(); }); - it('finds a port', done => { - findPort(1) + it('finds a port', () => { + return findPort(1) .then(port => { expect(Server.prototype.listen.callCount).to.be.above(1); expect(port).to.be.above(1); - done(); - }) - .catch(done); + }); }); - it('does not find a port', done => { - findPort(1, 2) - .then(() => done('no')) + it('does not find a port', () => { + return findPort(1, 2) + .then(() => expect(1).to.equal(2)) // should not happen .catch(err => { expect(err).to.exist; - done(); }); }); }); diff --git a/test/helpers/resolve-host.spec.js b/test/helpers/resolve-host.spec.js index 26c2be04..8762eaca 100644 --- a/test/helpers/resolve-host.spec.js +++ b/test/helpers/resolve-host.spec.js @@ -1,36 +1,51 @@ const {expect} = require('chai'); +const sinon = require('sinon'); const resolveHost = require('../../src/helpers/resolve-host'); describe('helpers //resolve-host', function () { this.timeout(4000); - it('fetches ip address', done => { + let sandbox; + beforeEach(() => { + sandbox = sinon.sandbox.create(); + }); + afterEach(() => sandbox.restore()); + + it('fetches ip address', () => { const hostname = '0.0.0.0'; - resolveHost(hostname) + return resolveHost(hostname) .then(resolvedHostname => { expect(resolvedHostname).to.match(/^\d+\.\d+\.\d+\.\d+$/); - done(); - }) - .catch(done); + }); }); - it('fetches ip address', done => { + it('fetches ip address', () => { const hostname = null; - resolveHost(hostname) + return resolveHost(hostname) .then(resolvedHostname => { expect(resolvedHostname).to.match(/^\d+\.\d+\.\d+\.\d+$/); - done(); - }) - .catch(done); + }); }); - it('does nothing', done => { + it('does nothing', () => { const hostname = '127.0.0.1'; - resolveHost(hostname) + return resolveHost(hostname) .then(resolvedHostname => { expect(resolvedHostname).to.equal(hostname); - done(); - }) - .catch(done); + }); + }); + + it('fails on getting hostname', () => { + sandbox.stub(require('http'), 'get').callsFake(function (url, cb) { + cb({ + statusCode: 420 + }); + }); + + return resolveHost(null) + .then(() => expect(1).to.equal(2)) + .catch(err => { + expect(err.code).to.equal(420); + }); }); }); diff --git a/test/index.spec.js b/test/index.spec.js index f21c5ea9..53c5320f 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -14,7 +14,7 @@ describe('FtpServer', function () { let server; let client; - before(done => { + before(() => { server = new FtpServer(process.env.FTP_URL, { log, pasv_range: process.env.PASV_RANGE, @@ -22,14 +22,14 @@ describe('FtpServer', function () { key: `${process.cwd()}/test/cert/server.key`, cert: `${process.cwd()}/test/cert/server.crt`, ca: `${process.cwd()}/test/cert/server.csr` - } + }, + greeting: ['hello', 'world'] }); server.on('login', (data, resolve) => { resolve({root: process.cwd()}); }); - server.listen() - .then(() => done()); + return server.listen(); }); after(() => { server.close();