Skip to content

Commit

Permalink
PP-11681 Replace ‘base-client’ with ‘axios-base-client’ review changes.
Browse files Browse the repository at this point in the history
- Add pact tests for webhooks messages.
- Linting fixes adding/removing spacing.
  • Loading branch information
JFSGDS committed Mar 5, 2024
1 parent df2cfa3 commit c5b8ae3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
21 changes: 11 additions & 10 deletions app/services/clients/webhooks.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -48,26 +48,27 @@ 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')
return response.data
}

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')
return response.data
}

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)
Expand All @@ -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')
Expand All @@ -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')
Expand Down
28 changes: 28 additions & 0 deletions test/pact/webhooks-client/webhooks.pact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
})
})
})

0 comments on commit c5b8ae3

Please sign in to comment.