From 1e505791c05d882a3f9ffdda71113259f28364b4 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 17:37:28 +0200 Subject: [PATCH 1/3] Implement s3 checks --- packages/server/src/integrations/s3.ts | 15 ++++++-- .../src/integrations/validators/s3.spec.ts | 37 +++++++++++++++++++ qa-core/src/shared/generator.ts | 2 +- 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 qa-core/src/integrations/validators/s3.spec.ts diff --git a/packages/server/src/integrations/s3.ts b/packages/server/src/integrations/s3.ts index ad3bb09109..96b745ae08 100644 --- a/packages/server/src/integrations/s3.ts +++ b/packages/server/src/integrations/s3.ts @@ -5,8 +5,8 @@ import { DatasourceFieldType, } from "@budibase/types" -const AWS = require("aws-sdk") -const csv = require("csvtojson") +import AWS from "aws-sdk" +import csv from "csvtojson" interface S3Config { region: string @@ -152,7 +152,7 @@ const SCHEMA: Integration = { class S3Integration implements IntegrationBase { private readonly config: S3Config - private client: any + private client constructor(config: S3Config) { this.config = config @@ -165,6 +165,15 @@ class S3Integration implements IntegrationBase { this.client = new AWS.S3(this.config) } + async testConnection() { + try { + const buckets = await this.client.listBuckets().promise() + return true + } catch (e: any) { + return { error: e.message as string } + } + } + async create(query: { bucket: string location: string diff --git a/qa-core/src/integrations/validators/s3.spec.ts b/qa-core/src/integrations/validators/s3.spec.ts new file mode 100644 index 0000000000..bd76bea26a --- /dev/null +++ b/qa-core/src/integrations/validators/s3.spec.ts @@ -0,0 +1,37 @@ +import s3 from "../../../../packages/server/src/integrations/s3" +import { GenericContainer } from "testcontainers" +import { generator } from "../../shared" + +jest.unmock("aws-sdk") + +describe("datasource validators", () => { + describe("s3", () => { + let host: string + let port: number + + beforeAll(async () => { + const container = await new GenericContainer("localstack/localstack") + .withExposedPorts(4566) + .withEnv("SERVICES", "s3") + .withEnv("DEFAULT_REGION", "eu-west-1") + .withEnv("AWS_ACCESS_KEY_ID", "testkey") + .withEnv("AWS_SECRET_ACCESS_KEY", "testsecret") + .start() + + host = container.getContainerIpAddress() + port = container.getMappedPort(4566) + }) + + it("test valid connection", async () => { + const integration = new s3.integration({ + region: "eu-west-1", + accessKeyId: "testkey", + secretAccessKey: "testsecret", + s3ForcePathStyle: false, + endpoint: `http://${host}:${port}`, + }) + const result = await integration.testConnection() + expect(result).toBe(true) + }) + }) +}) diff --git a/qa-core/src/shared/generator.ts b/qa-core/src/shared/generator.ts index c9395f7e47..1789fc0f75 100644 --- a/qa-core/src/shared/generator.ts +++ b/qa-core/src/shared/generator.ts @@ -1,3 +1,3 @@ -const Chance = require("chance") +import Chance from "chance" export default new Chance() From d893fdf6cff661c1a0a6610a62783debba6a2791 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 17:40:40 +0200 Subject: [PATCH 2/3] Test wrong endpoint --- qa-core/src/integrations/validators/s3.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/qa-core/src/integrations/validators/s3.spec.ts b/qa-core/src/integrations/validators/s3.spec.ts index bd76bea26a..57c2377c76 100644 --- a/qa-core/src/integrations/validators/s3.spec.ts +++ b/qa-core/src/integrations/validators/s3.spec.ts @@ -33,5 +33,20 @@ describe("datasource validators", () => { const result = await integration.testConnection() expect(result).toBe(true) }) + + it("test wrong endpoint", async () => { + const integration = new s3.integration({ + region: "eu-west-2", + accessKeyId: "testkey", + secretAccessKey: "testsecret", + s3ForcePathStyle: false, + endpoint: `http://wrong:123`, + }) + const result = await integration.testConnection() + expect(result).toEqual({ + error: + "Inaccessible host: `wrong' at port `undefined'. This service may not be available in the `eu-west-2' region.", + }) + }) }) }) From 79b30942f6666b9cbbe6054cbb4b83dd05e4c77d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 17:52:10 +0200 Subject: [PATCH 3/3] Clean code --- qa-core/src/integrations/validators/s3.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/qa-core/src/integrations/validators/s3.spec.ts b/qa-core/src/integrations/validators/s3.spec.ts index 57c2377c76..6ed5c44449 100644 --- a/qa-core/src/integrations/validators/s3.spec.ts +++ b/qa-core/src/integrations/validators/s3.spec.ts @@ -1,6 +1,5 @@ import s3 from "../../../../packages/server/src/integrations/s3" import { GenericContainer } from "testcontainers" -import { generator } from "../../shared" jest.unmock("aws-sdk")