Skip to content

Commit

Permalink
Tests: Make ssh2's Client class stubbable. Add tests for explicit clo…
Browse files Browse the repository at this point in the history
…sed = true, implicit close event
  • Loading branch information
rwaldron committed Oct 30, 2015
1 parent 83e8d13 commit 91e8724
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/lan_connection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Client = require('ssh2').Client,
var ssh = require('ssh2'),
fs = require('fs'),
Promise = require('bluebird'),
mdns = require('mdns-js'),
Expand Down Expand Up @@ -76,7 +76,7 @@ LAN.Connection.prototype.open = function() {
var self = this;
return new Promise(function(resolve) {
// Create a new SSH client connection object
self.ssh = new Client();
self.ssh = new ssh.Client();
// Open up an SSH Connection
self.ssh.on('ready', function() {
debug('Device ready:', self.host);
Expand Down
53 changes: 51 additions & 2 deletions test/unit/lan_connection.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var sinon = require('sinon');
var Emitter = require('events').EventEmitter;
var LAN = require('../../lib/lan_connection').LAN;
var fs = require('fs');
var LAN = require('../../lib/lan_connection').LAN;
var Tessel = require('../../lib/tessel/tessel');
var ssh = require('ssh2');
var mdns = require('mdns-js');
var sinon = require('sinon');

exports['LAN.Connection'] = {
setUp: function(done) {
Expand Down Expand Up @@ -51,6 +53,53 @@ exports['LAN.Scanner'] = {
},
};

exports['LAN.Connection.prototype.exec'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.isProvisioned = this.sandbox.stub(Tessel, 'isProvisioned', function() {
return false;
});

this.Client = this.sandbox.spy(ssh, 'Client');

this.lanConnection = new LAN.Connection({});

done();
},

tearDown: function(done) {
this.sandbox.restore();
done();
},

closed: function(test) {
test.expect(1);

this.lanConnection.closed = true;
this.lanConnection.exec()
.catch(function(error) {
test.equal(error.message, 'Remote SSH connection has already been closed');
test.done();
});
},

emitClose: function(test) {
test.expect(2);

this.lanConnection.open()
.then(function() {
test.equal(this.Client.callCount, 1);

this.lanConnection.ssh.emit('close');
this.lanConnection.exec()
.catch(function(error) {
test.equal(error.message, 'Remote SSH connection has already been closed');
test.done();
});
}.bind(this));
},
};

exports['LAN.Scanner.prototype.start'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
Expand Down

0 comments on commit 91e8724

Please sign in to comment.