diff --git a/lib/connection/connection.coffee b/lib/connection/connection.coffee index 11ec1c4b..9998c5f8 100644 --- a/lib/connection/connection.coffee +++ b/lib/connection/connection.coffee @@ -1,6 +1,33 @@ +_ = require('lodash') isOnline = require('is-online') +CONNECTION_PARAMETERS = [ + 'network' + 'wifiEssid' + 'wifiKey' +] + # A wrapper around isOnline in order # to be able to stub it with Sinon - exports.isOnline = isOnline + +validateEthernetConnectionParameters = (parameters = {}) -> + return if not parameters.wifiEssid? and not parameters.wifiKey? + return new Error('You can only use wifi options if network is wifi') + +validateWifiConnectionParameters = (parameters = {}) -> + return if parameters.wifiEssid? and parameters.wifiKey? + return new Error('You have to provide an essid and key if network is wifi') + +exports.parseConnectionParameters = (parameters = {}, callback) -> + parameters = _.pick(parameters, CONNECTION_PARAMETERS) + + if parameters.network is 'ethernet' + error = validateEthernetConnectionParameters(parameters) + return callback(error, parameters) + + else if parameters.network is 'wifi' + error = validateWifiConnectionParameters(parameters) + return callback(error, parameters) + + return callback(new Error('Unknown network type')) diff --git a/lib/connection/connection.spec.coffee b/lib/connection/connection.spec.coffee index 12f02979..3e9bde5f 100644 --- a/lib/connection/connection.spec.coffee +++ b/lib/connection/connection.spec.coffee @@ -1,7 +1,32 @@ expect = require('chai').expect +_ = require('lodash') sinon = require('sinon') connection = require('./connection') +CONNECTION_PARAMETERS = + validEthernet: + network: 'ethernet' + validEthernetPlusExtra: + network: 'ethernet' + foo: 'bar' + hello: 'world' + validWifi: + network: 'wifi' + wifiEssid: 'myEssid' + wifiKey: 'mySecret' + ethernetAndWifiOptions: + network: 'ethernet' + wifiEssid: 'myEssid' + wifiKey: 'mySecret' + wifiWithoutOptions: + network: 'wifi' + unknownWithOptions: + network: 'foobar' + wifiEssid: 'myEssid' + wifiKey: 'mySecret' + unknownWithoutOptions: + network: 'foobar' + describe 'Connection:', -> describe '#isOnline()', -> @@ -34,3 +59,72 @@ describe 'Connection:', -> expect(isOnline).to.not.exist done() , new Error() + + describe '#parseConnectionParameters()', -> + + checkParamsSuccess = (params, done) -> + connection.parseConnectionParameters params, (error) -> + expect(error).to.not.exist + done() + + checkParamsFailure = (params, done) -> + connection.parseConnectionParameters params, (error) -> + expect(error).to.be.an.instanceof(Error) + done() + + it 'should fail is parameters is empty', (done) -> + checkParamsFailure({}, done) + + it 'should fail is parameters is not valid', (done) -> + for input in [ + undefined + null + [ 1, 2 ] + [] + 'string' + true + ] + checkParamsFailure(input, _.noop) + done() + + describe 'if it succeeds', -> + + it 'should pass the parameters as the second argument', -> + params = CONNECTION_PARAMETERS.validEthernet + connection.parseConnectionParameters params, (error, parameters) -> + expect(parameters).to.deep.equal(params) + + it 'should discard extra parameters', -> + params = CONNECTION_PARAMETERS.validEthernetPlusExtra + connection.parseConnectionParameters params, (error, parameters) -> + expect(parameters).to.deep.equal(CONNECTION_PARAMETERS.validEthernet) + + describe 'if network is ethernet', -> + + it 'should succeed if no wifi options', (done) -> + params = CONNECTION_PARAMETERS.validEthernet + checkParamsSuccess(params, done) + + it 'should fail if it has wifi options', (done) -> + params = CONNECTION_PARAMETERS.ethernetAndWifiOptions + checkParamsFailure(params, done) + + describe 'if network is wifi', -> + + it 'should succeed if has options', (done) -> + params = CONNECTION_PARAMETERS.validWifi + checkParamsSuccess(params, done) + + it 'should fail if missing options', (done) -> + params = CONNECTION_PARAMETERS.wifiWithoutOptions + checkParamsFailure(params, done) + + describe 'if network is unknown', -> + + it 'should fail with options', (done) -> + params = CONNECTION_PARAMETERS.unknownWithOptions + checkParamsFailure(params, done) + + it 'should fail without options', (done) -> + params = CONNECTION_PARAMETERS.unknownWithoutOptions + checkParamsFailure(params, done)