Add mongo checks

This commit is contained in:
Adria Navarro 2023-05-12 10:07:59 +02:00
parent cb398dad02
commit e8fa690566
2 changed files with 98 additions and 0 deletions

View File

@ -631,8 +631,18 @@ 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,
}

View File

@ -0,0 +1,88 @@
import { GenericContainer } from "testcontainers"
import mongodb from "../../../../packages/server/src/integrations/mongodb"
jest.unmock("mongodb")
describe("datasource validators", () => {
describe("mongo", () => {
const validator = integrations.getValidator[SourceName.MONGODB]
let connectionSettings: {
user: string
password: string
host: string
port: number
}
function getConnectionString(
settings: Partial<typeof connectionSettings> = {}
) {
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 result = await validator({
connectionString: getConnectionString(),
db: "",
tlsCertificateFile: "",
tlsCertificateKeyFile: "",
tlsCAFile: "",
})
expect(result).toBe(true)
})
it("test invalid password", async () => {
const result = await validator({
connectionString: getConnectionString({ password: "wrong" }),
db: "",
tlsCertificateFile: "",
tlsCertificateKeyFile: "",
tlsCAFile: "",
})
expect(result).toEqual({ error: "Authentication failed." })
})
it("test invalid username", async () => {
const result = await validator({
connectionString: getConnectionString({ user: "wrong" }),
db: "",
tlsCertificateFile: "",
tlsCertificateKeyFile: "",
tlsCAFile: "",
})
expect(result).toEqual({ error: "Authentication failed." })
})
it("test invalid connection", async () => {
const result = await validator({
connectionString: getConnectionString({ host: "http://nothinghere" }),
db: "",
tlsCertificateFile: "",
tlsCertificateKeyFile: "",
tlsCAFile: "",
})
expect(result).toEqual({ error: "Error: getaddrinfo ENOTFOUND http" })
})
})
})