Skip to content

Commit

Permalink
Merge pull request #58 from barracksiot/BO-1558
Browse files Browse the repository at this point in the history
Bo 1558: Creation of webhooks for device enrollment
  • Loading branch information
Greg committed Aug 8, 2017
2 parents d6d5180 + ac8d0f6 commit f4bc5dd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/clients/HookClient.js
Expand Up @@ -71,7 +71,7 @@ class HookClient {
resolve(response.body);
}).catch(errResponse => {
if (errResponse.statusCode === 404) {
reject('This is not the hook you are looking for.');
reject(`Hook "${name}" could not be found`);
}
else {
reject(errResponse.message);
Expand Down Expand Up @@ -115,7 +115,7 @@ class HookClient {
resolve(response.body);
}).catch(errResponse => {
if (errResponse.statusCode === 404) {
reject('The hook you want to update does not exist.');
reject(`Hook "${name}" does not exists.`);
}
else if (errResponse.statusCode === 409){
reject('A hook with this name already exists.');
Expand Down
18 changes: 16 additions & 2 deletions src/commands/hook/CreateHookCommand.js
@@ -1,22 +1,35 @@
const BarracksCommand = require('../BarracksCommand');

function getType(program) {
function getHookType(program) {
if (program.web) {
return 'web';
}
}

function getEventType(program) {
if (program.ping) {
return 'PING';
}
if (program.enrollment) {
return 'ENROLLMENT';
}
}

class CreateHookCommand extends BarracksCommand {

configureCommand(program) {
return program
.option('--ping', 'To create a hook triggered by the ping of a device.')
.option('--enrollment', 'To create a hook for the first ping of a device')
.option('--web', 'To create a web hook')
.option('--name [value]', 'The unique name of the webhook')
.option('--url [value]', 'The URL for this webhook');
}

validateCommand(program) {
return !!(
(!program.ping && program.enrollment ||
program.ping && !program.enrollment) &&
program.web &&
program.url && program.url !== true &&
program.name && program.name !== true
Expand All @@ -26,8 +39,9 @@ class CreateHookCommand extends BarracksCommand {
execute(program) {
return this.getAuthenticationToken().then(token => {
return this.barracks.createHook(token, {
type: getType(program),
type: getHookType(program),
name: program.name,
eventType: getEventType(program),
url: program.url
});
});
Expand Down
4 changes: 2 additions & 2 deletions tests/clients/HookClient.spec.js
Expand Up @@ -138,7 +138,7 @@ describe('hookClient', () => {
hookClient.getHook(token, hookName).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals('This is not the hook you are looking for.');
expect(err).to.be.equals('Hook "myHook" could not be found');
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('hookClient', () => {
hookClient.updateHook(token, hookName, newHook).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals('The hook you want to update does not exist.');
expect(err).to.be.equals('Hook "myHook" does not exists.');
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
Expand Down
62 changes: 59 additions & 3 deletions tests/commands/hook/CreateHookCommand.spec.js
Expand Up @@ -27,6 +27,7 @@ describe('CreateHookCommand', () => {

const token = 'i8uhkj.token.65ryft';
const programWithValidOptions = {
ping: true,
web: true,
name: 'HookName',
url: 'https://not.barracks.io'
Expand Down Expand Up @@ -77,9 +78,36 @@ describe('CreateHookCommand', () => {
expect(result).to.be.false;
});

it('should return false when url is missing', () => {
it('should return true when event type is ping', () => {
// Given
const program = Object.assign({}, programWithValidOptions, { url: undefined });
const program = Object.assign({}, programWithValidOptions, { ping: true });
// When
const result = createHookCommand.validateCommand(program);
// Then
expect(result).to.be.true;
});

it('should return true when event type is enrollment', () => {
// Given
const program = Object.assign({}, programWithValidOptions, { ping: false, enrollment: true });
// When
const result = createHookCommand.validateCommand(program);
// Then
expect(result).to.be.true;
});

it('should return false when event type is missing', () => {
// Given
const program = Object.assign({}, programWithValidOptions, { ping: undefined });
// When
const result = createHookCommand.validateCommand(program);
// Then
expect(result).to.be.false;
});

it('should return false when multiple event types', () => {
// Given
const program = Object.assign({}, programWithValidOptions, { ping: true, enrollment:true });
// When
const result = createHookCommand.validateCommand(program);
// Then
Expand All @@ -90,7 +118,7 @@ describe('CreateHookCommand', () => {

describe('#execute(program)', () => {

it('should return the created hook when the request was successful', done => {
it('should return the created hook when the request was successful and event type is ping', done => {
// Given
createHookCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token));
createHookCommand.barracks = {
Expand All @@ -104,6 +132,7 @@ describe('CreateHookCommand', () => {
expect(createHookCommand.getAuthenticationToken).to.have.been.calledWithExactly();
expect(createHookCommand.barracks.createHook).to.have.been.calledOnce;
expect(createHookCommand.barracks.createHook).to.have.been.calledWithExactly(token, {
eventType: 'PING',
type: 'web',
name: programWithValidOptions.name,
url: programWithValidOptions.url
Expand All @@ -114,6 +143,32 @@ describe('CreateHookCommand', () => {
});
});

it('should return the created hook when the request was successful and event type is enrollment', done => {
// Given
const program = Object.assign({}, programWithValidOptions, { ping: false, enrollment:true });
createHookCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token));
createHookCommand.barracks = {
createHook: sinon.stub().returns(Promise.resolve(createdHook))
};

// When / Then
createHookCommand.execute(program).then(result => {
expect(result).to.be.equals(createdHook);
expect(createHookCommand.getAuthenticationToken).to.have.been.calledOnce;
expect(createHookCommand.getAuthenticationToken).to.have.been.calledWithExactly();
expect(createHookCommand.barracks.createHook).to.have.been.calledOnce;
expect(createHookCommand.barracks.createHook).to.have.been.calledWithExactly(token, {
eventType: 'ENROLLMENT',
type: 'web',
name: program.name,
url: program.url
});
done();
}).catch(err => {
done(err);
});
});

it('should return error when the request failed', done => {
// Given
const error = 'Marche pas!!!';
Expand All @@ -132,6 +187,7 @@ describe('CreateHookCommand', () => {
expect(createHookCommand.getAuthenticationToken).to.have.been.calledWithExactly();
expect(createHookCommand.barracks.createHook).to.have.been.calledOnce;
expect(createHookCommand.barracks.createHook).to.have.been.calledWithExactly(token, {
eventType: 'PING',
type: 'web',
name: programWithValidOptions.name,
url: programWithValidOptions.url
Expand Down

0 comments on commit f4bc5dd

Please sign in to comment.