Skip to content

Commit

Permalink
fix(access-point): creates boilerplate for new access point when requ…
Browse files Browse the repository at this point in the history
…ired
  • Loading branch information
HipsterBrown committed Nov 16, 2015
1 parent c3069ea commit 3a96d97
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 44 deletions.
27 changes: 24 additions & 3 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,30 @@ controller.connectToNetwork = function(opts) {

controller.createAccessPoint = function(opts) {
opts.authorized = true;
return controller.standardTesselCommand(opts, function(tessel) {
return tessel.createAccessPoint(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) {
reject(new Error('Invalid credentials. Must set ssid'));
}

if (security && !password) {
reject(new Error('Invalid credentials. Must set a password with security option'));
}

if (security && securityOptions.indexOf(security) < 0) {
reject(new Error(security + ' is not a valid security option. Please choose on of the following: ' + securityOptions.join(', ')));
}
resolve();
})
.then(function() {
return controller.standardTesselCommand(opts, function(tessel) {
return tessel.createAccessPoint(opts);
});
});
};

controller.enableAccessPoint = function(opts) {
Expand Down
48 changes: 27 additions & 21 deletions lib/tessel/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,8 @@ 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'];

var setupAccessPoint = function() {

if (!ssid) {
return Promise.reject(new Error('Invalid credentials. Must set ssid'));
}

if (security && !password) {
return Promise.reject(new Error('Invalid credentials. Must set a password with security option'));
}

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';
}
Expand Down Expand Up @@ -130,17 +116,37 @@ Tessel.prototype.createAccessPoint = function(opts) {
};


return this.connection.exec(commands.getAccessPointSSID())
return self.connection.exec(commands.getAccessPointSSID())
.then(function(remoteProcess) {


return self.receive(remoteProcess).then(function(data) {
return self.receive(remoteProcess).then(function() {
return setupAccessPoint();
}).catch(function(error) {

// TODO: @nick, this is where you need to create the NEW
// network iface, then call setupAccessPoint()
console.log('error: ', error.message);
}).catch(function() {

var setAccessPoint = function() {
return self.connection.exec(commands.setAccessPoint())
.then(self.connection.exec.bind(self.connection, commands.setAccessPointDevice()))
.then(self.connection.exec.bind(self.connection, commands.setAccessPointNetwork()))
.then(self.connection.exec.bind(self.connection, commands.setAccessPointMode()));
};

var setLanNetwork = function() {
return self.connection.exec(commands.setLanNetwork())
.then(self.connection.exec.bind(self.connection, commands.setLanNetworkIfname()))
.then(self.connection.exec.bind(self.connection, commands.setLanNetworkProto()))
.then(self.connection.exec.bind(self.connection, commands.setLanNetworkIP()))
.then(self.connection.exec.bind(self.connection, commands.setLanNetworkNetmask()));
};

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

return setAccessPoint()
.then(setLanNetwork)
.then(commitNetwork)
.then(setupAccessPoint);
});
});
};
30 changes: 30 additions & 0 deletions lib/tessel/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,39 @@ module.exports.sysupgrade = function(path) {
module.exports.getMemoryInfo = function() {
return ['cat', '/proc/meminfo'];
};
module.exports.setLanNetwork = function() {
return ['uci', 'set', 'network.lan=interface'];
};
module.exports.setLanNetworkIfname = function() {
return ['uci', 'set', 'network.lan.ifname=wlan0'];
};
module.exports.setLanNetworkProto = function() {
return ['uci', 'set', 'network.lan.proto=static'];
};
module.exports.setLanNetworkIP = function() {
return ['uci', 'set', 'network.lan.ipaddr=192.168.1.101'];
};
module.exports.setLanNetworkNetmask = function() {
return ['uci', 'set', 'network.lan.netmask=255.255.255.0'];
};
module.exports.commitNetwork = function() {
return ['uci', 'commit', 'network'];
};
module.exports.getAccessPointSSID = function() {
return ['uci', 'get', 'wireless.@wifi-iface[1].ssid'];
};
module.exports.setAccessPoint = function() {
return ['uci', 'add', 'wireless', 'wifi-iface'];
};
module.exports.setAccessPointDevice = function() {
return ['uci', 'set', 'wireless.@wifi-iface[1].device=radio0'];
};
module.exports.setAccessPointNetwork = function() {
return ['uci', 'set', 'wireless.@wifi-iface[1].network=lan'];
};
module.exports.setAccessPointMode = function() {
return ['uci', 'set', 'wireless.@wifi-iface[1].mode=ap'];
};
module.exports.setAccessPointSSID = function(ssid) {
return ['uci', 'set', 'wireless.@wifi-iface[1].ssid=' + ssid];
};
Expand Down
85 changes: 65 additions & 20 deletions test/unit/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ exports['Tessel.prototype.createAccessPoint'] = {
this.sandbox = sinon.sandbox.create();
this.createAccessPoint = this.sandbox.spy(Tessel.prototype, 'createAccessPoint');
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.setLanNetwork = this.sandbox.spy(commands, 'setLanNetwork');
this.setLanNetworkIfname = this.sandbox.spy(commands, 'setLanNetworkIfname');
this.setLanNetworkProto = this.sandbox.spy(commands, 'setLanNetworkProto');
this.setLanNetworkIP = this.sandbox.spy(commands, 'setLanNetworkIP');
this.setLanNetworkNetmask = this.sandbox.spy(commands, 'setLanNetworkNetmask');
this.commitNetwork = this.sandbox.spy(commands, 'commitNetwork');
this.setAccessPoint = this.sandbox.spy(commands, 'setAccessPoint');
this.getAccessPointSSID = this.sandbox.spy(commands, 'getAccessPointSSID');
this.setAccessPointDevice = this.sandbox.spy(commands, 'setAccessPointDevice');
this.setAccessPointNetwork = this.sandbox.spy(commands, 'setAccessPointNetwork');
this.setAccessPointMode = this.sandbox.spy(commands, 'setAccessPointMode');
this.setAccessPointSSID = this.sandbox.spy(commands, 'setAccessPointSSID');
this.setAccessPointPassword = this.sandbox.spy(commands, 'setAccessPointPassword');
this.setAccessPointSecurity = this.sandbox.spy(commands, 'setAccessPointSecurity');
Expand All @@ -28,34 +39,65 @@ exports['Tessel.prototype.createAccessPoint'] = {
done();
},

noSSID: function(test) {
test.expect(1);
newAccessPoint: function(test) {
test.expect(20);
var self = this;
var creds = {
ssid: 'test',
pass: 'test-password',
security: 'psk2'
};

this.tessel.createAccessPoint({
ssid: undefined
})
.catch(function(error) {
test.ok(error);
test.done();
// Test is expecting two closes...;
self.tessel._rps.on('control', function() {
setImmediate(function() {
self.tessel._rps.emit('close');
});
},
});

// tessel.receive is called twice but needs to be rejected the first time
var count = 0;
this.sandbox.stub(this.tessel, 'receive', function() {
if (count === 0) {
count++;
return Promise.reject(new Error('uci: Entry not found'));
} else {
return Promise.resolve();
}
});

noPasswordWithSecurity: function(test) {
test.expect(1);
this.tessel.createAccessPoint(creds)
.then(function() {
test.equal(self.getAccessPointSSID.callCount, 1);
test.equal(self.setAccessPoint.callCount, 1);
test.equal(self.setAccessPointDevice.callCount, 1);
test.equal(self.setAccessPointNetwork.callCount, 1);
test.equal(self.setAccessPointMode.callCount, 1);
test.equal(self.setAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointPassword.callCount, 1);
test.equal(self.setAccessPointSecurity.callCount, 1);
test.equal(self.setLanNetwork.callCount, 1);
test.equal(self.setLanNetworkIfname.callCount, 1);
test.equal(self.setLanNetworkProto.callCount, 1);
test.equal(self.setLanNetworkIP.callCount, 1);
test.equal(self.setLanNetworkNetmask.callCount, 1);
test.equal(self.commitNetwork.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.setAccessPointPassword.lastCall.calledWith(creds.pass));
test.ok(self.setAccessPointSecurity.lastCall.calledWith(creds.security));

this.tessel.createAccessPoint({
ssid: 'test',
password: undefined,
security: 'psk2'
test.done();
})
.catch(function(error) {
test.ok(error);
test.done();
test.fail(error);
});
},

noPasswordNoSecurity: function(test) {
test.expect(8);
test.expect(9);
var self = this;
var creds = {
ssid: 'test',
Expand All @@ -72,6 +114,7 @@ exports['Tessel.prototype.createAccessPoint'] = {

this.tessel.createAccessPoint(creds)
.then(function() {
test.equal(self.getAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointPassword.callCount, 0);
test.equal(self.setAccessPointSecurity.callCount, 1);
Expand All @@ -88,7 +131,7 @@ exports['Tessel.prototype.createAccessPoint'] = {
},

properCredentials: function(test) {
test.expect(9);
test.expect(10);
var self = this;
var creds = {
ssid: 'test',
Expand All @@ -105,6 +148,7 @@ exports['Tessel.prototype.createAccessPoint'] = {

this.tessel.createAccessPoint(creds)
.then(function() {
test.equal(self.getAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointPassword.callCount, 1);
test.equal(self.setAccessPointSecurity.callCount, 1);
Expand All @@ -122,7 +166,7 @@ exports['Tessel.prototype.createAccessPoint'] = {
},

passwordNoSecurity: function(test) {
test.expect(9);
test.expect(10);
var self = this;
var creds = {
ssid: 'test',
Expand All @@ -139,6 +183,7 @@ exports['Tessel.prototype.createAccessPoint'] = {

this.tessel.createAccessPoint(creds)
.then(function() {
test.equal(self.getAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointSSID.callCount, 1);
test.equal(self.setAccessPointPassword.callCount, 1);
test.equal(self.setAccessPointSecurity.callCount, 1);
Expand Down
56 changes: 56 additions & 0 deletions test/unit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,59 @@ exports['Tessel.get'] = {
}.bind(this));
},
};

exports['controller.closeTesselConnections'] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.logsWarn = this.sandbox.stub(logs, 'warn', function() {});
this.logsInfo = this.sandbox.stub(logs, 'info', function() {});
this.logsBasic = this.sandbox.stub(logs, 'basic', function() {});

done();
},

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

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

controller.createAccessPoint({
ssid: undefined
})
.catch(function(error) {
test.ok(error);
test.done();
});
},

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

controller.createAccessPoint({
ssid: 'test',
password: undefined,
security: 'psk2'
})
.catch(function(error) {
test.ok(error);
test.done();
});
},

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

controller.createAccessPoint({
ssid: 'test',
password: undefined,
security: 'reallySecure'
})
.catch(function(error) {
test.ok(error);
test.done();
});
},
};

0 comments on commit 3a96d97

Please sign in to comment.