Integrate with connection.ConnectionParams

This commit is contained in:
Juan Cruz Viotti 2015-01-08 10:55:01 -03:00
parent f000509995
commit 0452916a87
3 changed files with 12 additions and 148 deletions

View File

@ -9,15 +9,23 @@ log = require('../log/log')
permissions = require('../permissions/permissions')
errors = require('../errors/errors')
cache = require('../cache/cache')
resin = require('../resin')
exports.download = (params, options) ->
networkParams =
# TODO: Evaluate if ConnectionParams is a good name for this object
# as it includes an application id, which is not connection related
# Maybe we should move appId outside this class?
connectionParams = new resin.connection.ConnectionParams
network: options.network
wifiSsid: options.ssid
wifiKey: options.key
appId: params.id
# TODO: Change cache.generateCacheName to accept a ConnectionParams instance
# to avoid the complication of having to omit it from the object and pass
# as another parameter
fileName = cache.generateCacheName(params.id, _.omit(connectionParams, 'appId'))
fileName = cache.generateCacheName(params.id, networkParams)
outputFile = options.output or path.join(resin.settings.get('directories.os'), fileName)
async.waterfall [
@ -29,15 +37,11 @@ exports.download = (params, options) ->
return callback(error)
(callback) ->
connection.parseConnectionParameters(networkParams, callback)
(parameters, callback) ->
parameters.appId = params.id
bar = null
received = 0
resin.models.os.download parameters, outputFile, callback, (state) ->
resin.models.os.download connectionParams, outputFile, callback, (state) ->
# TODO: Allow quieting this progress bar
bar ?= new ProgressBar 'Downloading device OS [:bar] :percent :etas',

View File

@ -1,30 +0,0 @@
_ = require('lodash')
CONNECTION_PARAMETERS = [
'network'
'wifiSsid'
'wifiKey'
]
validateEthernetConnectionParameters = (parameters = {}) ->
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.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)
return callback(error, parameters)
else if parameters.network is 'wifi'
error = validateWifiConnectionParameters(parameters)
return callback(error, parameters)
return callback(new Error('Unknown network type'))

View File

@ -1,110 +0,0 @@
expect = require('chai').expect
_ = require('lodash')
sinon = require('sinon')
connection = require('./connection')
describe 'Connection:', ->
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 =
network: 'ethernet'
connection.parseConnectionParameters params, (error, parameters) ->
expect(parameters).to.deep.equal(params)
it 'should discard extra parameters', ->
params =
network: 'ethernet'
foo: 'bar'
hello: 'world'
connection.parseConnectionParameters params, (error, parameters) ->
expect(parameters).to.deep.equal(network: 'ethernet')
describe 'if network is ethernet', ->
it 'should succeed if no wifi options', (done) ->
params =
network: 'ethernet'
checkParamsSuccess(params, done)
it 'should fail if it has wifi options', (done) ->
params =
network: 'ethernet'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
checkParamsFailure(params, done)
it 'should discard undefined wifi related options', (done) ->
params =
network: 'ethernet'
wifiSsid: undefined
wifiKey: undefined
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) ->
params =
network: 'wifi'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
checkParamsSuccess(params, done)
it 'should fail if missing options', (done) ->
params =
network: 'wifi'
checkParamsFailure(params, done)
describe 'if network is unknown', ->
it 'should fail with options', (done) ->
params =
network: 'foobar'
wifiSsid: 'mySsid'
wifiKey: 'mySecret'
checkParamsFailure(params, done)
it 'should fail without options', (done) ->
params =
network: 'foobar'
checkParamsFailure(params, done)