diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index 8fa84ac05d..4cb93160f7 100644 --- a/packages/server/src/api/routes/tests/datasource.spec.ts +++ b/packages/server/src/api/routes/tests/datasource.spec.ts @@ -156,8 +156,10 @@ describe("/datasources", () => { [DatabaseName.SQL_SERVER, getDatasource(DatabaseName.SQL_SERVER)], [DatabaseName.MARIADB, getDatasource(DatabaseName.MARIADB)], ])("%s", (_, dsProvider) => { + let rawDatasource: Datasource beforeEach(async () => { - datasource = await config.api.datasource.create(await dsProvider) + rawDatasource = await dsProvider + datasource = await config.api.datasource.create(rawDatasource) }) describe("get", () => { @@ -377,5 +379,58 @@ describe("/datasources", () => { expect(updated).toEqual(expected) }) }) + + describe("verify", () => { + it("should be able to verify the connection", async () => { + await config.api.datasource.verify( + { + datasource: rawDatasource, + }, + { + body: { + connected: true, + }, + } + ) + }) + + it("should state an invalid datasource cannot connect", async () => { + await config.api.datasource.verify( + { + datasource: { + ...rawDatasource, + config: { + ...rawDatasource.config, + password: "wrongpassword", + }, + }, + }, + { + body: { + connected: false, + error: /.*/, // error message differs between databases + }, + } + ) + }) + }) + + describe("info", () => { + it("should fetch information about postgres datasource", async () => { + const table = await config.api.table.save( + tableForDatasource(datasource, { + schema: { + name: { + name: "name", + type: FieldType.STRING, + }, + }, + }) + ) + + const info = await config.api.datasource.info(datasource) + expect(info.tableNames).toContain(table.name) + }) + }) }) }) diff --git a/packages/server/src/integration-test/mysql.spec.ts b/packages/server/src/integration-test/mysql.spec.ts index 04a2541ad6..4dff40b61d 100644 --- a/packages/server/src/integration-test/mysql.spec.ts +++ b/packages/server/src/integration-test/mysql.spec.ts @@ -4,13 +4,14 @@ import { MakeRequestResponse, } from "../api/routes/public/tests/utils" import * as setup from "../api/routes/tests/utilities" -import { Datasource, FieldType, Table, TableSourceType } from "@budibase/types" +import { Datasource, FieldType } from "@budibase/types" import { DatabaseName, getDatasource, rawQuery, } from "../integrations/tests/utils" import { generator } from "@budibase/backend-core/tests" +import { tableForDatasource } from "../../src/tests/utilities/structures" // @ts-ignore fetch.mockSearch() @@ -41,8 +42,7 @@ jest.mock("../websockets", () => ({ describe("mysql integrations", () => { let makeRequest: MakeRequestResponse, rawDatasource: Datasource, - datasource: Datasource, - primaryMySqlTable: Table + datasource: Datasource beforeAll(async () => { await config.init() @@ -54,38 +54,12 @@ describe("mysql integrations", () => { datasource = await config.api.datasource.create(rawDatasource) }) - beforeEach(async () => { - primaryMySqlTable = await config.createTable({ - name: uniqueTableName(), - type: "table", - primary: ["id"], - schema: { - id: { - name: "id", - type: FieldType.AUTO, - autocolumn: true, - }, - name: { - name: "name", - type: FieldType.STRING, - }, - description: { - name: "description", - type: FieldType.STRING, - }, - value: { - name: "value", - type: FieldType.NUMBER, - }, - }, - sourceId: datasource._id, - sourceType: TableSourceType.EXTERNAL, - }) - }) - afterAll(config.end) it("validate table schema", async () => { + // Creating a table so that `entities` is populated. + await config.api.table.save(tableForDatasource(datasource)) + const res = await makeRequest("get", `/api/datasources/${datasource._id}`) expect(res.status).toBe(200) @@ -109,54 +83,6 @@ describe("mysql integrations", () => { }) }) - describe("POST /api/datasources/verify", () => { - it("should be able to verify the connection", async () => { - await config.api.datasource.verify( - { - datasource: rawDatasource, - }, - { - body: { - connected: true, - }, - } - ) - }) - - it("should state an invalid datasource cannot connect", async () => { - await config.api.datasource.verify( - { - datasource: { - ...rawDatasource, - config: { - ...rawDatasource.config, - password: "wrongpassword", - }, - }, - }, - { - body: { - connected: false, - error: - "Access denied for the specified user. User does not have the necessary privileges or the provided credentials are incorrect. Please verify the credentials, and ensure that the user has appropriate permissions.", - }, - } - ) - }) - }) - - describe("POST /api/datasources/info", () => { - it("should fetch information about mysql datasource", async () => { - const primaryName = primaryMySqlTable.name - const response = await makeRequest("post", "/api/datasources/info", { - datasource: datasource, - }) - expect(response.status).toBe(200) - expect(response.body.tableNames).toBeDefined() - expect(response.body.tableNames.indexOf(primaryName)).not.toBe(-1) - }) - }) - describe("Integration compatibility with mysql search_path", () => { let datasource: Datasource, rawDatasource: Datasource const database = generator.guid() diff --git a/packages/server/src/integration-test/postgres.spec.ts b/packages/server/src/integration-test/postgres.spec.ts index 32d74e5c45..1bd2842951 100644 --- a/packages/server/src/integration-test/postgres.spec.ts +++ b/packages/server/src/integration-test/postgres.spec.ts @@ -1035,54 +1035,6 @@ describe("postgres integrations", () => { }) }) - describe("POST /api/datasources/verify", () => { - it("should be able to verify the connection", async () => { - await config.api.datasource.verify( - { - datasource: await getDatasource(DatabaseName.POSTGRES), - }, - { - body: { - connected: true, - }, - } - ) - }) - - it("should state an invalid datasource cannot connect", async () => { - const dbConfig = await getDatasource(DatabaseName.POSTGRES) - await config.api.datasource.verify( - { - datasource: { - ...dbConfig, - config: { - ...dbConfig.config, - password: "wrongpassword", - }, - }, - }, - { - body: { - connected: false, - error: 'password authentication failed for user "postgres"', - }, - } - ) - }) - }) - - describe("POST /api/datasources/info", () => { - it("should fetch information about postgres datasource", async () => { - const primaryName = primaryPostgresTable.name - const response = await makeRequest("post", "/api/datasources/info", { - datasource: datasource, - }) - expect(response.status).toBe(200) - expect(response.body.tableNames).toBeDefined() - expect(response.body.tableNames.indexOf(primaryName)).not.toBe(-1) - }) - }) - describe("POST /api/datasources/:datasourceId/schema", () => { let tableName: string diff --git a/packages/server/src/tests/utilities/api/datasource.ts b/packages/server/src/tests/utilities/api/datasource.ts index 1eb811123d..67484a688a 100644 --- a/packages/server/src/tests/utilities/api/datasource.ts +++ b/packages/server/src/tests/utilities/api/datasource.ts @@ -6,6 +6,7 @@ import { UpdateDatasourceRequest, QueryJson, BuildSchemaFromSourceResponse, + FetchDatasourceInfoResponse, } from "@budibase/types" import { Expectations, TestAPI } from "./base" @@ -92,4 +93,14 @@ export class DatasourceAPI extends TestAPI { } ) } + + info = async (datasource: Datasource, expectations?: Expectations) => { + return await this._post( + `/api/datasources/info`, + { + body: { datasource }, + expectations, + } + ) + } }