Fix and improve connection param parsing function

This commit is contained in:
Juan Cruz Viotti 2014-12-01 11:41:32 -04:00
parent a4db6ce572
commit 141fa9be6d
2 changed files with 20 additions and 7 deletions

View File

@ -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)

View File

@ -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) ->