From 3e98b9e06c41dec132ec4ebbddfaf72ed1bef713 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Fri, 20 Jul 2018 12:55:18 -0400 Subject: [PATCH] LAN.Connection: ensure that ssh client stubs activate correctly --- lib/lan-connection.js | 8 ++++++-- test/unit/lan-connection.js | 31 +++++++++++++++++-------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/lib/lan-connection.js b/lib/lan-connection.js index 7f46e68f..cc165495 100644 --- a/lib/lan-connection.js +++ b/lib/lan-connection.js @@ -106,7 +106,7 @@ LAN.Connection.prototype.open = function() { return new Promise((resolve) => { // Create a new SSH client connection object this.ssh = new ssh.Client(); - // Open up an SSH Connection + this.ssh.on('ready', () => { debug(`Device ready: ${this.host}`); // Tessel allows connection @@ -114,16 +114,20 @@ LAN.Connection.prototype.open = function() { // Resolve with connection resolve(this); }); + this.ssh.on('error', () => { debug(`Device open error: ${this.host}`); // Tessel does not allow connection this.authorized = false; // Reject with error resolve(this); - }).connect(this.auth); + }); + this.ssh.once('close', () => { this.closed = true; }); + + this.ssh.connect(this.auth); }); }; diff --git a/test/unit/lan-connection.js b/test/unit/lan-connection.js index 59cd0778..441d26ab 100644 --- a/test/unit/lan-connection.js +++ b/test/unit/lan-connection.js @@ -97,13 +97,16 @@ exports['LAN.Scanner'] = { exports['LAN.Connection.prototype.exec'] = { setUp(done) { this.sandbox = sinon.sandbox.create(); - this.isProvisioned = this.sandbox.stub(Tessel, 'isProvisioned').callsFake(function() { - return false; - }); + this.isProvisioned = this.sandbox.stub(Tessel, 'isProvisioned').returns(true); - this.Client = this.sandbox.spy(ssh, 'Client'); + const emitter = new Emitter(); + emitter.connect = () => emitter.emit('ready'); - this.lanConnection = new LAN.Connection({}); + this.Client = this.sandbox.stub(ssh, 'Client').returns(emitter); + + this.lanConnection = new LAN.Connection({ + privateKey: 'foo' + }); done(); }, @@ -113,15 +116,15 @@ exports['LAN.Connection.prototype.exec'] = { done(); }, - // closed(test) { - // test.expect(1); - // - // this.lanConnection.closed = true; - // this.lanConnection.exec(undefined, (error) => { - // test.equal(error.message, 'Remote SSH connection has already been closed'); - // test.done(); - // }); - // }, + closed(test) { + test.expect(1); + + this.lanConnection.closed = true; + this.lanConnection.exec(undefined, (error) => { + test.equal(error.message, 'Remote SSH connection has already been closed'); + test.done(); + }); + }, emitClose(test) { test.expect(2);