Refactor valid UUID check into helpers

This commit is contained in:
Juan Cruz Viotti 2014-11-28 15:36:03 -04:00
parent 721ef8413a
commit 4702e7b563
3 changed files with 42 additions and 3 deletions

View File

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

View File

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

View File

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