Implement the check as part of the integration

This commit is contained in:
Adria Navarro 2023-05-12 12:59:59 +02:00
parent e8fa690566
commit 2f9b076381
2 changed files with 19 additions and 17 deletions

View File

@ -358,6 +358,15 @@ class MongoIntegration implements IntegrationBase {
this.client = new MongoClient(config.connectionString, options) 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() { async connect() {
return this.client.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 { export default {
schema: SCHEMA, schema: SCHEMA,
integration: MongoIntegration, integration: MongoIntegration,
validateConnection,
} }

View File

@ -1,12 +1,11 @@
import { GenericContainer } from "testcontainers" 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") jest.unmock("mongodb")
describe("datasource validators", () => { describe("datasource validators", () => {
describe("mongo", () => { describe("mongo", () => {
const validator = integrations.getValidator[SourceName.MONGODB]
let connectionSettings: { let connectionSettings: {
user: string user: string
password: string password: string
@ -42,46 +41,50 @@ describe("datasource validators", () => {
}) })
it("test valid connection string", async () => { it("test valid connection string", async () => {
const result = await validator({ const integration = new mongo.integration({
connectionString: getConnectionString(), connectionString: getConnectionString(),
db: "", db: "",
tlsCertificateFile: "", tlsCertificateFile: "",
tlsCertificateKeyFile: "", tlsCertificateKeyFile: "",
tlsCAFile: "", tlsCAFile: "",
}) })
const result = await integration.testConnection()
expect(result).toBe(true) expect(result).toBe(true)
}) })
it("test invalid password", async () => { it("test invalid password", async () => {
const result = await validator({ const integration = new mongo.integration({
connectionString: getConnectionString({ password: "wrong" }), connectionString: getConnectionString({ password: "wrong" }),
db: "", db: "",
tlsCertificateFile: "", tlsCertificateFile: "",
tlsCertificateKeyFile: "", tlsCertificateKeyFile: "",
tlsCAFile: "", tlsCAFile: "",
}) })
const result = await integration.testConnection()
expect(result).toEqual({ error: "Authentication failed." }) expect(result).toEqual({ error: "Authentication failed." })
}) })
it("test invalid username", async () => { it("test invalid username", async () => {
const result = await validator({ const integration = new mongo.integration({
connectionString: getConnectionString({ user: "wrong" }), connectionString: getConnectionString({ user: "wrong" }),
db: "", db: "",
tlsCertificateFile: "", tlsCertificateFile: "",
tlsCertificateKeyFile: "", tlsCertificateKeyFile: "",
tlsCAFile: "", tlsCAFile: "",
}) })
const result = await integration.testConnection()
expect(result).toEqual({ error: "Authentication failed." }) expect(result).toEqual({ error: "Authentication failed." })
}) })
it("test invalid connection", async () => { it("test invalid connection", async () => {
const result = await validator({ const integration = new mongo.integration({
connectionString: getConnectionString({ host: "http://nothinghere" }), connectionString: getConnectionString({ host: "http://nothinghere" }),
db: "", db: "",
tlsCertificateFile: "", tlsCertificateFile: "",
tlsCertificateKeyFile: "", tlsCertificateKeyFile: "",
tlsCAFile: "", tlsCAFile: "",
}) })
const result = await integration.testConnection()
expect(result).toEqual({ error: "Error: getaddrinfo ENOTFOUND http" }) expect(result).toEqual({ error: "Error: getaddrinfo ENOTFOUND http" })
}) })
}) })