Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect implementation and test for function signatureUtils::fetchPemEncodedPublicKeys #57

Closed
shazron opened this issue Aug 18, 2023 · 1 comment · Fixed by #65
Closed
Labels
bug Something isn't working

Comments

@shazron
Copy link
Member

shazron commented Aug 18, 2023

Function signatureUtils::fetchPemEncodedPublicKeys :

/**
* Feteched the pem encoded public keys either from the state lib cache or directly via cloud front url
*
* @param {*} pubKeyUrl1 cloud front url of format https://static.adobeioevents.com/prod/keys/pub-key-<random-uuid>.pem
* @param {*} pubKeyUrl2 cloud front url of format https://static.adobeioevents.com/prod/keys/pub-key-<random-uuid>.pem
* @returns {Array} of two public keys
*/
async function fetchPemEncodedPublicKeys (pubKeyUrl1, pubKeyUrl2) {
let pubKey1Pem, pubKey2Pem
try {
const state = await stateLib.init()
pubKey1Pem = await fetchPubKeyFromCacheOrApi(pubKeyUrl1, state)
pubKey2Pem = await fetchPubKeyFromCacheOrApi(pubKeyUrl2, state)
} catch (error) {
logger.error('error occurred while fetching pem encoded public keys either from cache or public key urls due to %s', error.message)
return helpers.exportFunctions.genErrorResponse(500, 'Error occurred while fetching pem encoded Public Key')
}
return [pubKey1Pem, pubKey2Pem]
}

The problematic item is the catch() clause. It returns helpers.exportFunctions.genErrorResponse(500, 'Error occurred while fetching pem encoded Public Key'), which has a problem:

  1. helpers.exportFunctions.genErrorResponse returns an object, and this function explicitly says it should return an Array of (two) strings (tuple).

This therefore renders the test "Test Fetch Pem Encoded Public Keys -> verify for invalid pub key url throws" in signatureUtils.test.js as an invalid test since it will never throw an error. In fact the test is bogus - it doesn't really test the (mocked) return value - it tests an error that it sets up itself.

The test is here:

describe('Test Fetch Pem Encoded Public Keys', () => {
it('verify for invalid pub key url throws', async () => {
const invalidCloudFrontUrl = undefined
fetch.mockImplementation(() => {
return Promise.resolve(Error)
})
await signatureUtils.fetchPemEncodedPublicKeys(invalidCloudFrontUrl)
.then(res => {
throw new Error('invalid url')
})
.catch(e => {
// eslint-disable-next-line jest/no-conditional-expect
expect(e instanceof Error).toBe(true)
// eslint-disable-next-line jest/no-conditional-expect
expect(e.message).toEqual('invalid url')
})
})
})

@shazron shazron added the bug Something isn't working label Aug 18, 2023
@aiojbot
Copy link
Collaborator

aiojbot commented Aug 18, 2023

JIRA issue created: https://jira.corp.adobe.com/browse/ACNA-2498

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants