Skip to content

Commit

Permalink
Update rest of pay connector to toggle headers
Browse files Browse the repository at this point in the history
  • Loading branch information
irisfaraway committed Nov 18, 2024
1 parent cab2e27 commit f72f8c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
57 changes: 34 additions & 23 deletions packages/connectors-lib/src/__tests__/govuk-pay-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const headers = {
'content-type': 'application/json'
}

const recurringHeaders = {
accept: 'application/json',
authorization: `Bearer ${process.env.GOV_PAY_RECURRING_APIKEY}`,
'content-type': 'application/json'
}

describe('govuk-pay-api-connector', () => {
beforeEach(jest.clearAllMocks)

Expand Down Expand Up @@ -46,27 +52,12 @@ describe('govuk-pay-api-connector', () => {
it('uses the correct API key if recurring arg is set to true', async () => {
fetch.mockReturnValue({ ok: true, status: 200 })
await expect(govUkPayApi.createPayment({ cost: 0 }, true)).resolves.toEqual({ ok: true, status: 200 })
expect(fetch).toHaveBeenCalledWith(
'http://0.0.0.0/payment',
expect.objectContaining({
headers: expect.objectContaining({
authorization: `Bearer ${process.env.GOV_PAY_RECURRING_APIKEY}`
})
})
)
})

it('uses the correct API key if recurring arg is set to false', async () => {
fetch.mockReturnValue({ ok: true, status: 200 })
await expect(govUkPayApi.createPayment({ cost: 0 }, false)).resolves.toEqual({ ok: true, status: 200 })
expect(fetch).toHaveBeenCalledWith(
'http://0.0.0.0/payment',
expect.objectContaining({
headers: expect.objectContaining({
authorization: `Bearer ${process.env.GOV_PAY_APIKEY}`
})
})
)
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment', {
body: JSON.stringify({ cost: 0 }),
headers: recurringHeaders,
method: 'post',
timeout: 10000
})
})
})

Expand All @@ -90,6 +81,16 @@ describe('govuk-pay-api-connector', () => {
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123', { headers, method: 'get', timeout: 10000 })
expect(consoleErrorSpy).toHaveBeenCalled()
})

it('uses the correct API key if recurring arg is set to true', async () => {
fetch.mockReturnValue({ ok: true, status: 200, json: () => {} })
await expect(govUkPayApi.fetchPaymentStatus(123, true)).resolves.toEqual(expect.objectContaining({ ok: true, status: 200 }))
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123', {
headers: recurringHeaders,
method: 'get',
timeout: 10000
})
})
})

describe('fetchPaymentEvents', () => {
Expand All @@ -107,6 +108,16 @@ describe('govuk-pay-api-connector', () => {
await expect(govUkPayApi.fetchPaymentEvents(123)).rejects.toEqual(Error('test event error'))
expect(consoleErrorSpy).toHaveBeenCalled()
})

it('uses the correct API key if recurring arg is set to true', async () => {
fetch.mockReturnValue({ ok: true, status: 200, json: () => {} })
await expect(govUkPayApi.fetchPaymentEvents(123, true)).resolves.toEqual(expect.objectContaining({ ok: true, status: 200 }))
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/payment/123/events', {
headers: recurringHeaders,
method: 'get',
timeout: 10000
})
})
})

describe('createRecurringPayment', () => {
Expand All @@ -115,7 +126,7 @@ describe('govuk-pay-api-connector', () => {
await expect(govUkPayApi.createRecurringPayment({ cost: 0 })).resolves.toEqual({ ok: true, status: 200 })
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/agreement', {
body: JSON.stringify({ cost: 0 }),
headers,
headers: recurringHeaders,
method: 'post',
timeout: 10000
})
Expand All @@ -129,7 +140,7 @@ describe('govuk-pay-api-connector', () => {
expect(govUkPayApi.createRecurringPayment({ reference: '123' })).rejects.toEqual(Error(''))
expect(fetch).toHaveBeenCalledWith('http://0.0.0.0/agreement', {
body: JSON.stringify({ reference: '123' }),
headers,
headers: recurringHeaders,
method: 'post',
timeout: 10000
})
Expand Down
10 changes: 5 additions & 5 deletions packages/connectors-lib/src/govuk-pay-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const headers = (recurring = false) => ({
export const createRecurringPayment = async preparedPayment => {
try {
return fetch(process.env.GOV_PAY_RCP_API_URL, {
headers: headers(),
headers: headers(true),
method: 'post',
body: JSON.stringify(preparedPayment),
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
Expand Down Expand Up @@ -56,10 +56,10 @@ export const createPayment = async (preparedPayment, recurring = false) => {
* @param paymentId
* @returns {Promise<unknown>}
*/
export const fetchPaymentStatus = async paymentId => {
export const fetchPaymentStatus = async (paymentId, recurring = false) => {
try {
return fetch(`${process.env.GOV_PAY_API_URL}/${paymentId}`, {
headers: headers(),
headers: headers(recurring),
method: 'get',
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
})
Expand All @@ -74,10 +74,10 @@ export const fetchPaymentStatus = async paymentId => {
* @param paymentId
* @returns {Promise<unknown>}
*/
export const fetchPaymentEvents = async paymentId => {
export const fetchPaymentEvents = async (paymentId, recurring = false) => {
try {
return fetch(`${process.env.GOV_PAY_API_URL}/${paymentId}/events`, {
headers: headers(),
headers: headers(recurring),
method: 'get',
timeout: process.env.GOV_PAY_REQUEST_TIMEOUT_MS || GOV_PAY_REQUEST_TIMEOUT_MS_DEFAULT
})
Expand Down

0 comments on commit f72f8c4

Please sign in to comment.