Merge pull request #10554 from Budibase/budi-6932/verify_mongo
Implement mongo connection verification
This commit is contained in:
commit
f9fd3d942a
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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<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 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" })
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue