From 901bff5399ea8c54877d0814db2cc12a229183fa Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 12 May 2023 11:17:14 +0200 Subject: [PATCH 1/3] Validate arango --- packages/server/src/integrations/arangodb.ts | 21 ++++++-- .../integrations/validators/arango.spec.ts | 48 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 qa-core/src/integrations/validators/arango.spec.ts diff --git a/packages/server/src/integrations/arangodb.ts b/packages/server/src/integrations/arangodb.ts index e28940f36e..31bbffe060 100644 --- a/packages/server/src/integrations/arangodb.ts +++ b/packages/server/src/integrations/arangodb.ts @@ -5,9 +5,9 @@ import { IntegrationBase, } from "@budibase/types" -const { Database, aql } = require("arangojs") +import { Database, aql } from "arangojs" -interface ArangodbConfig { +export interface ArangodbConfig { url: string username: string password: string @@ -58,7 +58,7 @@ const SCHEMA: Integration = { class ArangoDBIntegration implements IntegrationBase { private config: ArangodbConfig - private client: any + private client constructor(config: ArangodbConfig) { const newConfig = { @@ -102,9 +102,24 @@ class ArangoDBIntegration implements IntegrationBase { this.client.close() } } + + async check() { + await this.client.get() + } +} + +async function validateConnection(config: ArangodbConfig) { + const integration = new ArangoDBIntegration(config) + try { + await integration.check() + return true + } catch (e: any) { + return { error: e.message as string } + } } export default { schema: SCHEMA, integration: ArangoDBIntegration, + validateConnection, } diff --git a/qa-core/src/integrations/validators/arango.spec.ts b/qa-core/src/integrations/validators/arango.spec.ts new file mode 100644 index 0000000000..b865df6147 --- /dev/null +++ b/qa-core/src/integrations/validators/arango.spec.ts @@ -0,0 +1,48 @@ +import { GenericContainer, Wait } from "testcontainers" +import arangodb from "../../../../packages/server/src/integrations/arangodb" +import { generator } from "../../shared" + +jest.unmock("arangojs") + +describe("datasource validators", () => { + describe("arangodb", () => { + const validator = integrations.getValidator[SourceName.ARANGODB] + + let connectionSettings: { + user: string + password: string + url: string + } + + beforeAll(async () => { + const user = "root" + const password = generator.hash() + const container = await new GenericContainer("arangodb") + .withExposedPorts(8529) + .withEnv("ARANGO_ROOT_PASSWORD", password) + .withWaitStrategy( + Wait.forLogMessage("is ready for business. Have fun!") + ) + .start() + + connectionSettings = { + user, + password, + url: `http://${container.getContainerIpAddress()}:${container.getMappedPort( + 8529 + )}`, + } + }) + + it("test valid connection string", async () => { + const result = await validator({ + url: connectionSettings.url, + username: connectionSettings.user, + password: connectionSettings.password, + databaseName: "", + collection: "", + }) + expect(result).toBe(true) + }) + }) +}) From b8d11fa351e67adb2622f9986d1305d05ec17585 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 12 May 2023 11:23:51 +0200 Subject: [PATCH 2/3] Test bad connections --- .../integrations/validators/arango.spec.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/qa-core/src/integrations/validators/arango.spec.ts b/qa-core/src/integrations/validators/arango.spec.ts index b865df6147..716fc19087 100644 --- a/qa-core/src/integrations/validators/arango.spec.ts +++ b/qa-core/src/integrations/validators/arango.spec.ts @@ -44,5 +44,31 @@ describe("datasource validators", () => { }) expect(result).toBe(true) }) + + it("test wrong password", async () => { + const result = await validator({ + url: connectionSettings.url, + username: connectionSettings.user, + password: "wrong", + databaseName: "", + collection: "", + }) + expect(result).toEqual({ + error: "not authorized to execute this request", + }) + }) + + it("test wrong url", async () => { + const result = await validator({ + url: "http://not.here", + username: connectionSettings.user, + password: connectionSettings.password, + databaseName: "", + collection: "", + }) + expect(result).toEqual({ + error: "getaddrinfo ENOTFOUND not.here", + }) + }) }) }) From fad57db634e0675740c6e61cc7b368b92253984b Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 12 May 2023 13:07:45 +0200 Subject: [PATCH 3/3] Implement the check as part of the integration --- packages/server/src/integrations/arangodb.ts | 26 +++++++------------ .../integrations/validators/arango.spec.ts | 11 ++++---- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/packages/server/src/integrations/arangodb.ts b/packages/server/src/integrations/arangodb.ts index 31bbffe060..297a164383 100644 --- a/packages/server/src/integrations/arangodb.ts +++ b/packages/server/src/integrations/arangodb.ts @@ -7,7 +7,7 @@ import { import { Database, aql } from "arangojs" -export interface ArangodbConfig { +interface ArangodbConfig { url: string username: string password: string @@ -74,6 +74,15 @@ class ArangoDBIntegration implements IntegrationBase { this.client = new Database(newConfig) } + async testConnection() { + try { + await this.client.get() + return true + } catch (e: any) { + return { error: e.message as string } + } + } + async read(query: { sql: any }) { try { const result = await this.client.query(query.sql) @@ -102,24 +111,9 @@ class ArangoDBIntegration implements IntegrationBase { this.client.close() } } - - async check() { - await this.client.get() - } -} - -async function validateConnection(config: ArangodbConfig) { - const integration = new ArangoDBIntegration(config) - try { - await integration.check() - return true - } catch (e: any) { - return { error: e.message as string } - } } export default { schema: SCHEMA, integration: ArangoDBIntegration, - validateConnection, } diff --git a/qa-core/src/integrations/validators/arango.spec.ts b/qa-core/src/integrations/validators/arango.spec.ts index 716fc19087..51282de13f 100644 --- a/qa-core/src/integrations/validators/arango.spec.ts +++ b/qa-core/src/integrations/validators/arango.spec.ts @@ -6,8 +6,6 @@ jest.unmock("arangojs") describe("datasource validators", () => { describe("arangodb", () => { - const validator = integrations.getValidator[SourceName.ARANGODB] - let connectionSettings: { user: string password: string @@ -35,37 +33,40 @@ describe("datasource validators", () => { }) it("test valid connection string", async () => { - const result = await validator({ + const integration = new arangodb.integration({ url: connectionSettings.url, username: connectionSettings.user, password: connectionSettings.password, databaseName: "", collection: "", }) + const result = await integration.testConnection() expect(result).toBe(true) }) it("test wrong password", async () => { - const result = await validator({ + const integration = new arangodb.integration({ url: connectionSettings.url, username: connectionSettings.user, password: "wrong", databaseName: "", collection: "", }) + const result = await integration.testConnection() expect(result).toEqual({ error: "not authorized to execute this request", }) }) it("test wrong url", async () => { - const result = await validator({ + const integration = new arangodb.integration({ url: "http://not.here", username: connectionSettings.user, password: connectionSettings.password, databaseName: "", collection: "", }) + const result = await integration.testConnection() expect(result).toEqual({ error: "getaddrinfo ENOTFOUND not.here", })