From 058ac416eac3950292c27e02fa4eb348cecb05f7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 15 May 2023 10:16:06 +0200 Subject: [PATCH] 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) + }) + }) + }) +})