Skip to content

Commit

Permalink
refactor aws.spec test
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottDormand96 committed Oct 17, 2024
1 parent 833714f commit 780efb8
Showing 1 changed file with 47 additions and 31 deletions.
78 changes: 47 additions & 31 deletions packages/connectors-lib/src/__tests__/aws.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'
import Config from '../config'

jest.mock('aws-sdk', () => {
const SQS = jest.fn().mockImplementation((config) => ({
const SQS = jest.fn().mockImplementation(config => ({
config: { ...config, apiVersion: '2012-11-05', region: config.region || 'eu-west-2' }
}))
const S3 = jest.fn().mockImplementation((config) => ({
const S3 = jest.fn().mockImplementation(config => ({
config: { ...config, apiVersion: '2006-03-01', region: config.region || 'eu-west-2', s3ForcePathStyle: config.s3ForcePathStyle }
}))
const SecretsManager = jest.fn().mockImplementation((config) => ({
const SecretsManager = jest.fn().mockImplementation(config => ({
config: { ...config, apiVersion: '2017-10-17', region: config.region || 'eu-west-2' }
}))

Expand Down Expand Up @@ -52,77 +52,93 @@ describe('AWS Connectors', () => {
const TEST_ENDPOINT = 'http://localhost:8080'
Config.aws.dynamodb.endpoint = TEST_ENDPOINT
require('../aws.js').default()
expect(DynamoDB).toHaveBeenCalledWith(expect.objectContaining({
endpoint: TEST_ENDPOINT
}))
expect(DynamoDB).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: TEST_ENDPOINT
})
)
expect(DynamoDBDocument.from).toHaveBeenCalledWith(expect.any(DynamoDB))
})

it('uses the default dynamodb endpoint if it is not overridden in configuration', () => {
process.env.AWS_REGION = 'eu-west-2'
delete Config.aws.dynamodb.endpoint
require('../aws.js').default()
expect(DynamoDB).toHaveBeenCalledWith(expect.objectContaining({
region: 'eu-west-2'
}))
expect(DynamoDB).toHaveBeenCalledWith(
expect.objectContaining({
region: 'eu-west-2'
})
)
expect(DynamoDBDocument.from).toHaveBeenCalledWith(expect.any(DynamoDB))
})

it('configures sqs with a custom endpoint if one is defined in configuration', () => {
const TEST_ENDPOINT = 'http://localhost:8080'
Config.aws.sqs.endpoint = TEST_ENDPOINT
require('../aws.js').default()
expect(SQS).toHaveBeenCalledWith(expect.objectContaining({
endpoint: TEST_ENDPOINT,
region: 'eu-west-2'
}))
expect(SQS).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: TEST_ENDPOINT,
region: 'eu-west-2'
})
)
})

it('uses the default sqs endpoint if it is not overridden in configuration', () => {
process.env.AWS_REGION = 'eu-west-2'
delete Config.aws.sqs.endpoint
require('../aws.js').default()
expect(SQS).toHaveBeenCalledWith(expect.objectContaining({
region: 'eu-west-2'
}))
expect(SQS).toHaveBeenCalledWith(
expect.objectContaining({
region: 'eu-west-2'
})
)
})

it('configures s3 with a custom endpoint if one is defined in configuration', () => {
const TEST_ENDPOINT = 'http://localhost:8080'
Config.aws.s3.endpoint = TEST_ENDPOINT
require('../aws.js').default()
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
endpoint: TEST_ENDPOINT,
s3ForcePathStyle: true,
region: 'eu-west-2'
}))
expect(S3).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: TEST_ENDPOINT,
s3ForcePathStyle: true,
region: 'eu-west-2'
})
)
})

it('uses default s3 settings if a custom endpoint is not defined', () => {
process.env.AWS_REGION = 'eu-west-2'
delete Config.aws.s3.endpoint
require('../aws.js').default()
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
region: 'eu-west-2'
}))
expect(S3).toHaveBeenCalledWith(
expect.objectContaining({
region: 'eu-west-2'
})
)
})

it('configures secretsmanager with a custom endpoint if one is defined in configuration', () => {
const TEST_ENDPOINT = 'http://localhost:8080'
Config.aws.secretsManager.endpoint = TEST_ENDPOINT
require('../aws.js').default()
expect(SecretsManager).toHaveBeenCalledWith(expect.objectContaining({
endpoint: TEST_ENDPOINT,
region: 'eu-west-2'
}))
expect(SecretsManager).toHaveBeenCalledWith(
expect.objectContaining({
endpoint: TEST_ENDPOINT,
region: 'eu-west-2'
})
)
})

it('uses default secretsmanager settings if a custom endpoint is not defined', () => {
process.env.AWS_REGION = 'eu-west-2'
delete Config.aws.secretsManager.endpoint
require('../aws.js').default()
expect(SecretsManager).toHaveBeenCalledWith(expect.objectContaining({
region: 'eu-west-2'
}))
expect(SecretsManager).toHaveBeenCalledWith(
expect.objectContaining({
region: 'eu-west-2'
})
)
})
})

0 comments on commit 780efb8

Please sign in to comment.