1
0
Fork 0
mirror of synced 2024-07-03 21:40:55 +12:00

Validate and test microsoft sql

This commit is contained in:
Adria Navarro 2023-05-11 17:48:37 +02:00
parent cb398dad02
commit 32695018bf
2 changed files with 63 additions and 1 deletions

View file

@ -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,
}

View file

@ -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'.",
})
})
})
})