diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index eb87c1ccf1..3f14360d57 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -22,7 +22,7 @@ import { MSSQLTablesResponse, MSSQLColumn } from "./base/types" const sqlServer = require("mssql") const DEFAULT_SCHEMA = "dbo" -interface MSSQLConfig { +export interface MSSQLConfig { user: string password: string server: string @@ -138,6 +138,10 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { } } + // async end(){ + // this.client!. + // } + async internalQuery( query: SqlQuery, operation: string | undefined = undefined @@ -306,7 +310,18 @@ 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, } diff --git a/qa-core/src/integrations/validators/postgres.spec.ts b/qa-core/src/integrations/validators/postgres.spec.ts index 5aa3250e2a..dcc0b4cb73 100644 --- a/qa-core/src/integrations/validators/postgres.spec.ts +++ b/qa-core/src/integrations/validators/postgres.spec.ts @@ -2,6 +2,7 @@ import { GenericContainer } from "testcontainers" import postgres from "../../../../packages/server/src/integrations/postgres" jest.unmock("pg") +jest.unmock("mssql") describe("datasource validators", () => { describe("postgres", () => { @@ -50,4 +51,50 @@ describe("datasource validators", () => { }) }) }) + + describe("mssql", () => { + const validator = integrations.getValidator[SourceName.SQL_SERVER]! + + let host: string, port: number + + beforeAll(async () => { + const container = await new GenericContainer( + "mcr.microsoft.com/mssql/server" + ) + .withExposedPorts(1433) + .withEnv("ACCEPT_EULA", "Y") + .withEnv("MSSQL_SA_PASSWORD", "Str0Ng_p@ssW0rd!") + .withEnv("MSSQL_PID", "Developer") + .start() + + host = container.getContainerIpAddress() + port = container.getMappedPort(1433) + }) + + it("test valid connection string", async () => { + const result = await validator({ + user: "sa", + password: "Str0Ng_p@ssW0rd!", + server: host, + port: port, + database: "master", + schema: "dbo", + }) + expect(result).toBe(true) + }) + + it("test invalid password", async () => { + const result = await validator({ + user: "sa", + password: "wrong_pwd", + server: host, + port: port, + database: "master", + schema: "dbo", + }) + expect(result).toEqual({ + error: "ConnectionError: Login failed for user 'sa'.", + }) + }) + }) })