From 141fa9be6da108d7189ff7e092f3b5b186dc79a8 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Mon, 1 Dec 2014 11:41:32 -0400 Subject: [PATCH] Fix and improve connection param parsing function --- lib/connection/connection.coffee | 10 ++++++---- lib/connection/connection.spec.coffee | 17 ++++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/connection/connection.coffee b/lib/connection/connection.coffee index 9998c5f8..f4c2dddf 100644 --- a/lib/connection/connection.coffee +++ b/lib/connection/connection.coffee @@ -3,7 +3,7 @@ isOnline = require('is-online') CONNECTION_PARAMETERS = [ 'network' - 'wifiEssid' + 'wifiSsid' 'wifiKey' ] @@ -12,15 +12,17 @@ CONNECTION_PARAMETERS = [ exports.isOnline = isOnline validateEthernetConnectionParameters = (parameters = {}) -> - return if not parameters.wifiEssid? and not parameters.wifiKey? + return if not parameters.wifiSsid? 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') + return if parameters.wifiSsid? and parameters.wifiKey? + return new Error('You have to provide an ssid and key if network is wifi') exports.parseConnectionParameters = (parameters = {}, callback) -> parameters = _.pick(parameters, CONNECTION_PARAMETERS) + parameters = _.omit parameters, (value) -> + return not value? if parameters.network is 'ethernet' error = validateEthernetConnectionParameters(parameters) diff --git a/lib/connection/connection.spec.coffee b/lib/connection/connection.spec.coffee index 3e9bde5f..b69e938d 100644 --- a/lib/connection/connection.spec.coffee +++ b/lib/connection/connection.spec.coffee @@ -12,17 +12,21 @@ CONNECTION_PARAMETERS = hello: 'world' validWifi: network: 'wifi' - wifiEssid: 'myEssid' + wifiSsid: 'mySsid' wifiKey: 'mySecret' ethernetAndWifiOptions: network: 'ethernet' - wifiEssid: 'myEssid' + wifiSsid: 'mySsid' wifiKey: 'mySecret' + ethernetAndUndefinedWifi: + network: 'ethernet' + wifiSsid: undefined + wifiKey: undefined wifiWithoutOptions: network: 'wifi' unknownWithOptions: network: 'foobar' - wifiEssid: 'myEssid' + wifiSsid: 'mySsid' wifiKey: 'mySecret' unknownWithoutOptions: network: 'foobar' @@ -109,6 +113,13 @@ describe 'Connection:', -> params = CONNECTION_PARAMETERS.ethernetAndWifiOptions checkParamsFailure(params, done) + it 'should discard undefined wifi related options', (done) -> + params = CONNECTION_PARAMETERS.ethernetAndUndefinedWifi + connection.parseConnectionParameters params, (error, result) -> + expect(error).to.not.exist + expect(result).to.deep.equal(network: 'ethernet') + done() + describe 'if network is wifi', -> it 'should succeed if has options', (done) ->