2017-11-01 06:47:48 +00:00
|
|
|
prepare = require './lib/prepare'
|
|
|
|
Promise = require 'bluebird'
|
2018-10-18 18:52:35 +00:00
|
|
|
balenaAPI = require './lib/mocked-balena-api'
|
2017-11-01 06:47:48 +00:00
|
|
|
fs = Promise.promisifyAll(require('fs'))
|
|
|
|
|
|
|
|
m = require 'mochainon'
|
|
|
|
{ expect } = m.chai
|
|
|
|
{ stub, spy } = m.sinon
|
|
|
|
|
|
|
|
DB = require('../src/db')
|
|
|
|
Config = require('../src/config')
|
|
|
|
DeviceState = require('../src/device-state')
|
|
|
|
APIBinder = require('../src/api-binder')
|
|
|
|
|
2018-05-11 09:29:15 +00:00
|
|
|
initModels = (filename) ->
|
2017-11-01 06:47:48 +00:00
|
|
|
prepare()
|
|
|
|
@db = new DB()
|
2018-05-11 09:29:15 +00:00
|
|
|
@config = new Config({ @db, configPath: filename })
|
2017-11-01 06:47:48 +00:00
|
|
|
@eventTracker = {
|
|
|
|
track: stub().callsFake (ev, props) ->
|
|
|
|
console.log(ev, props)
|
|
|
|
}
|
|
|
|
@deviceState = new DeviceState({ @db, @config, @eventTracker })
|
|
|
|
@apiBinder = new APIBinder({ @db, @config, @eventTracker, @deviceState })
|
|
|
|
@db.init()
|
|
|
|
.then =>
|
|
|
|
@config.init()
|
|
|
|
.then =>
|
|
|
|
@apiBinder.initClient() # Initializes the clients but doesn't trigger provisioning
|
|
|
|
|
|
|
|
mockProvisioningOpts = {
|
|
|
|
apiEndpoint: 'http://0.0.0.0:3000'
|
|
|
|
uuid: 'abcd'
|
|
|
|
deviceApiKey: 'averyvalidkey'
|
|
|
|
provisioningApiKey: 'anotherveryvalidkey'
|
|
|
|
apiTimeout: 30000
|
|
|
|
}
|
|
|
|
|
|
|
|
describe 'APIBinder', ->
|
|
|
|
before ->
|
2018-10-18 18:52:35 +00:00
|
|
|
spy(balenaAPI.balenaBackend, 'registerHandler')
|
|
|
|
@server = balenaAPI.listen(3000)
|
2017-11-01 06:47:48 +00:00
|
|
|
after ->
|
2018-10-18 18:52:35 +00:00
|
|
|
balenaAPI.balenaBackend.registerHandler.restore()
|
2017-11-01 06:47:48 +00:00
|
|
|
try
|
|
|
|
@server.close()
|
|
|
|
|
|
|
|
# We do not support older OS versions anymore, so we only test this case
|
|
|
|
describe 'on an OS with deviceApiKey support', ->
|
|
|
|
before ->
|
2018-05-11 09:29:15 +00:00
|
|
|
initModels.call(this, '/config-apibinder.json')
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
it 'provisions a device', ->
|
|
|
|
promise = @apiBinder.provisionDevice()
|
|
|
|
expect(promise).to.be.fulfilled
|
|
|
|
.then =>
|
2018-10-18 18:52:35 +00:00
|
|
|
expect(balenaAPI.balenaBackend.registerHandler).to.be.calledOnce
|
|
|
|
balenaAPI.balenaBackend.registerHandler.reset()
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(@eventTracker.track).to.be.calledWith('Device bootstrap success')
|
|
|
|
|
|
|
|
it 'deletes the provisioning key', ->
|
|
|
|
expect(@config.get('apiKey')).to.eventually.be.undefined
|
|
|
|
|
|
|
|
it 'sends the correct parameters when provisioning', ->
|
|
|
|
fs.readFileAsync('./test/data/config-apibinder.json')
|
|
|
|
.then(JSON.parse)
|
|
|
|
.then (conf) ->
|
2018-10-18 18:52:35 +00:00
|
|
|
expect(balenaAPI.balenaBackend.devices).to.deep.equal({
|
2017-11-01 06:47:48 +00:00
|
|
|
'1': {
|
|
|
|
id: 1
|
|
|
|
user: conf.userId
|
|
|
|
application: conf.applicationId
|
|
|
|
uuid: conf.uuid
|
|
|
|
device_type: conf.deviceType
|
|
|
|
api_key: conf.deviceApiKey
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
describe 'fetchDevice', ->
|
|
|
|
before ->
|
2018-05-11 09:29:15 +00:00
|
|
|
initModels.call(this, '/config-apibinder.json')
|
2017-11-01 06:47:48 +00:00
|
|
|
|
2018-10-18 18:52:35 +00:00
|
|
|
it 'gets a device by its uuid from the balena API', ->
|
2017-11-01 06:47:48 +00:00
|
|
|
# Manually add a device to the mocked API
|
2018-10-18 18:52:35 +00:00
|
|
|
balenaAPI.balenaBackend.devices[3] = {
|
2017-11-01 06:47:48 +00:00
|
|
|
id: 3
|
|
|
|
user: 'foo'
|
|
|
|
application: 1337
|
|
|
|
uuid: 'abcd'
|
|
|
|
device_type: 'intel-nuc'
|
|
|
|
api_key: 'verysecure'
|
|
|
|
}
|
|
|
|
@apiBinder.fetchDevice('abcd', 'someApiKey', 30000)
|
|
|
|
.then (theDevice) ->
|
2018-10-18 18:52:35 +00:00
|
|
|
expect(theDevice).to.deep.equal(balenaAPI.balenaBackend.devices[3])
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
describe '_exchangeKeyAndGetDevice', ->
|
|
|
|
before ->
|
2018-05-11 09:29:15 +00:00
|
|
|
initModels.call(this, '/config-apibinder.json')
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
it 'returns the device if it can fetch it with the deviceApiKey', ->
|
2018-10-18 18:52:35 +00:00
|
|
|
spy(balenaAPI.balenaBackend, 'deviceKeyHandler')
|
2017-11-01 06:47:48 +00:00
|
|
|
fetchDeviceStub = stub(@apiBinder, 'fetchDevice')
|
|
|
|
fetchDeviceStub.onCall(0).resolves({ id: 1 })
|
|
|
|
@apiBinder._exchangeKeyAndGetDevice(mockProvisioningOpts)
|
|
|
|
.then (device) =>
|
2018-10-18 18:52:35 +00:00
|
|
|
expect(balenaAPI.balenaBackend.deviceKeyHandler).to.not.be.called
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(device).to.deep.equal({ id: 1 })
|
|
|
|
expect(@apiBinder.fetchDevice).to.be.calledOnce
|
|
|
|
@apiBinder.fetchDevice.restore()
|
2018-10-18 18:52:35 +00:00
|
|
|
balenaAPI.balenaBackend.deviceKeyHandler.restore()
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
it 'throws if it cannot get the device with any of the keys', ->
|
2018-10-18 18:52:35 +00:00
|
|
|
spy(balenaAPI.balenaBackend, 'deviceKeyHandler')
|
2017-11-01 06:47:48 +00:00
|
|
|
stub(@apiBinder, 'fetchDevice').returns(Promise.resolve(null))
|
|
|
|
promise = @apiBinder._exchangeKeyAndGetDevice(mockProvisioningOpts)
|
|
|
|
promise.catch(->)
|
|
|
|
expect(promise).to.be.rejected
|
|
|
|
.then =>
|
2018-10-18 18:52:35 +00:00
|
|
|
expect(balenaAPI.balenaBackend.deviceKeyHandler).to.not.be.called
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(@apiBinder.fetchDevice).to.be.calledTwice
|
|
|
|
@apiBinder.fetchDevice.restore()
|
2018-10-18 18:52:35 +00:00
|
|
|
balenaAPI.balenaBackend.deviceKeyHandler.restore()
|
2017-11-01 06:47:48 +00:00
|
|
|
|
|
|
|
it 'exchanges the key and returns the device if the provisioning key is valid', ->
|
2018-10-18 18:52:35 +00:00
|
|
|
spy(balenaAPI.balenaBackend, 'deviceKeyHandler')
|
2017-11-01 06:47:48 +00:00
|
|
|
fetchDeviceStub = stub(@apiBinder, 'fetchDevice')
|
|
|
|
fetchDeviceStub.onCall(0).returns(Promise.resolve(null))
|
|
|
|
fetchDeviceStub.onCall(1).returns(Promise.resolve({ id: 1 }))
|
|
|
|
@apiBinder._exchangeKeyAndGetDevice(mockProvisioningOpts)
|
|
|
|
.then (device) =>
|
2018-10-18 18:52:35 +00:00
|
|
|
expect(balenaAPI.balenaBackend.deviceKeyHandler).to.be.calledOnce
|
2017-11-01 06:47:48 +00:00
|
|
|
expect(device).to.deep.equal({ id: 1 })
|
|
|
|
expect(@apiBinder.fetchDevice).to.be.calledTwice
|
|
|
|
@apiBinder.fetchDevice.restore()
|
2018-10-18 18:52:35 +00:00
|
|
|
balenaAPI.balenaBackend.deviceKeyHandler.restore()
|
2018-05-11 09:29:15 +00:00
|
|
|
|
|
|
|
describe 'offline mode', ->
|
|
|
|
before ->
|
|
|
|
initModels.call(this, '/config-apibinder-offline.json')
|
|
|
|
|
|
|
|
it 'does not generate a key if the device is in offline mode', ->
|
2018-12-13 14:14:15 +00:00
|
|
|
@config.get('unmanaged').then (mode) =>
|
2018-05-11 09:29:15 +00:00
|
|
|
# Ensure offline mode is set
|
|
|
|
expect(mode).to.equal(true)
|
|
|
|
# Check that there is no deviceApiKey
|
|
|
|
@config.getMany([ 'deviceApiKey', 'uuid' ]).then (conf) ->
|
2018-11-28 14:19:25 +00:00
|
|
|
expect(conf['deviceApiKey']).to.be.empty
|
2018-05-11 09:29:15 +00:00
|
|
|
expect(conf['uuid']).to.not.be.undefined
|
|
|
|
|
|
|
|
describe 'Minimal config offline mode', ->
|
|
|
|
before ->
|
|
|
|
initModels.call(this, '/config-apibinder-offline2.json')
|
|
|
|
|
|
|
|
it 'does not generate a key with the minimal config', ->
|
2018-12-13 14:14:15 +00:00
|
|
|
@config.get('unmanaged').then (mode) =>
|
2018-05-11 09:29:15 +00:00
|
|
|
expect(mode).to.equal(true)
|
|
|
|
@config.getMany([ 'deviceApiKey', 'uuid' ]).then (conf) ->
|
2018-11-28 14:19:25 +00:00
|
|
|
expect(conf['deviceApiKey']).to.be.empty
|
2018-05-11 09:29:15 +00:00
|
|
|
expect(conf['uuid']).to.not.be.undefined
|