Test dynamodb connection

This commit is contained in:
Adria Navarro 2023-05-12 18:34:56 +02:00
parent f2f0c2e708
commit 3933a3881b
3 changed files with 72 additions and 1 deletions

View File

@ -140,7 +140,7 @@ export function init(endpoint: string) {
docClient = new AWS.DynamoDB.DocumentClient(docClientParams) docClient = new AWS.DynamoDB.DocumentClient(docClientParams)
} }
if (!env.isProd()) { if (!env.isProd() && !env.isJest()) {
env._set("AWS_ACCESS_KEY_ID", "KEY_ID") env._set("AWS_ACCESS_KEY_ID", "KEY_ID")
env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY") env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY")
init("http://localhost:8333") init("http://localhost:8333")

View File

@ -149,6 +149,15 @@ class DynamoDBIntegration implements IntegrationBase {
this.client = new AWS.DynamoDB.DocumentClient(this.config) this.client = new AWS.DynamoDB.DocumentClient(this.config)
} }
async testConnection() {
try {
const scanRes = await new AWS.DynamoDB(this.config).listTables().promise()
return !!scanRes.$response
} catch (e: any) {
return { error: e.message as string }
}
}
async create(query: { async create(query: {
table: string table: string
json: Omit<DocumentClient.PutItemInput, "TableName"> json: Omit<DocumentClient.PutItemInput, "TableName">

View File

@ -0,0 +1,62 @@
import { GenericContainer } from "testcontainers"
import { env } from "@budibase/backend-core"
import dynamodb from "../../../../packages/server/src/integrations/dynamodb"
import { generator } from "../../shared"
jest.unmock("aws-sdk")
describe("datasource validators", () => {
describe("dynamodb", () => {
let connectionSettings: {
user: string
password: string
url: string
}
beforeAll(async () => {
const user = "root"
const password = generator.hash()
const container = await new GenericContainer("amazon/dynamodb-local")
.withExposedPorts(8000)
.start()
connectionSettings = {
user,
password,
url: `http://${container.getContainerIpAddress()}:${container.getMappedPort(
8000
)}`,
}
env._set("AWS_ACCESS_KEY_ID", "mocked_key")
env._set("AWS_SECRET_ACCESS_KEY", "mocked_secret")
})
it("test valid connection string", async () => {
const integration = new dynamodb.integration({
endpoint: connectionSettings.url,
region: "",
accessKeyId: "",
secretAccessKey: "",
})
const result = await integration.testConnection()
expect(result).toBe(true)
})
it("test wrong endpoint", async () => {
const integration = new dynamodb.integration({
endpoint: "http://wrong.url:2880",
region: "",
accessKeyId: "",
secretAccessKey: "",
})
const result = await integration.testConnection()
expect(result).toEqual({
error:
"Inaccessible host: `wrong.url' at port `undefined'. This service may not be available in the `eu-west-1' region.",
})
})
})
})