From 2a9f769a4835c227d5c2b109d8266fa4b0d5d3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81mi?= Date: Fri, 21 Jul 2017 17:09:29 -0400 Subject: [PATCH] Fixed imports and add tests --- src/clients/HookClient.js | 2 -- tests/clients/HookClient.spec.js | 52 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/clients/HookClient.js b/src/clients/HookClient.js index 96f0493..a7c1d4c 100644 --- a/src/clients/HookClient.js +++ b/src/clients/HookClient.js @@ -1,6 +1,4 @@ -const PageableStream = require('./PageableStream'); const HTTPClient = require('./HTTPClient'); -const logger = require('../utils/logger'); const endpoints = { createHook: { diff --git a/tests/clients/HookClient.spec.js b/tests/clients/HookClient.spec.js index bc19c17..4c9734e 100644 --- a/tests/clients/HookClient.spec.js +++ b/tests/clients/HookClient.spec.js @@ -53,6 +53,32 @@ describe('hookClient', () => { }); }); + it('should return an error message when request fails with statusCode 400', done => { + // Given + const hook = { type: 'web', name: 'Hook', url: 'https://localhost/hookName' }; + const error = { message: 'Error !', statusCode: 400 }; + 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('A hook with this name already exists.'); + 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' }; @@ -109,6 +135,32 @@ describe('hookClient', () => { }); }); + it('should return an error message when request fails with a 404', done => { + // Given + const name = 'aHook'; + const error = { message: 'Error !', statusCode: 404 }; + hookClient.httpClient.sendEndpointRequest = sinon.stub().returns(Promise.reject(error)); + + // When / Then + hookClient.deleteHook(token, name).then(result => { + done('should have failed'); + }).catch(err => { + expect(err).to.be.equals('The hook you are trying to remove does not exist.'); + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledOnce; + expect(hookClient.httpClient.sendEndpointRequest).to.have.been.calledWithExactly( + { + method: 'DELETE', + path: '/api/dispatcher/hooks/:hook' + }, + { + headers: { 'x-auth-token': token }, + pathVariables: { hook: name } + } + ); + done(); + }); + }); + it('should return nothing when hook is deleted', done => { // Given const name = 'aHook';