-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rémi
committed
Jul 17, 2017
1 parent
eab3f73
commit 98d16b7
Showing
3 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
}); | ||
|
||
}); | ||
}); |