test: update tests with refactors

This commit is contained in:
Tyler Stewart
2018-06-24 23:37:23 -06:00
parent 5a36a6685d
commit 95471bdd15
4 changed files with 99 additions and 50 deletions

View File

@@ -6,9 +6,10 @@ const net = require('net');
const tls = require('tls');
const ActiveConnector = require('../../src/connector/active');
const findPort = require('../../src/helpers/find-port');
const {getNextPortFactory} = require('../../src/helpers/find-port');
describe('Connector - Active //', function () {
let getNextPort = getNextPortFactory(1024);
let PORT;
let active;
let mockConnection = {};
@@ -21,7 +22,7 @@ describe('Connector - Active //', function () {
beforeEach(done => {
sandbox = sinon.sandbox.create();
findPort()
getNextPort()
.then(port => {
PORT = port;
server = net.createServer()

View File

@@ -9,85 +9,114 @@ const bunyan = require('bunyan');
const PassiveConnector = require('../../src/connector/passive');
describe('Connector - Passive //', function () {
let passive;
let mockConnection = {
reply: () => Promise.resolve({}),
close: () => Promise.resolve({}),
encoding: 'utf8',
log: bunyan.createLogger({name: 'passive-test'}),
commandSocket: {},
server: {options: {}, url: {}}
commandSocket: {
remoteAddress: '::ffff:127.0.0.1'
},
server: {
options: {
pasv_min: 1024
},
url: ''
}
};
let sandbox;
function shouldNotResolve() {
throw new Error('Should not resolve');
}
before(() => {
passive = new PassiveConnector(mockConnection);
});
beforeEach(() => {
sandbox = sinon.sandbox.create();
});
beforeEach(() => {
sandbox.spy(mockConnection, 'reply');
sandbox.spy(mockConnection, 'close');
mockConnection.commandSocket.remoteAddress = '::ffff:127.0.0.1';
mockConnection.server.options.pasv_range = '8000';
});
afterEach(() => {
sandbox.restore();
});
it('cannot wait for connection with no server', function () {
return passive.waitForConnection()
.then(shouldNotResolve)
it('cannot wait for connection with no server', function (done) {
let passive = new PassiveConnector(mockConnection);
passive.waitForConnection()
.catch(err => {
expect(err.name).to.equal('ConnectorError');
done();
});
});
it('no pasv range provided', function () {
delete mockConnection.server.options.pasv_range;
describe('setup', function () {
before(function () {
sandbox.stub(mockConnection.server.options, 'pasv_min').value(undefined);
sandbox.stub(mockConnection.server.options, 'pasv_max').value(undefined);
});
return passive.setupServer()
.then(shouldNotResolve)
it('no pasv range provided', function (done) {
let passive = new PassiveConnector(mockConnection);
passive.setupServer()
.catch(err => {
try {
expect(err.name).to.equal('ConnectorError');
done();
} catch (ex) {
done(ex);
}
});
});
});
describe('setup', function () {
let connection;
before(function () {
sandbox.stub(mockConnection.server.options, 'pasv_min').value(-1);
sandbox.stub(mockConnection.server.options, 'pasv_max').value(-1);
connection = new PassiveConnector(mockConnection);
});
it('has invalid pasv range', function (done) {
connection.setupServer()
.catch(err => {
expect(err.name).to.equal('ConnectorError');
done();
});
});
it('has invalid pasv range', function () {
mockConnection.server.options.pasv_range = -1;
return passive.setupServer()
.then(shouldNotResolve)
.catch(err => {
expect(err).to.be.instanceOf(RangeError);
});
});
it('sets up a server', function () {
let passive = new PassiveConnector(mockConnection);
return passive.setupServer()
.then(() => {
expect(passive.dataServer).to.exist;
});
});
it('destroys existing server, then sets up a server', function () {
const closeFnSpy = sandbox.spy(passive.dataServer, 'close');
describe('setup', function () {
let passive;
let closeFnSpy;
before(function () {
passive = new PassiveConnector(mockConnection);
return passive.setupServer()
.then(() => {
closeFnSpy = sandbox.spy(passive.dataServer, 'close');
});
});
it('destroys existing server, then sets up a server', function () {
return passive.setupServer()
.then(() => {
expect(closeFnSpy.callCount).to.equal(1);
expect(passive.dataServer).to.exist;
});
});
});
it('refuses connection with different remote address', function (done) {
mockConnection.commandSocket.remoteAddress = 'bad';
sandbox.stub(mockConnection.commandSocket, 'remoteAddress').value('bad');
let passive = new PassiveConnector(mockConnection);
passive.setupServer()
.then(() => {
expect(passive.dataServer).to.exist;
@@ -106,6 +135,7 @@ describe('Connector - Passive //', function () {
});
it('accepts connection', function () {
let passive = new PassiveConnector(mockConnection);
return passive.setupServer()
.then(() => {
expect(passive.dataServer).to.exist;

View File

@@ -1,35 +1,52 @@
/* eslint no-unused-expressions: 0 */
const {expect} = require('chai');
const {Server} = require('net');
const net = require('net');
const sinon = require('sinon');
const findPort = require('../../src/helpers/find-port');
const {getNextPortFactory} = require('../../src/helpers/find-port');
describe('helpers // find-port', function () {
let sandbox;
let getNextPort;
beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.spy(Server.prototype, 'listen');
getNextPort = getNextPortFactory(1, 2);
});
afterEach(() => {
sandbox.restore();
});
it('finds a port', () => {
return findPort(1)
sandbox.stub(net.Server.prototype, 'listen').callsFake(function (port) {
this.address = () => ({port});
setImmediate(() => this.emit('listening'));
});
return getNextPort()
.then(port => {
expect(Server.prototype.listen.callCount).to.be.above(1);
expect(port).to.be.above(1);
expect(port).to.equal(1);
});
});
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;
it('restarts count', () => {
sandbox.stub(net.Server.prototype, 'listen').callsFake(function (port) {
this.address = () => ({port});
setImmediate(() => this.emit('listening'));
});
return getNextPort()
.then(port => {
expect(port).to.equal(1);
})
.then(() => getNextPort())
.then(port => {
expect(port).to.equal(2);
})
.then(() => getNextPort())
.then(port => {
expect(port).to.equal(1);
});
});
});

View File

@@ -37,9 +37,10 @@ describe('Integration', function () {
after(() => directoryPurge(clientDirectory));
function startServer(url, options = {}) {
server = new FtpServer(url, _.assign({
server = new FtpServer(_.assign({
url,
log,
pasv_range: 8881,
pasv_min: 8881,
greeting: ['hello', 'world'],
anonymous: true
}, options));