Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device get command for V1 #41

Merged
merged 1 commit into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/bin/barracks-device
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ const pjson = require('../../package.json');

const barracks = program.version(pjson.version);

if (config.v2Enabled) {
barracks.command('ls', 'List devices')
.command('get', 'Display detailed information of a device')
.command('history', 'Display all events of a device');
} else {
barracks.command('ls', 'List devices')
.command('history', 'Display all events of a device');
}
barracks.command('ls', 'List devices')
.command('get', 'Display detailed information of a device')
.command('history', 'Display all events of a device');

barracks.parse(process.argv);

Expand Down
11 changes: 9 additions & 2 deletions src/clients/DeviceClient.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
const PageableStream = require('./PageableStream');
const HTTPClient = require('./HTTPClient');
const logger = require('../utils/logger');
const config = require('../config');

const endpoints = {
getDevice: {
getDeviceV1: {
method: 'GET',
path: '/api/member/devices/:unitId'
},
getDeviceV2: {
method: 'GET',
path: '/v2/api/member/devices/:unitId'
},
Expand Down Expand Up @@ -41,13 +46,15 @@ class DeviceClient {

constructor() {
this.httpClient = new HTTPClient();
this.v2Enabled = config.v2Enabled;
}

getDevice(token, unitId) {
return new Promise((resolve, reject) => {
logger.debug(`Getting device ${unitId}`);
const endpointKey = this.v2Enabled ? 'getDeviceV2' : 'getDeviceV1';
this.httpClient.sendEndpointRequest(
endpoints.getDevice,
endpoints[endpointKey],
{
headers: {
'x-auth-token': token
Expand Down
59 changes: 58 additions & 1 deletion tests/clients/DeviceClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('DeviceClient', () => {
expect(deviceClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'GET',
path: '/v2/api/member/devices/:unitId'
path: '/api/member/devices/:unitId'
},
{
headers: { 'x-auth-token': token },
Expand All @@ -59,6 +59,63 @@ describe('DeviceClient', () => {
const response = { body: device };
deviceClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.resolve(response));

// When / Then
deviceClient.getDevice(token, unitId).then(result => {
expect(result).to.deep.equals(device);
expect(deviceClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(deviceClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'GET',
path: '/api/member/devices/:unitId'
},
{
headers: { 'x-auth-token': token },
pathVariables: { unitId }
}
);
done();
}).catch(err => {
done(err);
});
});

it('should return an error message when request fails and V2 enabled', done => {
// Given
deviceClient.v2Enabled = true;
const error = { message: 'Error !' };
deviceClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error));

// When / Then
deviceClient.getDevice(token, unitId).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals(error.message);
expect(deviceClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(deviceClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'GET',
path: '/v2/api/member/devices/:unitId'
},
{
headers: { 'x-auth-token': token },
pathVariables: { unitId }
}
);
done();
});
});

it('should return the device information when request succeed and V2 enabled', done => {
// Given
deviceClient.v2Enabled = true;
const device = {
unitId,
lastEvent: {},
lastSeen: 'some date'
};
const response = { body: device };
deviceClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.resolve(response));

// When / Then
deviceClient.getDevice(token, unitId).then(result => {
expect(result).to.deep.equals(device);
Expand Down