diff --git a/app/services/clients/webhooks.client.js b/app/services/clients/webhooks.client.js index 66831ec5ae..9391fdf3e4 100644 --- a/app/services/clients/webhooks.client.js +++ b/app/services/clients/webhooks.client.js @@ -11,7 +11,7 @@ const defaultRequestOptions = { } async function webhook (id, serviceId, gatewayAccountId, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook', id) + const url = urlJoin(defaultRequestOptions.baseUrl, '/v1/webhook', id) this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` configureClient(this.client, fullUrl) @@ -20,7 +20,7 @@ async function webhook (id, serviceId, gatewayAccountId, options = {}) { } async function signingSecret (webhookId, serviceId, gatewayAccountId, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook/', webhookId, '/signing-key') + const url = urlJoin(defaultRequestOptions.baseUrl, '/v1/webhook/', webhookId, '/signing-key') this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` configureClient(this.client, fullUrl) @@ -29,7 +29,7 @@ async function signingSecret (webhookId, serviceId, gatewayAccountId, options = } async function resetSigningSecret (webhookId, serviceId, gatewayAccountId, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook/', webhookId, '/signing-key') + const url = urlJoin(defaultRequestOptions.baseUrl, '/v1/webhook/', webhookId, '/signing-key') this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` configureClient(this.client, fullUrl) @@ -39,7 +39,7 @@ async function resetSigningSecret (webhookId, serviceId, gatewayAccountId, optio async function webhooks (serviceId, gatewayAccountId, isLive, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl - const url = urlJoin(baseUrl,'/v1/webhook') + const url = urlJoin(baseUrl, '/v1/webhook') this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}&live=${isLive}` configureClient(this.client, fullUrl) @@ -48,7 +48,7 @@ async function webhooks (serviceId, gatewayAccountId, isLive, options = {}) { } async function message (id, webhookId, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook', webhookId, 'message', id) + const url = urlJoin(defaultRequestOptions.baseUrl, '/v1/webhook', webhookId, 'message', id) this.client = new Client(defaultRequestOptions.service) configureClient(this.client, url) const response = await this.client.get(url, 'Get webhook message') @@ -56,7 +56,7 @@ async function message (id, webhookId, options = {}) { } async function attempts (messageId, webhookId, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook', webhookId, 'message', messageId, 'attempt') + const url = urlJoin(defaultRequestOptions.baseUrl, '/v1/webhook', webhookId, 'message', messageId, 'attempt') this.client = new Client(defaultRequestOptions.service) configureClient(this.client, url) const response = await this.client.get(url, 'Get webhook message delivery attempts') @@ -64,10 +64,11 @@ async function attempts (messageId, webhookId, options = {}) { } async function messages (id, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook', id, 'message') + const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl + const url = urlJoin(baseUrl, '/v1/webhook', id, 'message') this.client = new Client(defaultRequestOptions.service) let fullUrl = `${url}?page=${options.page}` - if ( options.status ) { + if (options.status) { fullUrl = `${fullUrl}&status=${options.status.toUpperCase()}` } configureClient(this.client, fullUrl) @@ -85,7 +86,7 @@ async function createWebhook (serviceId, gatewayAccountId, isLive, options = {}) description: options.description } const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl - const url = urlJoin(baseUrl,'/v1/webhook') + const url = urlJoin(baseUrl, '/v1/webhook') this.client = new Client(defaultRequestOptions.service) configureClient(this.client, url) const response = await this.client.post(url, body, 'Create a Webhook') @@ -109,7 +110,7 @@ async function updateWebhook (id, serviceId, gatewayAccountId, options = {}) { } async function resendWebhookMessage (webhookId, messageId, options = {}) { - const url = urlJoin(defaultRequestOptions.baseUrl,'/v1/webhook', webhookId, 'message', messageId, 'resend') + const url = urlJoin(defaultRequestOptions.baseUrl, '/v1/webhook', webhookId, 'message', messageId, 'resend') this.client = new Client(defaultRequestOptions.service) configureClient(this.client, url) const response = await this.client.post(url, {}, 'Schedule resending a message') diff --git a/test/pact/webhooks-client/webhooks.pact.test.js b/test/pact/webhooks-client/webhooks.pact.test.js index 64fbfd41db..40637ad5d0 100644 --- a/test/pact/webhooks-client/webhooks.pact.test.js +++ b/test/pact/webhooks-client/webhooks.pact.test.js @@ -27,6 +27,8 @@ const serviceId = 'an-external-service-id' const gatewayAccountId = 'an-external-account-id' const isLive = true const webhookId = 'an-external-webhook-id' +const status = 'FAILED' +const page = 1 describe('webhooks client', function () { let webhooksUrl @@ -97,4 +99,30 @@ describe('webhooks client', function () { }) }) }) + + describe('messages', () => { + before(() => { + return provider.addInteraction( + new PactInteractionBuilder(`/v1/webhook/${serviceId}/message`) + .withQuery('page', page) + .withQuery('status', status) + .withUponReceiving('a valid list of messages for webhook') + .withState('webhooks exist for given service id') + .withMethod('GET') + .withStatusCode(200) + .withResponseBody(pactify(webhookFixtures.webhooksListResponse([{ external_id: webhookId }]))) + .build() + ) + }) + + afterEach(() => provider.verify()) + + it('should get list of messages for webhook for a given service', () => { + return webhooksClient.messages(serviceId, { baseUrl: webhooksUrl, page, status }) + .then((response) => { + // asserts that the client has correctly formatted the request to match the stubbed fixture provider + expect(response[0].external_id).to.equal(webhookId) + }) + }) + }) })