mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-03-10 22:44:14 +00:00
Refactor valid UUID check into helpers
This commit is contained in:
parent
721ef8413a
commit
4702e7b563
@ -1,15 +1,15 @@
|
||||
_ = require('lodash')
|
||||
PubNub = require('pubnub')
|
||||
resin = require('../resin')
|
||||
helpers = require('../helpers/helpers')
|
||||
|
||||
LOGS_HISTORY_COUNT = 200
|
||||
|
||||
exports.logs = (uuid) ->
|
||||
resin.models.device.getAll (error, devices) ->
|
||||
helpers.isDeviceUUIDValid uuid, (error, isValidUUID) ->
|
||||
resin.errors.handle(error) if error?
|
||||
|
||||
uuidExists = _.findWhere(devices, { uuid })?
|
||||
if not uuidExists
|
||||
if not isValidUUID
|
||||
return resin.errors.handle(new Error('Invalid UUID'))
|
||||
|
||||
# PubNub needs to be initialised after logs
|
||||
|
@ -1,5 +1,14 @@
|
||||
_ = require('lodash')
|
||||
resin = require('../resin')
|
||||
|
||||
exports.formatLongString = (string, n) ->
|
||||
return string if not n?
|
||||
splitRegexp = new RegExp(".{1,#{n}}", 'g')
|
||||
splittedString = string.match(splitRegexp)
|
||||
return splittedString.join('\n')
|
||||
|
||||
exports.isDeviceUUIDValid = (uuid, callback) ->
|
||||
resin.models.device.getAll (error, devices) ->
|
||||
return callback?(error) if error?
|
||||
uuidExists = _.findWhere(devices, { uuid })?
|
||||
return callback(null, uuidExists)
|
||||
|
@ -1,6 +1,8 @@
|
||||
expect = require('chai').expect
|
||||
sinon = require('sinon')
|
||||
_ = require('lodash')
|
||||
helpers = require('./helpers')
|
||||
resin = require('../resin')
|
||||
|
||||
STRING =
|
||||
numbers: '1234567812345678'
|
||||
@ -33,3 +35,31 @@ describe 'Helpers:', ->
|
||||
stringLength = STRING.numbers.length
|
||||
result = helpers.formatLongString(STRING.numbers, stringLength + 1)
|
||||
expect(result).to.equal(STRING.numbers)
|
||||
|
||||
describe '#isDeviceUUIDValid()', ->
|
||||
|
||||
devices = [
|
||||
{ uuid: 1234 }
|
||||
{ uuid: 5678 }
|
||||
]
|
||||
|
||||
deviceGetAllStub = null
|
||||
|
||||
beforeEach ->
|
||||
deviceGetAllStub = sinon.stub(resin.models.device, 'getAll')
|
||||
deviceGetAllStub.yields(null, devices)
|
||||
|
||||
afterEach ->
|
||||
deviceGetAllStub.restore()
|
||||
|
||||
it 'should return true if there is a device with that UUID', (done) ->
|
||||
helpers.isDeviceUUIDValid devices[0].uuid, (error, isValid) ->
|
||||
expect(error).to.not.exist
|
||||
expect(isValid).to.be.true
|
||||
done()
|
||||
|
||||
it 'should return false if there is not a device with that UUID', (done) ->
|
||||
helpers.isDeviceUUIDValid 'invalidUUID', (error, isValid) ->
|
||||
expect(error).to.not.exist
|
||||
expect(isValid).to.be.false
|
||||
done()
|
||||
|
Loading…
x
Reference in New Issue
Block a user