Skip to content

Commit

Permalink
Merge pull request #3888 from alphagov/pp-12853-create-axios-instance…
Browse files Browse the repository at this point in the history
…-first

PP-12853 Pass ProxyAgent to HttpsAgent for axios Apple Pay validation
  • Loading branch information
marcotranchino authored Aug 6, 2024
2 parents f1df7ab + cbf3e1b commit 46fa87a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ module.exports = async (req, res) => {
key: merchantIdentityVars.key
});

const proxyAgentOptions = {
cert: merchantIdentityVars.cert,
key: merchantIdentityVars.key
};

const proxyAgent = proxyUrl ? new HttpsProxyAgent(proxyUrl, proxyAgentOptions) : null;

if (proxyUrl) {
logger.info('Using proxy URL')
}
Expand Down Expand Up @@ -113,13 +106,22 @@ module.exports = async (req, res) => {
initiativeContext: process.env.APPLE_PAY_MERCHANT_DOMAIN
}

const alternativeOptions = {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
httpsAgent: proxyAgent
}

const httpsProxyAgent = new HttpsProxyAgent(proxyUrl);

const httpsAgent = new https.Agent({
cert: merchantIdentityVars.cert,
key: merchantIdentityVars.key,
proxy: httpsProxyAgent
});

const axiosInstance = axios.create({
httpsAgent: httpsAgent
});


try {
const response = await axios.post(url, data, alternativeOptions)
const response = await axiosInstance.post(url, data, { headers: { 'Content-Type': 'application/json; charset=utf-8' } })

logger.info('Apple Pay session successfully generated via axios and https proxy agent')
res.status(200).send(response.data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const sinon = require('sinon')
const proxyquire = require('proxyquire')
const https = require('https')
const { HttpsProxyAgent } = require('https-proxy-agent')

const merchantDomain = 'www.pymnt.uk'
const worldpayMerchantId = 'worldpay.merchant.id'
Expand Down Expand Up @@ -86,7 +85,11 @@ describe('Validate with Apple the merchant is legitimate', () => {
it('should return a payload for a Worldpay payment if Merchant is valid', async () => {
process.env.HTTPS_PROXY = 'https://fakeproxy.com'
const axiosPostStub = sinon.stub().resolves(appleResponse)
const axiosStub = { post: axiosPostStub }
const axiosCreateStub = sinon.stub().returns({ post: axiosPostStub })
const axiosStub = {
create: axiosCreateStub,
post: sinon.stub().resolves(appleResponse)
}
const controller = getControllerWithMocks(axiosStub)

const req = {
Expand All @@ -97,33 +100,35 @@ describe('Validate with Apple the merchant is legitimate', () => {
}
await controller(req, res)

sinon.assert.calledWith(axiosCreateStub, sinon.match({
httpsAgent: sinon.match.instanceOf(https.Agent)
}))

sinon.assert.calledWith(axiosPostStub,
sinon.match(url),
sinon.match({
cert: sinon.match(cert => cert.includes(worldpayCertificate)),
key: sinon.match(key => key.includes(worldpayKey)),
merchantIdentifier: worldpayMerchantId,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: merchantDomain
}),
sinon.match({
headers: { 'Content-Type': 'application/json; charset=utf-8' },
httpsAgent: sinon.match.instanceOf(HttpsProxyAgent)
headers: { 'Content-Type': 'application/json; charset=utf-8' }
})
)

const httpsAgentArg = axiosPostStub.getCall(0).args[2].httpsAgent
sinon.assert.match(httpsAgentArg.proxy.href, 'https://fakeproxy.com/')

sinon.assert.calledWith(res.status, 200)
sinon.assert.calledWith(sendSpy, appleResponse.data)
})

it('should return a payload for a Stripe payment if Merchant is valid', async () => {
process.env.HTTPS_PROXY = 'https://fakeproxy.com'
const axiosPostStub = sinon.stub().resolves({ data: appleResponse.data, status: 200 })
const axiosStub = { post: axiosPostStub }
const axiosCreateStub = sinon.stub().returns({ post: axiosPostStub })
const axiosStub = {
create: axiosCreateStub,
post: sinon.stub().resolves({ data: appleResponse.data, status: 200 })
}
const controller = getControllerWithMocks(axiosStub)

const req = {
Expand All @@ -134,25 +139,23 @@ describe('Validate with Apple the merchant is legitimate', () => {
}
await controller(req, res)

sinon.assert.calledWith(axiosCreateStub, sinon.match({
httpsAgent: sinon.match.instanceOf(https.Agent)
}))

sinon.assert.calledWith(axiosPostStub,
sinon.match(url),
sinon.match({
cert: sinon.match(cert => cert.includes(stripeCertificate)),
key: sinon.match(key => key.includes(stripeKey)),
merchantIdentifier: stripeMerchantId,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: merchantDomain
}),
sinon.match({
headers: { 'Content-Type': 'application/json; charset=utf-8' },
httpsAgent: sinon.match.instanceOf(HttpsProxyAgent)
headers: { 'Content-Type': 'application/json; charset=utf-8' }
})
)

const httpsAgentArg = axiosPostStub.getCall(0).args[2].httpsAgent
sinon.assert.match(httpsAgentArg.proxy.href, 'https://fakeproxy.com/')

sinon.assert.calledWith(res.status, 200)
sinon.assert.calledWith(sendSpy, appleResponse.data)
})
Expand All @@ -174,7 +177,11 @@ describe('Validate with Apple the merchant is legitimate', () => {
it('should return a payload for a Sandbox payment if Merchant is valid', async () => {
process.env.HTTPS_PROXY = 'https://fakeproxy.com'
const axiosPostStub = sinon.stub().resolves(appleResponse)
const axiosStub = { post: axiosPostStub }
const axiosCreateStub = sinon.stub().returns({ post: axiosPostStub })
const axiosStub = {
create: axiosCreateStub,
post: sinon.stub().resolves(appleResponse)
}
const controller = getControllerWithMocks(axiosStub)

const req = {
Expand All @@ -186,25 +193,23 @@ describe('Validate with Apple the merchant is legitimate', () => {

await controller(req, res)

sinon.assert.calledWith(axiosCreateStub, sinon.match({
httpsAgent: sinon.match.instanceOf(https.Agent)
}))

sinon.assert.calledWith(axiosPostStub,
sinon.match(url),
sinon.match({
cert: sinon.match(cert => cert.includes(worldpayCertificate)),
key: sinon.match(key => key.includes(worldpayKey)),
merchantIdentifier: worldpayMerchantId,
displayName: 'GOV.UK Pay',
initiative: 'web',
initiativeContext: merchantDomain
}),
sinon.match({
headers: { 'Content-Type': 'application/json; charset=utf-8' },
httpsAgent: sinon.match.instanceOf(HttpsProxyAgent)
headers: { 'Content-Type': 'application/json; charset=utf-8' }
})
)

const httpsAgentArg = axiosPostStub.getCall(0).args[2].httpsAgent
sinon.assert.match(httpsAgentArg.proxy.href, 'https://fakeproxy.com/')

sinon.assert.calledWith(res.status, 200)
sinon.assert.calledWith(sendSpy, appleResponse.data)
})
Expand Down

0 comments on commit 46fa87a

Please sign in to comment.