Skip to content

Commit

Permalink
refactor(access-point): simpler, cleaner tests and source code
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Nov 16, 2015
1 parent 29ed2c8 commit 5b3baee
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
50 changes: 31 additions & 19 deletions lib/tessel/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@ var Tessel = require('./tessel'),
logs = require('../logs');

function commitAndClose(self, resolve) {
return self.connection.exec(commands.commitWirelessCredentials())
.then(function(remoteProcess) {
remoteProcess.once('close', function() {
return self.connection.exec(commands.reconnectWifi())
.then(function() {
return self.connection.exec(commands.reconnectDnsmasq())
.then(function() {
return self.connection.exec(commands.reconnectDhcp())
.then(function(remoteProcess) {
return self.receive(remoteProcess).then(function() {
logs.info('Access Point credentials set!');

self.connection.end()
.then(resolve);
});
});
});
});
});
var waitForClose = function(remoteProcess) {
return new Promise(function(resolve) {
remoteProcess.once('close', resolve);
});
};

var reconnectWifi = function() {
return self.connection.exec(commands.reconnectWifi());
};

var reconnectDnsmasq = function() {
return self.connection.exec(commands.reconnectDnsmasq());
};

var reconnectDhcp = function() {
return self.connection.exec(commands.reconnectDhcp());
};

var cleanup = function(remoteProcess) {
return self.receive(remoteProcess).then(function() {
logs.info('Access Point credentials set!');
return self.connection.end();
});
};

return self.connection.exec(commands.commitWirelessCredentials())
.then(waitForClose)
.then(reconnectWifi)
.then(reconnectDnsmasq)
.then(reconnectDhcp)
.then(cleanup)
.then(resolve);
}

Tessel.prototype.enableAccessPoint = function() {
Expand Down
75 changes: 29 additions & 46 deletions test/unit/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ var TesselSimulator = require('../common/tessel-simulator');

exports['Tessel.prototype.createAccessPoint'] = {
setUp: function(done) {
this.createAccessPoint = sinon.spy(Tessel.prototype, 'createAccessPoint');
this.logsInfo = sinon.stub(logs, 'info', function() {});
this.setAccessPointSSID = sinon.spy(commands, 'setAccessPointSSID');
this.setAccessPointPassword = sinon.spy(commands, 'setAccessPointPassword');
this.setAccessPointSecurity = sinon.spy(commands, 'setAccessPointSecurity');
this.commitWirelessCredentials = sinon.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = sinon.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = sinon.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = sinon.spy(commands, 'reconnectDhcp');
this.sandbox = sinon.sandbox.create();
this.createAccessPoint = this.sandbox.spy(Tessel.prototype, 'createAccessPoint');
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.setAccessPointSSID = this.sandbox.spy(commands, 'setAccessPointSSID');
this.setAccessPointPassword = this.sandbox.spy(commands, 'setAccessPointPassword');
this.setAccessPointSecurity = this.sandbox.spy(commands, 'setAccessPointSecurity');
this.commitWirelessCredentials = this.sandbox.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = this.sandbox.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = this.sandbox.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = this.sandbox.spy(commands, 'reconnectDhcp');

this.tessel = TesselSimulator();

Expand All @@ -23,15 +24,7 @@ exports['Tessel.prototype.createAccessPoint'] = {

tearDown: function(done) {
this.tessel.mockClose();
this.createAccessPoint.restore();
this.logsInfo.restore();
this.setAccessPointSSID.restore();
this.setAccessPointPassword.restore();
this.setAccessPointSecurity.restore();
this.commitWirelessCredentials.restore();
this.reconnectWifi.restore();
this.reconnectDnsmasq.restore();
this.reconnectDhcp.restore();
this.sandbox.restore();
done();
},

Expand Down Expand Up @@ -164,13 +157,14 @@ exports['Tessel.prototype.createAccessPoint'] = {

exports['Tessel.prototype.enableAccessPoint'] = {
setUp: function(done) {
this.enableAccessPoint = sinon.spy(Tessel.prototype, 'enableAccessPoint');
this.logsInfo = sinon.stub(logs, 'info', function() {});
this.turnAccessPointOn = sinon.spy(commands, 'turnAccessPointOn');
this.commitWirelessCredentials = sinon.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = sinon.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = sinon.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = sinon.spy(commands, 'reconnectDhcp');
this.sandbox = sinon.sandbox.create();
this.enableAccessPoint = this.sandbox.spy(Tessel.prototype, 'enableAccessPoint');
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.turnAccessPointOn = this.sandbox.spy(commands, 'turnAccessPointOn');
this.commitWirelessCredentials = this.sandbox.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = this.sandbox.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = this.sandbox.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = this.sandbox.spy(commands, 'reconnectDhcp');

this.tessel = TesselSimulator();

Expand All @@ -179,13 +173,7 @@ exports['Tessel.prototype.enableAccessPoint'] = {

tearDown: function(done) {
this.tessel.mockClose();
this.enableAccessPoint.restore();
this.logsInfo.restore();
this.turnAccessPointOn.restore();
this.commitWirelessCredentials.restore();
this.reconnectWifi.restore();
this.reconnectDnsmasq.restore();
this.reconnectDhcp.restore();
this.sandbox.restore();
done();
},

Expand Down Expand Up @@ -216,13 +204,14 @@ exports['Tessel.prototype.enableAccessPoint'] = {

exports['Tessel.prototype.disableAccessPoint'] = {
setUp: function(done) {
this.disableAccessPoint = sinon.spy(Tessel.prototype, 'disableAccessPoint');
this.logsInfo = sinon.stub(logs, 'info', function() {});
this.turnAccessPointOff = sinon.spy(commands, 'turnAccessPointOff');
this.commitWirelessCredentials = sinon.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = sinon.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = sinon.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = sinon.spy(commands, 'reconnectDhcp');
this.sandbox = sinon.sandbox.create();
this.disableAccessPoint = this.sandbox.spy(Tessel.prototype, 'disableAccessPoint');
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.turnAccessPointOff = this.sandbox.spy(commands, 'turnAccessPointOff');
this.commitWirelessCredentials = this.sandbox.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = this.sandbox.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = this.sandbox.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = this.sandbox.spy(commands, 'reconnectDhcp');

this.tessel = TesselSimulator();

Expand All @@ -231,13 +220,7 @@ exports['Tessel.prototype.disableAccessPoint'] = {

tearDown: function(done) {
this.tessel.mockClose();
this.disableAccessPoint.restore();
this.logsInfo.restore();
this.turnAccessPointOff.restore();
this.commitWirelessCredentials.restore();
this.reconnectWifi.restore();
this.reconnectDnsmasq.restore();
this.reconnectDhcp.restore();
this.sandbox.restore();
done();
},

Expand Down

0 comments on commit 5b3baee

Please sign in to comment.