Skip to content

Commit

Permalink
Implement the check as part of the integration
Browse files Browse the repository at this point in the history
  • Loading branch information
adrinr committed May 12, 2023
1 parent 481c4c0 commit 1314958
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
20 changes: 9 additions & 11 deletions packages/server/src/integrations/microsoftSqlServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
}
}

async testConnection() {
try {
await this.connect()
return true
} catch (e: any) {
return { error: e.message as string }
}
}

getBindingIdentifier(): string {
return `@p${this.index++}`
}
Expand Down Expand Up @@ -310,18 +319,7 @@ class SqlServerIntegration extends Sql implements DatasourcePlus {
}
}

async function validateConnection(config: MSSQLConfig) {
const integration = new SqlServerIntegration(config)
try {
await integration.connect()
return true
} catch (e: any) {
return { error: e.message as string }
}
}

export default {
schema: SCHEMA,
integration: SqlServerIntegration,
validateConnection,
}
19 changes: 9 additions & 10 deletions packages/server/src/sdk/tests/datasources/validators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { GenericContainer } from "testcontainers"
import { GenericContainer, Wait } from "testcontainers"
import { generator } from "@budibase/backend-core/tests"
import { Duration, TemporalUnit } from "node-duration"
import { SourceName } from "@budibase/types"
import integrations from "../../../integrations"

import postgres from "../../../integrations/postgres"
import mysql from "../../../integrations/mysql"
import couchdb from "../../../integrations/couchdb"
import { SourceName } from "@budibase/types"
import integrations from "../../../integrations"
import mssql from "../../../integrations/microsoftSqlServer"

jest.unmock("pg")
jest.unmock("mysql2/promise")
Expand Down Expand Up @@ -149,8 +150,6 @@ describe("datasource validators", () => {
url = `http://${user}:${password}@${host}:${port}`
})

let url: string

beforeAll(async () => {
const user = generator.first()
const password = generator.hash()
Expand Down Expand Up @@ -206,9 +205,7 @@ describe("datasource validators", () => {
})
})

describe("mssql", () => {
const validator = integrations.getValidator[SourceName.SQL_SERVER]!

describe.only("mssql", () => {
let host: string, port: number

const password = "Str0Ng_p@ssW0rd!"
Expand Down Expand Up @@ -236,26 +233,28 @@ describe("datasource validators", () => {
})

it("test valid connection string", async () => {
const result = await validator({
const integration = new mssql.integration({
user: "sa",
password,
server: host,
port: port,
database: "master",
schema: "dbo",
})
const result = await integration.testConnection()
expect(result).toBe(true)
})

it("test invalid password", async () => {
const result = await validator({
const integration = new mssql.integration({
user: "sa",
password: "wrong_pwd",
server: host,
port: port,
database: "master",
schema: "dbo",
})
const result = await integration.testConnection()
expect(result).toEqual({
error: "ConnectionError: Login failed for user 'sa'.",
})
Expand Down

0 comments on commit 1314958

Please sign in to comment.