From 8a41278ee6f93cbe24a939d2c55191119461f61a Mon Sep 17 00:00:00 2001 From: Iqbal Ahmed Date: Fri, 10 May 2024 16:20:19 +0100 Subject: [PATCH] PP-11682 Add connector charge pact for the new Axios connector client - Updated the test to work with the connector Axios client. --- ...connector-axios-client-charge.pact.test.js | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/unit/clients/connector-axios-client-charge.pact.test.js diff --git a/test/unit/clients/connector-axios-client-charge.pact.test.js b/test/unit/clients/connector-axios-client-charge.pact.test.js new file mode 100644 index 000000000..ea327ade2 --- /dev/null +++ b/test/unit/clients/connector-axios-client-charge.pact.test.js @@ -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) + }) + }) +})