diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 77dc2ad7b2..b8e4089411 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -358,6 +358,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() } @@ -631,18 +640,8 @@ class MongoIntegration implements IntegrationBase { } } } -async function validateConnection(config: MongoDBConfig) { - const integration = new MongoIntegration(config) - try { - await integration.connect() - return true - } catch (e: any) { - return { error: e.message as string } - } -} export default { schema: SCHEMA, integration: MongoIntegration, - validateConnection, } diff --git a/qa-core/src/integrations/validators/mongo.spec.ts b/qa-core/src/integrations/validators/mongo.spec.ts index 1c4729513e..dd8e188f27 100644 --- a/qa-core/src/integrations/validators/mongo.spec.ts +++ b/qa-core/src/integrations/validators/mongo.spec.ts @@ -1,12 +1,11 @@ import { GenericContainer } from "testcontainers" -import mongodb from "../../../../packages/server/src/integrations/mongodb" +import mongo from "../../../../packages/server/src/integrations/mongodb" +import { generator } from "../../shared" jest.unmock("mongodb") describe("datasource validators", () => { describe("mongo", () => { - const validator = integrations.getValidator[SourceName.MONGODB] - let connectionSettings: { user: string password: string @@ -42,46 +41,50 @@ describe("datasource validators", () => { }) it("test valid connection string", async () => { - const result = await validator({ + 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 result = await validator({ + 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 result = await validator({ + 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 result = await validator({ + 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" }) }) })