diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 20ba2acada..9e3d7344b4 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -360,6 +360,15 @@ class MongoIntegration implements IntegrationBase { this.client = new MongoClient(config.connectionString, options) } + async testConnection() { + try { + await this.connect() + return true + } catch (e: any) { + return { error: e.message as string } + } + } + async connect() { return this.client.connect() } diff --git a/qa-core/src/integrations/validators/mongo.spec.ts b/qa-core/src/integrations/validators/mongo.spec.ts new file mode 100644 index 0000000000..dd8e188f27 --- /dev/null +++ b/qa-core/src/integrations/validators/mongo.spec.ts @@ -0,0 +1,91 @@ +import { GenericContainer } from "testcontainers" +import mongo from "../../../../packages/server/src/integrations/mongodb" +import { generator } from "../../shared" + +jest.unmock("mongodb") + +describe("datasource validators", () => { + describe("mongo", () => { + let connectionSettings: { + user: string + password: string + host: string + port: number + } + + function getConnectionString( + settings: Partial = {} + ) { + const { user, password, host, port } = { + ...connectionSettings, + ...settings, + } + return `mongodb://${user}:${password}@${host}:${port}` + } + + beforeAll(async () => { + const user = generator.name() + const password = generator.hash() + const container = await new GenericContainer("mongo") + .withExposedPorts(27017) + .withEnv("MONGO_INITDB_ROOT_USERNAME", user) + .withEnv("MONGO_INITDB_ROOT_PASSWORD", password) + .start() + + connectionSettings = { + user, + password, + host: container.getContainerIpAddress(), + port: container.getMappedPort(27017), + } + }) + + it("test valid connection string", async () => { + const integration = new mongo.integration({ + connectionString: getConnectionString(), + db: "", + tlsCertificateFile: "", + tlsCertificateKeyFile: "", + tlsCAFile: "", + }) + const result = await integration.testConnection() + expect(result).toBe(true) + }) + + it("test invalid password", async () => { + const integration = new mongo.integration({ + connectionString: getConnectionString({ password: "wrong" }), + db: "", + tlsCertificateFile: "", + tlsCertificateKeyFile: "", + tlsCAFile: "", + }) + const result = await integration.testConnection() + expect(result).toEqual({ error: "Authentication failed." }) + }) + + it("test invalid username", async () => { + const integration = new mongo.integration({ + connectionString: getConnectionString({ user: "wrong" }), + db: "", + tlsCertificateFile: "", + tlsCertificateKeyFile: "", + tlsCAFile: "", + }) + const result = await integration.testConnection() + expect(result).toEqual({ error: "Authentication failed." }) + }) + + it("test invalid connection", async () => { + const integration = new mongo.integration({ + connectionString: getConnectionString({ host: "http://nothinghere" }), + db: "", + tlsCertificateFile: "", + tlsCertificateKeyFile: "", + tlsCAFile: "", + }) + const result = await integration.testConnection() + expect(result).toEqual({ error: "Error: getaddrinfo ENOTFOUND http" }) + }) + }) +})