Skip to content

Commit

Permalink
Add tests for hook create
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémi committed Jul 17, 2017
1 parent eab3f73 commit 98d16b7
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/clients/BarracksClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ describe('Barracks', () => {
expect(barracksClient).to.have.property('scheduleUpdate').and.to.be.a('function');
});

it('should initialize methods from hookClient when constructor called', () => {
// When
const barracksClient = new Barracks();

// Then
expect(barracksClient).to.have.property('createHook').and.to.be.a('function');
});

it('should initialize methods from sdk proxy when constructor called', () => {
// When
const barracksClient = new Barracks();
Expand Down
84 changes: 84 additions & 0 deletions tests/clients/HookClient.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const HookClient = require('../../src/clients/HookClient');

chai.use(chaiAsPromised);
chai.use(sinonChai);

function buildSegment(segmentId) {
return {
id: segmentId,
name: 'Plop'
};
}

describe('hookClient', () => {

let hookClient;
const token = 'i8uhkj.token.65ryft';

beforeEach(() => {
hookClient = new HookClient();
hookClient.httpClient = {};
});

describe('#createHook()', () => {

it('should return an error message when request fails', done => {
// Given
const hook = { type: 'web', name: 'Hook', url: 'https://localhost/hookName' };
const error = { message: 'Error !' };
hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error));

// When / Then
hookClient.createHook(token, hook).then(result => {
done('should have failed');
}).catch(err => {
expect(err).to.be.equals(error.message);
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'POST',
path: '/api/dispatcher/hooks'
},
{
headers: { 'x-auth-token': token },
body: hook
}
);
done();
});
});

it('should return the created hook', done => {
// Given
const hook = { type: 'web', name: 'Hook', url: 'https://localhost/hookName' };
const savedHook = Object.assign({}, hook, { userId: '123456789' });
const response = { body: savedHook };
hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.resolve(response));

// When / Then
hookClient.createHook(token, hook).then(result => {
expect(result).to.be.equals(savedHook);
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce;
expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly(
{
method: 'POST',
path: '/api/dispatcher/hooks'
},
{
headers: { 'x-auth-token': token },
body: hook
}
);
done();
}).catch(err => {
done(err);
});
});
});

});
147 changes: 147 additions & 0 deletions tests/commands/hook/CreateHookCommand.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiAsPromised = require('chai-as-promised');
const proxyquire = require('proxyquire').noCallThru();

chai.use(chaiAsPromised);
chai.use(sinonChai);

describe('CreateHookCommand', () => {

let createHookCommand;
let proxyIsJsonObject;
let proxyFileExists;

const CreateHookCommand = proxyquire('../../../src/commands/hook/CreateHookCommand', {
'../../utils/Validator': {
isJsonObject: (str) => {
return proxyIsJsonObject(str);
},
fileExists: (path) => {
return proxyFileExists(path);
}
}
});

const token = 'i8uhkj.token.65ryft';
const programWithValidOptions = {
web: true,
name: 'HookName',
url: 'https://not.barracks.io'
};
const createdHook = {
type: 'web',
name: 'HookName',
url: 'https://not.barracks.io',
userId: '12345'
};

before(() => {
createHookCommand = new CreateHookCommand();
createHookCommand.barracks = {};
createHookCommand.userConfiguration = {};
proxyIsJsonObject = undefined;
roxyFileExists = undefined;
});

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

it('should return true when all the options are valid and present', () => {
// Given
const program = Object.assign({}, programWithValidOptions);

// When
const result = createHookCommand.validateCommand(program);

// Then
expect(result).to.be.true;
});

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

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

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

});

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

it('should return the created hook when the request was successful', done => {
// Given
createHookCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token));
createHookCommand.barracks = {
createHook: sinon.stub().returns(Promise.resolve(createdHook))
};

// When / Then
createHookCommand.execute(programWithValidOptions).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, {
type: 'web',
name: programWithValidOptions.name,
url: programWithValidOptions.url
});
done();
}).catch(err => {
done(err);
});
});

it('should return error when the request failed', done => {
// Given
const error = 'Marche pas!!!';
createHookCommand.getAuthenticationToken = sinon.stub().returns(Promise.resolve(token));
createHookCommand.barracks = {
createHook: sinon.stub().returns(Promise.reject(error))
};

// When / Then
createHookCommand.execute(programWithValidOptions).then(result => {
done('Should have failed');
}).catch(err => {
try {
expect(err).to.be.equals(error);
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, {
type: 'web',
name: programWithValidOptions.name,
url: programWithValidOptions.url
});
done();
} catch (e) {
done(e);
}
});
});

});
});

0 comments on commit 98d16b7

Please sign in to comment.