Skip to content

Commit

Permalink
PP-11682 Add connector charge pact for the new Axios connector client
Browse files Browse the repository at this point in the history
- Updated the test to work with the connector Axios client.
  • Loading branch information
iqbalgds committed May 10, 2024
1 parent d7c75a3 commit 8a41278
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions test/unit/clients/connector-axios-client-charge.pact.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict'

// NPM dependencies
const path = require('path')
const { Pact } = require('@pact-foundation/pact')
const { expect } = require('chai')

// Local dependencies
const connectorClient = require('../../../app/services/clients/connector-axios.client')
const fixtures = require('../../fixtures/payment.fixtures')
const { PactInteractionBuilder } = require('../../test-helpers/pact/pact-interaction-builder')
const { pactify } = require('../../test-helpers/pact/pact-base')()

const TEST_CHARGE_ID = 'testChargeId'
const GET_CHARGE_URL = `/v1/frontend/charges/${TEST_CHARGE_ID}`
const AUTHORISE_CHARGE_URL = `/v1/frontend/charges/${TEST_CHARGE_ID}/cards`

const PORT = Math.floor(Math.random() * 48127) + 1024
const BASE_URL = `http://127.0.0.1:${PORT}`

describe('Connector client - charge tests', function () {
const provider = new Pact({
consumer: 'frontend',
provider: 'connector',
port: PORT,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
pactfileWriteMode: 'merge'
})

before(() => provider.setup())
after(() => provider.finalize())

describe('Authorisation request success', function () {
const authRequest = fixtures.validAuthorisationRequest()

before(() => {
const response = fixtures.validChargeCardDetailsAuthorised()
const builder = new PactInteractionBuilder(AUTHORISE_CHARGE_URL)
.withRequestBody(authRequest)
.withMethod('POST')
.withState('a sandbox account exists with a charge with id testChargeId that is in state ENTERING_CARD_DETAILS.')
.withUponReceiving('an authorisation request')
.withResponseBody(pactify(response))
.withStatusCode(200)
.build()
return provider.addInteraction(builder)
})

afterEach(() => provider.verify())

it('should return authorisation success', async () => {
try {
const res = await connectorClient({ baseUrl: BASE_URL }).chargeAuth({
chargeId: TEST_CHARGE_ID,
payload: authRequest
})

expect(res.data.status).to.be.equal('AUTHORISATION SUCCESS')
} catch (err) {
throw new Error('should not be hit: ' + JSON.stringify(err))
}
})
})

describe('Authorisation request without a billing address success', function () {
const authRequest = fixtures.validAuthorisationRequest({ noBillingAddress: true })

before(() => {
const response = fixtures.validChargeCardDetailsAuthorised()
const builder = new PactInteractionBuilder(AUTHORISE_CHARGE_URL)
.withRequestBody(authRequest)
.withMethod('POST')
.withState('a sandbox account exists with a charge with id testChargeId that is in state ENTERING_CARD_DETAILS.')
.withUponReceiving('an authorisation request without a billing address')
.withResponseBody(pactify(response))
.withStatusCode(200)
.build()
return provider.addInteraction(builder)
})

afterEach(() => provider.verify())

it('should return authorisation success', async () => {
const res = await connectorClient({ baseUrl: BASE_URL }).chargeAuth({
chargeId: TEST_CHARGE_ID,
payload: authRequest
})
expect(res.data.status).to.be.equal('AUTHORISATION SUCCESS')
})
})

describe('Find charge success', function () {
before(() => {
const response = fixtures.validChargeDetails({
chargeId: TEST_CHARGE_ID,
cardTypes: [{ brand: 'visa' }]
})

const builder = new PactInteractionBuilder(GET_CHARGE_URL)
.withMethod('GET')
.withState('a sandbox account exists with a charge with id testChargeId that is in state ENTERING_CARD_DETAILS.')
.withUponReceiving('a valid get charge request')
.withResponseBody(pactify(response))
.withStatusCode(200)
.build()
return provider.addInteraction(builder)
})

afterEach(() => provider.verify())

it('should return charge', async () => {
const res = await connectorClient({ baseUrl: BASE_URL }).findCharge({ chargeId: TEST_CHARGE_ID })
expect(res.data.charge_id).to.be.equal(TEST_CHARGE_ID)
})
})
})

0 comments on commit 8a41278

Please sign in to comment.