Skip to content

Commit

Permalink
LAN.Connection: ensure that ssh client stubs activate correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Jul 20, 2018
1 parent 847946e commit 3e98b9e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
8 changes: 6 additions & 2 deletions lib/lan-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,28 @@ 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
this.authorized = true;
// 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);
});
};

Expand Down
31 changes: 17 additions & 14 deletions test/unit/lan-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand All @@ -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);
Expand Down

0 comments on commit 3e98b9e

Please # to comment.