Skip to content

Commit

Permalink
feat: reworked getting env, updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sshelomentsev committed Feb 26, 2024
1 parent 0fcf92e commit 0b29764
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 deletions.
24 changes: 15 additions & 9 deletions mgmt-lambda/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,23 @@ export async function handler(event: APIGatewayProxyEventV2WithRequestContext<AP
}

function loadDeploymentSettings(): DeploymentSettings {
const cfDistributionId = process.env.CFDistributionId
if (!cfDistributionId) {
throw new Error('load failed: environment variable CFDistributionId not found')
const missedVariables = []
const cfDistributionId = process.env.CFDistributionId || ''
if (cfDistributionId === '') {
missedVariables.push('CFDistributionId')
}
const lambdaFunctionName = process.env.LambdaFunctionName
if (!lambdaFunctionName) {
throw new Error('load failed: environment variable LambdaFunctionName not found')
const lambdaFunctionName = process.env.LambdaFunctionName || ''
if (lambdaFunctionName === '') {
missedVariables.push('LambdaFunctionName')
}
const lambdaFunctionArn = process.env.LambdaFunctionArn
if (!lambdaFunctionArn) {
throw new Error('load failed: environment variable LambdaFunctionArn not found')
const lambdaFunctionArn = process.env.LambdaFunctionArn || ''
if (lambdaFunctionArn === '') {
missedVariables.push('LambdaFunctionArn')
}

if (missedVariables.length > 0) {
const vars = missedVariables.join(', ')
throw new Error(`environment variables not found: ${vars}`)
}

const settings: DeploymentSettings = {
Expand Down
2 changes: 1 addition & 1 deletion mgmt-lambda/handlers/errorHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function handleWrongConfiguration(error: any): Promise<APIGatewayPr
const body = {
status:
'Wrong function configuration. Check environment variables for Lambda@Edge function and CloudFront Distribution id',
error: error,
error: error.message || error,
}
return {
statusCode: 500,
Expand Down
78 changes: 70 additions & 8 deletions mgmt-lambda/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const secretName = 'fingerprint-mgmt-lambda-auth-settings'
const correctToken = 'qazwsx123edc456'
const wrongToken = 'wrong-token'

const OLD_ENV = process.env

describe('Basic test', () => {
const OLD_ENV = process.env
beforeEach(() => {
jest.resetModules()
lambdaMock.reset()
Expand Down Expand Up @@ -43,13 +44,6 @@ describe('Basic test', () => {
expect(result.statusCode).toBe(401)
})

test('no secret env var', async () => {
const event = generateStatusRequest(correctToken)

const result = await handler(event)
expect(result.statusCode).toBe(500)
})

test('no update configuration env vars', async () => {
setSecretEnv()
mockSecret(correctToken)
Expand Down Expand Up @@ -83,6 +77,74 @@ describe('Basic test', () => {
})
})

describe('Check environment', () => {
beforeEach(() => {
jest.resetModules()
lambdaMock.reset()
secretMock.reset()
process.env = { ...OLD_ENV }
})

test('no secret env var', async () => {
const event = generateStatusRequest(correctToken)

const result = await handler(event)
expect(result.statusCode).toBe(500)
})

test('no deployment config', async () => {
setSecretEnv()
mockSecret(correctToken)

const event = generateStatusRequest(correctToken)

const result = await handler(event)
expect(result.statusCode).toBe(500)
const response = JSON.parse(result.body)
expect(response.status).toBe(
'Wrong function configuration. Check environment variables for Lambda@Edge function and CloudFront Distribution id',
)
expect(response.error).toBe(
'environment variables not found: CFDistributionId, LambdaFunctionName, LambdaFunctionArn',
)
})

test('CFDistributionConfig is not defined', async () => {
setSecretEnv()
process.env.LambdaFunctionName = 'arn:aws:lambda:us-east-1:1234567890:function:fingerprint-pro-lambda-function'
process.env.LambdaFunctionArn = 'fingerprint-pro-lambda-function'
mockSecret(correctToken)

const event = generateStatusRequest(correctToken)

const result = await handler(event)
expect(result.statusCode).toBe(500)
const response = JSON.parse(result.body)
expect(response.status).toBe(
'Wrong function configuration. Check environment variables for Lambda@Edge function and CloudFront Distribution id',
)
expect(response.error).toBe('environment variables not found: CFDistributionId')
})

test('CFDistributionConfig is defined, but empty', async () => {
setSecretEnv()
process.env.CFDistributionId = ''
process.env.LambdaFunctionName = 'arn:aws:lambda:us-east-1:1234567890:function:fingerprint-pro-lambda-function'
process.env.LambdaFunctionArn = 'fingerprint-pro-lambda-function'
mockSecret(correctToken)

const event = generateStatusRequest(correctToken)

const result = await handler(event)
expect(result.statusCode).toBe(500)
const response = JSON.parse(result.body)
expect(response.status).toBe(
'Wrong function configuration. Check environment variables for Lambda@Edge function and CloudFront Distribution id',
)
expect(response.error).toBe('environment variables not found: CFDistributionId')
})
})

function setSecretEnv() {
process.env.SettingsSecretName = secretName
}
Expand Down

0 comments on commit 0b29764

Please sign in to comment.