Skip to content

Commit

Permalink
PP-11681 Add unit tests for webhooks client.
Browse files Browse the repository at this point in the history
  • Loading branch information
JFSGDS committed Mar 12, 2024
1 parent 377d9b5 commit 86ddf6a
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
234 changes: 234 additions & 0 deletions app/services/clients/webhooks.client.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
})
1 change: 1 addition & 0 deletions test/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 86ddf6a

Please sign in to comment.