From ef6c4af98e0204b2db266db5f9ced1c2aad62261 Mon Sep 17 00:00:00 2001 From: Brice Argenson Date: Thu, 9 Feb 2017 18:44:15 -0500 Subject: [PATCH 1/3] Change endpoints for devices --- clients/Barracks.js | 20 +++++++++++++++--- clients/Barracks.spec.js | 32 +++++++++++++++++++++++++--- commands/DevicesCommand.js | 37 +-------------------------------- commands/DevicesCommand.spec.js | 2 -- config.js | 6 +++++- package.json | 4 ++-- 6 files changed, 54 insertions(+), 47 deletions(-) diff --git a/clients/Barracks.js b/clients/Barracks.js index 55cd99c..1154ebb 100644 --- a/clients/Barracks.js +++ b/clients/Barracks.js @@ -269,12 +269,12 @@ class Barracks { }); } - getDevices(token, segmentId) { + getSegmentDevices(token, segmentId) { return new Promise(resolve => { logger.debug('Getting devices for segment:', segmentId); const stream = new PageableStream(); resolve(stream); - this.client.retrieveAllPages(stream, 'getDevices', { + this.client.retrieveAllPages(stream, 'getSegmentDevices', { headers: { 'x-auth-token': token }, @@ -282,7 +282,21 @@ class Barracks { segmentId } }, - 'deviceEvents'); + 'devices'); + }); + } + + getDevices(token) { + return new Promise(resolve => { + logger.debug('Getting all devices...'); + const stream = new PageableStream(); + resolve(stream); + this.client.retrieveAllPages(stream, 'getDevices', { + headers: { + 'x-auth-token': token + } + }, + 'devices'); }); } diff --git a/clients/Barracks.spec.js b/clients/Barracks.spec.js index 994f628..a1eaeb5 100644 --- a/clients/Barracks.spec.js +++ b/clients/Barracks.spec.js @@ -921,7 +921,7 @@ describe('Barracks', () => { }); - describe('#getDevices()', () => { + describe('#getSegmentDevices()', () => { it('should return a stream object and deleguate to the client', done => { // Given @@ -933,14 +933,40 @@ describe('Barracks', () => { barracks.client.retrieveAllPages = sinon.spy(); // When / Then - barracks.getDevices(token, segmentId).then(result => { + barracks.getSegmentDevices(token, segmentId).then(result => { + expect(result).to.be.instanceOf(PageableStream); + expect(barracks.client.retrieveAllPages).to.have.been.calledOnce; + expect(barracks.client.retrieveAllPages).to.have.been.calledWithExactly( + new PageableStream(), + 'getSegmentDevices', + options, + 'devices' + ); + done(); + }).catch(err => { + done(err); + }); + }); + }); + + describe('#getDevices()', () => { + + it('should return a stream object and deleguate to the client', done => { + // Given + const options = { + headers: { 'x-auth-token': token } + } + barracks.client.retrieveAllPages = sinon.spy(); + + // When / Then + barracks.getDevices(token).then(result => { expect(result).to.be.instanceOf(PageableStream); expect(barracks.client.retrieveAllPages).to.have.been.calledOnce; expect(barracks.client.retrieveAllPages).to.have.been.calledWithExactly( new PageableStream(), 'getDevices', options, - 'deviceEvents' + 'devices' ); done(); }).catch(err => { diff --git a/commands/DevicesCommand.js b/commands/DevicesCommand.js index b599051..bd487b6 100644 --- a/commands/DevicesCommand.js +++ b/commands/DevicesCommand.js @@ -1,42 +1,7 @@ -const PageableStream = require('../clients/PageableStream'); const BarracksCommand = require('./BarracksCommand'); function getAllDevices(token, barracks) { - return new Promise(resolve => { - const stream = new PageableStream(); - resolve(stream); - getActiveSegmentsIdsWithOther(token, barracks).then(activeSegmentsIds => { - const activeSegmentCount = activeSegmentsIds.length; - let streamClosedCount = 0; - activeSegmentsIds.forEach(segmentId => { - barracks.getDevices(token, segmentId).then(resultStream => { - resultStream.onPageReceived(page => { - stream.write(page); - }); - resultStream.onLastPage(() => { - ++streamClosedCount; - if (streamClosedCount === activeSegmentCount) { - stream.lastPage(); - } - }); - }).catch(err => { - stream.fail(err); - }); - }); - }).catch(err => { - stream.fail(err); - }); - }); -} - -function getActiveSegmentsIdsWithOther(token, barracks) { - return new Promise((resolve, reject) => { - barracks.getSegments(token).then(result => { - resolve(result.active.map(segment => segment.id).concat('other')); - }).catch(err => { - reject(err); - }); - }); + return barracks.getDevices(); } function getAllDevicesFromSegment(token, barracks, segmentName) { diff --git a/commands/DevicesCommand.spec.js b/commands/DevicesCommand.spec.js index 4a8b8ea..f393203 100644 --- a/commands/DevicesCommand.spec.js +++ b/commands/DevicesCommand.spec.js @@ -40,10 +40,8 @@ describe('DevicesCommand', () => { it('should return a PageableStream object when services return data', done => { // Given const program = Object.assign({}, programWithValidOptions); - const channels = [{ name: 'channel1' }, { name: 'channel2' }]; devicesCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token)); devicesCommand.barracks = { - getChannels: sinon.stub().returns(Promise.resolve(channels)), getDevices: sinon.stub().returns(Promise.resolve(new PageableStream())) }; diff --git a/config.js b/config.js index f350072..f1eff9b 100644 --- a/config.js +++ b/config.js @@ -61,10 +61,14 @@ module.exports = { method: 'GET', path: '/api/member/segments/:id' }, - getDevices: { + getSegmentDevices: { method: 'GET', path: '/api/member/segments/:segmentId/devices?size=20' }, + getDevices: { + method: 'GET', + path: '/api/member/devices?size=20' + }, editUpdate: { method: 'PUT', path: '/api/member/updates/:uuid' diff --git a/package.json b/package.json index d75e4cb..3a8ff0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "barracks-cli", - "version": "0.0.2", + "version": "1.0.0", "description": "A CLI tool for Barracks", "main": "index.js", "scripts": { @@ -8,7 +8,7 @@ "lint": "jshint **/**.js", "test": "npm run lint && npm run coverage && npm run check-coverage", "coverage": "DEBUG=true istanbul cover ./node_modules/mocha/bin/_mocha **/*.spec.js -- -R spec", - "check-coverage": "istanbul check-coverage --statement 90 --branch 70 --function 90", + "check-coverage": "istanbul check-coverage --statement 90 --branch 75 --function 90", "coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls" }, "bin": { From dc4411103fa505305ffab3837f1a6903346836aa Mon Sep 17 00:00:00 2001 From: Brice Argenson Date: Fri, 10 Feb 2017 12:39:37 -0500 Subject: [PATCH 2/3] Fix missing token --- commands/DevicesCommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/DevicesCommand.js b/commands/DevicesCommand.js index bd487b6..8d643f8 100644 --- a/commands/DevicesCommand.js +++ b/commands/DevicesCommand.js @@ -1,7 +1,7 @@ const BarracksCommand = require('./BarracksCommand'); function getAllDevices(token, barracks) { - return barracks.getDevices(); + return barracks.getDevices(token); } function getAllDevicesFromSegment(token, barracks, segmentName) { From 288a3186f2e31cd5f5ba85d9ba4e4ec32e92a93b Mon Sep 17 00:00:00 2001 From: Brice Argenson Date: Fri, 10 Feb 2017 13:58:02 -0500 Subject: [PATCH 3/3] Fix segemnt devices command --- commands/DevicesCommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/DevicesCommand.js b/commands/DevicesCommand.js index 8d643f8..416ae47 100644 --- a/commands/DevicesCommand.js +++ b/commands/DevicesCommand.js @@ -7,7 +7,7 @@ function getAllDevices(token, barracks) { function getAllDevicesFromSegment(token, barracks, segmentName) { return new Promise((resolve, reject) => { barracks.getSegmentByName(token, segmentName).then(segment => { - return barracks.getDevices(token, segment.id); + return barracks.getSegmentDevices(token, segment.id); }).then(resultStream => { resolve(resultStream); }).catch(err => {