From 85c372c4d6fba7e9b066f97b807e4c44513af58f Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 7 Jun 2024 16:56:07 +0100 Subject: [PATCH 1/2] Move some mysql.spec.ts and postgres.spec.ts tests into datasource.spec.ts. --- .../src/api/routes/tests/datasource.spec.ts | 57 ++++++++++++++++++- .../server/src/integration-test/mysql.spec.ts | 48 ---------------- .../src/integration-test/postgres.spec.ts | 48 ---------------- .../src/tests/utilities/api/datasource.ts | 11 ++++ 4 files changed, 67 insertions(+), 97 deletions(-) diff --git a/packages/server/src/api/routes/tests/datasource.spec.ts b/packages/server/src/api/routes/tests/datasource.spec.ts index 2d433edb4f..f800ecc235 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", () => { @@ -372,5 +374,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..dbfacca403 100644 --- a/packages/server/src/integration-test/mysql.spec.ts +++ b/packages/server/src/integration-test/mysql.spec.ts @@ -109,54 +109,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 ccf63d0820..3ba47cb5fb 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 780a7fc7a3..7908c689d0 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" @@ -83,4 +84,14 @@ export class DatasourceAPI extends TestAPI { } ) } + + info = async (datasource: Datasource, expectations?: Expectations) => { + return await this._post( + `/api/datasources/info`, + { + body: { datasource }, + expectations, + } + ) + } } From d2c273542d11c87009f66e7bc2b6f15c23196dfb Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Fri, 7 Jun 2024 17:02:23 +0100 Subject: [PATCH 2/2] Fix tests. --- .../server/src/integration-test/mysql.spec.ts | 38 +++---------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/packages/server/src/integration-test/mysql.spec.ts b/packages/server/src/integration-test/mysql.spec.ts index dbfacca403..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)