Skip to content

Commit

Permalink
mock dynanodb endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lailien3 committed Oct 17, 2024
1 parent edd306a commit c8bf535
Showing 1 changed file with 96 additions and 27 deletions.
123 changes: 96 additions & 27 deletions packages/connectors-lib/src/__tests__/aws.spec.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,128 @@
import Config from '../config.js'
const TEST_ENDPOINT = 'http://localhost:8080'
import { DynamoDB } from '@aws-sdk/client-dynamodb'
import AWS from 'aws-sdk'
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'
import Config from '../config'

jest.dontMock('aws-sdk')
describe('aws connectors', () => {
it('configures dynamodb with a custom endpoint if one is defined in configuration', async () => {
Config.aws.dynamodb.endpoint = TEST_ENDPOINT
const { ddb } = require('../aws.js').default()
expect(ddb.config.endpoint).toEqual(TEST_ENDPOINT)
jest.mock('aws-sdk', () => {
const SQS = jest.fn().mockImplementation((config) => ({
config: { ...config, apiVersion: '2012-11-05', region: config.region || 'eu-west-2' }
}))
const S3 = jest.fn().mockImplementation((config) => ({
config: { ...config, apiVersion: '2006-03-01', region: config.region || 'eu-west-2' }
}))
const SecretsManager = jest.fn().mockImplementation((config) => ({
config: { ...config, apiVersion: '2017-10-17', region: config.region || 'eu-west-2' }
}))

return { SQS, S3, SecretsManager }
})

jest.mock('@aws-sdk/client-dynamodb')
jest.mock('@aws-sdk/lib-dynamodb', () => ({
DynamoDBDocument: {
from: jest.fn()
}
}))

describe('AWS Connectors', () => {
let SQS, S3, SecretsManager

beforeEach(() => {
DynamoDB.mockClear()
DynamoDBDocument.from.mockClear()

DynamoDBDocument.from.mockReturnValue({
send: jest.fn(),
queryAllPromise: jest.fn(),
scanAllPromise: jest.fn(),
batchWriteAllPromise: jest.fn(),
createUpdateExpression: jest.fn(),
})

SQS = AWS.SQS
S3 = AWS.S3
SecretsManager = AWS.SecretsManager

SQS.mockClear()
S3.mockClear()
SecretsManager.mockClear()
})

it('configures sqs with a custom endpoint if one is defined in configuration', async () => {
Config.aws.sqs.endpoint = TEST_ENDPOINT
const { sqs } = require('../aws.js').default()
expect(sqs.config.endpoint).toEqual(TEST_ENDPOINT)
it('configures dynamodb with a custom endpoint if one is defined in configuration', () => {
const TEST_ENDPOINT = 'http://localhost:8080'
Config.aws.dynamodb.endpoint = TEST_ENDPOINT
const { ddb, docClient } = require('../aws.js').default()
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', async () => {
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
const { ddb } = require('../aws.js').default()
expect(ddb.config.endpoint).toEqual('dynamodb.eu-west-2.amazonaws.com')
const { ddb, docClient } = require('../aws.js').default()
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
const { sqs } = require('../aws.js').default()
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', async () => {
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
const { sqs } = require('../aws.js').default()
expect(sqs.config.endpoint).toEqual('sqs.eu-west-2.amazonaws.com')
expect(SQS).toHaveBeenCalledWith(expect.objectContaining({
region: 'eu-west-2'
}))
})

it('configures s3 with a custom endpoint if one is defined in configuration', async () => {
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
const { s3 } = require('../aws.js').default()
expect(s3.config.endpoint).toEqual(TEST_ENDPOINT)
expect(s3.config.s3ForcePathStyle).toBeTruthy()
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', async () => {
it('uses default s3 settings if a custom endpoint is not defined', () => {
process.env.AWS_REGION = 'eu-west-2'
delete Config.aws.s3.endpoint
const { s3 } = require('../aws.js').default()
expect(s3.config.endpoint).toEqual('s3.eu-west-2.amazonaws.com')
expect(s3.config.s3ForcePathStyle).toBeFalsy()
expect(S3).toHaveBeenCalledWith(expect.objectContaining({
region: 'eu-west-2'
}))
})

it('configures secretsmanager with a custom endpoint if one is defined in configuration', async () => {
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
const { secretsManager } = require('../aws.js').default()
expect(secretsManager.config.endpoint).toEqual(TEST_ENDPOINT)
expect(SecretsManager).toHaveBeenCalledWith(expect.objectContaining({
endpoint: TEST_ENDPOINT,
region: 'eu-west-2'
}))
})

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

0 comments on commit c8bf535

Please sign in to comment.