From 058ac416eac3950292c27e02fa4eb348cecb05f7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 10:16:06 +0200 Subject: [PATCH 1/4] Test redis --- packages/server/src/integrations/redis.ts | 13 +++- .../datasources/validators/redis.spec.ts | 73 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 packages/server/src/sdk/tests/datasources/validators/redis.spec.ts diff --git a/packages/server/src/integrations/redis.ts b/packages/server/src/integrations/redis.ts index 73ef2bb55c..fd3c93125e 100644 --- a/packages/server/src/integrations/redis.ts +++ b/packages/server/src/integrations/redis.ts @@ -86,7 +86,7 @@ const SCHEMA: Integration = { class RedisIntegration { private readonly config: RedisConfig - private client: any + private client constructor(config: RedisConfig) { this.config = config @@ -99,6 +99,17 @@ class RedisIntegration { }) } + async testConnection() { + try { + await this.client.ping() + return true + } catch (e: any) { + return { error: e.message as string } + } finally { + await this.disconnect() + } + } + async disconnect() { return this.client.quit() } diff --git a/packages/server/src/sdk/tests/datasources/validators/redis.spec.ts b/packages/server/src/sdk/tests/datasources/validators/redis.spec.ts new file mode 100644 index 0000000000..48ae74779f --- /dev/null +++ b/packages/server/src/sdk/tests/datasources/validators/redis.spec.ts @@ -0,0 +1,73 @@ +import { generator } from "@budibase/backend-core/tests" +import redis from "../../../../integrations/redis" +import { GenericContainer } from "testcontainers" + +jest.unmock("pg") + +describe("datasource validators", () => { + describe("redis", () => { + describe("unsecured", () => { + let host: string + let port: number + + beforeAll(async () => { + const container = await new GenericContainer("redis") + .withExposedPorts(6379) + .start() + + host = container.getContainerIpAddress() + port = container.getMappedPort(6379) + }) + + it("test valid connection", async () => { + const integration = new redis.integration({ + host, + port, + username: "", + }) + const result = await integration.testConnection() + expect(result).toBe(true) + }) + + it("test invalid connection even with wrong user/password", async () => { + const integration = new redis.integration({ + host, + port, + username: generator.name(), + password: generator.hash(), + }) + const result = await integration.testConnection() + expect(result).toEqual({ + error: + "WRONGPASS invalid username-password pair or user is disabled.", + }) + }) + }) + + describe("secured", () => { + let host: string + let port: number + + beforeAll(async () => { + const container = await new GenericContainer("redis") + .withExposedPorts(6379) + .withCmd(["redis-server", "--requirepass", "P@ssW0rd!"]) + .start() + + host = container.getContainerIpAddress() + port = container.getMappedPort(6379) + }) + + it("test valid connection", async () => { + const integration = new redis.integration({ + host, + port, + username: "", + password: "P@ssW0rd!", + }) + const result = await integration.testConnection() + expect(result).toBe(true) + }) + }) + }) +}) From 47d8701578ed96a937248e7cb0b9a0c28987bffe Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 16:53:32 +0200 Subject: [PATCH 2/4] Move files --- .../src/integrations}/validators/redis.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {packages/server/src/sdk/tests/datasources => qa-core/src/integrations}/validators/redis.spec.ts (96%) diff --git a/packages/server/src/sdk/tests/datasources/validators/redis.spec.ts b/qa-core/src/integrations/validators/redis.spec.ts similarity index 96% rename from packages/server/src/sdk/tests/datasources/validators/redis.spec.ts rename to qa-core/src/integrations/validators/redis.spec.ts index 48ae74779f..d51a6d695a 100644 --- a/packages/server/src/sdk/tests/datasources/validators/redis.spec.ts +++ b/qa-core/src/integrations/validators/redis.spec.ts @@ -1,5 +1,5 @@ import { generator } from "@budibase/backend-core/tests" -import redis from "../../../../integrations/redis" +import redis from "../../../../packages/server/src/integrations/redis" import { GenericContainer } from "testcontainers" jest.unmock("pg") From 5cd3f676ac80feace8ec557afbd94a5076bc0930 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 16:53:55 +0200 Subject: [PATCH 3/4] Fix import --- qa-core/src/integrations/validators/redis.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qa-core/src/integrations/validators/redis.spec.ts b/qa-core/src/integrations/validators/redis.spec.ts index d51a6d695a..37ae648172 100644 --- a/qa-core/src/integrations/validators/redis.spec.ts +++ b/qa-core/src/integrations/validators/redis.spec.ts @@ -1,6 +1,6 @@ -import { generator } from "@budibase/backend-core/tests" import redis from "../../../../packages/server/src/integrations/redis" import { GenericContainer } from "testcontainers" +import { generator } from "../../shared" jest.unmock("pg") From 1c39946103e4b0e2eacce74582789a43cf378b4f Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 17:01:25 +0200 Subject: [PATCH 4/4] Clean code --- qa-core/src/integrations/validators/redis.spec.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/qa-core/src/integrations/validators/redis.spec.ts b/qa-core/src/integrations/validators/redis.spec.ts index 37ae648172..fd35f5d23e 100644 --- a/qa-core/src/integrations/validators/redis.spec.ts +++ b/qa-core/src/integrations/validators/redis.spec.ts @@ -2,8 +2,6 @@ import redis from "../../../../packages/server/src/integrations/redis" import { GenericContainer } from "testcontainers" import { generator } from "../../shared" -jest.unmock("pg") - describe("datasource validators", () => { describe("redis", () => { describe("unsecured", () => {