From 1f94d48c1bb835050a236546271ce4aff1307513 Mon Sep 17 00:00:00 2001 From: simov Date: Wed, 16 May 2018 15:58:07 +0300 Subject: [PATCH] :ghost: Major refactoring :scream: --- .travis.yml | 4 +- README.md | 172 ++++-------------- bin/cli.js | 75 ++++++++ bin/index.js | 73 -------- lib/device.js | 72 +++----- lib/mac-resolve.js | 60 ------ lib/online.js | 66 ++++--- lib/output.js | 60 ------ lib/resolve.js | 39 ++++ package.json | 21 ++- test/device.js | 50 ++--- test/fixtures/device/created.json | 17 ++ test/fixtures/device/updated.json | 9 +- test/fixtures/online/filtered.json | 18 +- .../active.json => online/formatted.json} | 0 test/fixtures/output/api.json | 13 -- test/fixtures/output/slack.json | 18 -- test/mac-resolve.js | 52 ------ test/online.js | 23 ++- test/output.js | 49 ----- test/resolve.js | 59 ++++++ 21 files changed, 328 insertions(+), 622 deletions(-) create mode 100755 bin/cli.js delete mode 100755 bin/index.js delete mode 100644 lib/mac-resolve.js delete mode 100644 lib/output.js create mode 100644 lib/resolve.js create mode 100644 test/fixtures/device/created.json rename test/fixtures/{output/active.json => online/formatted.json} (100%) delete mode 100644 test/fixtures/output/api.json delete mode 100644 test/fixtures/output/slack.json delete mode 100644 test/mac-resolve.js delete mode 100644 test/output.js create mode 100644 test/resolve.js diff --git a/.travis.yml b/.travis.yml index a56fdb0..a687e2a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,12 @@ language: node_js node_js: + - node + - 10 - 8 after_script: - - npm run test-cov + - npm run test:cov - cat ./coverage/lcov.info | coveralls sudo: false diff --git a/README.md b/README.md index 214e143..d7fef68 100644 --- a/README.md +++ b/README.md @@ -3,160 +3,50 @@ [![travis-ci]][travis] [![coveralls-status]][coveralls] - -# nvm - -```bash -nvm install node 4 -nvm install node 8 -``` - - -# varnalab-whois - -```bash -git clone git@github.com:VarnaLab/varnalab-whois.git -cd varnalab-whois -npm i --production -``` - - -# varnalab-cli - ```bash -git clone git@github.com:VarnaLab/varnalab-cli.git -cd varnalab-cli -npm i --production -``` - - -# mikrotik.json - -```json -{ - "development": { - "host": "", - "user": "", - "pass": "", - "port": 0 - }, - "production": { - "host": "", - "user": "", - "pass": "", - "port": 0 - } -} -``` - - -# users.json - -```json -[] -``` - -# devices.json - -```json -[] +varnalab-whois \ + --users /path/to/users.json \ + --devices /path/to/devices.json \ + --blacklist /path/to/blacklist.json \ + --online /path/to/online.json ``` -# whois-online.sh +## [@varnalab/cli][varnalab-cli] + varnalab-whois ```bash #!/bin/bash -# globals -user=varnalab -nodev4=v4.6.1 -nodev8=v8.2.1 - # node -node4=/home/$user/.nvm/versions/node/$nodev4/bin/node -node8=/home/$user/.nvm/versions/node/$nodev8/bin/node - -# locations -config=/home/$user/simo/config/varnalab-whois -projects=/home/$user/simo/projects -serve=/home/$user/services/public - -# projects -cli=$projects/varnalab-cli/bin/whois.js -whois=$projects/varnalab-whois/bin/ - - -######################################################### - +if [ $(hostname) = "nuc" ]; then + node4=/home/s/.nvm/versions/node/v4.8.7/bin/node + node=node +elif [ $(hostname) = "box" ]; then + node4=/home/s/.nvm/versions/node/v4.8.6/bin/node + node=/home/s/.nvm/versions/node/v8.9.1/bin/node +fi # @varnalab/cli -json=$($node4 $cli --config $config/mikrotik.json --env production --output json) - -# varnalab.org - active.json -echo $json > $serve/active.json - - -# --output api - api.varnalab.org -# --output slack - varnalab.slack.com -echo $online | - $node8 $whois \ - --users $config/users.json \ - --devices $config/devices.json \ - --output slack \ - > $serve/online.json -``` - - -# crontab +online=$($node4 \ + ~/projects/varnalab-cli/bin/whois.js \ + --config ~/config/varnalab-whois/mikrotik.json \ + --env production \ + --output json\ +) -```bash -# update on every 5 minutes -*/5 * * * * /home/varnalab/simo/config/varnalab-whois/whois-online.sh -``` - - -# Nginx - -```nginx -server { - listen *:80; - listen *:443 ssl; - - server_name json.varnalab.org ; - root /serve/location/; - - # ... set up SSL locations - - # convert POST to GET - error_page 405 =200 @405; - location @405 { - root /serve/location/; - proxy_method GET; - proxy_pass http://static_backend; - } -} - -# POST STATIC CONTENT -server { - listen 127.0.0.1:89; - server_name _; - root /serve/location/; -} +# varnalab-whois +echo $online | $node \ + ~/projects/varnalab-whois/bin/cli.js \ + --users ~/config/varnalab-api/users.json \ + --devices ~/config/varnalab-api/devices.json \ + --blacklist ~/config/varnalab-api/blacklist.json \ + --online ~/config/varnalab-api/online.json ``` -# URLs - -- used in *varnalab.org* -- https://json.varnalab.org/services/active.json -- used in *varnalab.github.io* -- https://json.varnalab.org/services/whois-online.json -- used in *varnalab.slack.com* -- https://json.varnalab.org/services/whois-active.json - + [travis-ci]: https://img.shields.io/travis/VarnaLab/varnalab-whois/master.svg?style=flat-square (Build Status - Travis CI) + [coveralls-status]: https://img.shields.io/coveralls/VarnaLab/varnalab-whois.svg?style=flat-square (Test Coverage - Coveralls) -[travis-ci]: https://img.shields.io/travis/VarnaLab/varnalab-whois/master.svg?style=flat-square (Build Status - Travis CI) -[coveralls-status]: https://img.shields.io/coveralls/VarnaLab/varnalab-whois.svg?style=flat-square (Test Coverage - Coveralls) + [travis]: https://travis-ci.org/VarnaLab/varnalab-whois + [coveralls]: https://coveralls.io/github/VarnaLab/varnalab-whois -[travis]: https://travis-ci.org/VarnaLab/varnalab-whois -[coveralls]: https://coveralls.io/github/VarnaLab/varnalab-whois + [varnalab-cli]: https://github.com/VarnaLab/varnalab-cli diff --git a/bin/cli.js b/bin/cli.js new file mode 100755 index 0000000..eeadea1 --- /dev/null +++ b/bin/cli.js @@ -0,0 +1,75 @@ +#!/usr/bin/env node + +var argv = require('minimist')(process.argv.slice(2)) + +if (argv.help) { + console.log(` + --users /path/to/users.json + --devices /path/to/devices.json + --blacklist /path/to/blacklist.json + --online /path/to/online.json + `) + process.exit() +} + +;['users', 'devices', 'blacklist', 'online'].forEach((file) => { + if (!argv[file]) { + console.log(`Specify --${file} /path/to/${file}.json`) + process.exit() + } +}) + + +var fs = require('fs') +var path = require('path') + +var fpath = { + users: path.resolve(process.cwd(), argv.users), + devices: path.resolve(process.cwd(), argv.devices), + blacklist: path.resolve(process.cwd(), argv.blacklist), + online: path.resolve(process.cwd(), argv.online), +} +var users = require(fpath.users) +var devices = require(fpath.devices) +var blacklist = require(fpath.blacklist) + +var device = require('../lib/device') +var resolve = require('../lib/resolve') +var online = require('../lib/online') + + +;(() => { + var data = '' + process.stdin.on('data', (chunk) => { + data += chunk + }) + process.stdin.on('end', (chunk) => { + try { + var json = JSON.parse(data) + run({devices, users, active: json.active}) + } + catch (err) { + console.log(JSON.stringify({error: 'Услугата временно не е налична'})) + process.exit() + } + }) +})() + + +async function run ({devices, users, active}) { + + var created = device.create({devices, active}) + if (created.length) { + var resolved = await resolve({devices: created}) + devices = devices.concat(resolved) + } + + var dirty = device.update({devices, active}) + if (dirty || created.length) { + fs.writeFileSync(fpath.devices, JSON.stringify(devices, null, 2), 'utf8') + } + + var active = online.format({users, devices, active}) + fs.writeFileSync(fpath.online, JSON.stringify(online.filter(active)), 'utf8') + +} diff --git a/bin/index.js b/bin/index.js deleted file mode 100755 index 2de748a..0000000 --- a/bin/index.js +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env node - -var argv = require('minimist')(process.argv.slice(2)) - -if (argv.help) { - console.log('--users /path/to/users.json') - console.log('--devices /path/to/devices.json') - console.log('--output api|slack') - process.exit() -} - -if (!argv.users) { - console.log('Specify /path/to/users.json') - process.exit() -} - -if (!argv.devices) { - console.log('Specify /path/to/devices.json') - process.exit() -} - -var fs = require('fs') -var path = require('path') - -var fpath = { - users: path.resolve(process.cwd(), argv.users), - devices: path.resolve(process.cwd(), argv.devices) -} -var users = require(fpath.users) -var devices = require(fpath.devices) - -var online = require('../lib/online')() -var resolve = require('../lib/mac-resolve')() -var device = require('../lib/device')(resolve) -var output = require('../lib/output')(device.sort) - -var out = argv.output || 'api' - - -;(() => { - var data = '' - process.stdin.on('data', (chunk) => { - data += chunk - }) - process.stdin.on('end', (chunk) => { - try { - var json = JSON.parse(data) - run(json) - } - catch (err) { - console.log(JSON.stringify(output.error[out])) - process.exit() - } - }) -})() - - -function run ({active}) { - - device.update(devices, active).then((updated) => { - fs.writeFileSync( - fpath.devices, JSON.stringify(updated, null, 2), - 'utf8' - ) - console.log(JSON.stringify( - output[out](online.filter(users, updated, active)) - )) - }) - .catch((err) => { - console.log(JSON.stringify(output.error[out])) - console.error(err) - }) -} diff --git a/lib/device.js b/lib/device.js index 8bac7c5..8da893e 100644 --- a/lib/device.js +++ b/lib/device.js @@ -2,61 +2,31 @@ var uuid = require('uuid') -module.exports = (macresolve) => { - - var update = (devices, active) => new Promise((resolve, reject) => { - var addresses = devices - .map((device) => device.mac) - - var missing = {} - - active.forEach((device) => { - var index = addresses - .indexOf(device.mac) - // better match (if needed) - // .filter((user) => user.mac.some((mac) => - // mac.replace(/[-:\s]/g, '').toLowerCase() === - // active.mac.replace(/[-:\s]/g, '').toLowerCase() - // )) - - if (index === -1) { - missing[device.mac] = {id: uuid(), mac: device.mac, host: device.host} - } - else { - devices[index].host = device.host - } - }) - - missing = Object.keys(missing) - .map((mac) => missing[mac]) - - if (missing.length) { - macresolve(missing, () => { - resolve(devices.concat(missing)) - }) - } - else { - process.nextTick(() => resolve(devices)) +exports.create = ({devices, active}) => { + var macs = devices.map((device) => device.mac) + var missing = {} + + active.forEach((device) => { + var index = macs.indexOf(device.mac) + if (index === -1) { + missing[device.mac] = {id: uuid(), mac: device.mac, host: device.host} } }) - var sort = (devices) => { - var phones = devices - .filter((device) => device.host && /android|i?phone/i.test(device.host)) - .sort((a, b) => ( - a.host.toLowerCase() > b.host.toLowerCase() ? 1 : - a.host.toLowerCase() < b.host.toLowerCase() ? -1 : 0)) - - var other = devices - .filter((device) => device.host && !/android|i?phone/i.test(device.host)) - .sort((a, b) => ( - a.host.toLowerCase() > b.host.toLowerCase() ? 1 : - a.host.toLowerCase() < b.host.toLowerCase() ? -1 : 0)) + return Object.keys(missing).map((mac) => missing[mac]) +} - var nohost = devices.filter((device) => !device.host) +exports.update = ({devices, active}) => { + var macs = devices.map((device) => device.mac) + var dirty = false - return phones.concat(other).concat(nohost) - } + active.forEach((device) => { + var index = macs.indexOf(device.mac) + if (devices[index].host !== device.host) { + devices[index].host = device.host + dirty = true + } + }) - return {update, sort} + return dirty } diff --git a/lib/mac-resolve.js b/lib/mac-resolve.js deleted file mode 100644 index 1b1917f..0000000 --- a/lib/mac-resolve.js +++ /dev/null @@ -1,60 +0,0 @@ - -var https = require('https') - - -module.exports = (config) => { - var options = config || { - method: 'GET', - protocol: 'https:', - host: 'api.macvendors.com', - agent: new https.Agent({ - keepAlive: true, - maxSockets: 1, - }), - timeout: 3000, - } - - var client = require(options.protocol.replace(':', '')) - - function get (mac, done) { - var req = client.request(Object.assign({}, options, {path: '/' + mac})) - - req.on('error', (err) => { - done(err) - }) - - req.on('timeout', () => { - req.abort() - }) - - req.on('response', (res) => { - var body = '' - res.on('error', (err) => { - done(err) - }) - res.on('data', (chunk) => { - body += chunk - }) - res.on('end', () => { - done(null, (body === 'Vendor not found\n') ? false : body) - }) - }) - - req.end() - } - - var resolve = (list, done) => { - ;(function next (index) { - if (index === list.length) { - done() - return - } - get(list[index].mac.slice(0, 8), (err, vendor) => { - list[index].vendor = vendor === undefined ? null : vendor - next(++index) - }) - })(0) - } - - return resolve -} diff --git a/lib/online.js b/lib/online.js index 65854d6..08f1561 100644 --- a/lib/online.js +++ b/lib/online.js @@ -1,38 +1,36 @@ -module.exports = () => { - - var filter = (users, devices, active) => { - var addresses = devices - .map((device) => device.mac) - - var known = {} - var unknown = {} - - active.forEach((device) => { - var index = addresses - .indexOf(device.mac) - // better match (if needed) - // .filter((user) => user.mac.some((mac) => - // mac.replace(/[-:\s]/g, '').toLowerCase() === - // active.mac.replace(/[-:\s]/g, '').toLowerCase() - // )) - - var user = users - .find((user) => user.devices.includes(devices[index].id)) - - if (user) { - known[user.id] = user - } - else { - unknown[device.mac] = devices[index] - } - }) - - return { - known: Object.keys(known).map((id) => known[id]), - unknown: Object.keys(unknown).map((mac) => unknown[mac]) +exports.format = ({users, devices, active}) => { + + var macs = devices.map((device) => device.mac) + + var known = {} + var unknown = {} + + active.forEach((device) => { + var index = macs.indexOf(device.mac) + // better match (if needed) + // .filter((user) => user.mac.some((mac) => + // mac.replace(/[-:\s]/g, '').toLowerCase() === + // active.mac.replace(/[-:\s]/g, '').toLowerCase() + // )) + + var user = users.find((user) => user.devices.includes(devices[index].id)) + + if (user) { + known[user.id] = user } - } + else { + unknown[device.mac] = devices[index] + } + }) - return {filter} + return { + known: Object.keys(known).map((id) => known[id]), + unknown: Object.keys(unknown).map((mac) => unknown[mac]), + } } + +exports.filter = ({known, unknown}) => ({ + known: known.map((user) => user.id), + unknown: unknown.map(({id, host, vendor}) => ({id, host, vendor})) +}) diff --git a/lib/output.js b/lib/output.js deleted file mode 100644 index 1c68194..0000000 --- a/lib/output.js +++ /dev/null @@ -1,60 +0,0 @@ - -module.exports = () => { - - // api.varnalab.org - var api = ({known, unknown}) => ({ - known: known.map((user) => user.id), - unknown: unknown.map(({id, host, vendor}) => ({id, host, vendor})) - }) - - // varnalab.slack.com - var slack = ({known, unknown}) => - ({ - attachments: !known.length && !unknown.length - ? - [ - { - fallback: 'VarnaLab Whois Command', - text: '_Няма никой_', - mrkdwn_in: ['text'] - } - ] - : - [ - { - fallback: 'VarnaLab Whois Command', - text: known.reduce((text, user) => text += - ( - user.slack - ? ' ' - : '' - ) + - '_' + user.name + '_\n' - , ''), - mrkdwn_in: ['text'] - }, - { - fallback: 'VarnaLab Whois Command', - text: unknown.reduce((text, device) => text += - '_' + device.host + '_' + - (device.vendor ? ' - _' + device.vendor + '_' : '') + '\n' - , ''), - mrkdwn_in: ['text'] - } - ] - }) - - var error = { - api: { - error: 'Услугата временно не е налична' - }, - slack: { - fallback: 'VarnaLab Whois Command', - text: '_Услугата временно не е налична_', - mrkdwn_in: ['text'] - }, - } - - return {api, slack, error} -} diff --git a/lib/resolve.js b/lib/resolve.js new file mode 100644 index 0000000..4f3906a --- /dev/null +++ b/lib/resolve.js @@ -0,0 +1,39 @@ + +var request = require('request-compose').client +var https = require('https') + + +var timeout = () => new Promise((resolve) => setTimeout(resolve, 1250)) + +var resolve = async ({mac, options}) => { + await timeout() + + try { + var {body:vendor} = await request(Object.assign({}, options, { + url: `${options.url}/${mac}`, + })) + } + catch ({res}) { + vendor = res && res.statusCode === 404 ? false : null + } + + return vendor +} + +module.exports = async ({devices, options}) => { + options = options || { + url: 'https://api.macvendors.com', + agent: new https.Agent({keepAlive: true, maxSockets: 1}), + } + + var result = [] + + for (var {id, mac, host, vendor} of devices) { + result.push({ + id, mac, host, vendor: vendor || vendor === false ? vendor : + await resolve({mac: mac.slice(0, 8), options}) + }) + } + + return result +} diff --git a/package.json b/package.json index 1609ded..49577f3 100644 --- a/package.json +++ b/package.json @@ -13,15 +13,17 @@ }, "dependencies": { "minimist": "^1.2.0", - "uuid": "^3.1.0" + "request-compose": "0.0.16", + "uuid": "^3.2.1" }, "devDependencies": { - "coveralls": "^2.13.1", - "istanbul": "^0.4.5", - "mocha": "^3.4.2" + "coveralls": "^3.0.1", + "istanbul": "1.1.0-alpha.1", + "mocha": "^5.1.1", + "request-logs": "0.0.5" }, "bin": { - "varnalab-whois": "./bin/index.js" + "varnalab-whois": "./bin/cli.js" }, "main": "", "files": [ @@ -32,12 +34,11 @@ "package.json" ], "scripts": { - "test": "npm run test-ci", - "test-ci": "mocha test/", - "test-cov": "istanbul cover _mocha" + "test": "npm run test:ci", + "test:ci": "mocha --timeout 5000 test/", + "test:cov": "istanbul cover _mocha -- --timeout 5000" }, "engines": { - "node": ">=8.0.0", - "npm": ">=5.0.0" + "node": ">=8.0.0" } } diff --git a/test/device.js b/test/device.js index 7921f76..5160b88 100644 --- a/test/device.js +++ b/test/device.js @@ -3,49 +3,37 @@ require('uuid') require.cache[require.resolve('uuid')].exports = () => 'hey' var t = require('assert') -var http = require('http') -var resolve = require('../lib/mac-resolve')({ - method: 'GET', - protocol: 'http:', - host: 'localhost', - port: 3000, - timeout: 100, -}) -var device = require('../lib/device')(resolve) +var device = require('../lib/device') var fixtures = { devices: require('./fixtures/device/devices'), - mikrotik: require('./fixtures/device/mikrotik').active, + active: require('./fixtures/device/mikrotik').active, + created: require('./fixtures/device/created'), updated: require('./fixtures/device/updated'), } describe('device', () => { - var server - - before((done) => { - server = http.createServer() - server.on('request', (req, res) => { - res.end('Vendor not found\n') - }) - - server.listen(3000, done) + it('create', () => { + t.deepStrictEqual( + device.create(fixtures), + fixtures.created, + 'should create missing devices' + ) }) - it('update', (done) => { - device.update( + it('update', () => { + fixtures.devices = fixtures.devices.concat(device.create(fixtures)) + t.equal( + device.update(fixtures), + true, + 'should mark the collection as dirty' + ) + t.deepStrictEqual( fixtures.devices, - fixtures.mikrotik + fixtures.updated, + 'should update missing devices' ) - .then((updated) => { - t.deepEqual(updated, fixtures.updated) - done() - }) - .catch(done) - }) - - after((done) => { - server.close(done) }) }) diff --git a/test/fixtures/device/created.json b/test/fixtures/device/created.json new file mode 100644 index 0000000..1b81708 --- /dev/null +++ b/test/fixtures/device/created.json @@ -0,0 +1,17 @@ +[ + { + "id": "hey", + "mac": "mac1", + "host": "new-unknown-pc" + }, + { + "id": "hey", + "mac": "mac2", + "host": "new-unknown-android-28ab9fba99655591" + }, + { + "id": "hey", + "mac": "mac3", + "host": "new-unknown-iPhone5" + } +] diff --git a/test/fixtures/device/updated.json b/test/fixtures/device/updated.json index 4ad2ff3..50ea3ca 100644 --- a/test/fixtures/device/updated.json +++ b/test/fixtures/device/updated.json @@ -8,19 +8,16 @@ { "id": "hey", "mac": "mac1", - "host": "new-unknown-pc", - "vendor": false + "host": "new-unknown-pc" }, { "id": "hey", "mac": "mac2", - "host": "new-unknown-android-28ab9fba99655591", - "vendor": false + "host": "new-unknown-android-28ab9fba99655591" }, { "id": "hey", "mac": "mac3", - "host": "new-unknown-iPhone5", - "vendor": false + "host": "new-unknown-iPhone5" } ] diff --git a/test/fixtures/online/filtered.json b/test/fixtures/online/filtered.json index 150d145..e8c3ce6 100644 --- a/test/fixtures/online/filtered.json +++ b/test/fixtures/online/filtered.json @@ -1,25 +1,11 @@ { "known": [ - { - "id": "user0", - "name": "name0", - "devices": [ - "device0" - ] - }, - { - "id": "user1", - "name": "name2", - "devices": [ - "device2", - "device3" - ] - } + "user0", + "user1" ], "unknown": [ { "id": "device1", - "mac": "mac1", "host": "host1", "vendor": "vendor1" } diff --git a/test/fixtures/output/active.json b/test/fixtures/online/formatted.json similarity index 100% rename from test/fixtures/output/active.json rename to test/fixtures/online/formatted.json diff --git a/test/fixtures/output/api.json b/test/fixtures/output/api.json deleted file mode 100644 index e8c3ce6..0000000 --- a/test/fixtures/output/api.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "known": [ - "user0", - "user1" - ], - "unknown": [ - { - "id": "device1", - "host": "host1", - "vendor": "vendor1" - } - ] -} diff --git a/test/fixtures/output/slack.json b/test/fixtures/output/slack.json deleted file mode 100644 index d30ddc3..0000000 --- a/test/fixtures/output/slack.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "attachments": [ - { - "fallback": "VarnaLab Whois Command", - "text": "_name0_\n_name2_\n", - "mrkdwn_in": [ - "text" - ] - }, - { - "fallback": "VarnaLab Whois Command", - "text": "_host1_ - _vendor1_\n", - "mrkdwn_in": [ - "text" - ] - } - ] -} diff --git a/test/mac-resolve.js b/test/mac-resolve.js deleted file mode 100644 index 5d951cd..0000000 --- a/test/mac-resolve.js +++ /dev/null @@ -1,52 +0,0 @@ - -var t = require('assert') -var http = require('http') -var resolve = require('../lib/mac-resolve')({ - method: 'GET', - protocol: 'http:', - host: 'localhost', - port: 3000, - timeout: 100, -}) - - -describe('mac-resolve', () => { - var server - - before((done) => { - server = http.createServer() - - server.on('request', (req, res) => { - if (req.url === '/mac1') { - res.end('vendor1') - } - else if (req.url === '/mac2') { - setTimeout(() => {}, 150) - } - else if (req.url === '/mac3') { - res.end('Vendor not found\n') - } - }) - - server.listen(3000, done) - }) - - it('resolve', (done) => { - var list = [{mac: 'mac1'}, {mac: 'mac2'}, {mac: 'mac3'}] - resolve(list, (err) => { - if (err) { - done(err) - } - t.deepEqual(list, [ - {mac: 'mac1', vendor: 'vendor1'}, - {mac: 'mac2', vendor: undefined}, - {mac: 'mac3', vendor: false}, - ]) - done() - }) - }) - - after((done) => { - server.close(done) - }) -}) diff --git a/test/online.js b/test/online.js index 3340bf4..3c6322d 100644 --- a/test/online.js +++ b/test/online.js @@ -1,23 +1,32 @@ var t = require('assert') -var online = require('../lib/online')() +var online = require('../lib/online') var fixtures = { users: require('./fixtures/online/users'), devices: require('./fixtures/online/devices'), - mikrotik: require('./fixtures/online/mikrotik').active, + active: require('./fixtures/online/mikrotik').active, + formatted: require('./fixtures/online/formatted'), filtered: require('./fixtures/online/filtered'), } describe('online', () => { + it('format', () => { + t.deepStrictEqual( + online.format(fixtures), + fixtures.formatted, + 'should format the online devices into known and unknown' + ) + }) + it('filter', () => { - var filtered = online.filter( - fixtures.users, - fixtures.devices, - fixtures.mikrotik, + t.deepStrictEqual( + online.filter(online.format(fixtures)), + fixtures.filtered, + 'should filter known and unknown device keys' ) - t.deepEqual(filtered, fixtures.filtered) }) + }) diff --git a/test/output.js b/test/output.js deleted file mode 100644 index dc997fb..0000000 --- a/test/output.js +++ /dev/null @@ -1,49 +0,0 @@ - -require('uuid') -require.cache[require.resolve('uuid')].exports = () => 'hey' - -var t = require('assert') -var http = require('http') -var online = require('../lib/online')() -var resolve = require('../lib/mac-resolve')({ - method: 'GET', - protocol: 'http:', - host: 'localhost', - port: 3000, - timeout: 100, -}) -var device = require('../lib/device')(resolve) -var output = require('../lib/output')(device.sort) - -var fixtures = { - active: require('./fixtures/output/active'), - api: require('./fixtures/output/api'), - slack: require('./fixtures/output/slack'), -} - - -describe('output', () => { - var server - - before((done) => { - server = http.createServer() - - server.on('request', (req, res) => { - res.end('Vendor not found\n') - }) - - server.listen(3000, done) - }) - - it('api', () => { - t.deepEqual(output.api(fixtures.active), fixtures.api) - }) - - it('slack', () => { - t.deepEqual(output.slack(fixtures.active), fixtures.slack) - }) - - after((done) => { - server.close(done) - }) -}) diff --git a/test/resolve.js b/test/resolve.js new file mode 100644 index 0000000..ae36701 --- /dev/null +++ b/test/resolve.js @@ -0,0 +1,59 @@ + +var t = require('assert') +var http = require('http') +var resolve = require('../lib/resolve') + + +describe('resolve', () => { + var server + + before((done) => { + server = http.createServer() + + server.on('request', (req, res) => { + if (req.url === '/mac1') { + res.end('vendor1') + } + else if (req.url === '/mac2') { + setTimeout(() => {}, 150) + } + else if (req.url === '/mac3') { + res.writeHead(404) + res.end('not found') + } + }) + + server.listen(3000, done) + }) + + it('resolve', async () => { + var devices = [ + {id: 1, mac: 'mac1', host: 1}, + {id: 2, mac: 'mac2', host: 2}, + {id: 3, mac: 'mac3', host: 3}, + ] + var options = { + url: 'http://localhost:3000', + timeout: 100, + } + var output = await resolve({devices, options}) + t.deepStrictEqual( + output, + [ + {id: 1, mac: 'mac1', host: 1, vendor: 'vendor1'}, + {id: 2, mac: 'mac2', host: 2, vendor: null}, + {id: 3, mac: 'mac3', host: 3, vendor: false}, + ], + 'mac1 - resolved, mac2 - timeout/error, mac3 - not found' + ) + t.notDeepStrictEqual( + devices, + output, + 'should return a new object' + ) + }) + + after((done) => { + server.close(done) + }) +})