From 86ddf6a0a4802b7143d2a32031b1d929659bfc8d Mon Sep 17 00:00:00 2001 From: John Shields Date: Tue, 12 Mar 2024 14:51:21 +0000 Subject: [PATCH] PP-11681 Add unit tests for webhooks client. --- app/services/clients/webhooks.client.test.js | 234 +++++++++++++++++++ test/test.env | 1 + 2 files changed, 235 insertions(+) create mode 100644 app/services/clients/webhooks.client.test.js diff --git a/app/services/clients/webhooks.client.test.js b/app/services/clients/webhooks.client.test.js new file mode 100644 index 0000000000..cd526f7c68 --- /dev/null +++ b/app/services/clients/webhooks.client.test.js @@ -0,0 +1,234 @@ +'use strict' + +const sinon = require('sinon') +const proxyquire = require('proxyquire') +const { expect } = require('chai') + +const configureSpy = sinon.spy() + +class MockClient { + configure (baseUrl, options) { + configureSpy(baseUrl, options) + } + + async get (url, description) { + const dataResponse = {} + return Promise.resolve({ data: dataResponse }) + } + + async post (url, description) { + const dataResponse = {} + return Promise.resolve({ data: dataResponse }) + } + + async patch (url, description) { + const dataResponse = {} + return Promise.resolve({ data: dataResponse }) + } +} + +function getWebhooksClient () { + return proxyquire('./webhooks.client', { + '@govuk-pay/pay-js-commons/lib/utils/axios-base-client/axios-base-client': { Client: MockClient } + }) +} + +describe('Webhook client', () => { + describe('webhook function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.webhook('id', 'a-service-id', 'a-gateway-account-id', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/id?service_id=a-service-id&gateway_account_id=a-gateway-account-id') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.webhook('id', 'a-service-id', 'a-gateway-account-id', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/id?service_id=a-service-id&gateway_account_id=a-gateway-account-id') + }) + }) + + describe('signingSecret function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.signingSecret('id', 'a-service-id', 'a-gateway-account-id', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/id/signing-key?service_id=a-service-id&gateway_account_id=a-gateway-account-id') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.signingSecret('id', 'a-service-id', 'a-gateway-account-id', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/id/signing-key?service_id=a-service-id&gateway_account_id=a-gateway-account-id') + }) + }) + + describe('webhooks function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.webhooks('a-service-id', 'a-gateway-account-id', 'isLive', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook?service_id=a-service-id&gateway_account_id=a-gateway-account-id&live=isLive') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.webhooks('a-service-id', 'a-gateway-account-id', 'isLive', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook?service_id=a-service-id&gateway_account_id=a-gateway-account-id&live=isLive') + }) + }) + + describe('message function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.message('id', 'a-webhook-id', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/a-webhook-id/message/id') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.message('id', 'a-webhook-id', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/a-webhook-id/message/id') + }) + }) + + describe('attempts function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.attempts('id', 'a-webhook-id', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/a-webhook-id/message/id/attempt') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.attempts('id', 'a-webhook-id', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/a-webhook-id/message/id/attempt') + }) + }) + + describe('messages function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.messages('id', { page: 1 }) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/id/message?page=1') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.messages('id', { baseUrl: 'https://example.com', page: 1 }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/id/message?page=1') + }) + }) + + describe('createWebhook function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.createWebhook('a-service-id', 'a-gateway-account-id', 'isLive', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.createWebhook('a-service-id', 'a-gateway-account-id', 'isLive', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook') + }) + }) + + describe('updateWebhook function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.updateWebhook('id', 'a-service-id', 'a-gateway-account-id', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/id?service_id=a-service-id&gateway_account_id=a-gateway-account-id') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.updateWebhook('id', 'a-service-id', 'a-gateway-account-id', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/id?service_id=a-service-id&gateway_account_id=a-gateway-account-id') + }) + }) + + describe('resendWebhookMessage function', () => { + beforeEach(() => { + configureSpy.resetHistory() + }) + + it('should use default base URL when base URL has not been set', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.resendWebhookMessage('a-webhook-id', 'a-message-id', {}) + + expect(configureSpy.getCall(0).args[0]).to.equal('http://127.0.0.1:8008/v1/webhook/a-webhook-id/message/a-message-id/resend') + }) + + it('should use configured base url', async () => { + const webhooksClient = getWebhooksClient() + + await webhooksClient.resendWebhookMessage('a-webhook-id', 'a-message-id', { baseUrl: 'https://example.com' }) + + expect(configureSpy.getCall(0).args[0]).to.equal('https://example.com/v1/webhook/a-webhook-id/message/a-message-id/resend') + }) + }) +}) diff --git a/test/test.env b/test/test.env index 6e8da07bf0..74feef3fca 100644 --- a/test/test.env +++ b/test/test.env @@ -4,6 +4,7 @@ PUBLIC_AUTH_URL=http://127.0.0.1:8003/v1/frontend/auth ADMINUSERS_URL=http://127.0.0.1:8002 PRODUCTS_URL=http://127.0.0.1:8004 LEDGER_URL=http://127.0.0.1:8006 +WEBHOOKS_URL=http://127.0.0.1:8008 DISABLE_INTERNAL_HTTPS=true COOKIE_MAX_AGE=10800000 SESSION_ENCRYPTION_KEY=naskjwefvwei72rjkwfmjwfi72rfkjwefmjwefiuwefjkbwfiu24fmjbwfk