diff --git a/bin/tessel-2.js b/bin/tessel-2.js index f1d69e56..516fcbb0 100755 --- a/bin/tessel-2.js +++ b/bin/tessel-2.js @@ -288,7 +288,7 @@ makeCommand('ap') }) .option('security', { abbr: 's', - help: 'Encryption to use on network (i.e. WEP, WPA, PSK).' + help: 'Encryption to use on network (i.e. wep, psk, psk2, wpa, wpa2).' }) .option('trigger', { position: 1, diff --git a/lib/tessel/access-point.js b/lib/tessel/access-point.js index 8d5b0c5f..ca89211d 100644 --- a/lib/tessel/access-point.js +++ b/lib/tessel/access-point.js @@ -64,46 +64,65 @@ Tessel.prototype.createAccessPoint = function(opts) { var ssid = opts.ssid; var password = opts.pass; var security = opts.security; + var securityOptions = ['none', 'wep', 'psk', 'psk2', 'wpa', 'wpa2']; - return new Promise(function(resolve, reject) { - if (!ssid) { - return reject(new Error('Invalid credentials. Must set ssid')); - } + if (!ssid) { + return Promise.reject(new Error('Invalid credentials. Must set ssid')); + } - if (security && !password) { - return reject(new Error('Invalid credentials. Must set a password with security option')); - } + if (security && !password) { + return Promise.reject(new Error('Invalid credentials. Must set a password with security option')); + } - if (password && !security) { - security = 'psk2'; - } + if (security && securityOptions.indexOf(security) < 0) { + return Promise.reject(new Error(security + ' is not a valid security option. Please choose on of the following: ' + securityOptions.join(', '))); + } + + if (password && !security) { + security = 'psk2'; + } + + if (password && security) { + logs.info('Setting Access Point with SSID:', ssid, 'and password:', password, 'and security mode:', security); + } else if (!password && !security) { + security = 'none'; + logs.info('Setting Access Point with SSID:', ssid); + } + + var setSSID = function() { + return self.connection.exec(commands.setAccessPointSSID(ssid)); + }; + + var setAccessPointPassword = function() { + return self.connection.exec(commands.setAccessPointPassword(password)); + }; + + var setAccessPointSecurity = function() { + self.connection.exec(commands.setAccessPointSecurity(security)); + }; - if (password && security) { - logs.info('Setting Access Point with SSID:', ssid, 'and password:', password, 'and security mode:', security); - } else if (!password && !security) { - logs.info('Setting Access Point with SSID:', ssid); + var turnAccessPointOn = function() { + return self.connection.exec(commands.turnAccessPointOn()); + }; + + var commitAndClosePromise = function() { + return new Promise(function(resolve) { + commitAndClose(self, resolve); + }); + }; + + var setup = function() { + if (password) { + return setSSID(ssid) + .then(setAccessPointPassword) + .then(setAccessPointSecurity); + } else { + return setSSID(ssid) + .then(setAccessPointSecurity); } + }; - return self.connection.exec(commands.setAccessPointSSID(ssid)) - .then(function() { - if (password) { - return self.connection.exec(commands.setAccessPointPassword(password)) - .then(function() { - if (security) { - return self.connection.exec(commands.setAccessPointSecurity(security)) - .then(function() { - return self.connection.exec(commands.turnAccessPointOn()) - .then(commitAndClose.bind(null, self, resolve)); - }); - } else { - return self.connection.exec(commands.turnAccessPointOn()) - .then(commitAndClose.bind(null, self, resolve)); - } - }); - } else { - return self.connection.exec(commands.turnAccessPointOn()) - .then(commitAndClose.bind(null, self, resolve)); - } - }); - }); + return setup() + .then(turnAccessPointOn) + .then(commitAndClosePromise); }; diff --git a/lib/tessel/wifi.js b/lib/tessel/wifi.js index 9173f068..988a1810 100644 --- a/lib/tessel/wifi.js +++ b/lib/tessel/wifi.js @@ -80,6 +80,10 @@ Tessel.prototype.connectToNetwork = function(opts) { // Then set the password return self.connection.exec(commands.setNetworkPassword(password)); }) + .then(function() { + // Then set the encryption + return self.connection.exec(commands.setNetworkEncryption('psk2')); + }) .then(function() { // Then make sure wireless is enabled return self.connection.exec(commands.turnOnWifi(true)); diff --git a/test/unit/access-point.js b/test/unit/access-point.js index e77e8c1d..28b33314 100644 --- a/test/unit/access-point.js +++ b/test/unit/access-point.js @@ -55,7 +55,7 @@ exports['Tessel.prototype.createAccessPoint'] = { }, noPasswordNoSecurity: function(test) { - test.expect(7); + test.expect(8); var self = this; var creds = { ssid: 'test', @@ -74,11 +74,12 @@ exports['Tessel.prototype.createAccessPoint'] = { .then(function() { test.equal(self.setAccessPointSSID.callCount, 1); test.equal(self.setAccessPointPassword.callCount, 0); - test.equal(self.setAccessPointSecurity.callCount, 0); + test.equal(self.setAccessPointSecurity.callCount, 1); test.equal(self.reconnectWifi.callCount, 1); test.equal(self.reconnectDnsmasq.callCount, 1); test.equal(self.reconnectDhcp.callCount, 1); test.ok(self.setAccessPointSSID.lastCall.calledWith(creds.ssid)); + test.ok(self.setAccessPointSecurity.lastCall.calledWith('none')); test.done(); }) .catch(function(error) { diff --git a/test/unit/wifi.js b/test/unit/wifi.js index 6a843133..b16fdaca 100644 --- a/test/unit/wifi.js +++ b/test/unit/wifi.js @@ -127,6 +127,7 @@ module.exports['Tessel.prototype.connectToNetwork'] = { this.logsInfo = sinon.stub(logs, 'info', function() {}); this.setNetworkSSID = sinon.spy(commands, 'setNetworkSSID'); this.setNetworkPassword = sinon.spy(commands, 'setNetworkPassword'); + this.setNetworkEncryption = sinon.spy(commands, 'setNetworkEncryption'); this.commitWirelessCredentials = sinon.spy(commands, 'commitWirelessCredentials'); this.reconnectWifi = sinon.spy(commands, 'reconnectWifi'); this.tessel = TesselSimulator(); @@ -140,13 +141,14 @@ module.exports['Tessel.prototype.connectToNetwork'] = { this.logsInfo.restore(); this.setNetworkSSID.restore(); this.setNetworkPassword.restore(); + this.setNetworkEncryption.restore(); this.commitWirelessCredentials.restore(); this.reconnectWifi.restore(); done(); }, noSSID: function(test) { var self = this; - test.expect(3); + test.expect(4); this.tessel.connectToNetwork({ ssid: undefined, password: 'fish' @@ -155,12 +157,13 @@ module.exports['Tessel.prototype.connectToNetwork'] = { test.ok(error); test.equal(self.setNetworkSSID.callCount, 0); test.equal(self.setNetworkPassword.callCount, 0); + test.equal(self.setNetworkEncryption.callCount, 0); test.done(); }); }, noPassword: function(test) { var self = this; - test.expect(3); + test.expect(4); this.tessel.connectToNetwork({ ssid: 'tank', password: undefined @@ -169,12 +172,13 @@ module.exports['Tessel.prototype.connectToNetwork'] = { test.ok(error); test.equal(self.setNetworkSSID.callCount, 0); test.equal(self.setNetworkPassword.callCount, 0); + test.equal(self.setNetworkEncryption.callCount, 0); test.done(); }); }, properCredentials: function(test) { var self = this; - test.expect(6); + test.expect(8); var creds = { ssid: 'tank', password: 'fish' @@ -191,10 +195,12 @@ module.exports['Tessel.prototype.connectToNetwork'] = { .then(function() { test.equal(self.setNetworkSSID.callCount, 1); test.equal(self.setNetworkPassword.callCount, 1); + test.equal(self.setNetworkEncryption.callCount, 1); test.equal(self.commitWirelessCredentials.callCount, 1); test.equal(self.reconnectWifi.callCount, 1); test.ok(self.setNetworkSSID.lastCall.calledWith(creds.ssid)); test.ok(self.setNetworkPassword.lastCall.calledWith(creds.password)); + test.ok(self.setNetworkEncryption.lastCall.calledWith('psk2')); test.done(); }) .catch(function(error) {