mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-21 22:47:48 +00:00
Fix and improve connection param parsing function
This commit is contained in:
parent
a4db6ce572
commit
141fa9be6d
@ -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)
|
||||
|
@ -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) ->
|
||||
|
Loading…
Reference in New Issue
Block a user