Migrate DatasourceAPI.

This commit is contained in:
Sam Rose 2024-02-29 15:50:18 +00:00
parent 5163434b08
commit 4fbe03bbda
No known key found for this signature in database
5 changed files with 77 additions and 75 deletions

View File

@ -397,15 +397,16 @@ describe("/queries", () => {
}) })
it("should fail with invalid integration type", async () => { it("should fail with invalid integration type", async () => {
const response = await config.api.datasource.create( const datasource: Datasource = {
{ ...basicDatasource().datasource,
...basicDatasource().datasource, source: "INVALID_INTEGRATION" as SourceName,
source: "INVALID_INTEGRATION" as SourceName, }
await config.api.datasource.create(datasource, {
status: 500,
body: {
message: "No datasource implementation found.",
}, },
{ expectStatus: 500, rawResponse: true } })
)
expect(response.body.message).toBe("No datasource implementation found.")
}) })
}) })

View File

@ -1040,28 +1040,37 @@ describe("postgres integrations", () => {
describe("POST /api/datasources/verify", () => { describe("POST /api/datasources/verify", () => {
it("should be able to verify the connection", async () => { it("should be able to verify the connection", async () => {
const response = await config.api.datasource.verify({ await config.api.datasource.verify(
datasource: await databaseTestProviders.postgres.datasource(), {
}) datasource: await databaseTestProviders.postgres.datasource(),
expect(response.status).toBe(200) },
expect(response.body.connected).toBe(true) {
body: {
connected: true,
},
}
)
}) })
it("should state an invalid datasource cannot connect", async () => { it("should state an invalid datasource cannot connect", async () => {
const dbConfig = await databaseTestProviders.postgres.datasource() const dbConfig = await databaseTestProviders.postgres.datasource()
const response = await config.api.datasource.verify({ const response = await config.api.datasource.verify(
datasource: { {
...dbConfig, datasource: {
config: { ...dbConfig,
...dbConfig.config, config: {
password: "wrongpassword", ...dbConfig.config,
password: "wrongpassword",
},
}, },
}, },
}) {
body: {
expect(response.status).toBe(200) connected: false,
expect(response.body.connected).toBe(false) error: 'password authentication failed for user "postgres"',
expect(response.body.error).toBeDefined() },
}
)
}) })
}) })

View File

@ -3,6 +3,10 @@ import { SuperTest, Test, Response } from "supertest"
import { ReadStream } from "fs" import { ReadStream } from "fs"
type Headers = Record<string, string | string[] | undefined> type Headers = Record<string, string | string[] | undefined>
type SuccessStatus = 200 | 201 | 204
type ErrorStatus = 400 | 401 | 403 | 404 | 500 | 502 | 503 | 504
type Status = SuccessStatus | ErrorStatus
type Method = "get" | "post" | "put" | "patch" | "delete"
export interface AttachedFile { export interface AttachedFile {
name: string name: string
@ -21,9 +25,10 @@ function isAttachedFile(file: any): file is AttachedFile {
} }
export interface Expectations { export interface Expectations {
status?: number status?: Status
headers?: Record<string, string | RegExp> headers?: Record<string, string | RegExp>
headersNotPresent?: string[] headersNotPresent?: string[]
body?: Record<string, any>
} }
export interface RequestOpts { export interface RequestOpts {
@ -137,7 +142,7 @@ export abstract class TestAPI {
} }
protected _request = async <T>( protected _request = async <T>(
method: "get" | "post" | "put" | "patch" | "delete", method: Method,
url: string, url: string,
opts?: RequestOpts opts?: RequestOpts
): Promise<T> => { ): Promise<T> => {
@ -180,6 +185,10 @@ export abstract class TestAPI {
} }
} }
return response.body as T if (expectations?.body) {
expect(response.body).toMatchObject(expectations.body)
}
return response.body
} }
} }

View File

@ -1,63 +1,48 @@
import { import {
CreateDatasourceRequest,
Datasource, Datasource,
VerifyDatasourceRequest, VerifyDatasourceRequest,
CreateDatasourceResponse,
UpdateDatasourceResponse,
UpdateDatasourceRequest,
} from "@budibase/types" } from "@budibase/types"
import TestConfiguration from "../TestConfiguration" import { Expectations, TestAPI } from "./base"
import { TestAPI } from "./base"
import supertest from "supertest"
export class DatasourceAPI extends TestAPI { export class DatasourceAPI extends TestAPI {
constructor(config: TestConfiguration) { create = async (
super(config)
}
create = async <B extends boolean = false>(
config: Datasource, config: Datasource,
{ expectations?: Expectations
expectStatus, ): Promise<Datasource> => {
rawResponse, const response = await this._post<CreateDatasourceResponse>(
}: { expectStatus?: number; rawResponse?: B } = {} `/api/datasources`,
): Promise<B extends false ? Datasource : supertest.Response> => { {
const body: CreateDatasourceRequest = { body: {
datasource: config, datasource: config,
tablesFilter: [], tablesFilter: [],
} },
const result = await this.request expectations,
.post(`/api/datasources`) }
.send(body) )
.set(this.config.defaultHeaders()) return response.datasource
.expect("Content-Type", /json/)
.expect(expectStatus || 200)
if (rawResponse) {
return result as any
}
return result.body.datasource
} }
update = async ( update = async (
datasource: Datasource, datasource: UpdateDatasourceRequest,
{ expectStatus } = { expectStatus: 200 } expectations?: Expectations
): Promise<Datasource> => { ): Promise<Datasource> => {
const result = await this.request const response = await this._put<UpdateDatasourceResponse>(
.put(`/api/datasources/${datasource._id}`) `/api/datasources/${datasource._id}`,
.send(datasource) { body: datasource, expectations }
.set(this.config.defaultHeaders()) )
.expect("Content-Type", /json/) return response.datasource
.expect(expectStatus)
return result.body.datasource as Datasource
} }
verify = async ( verify = async (
data: VerifyDatasourceRequest, data: VerifyDatasourceRequest,
{ expectStatus } = { expectStatus: 200 } expectations?: Expectations
) => { ) => {
const result = await this.request return await this._post(`/api/datasources/verify`, {
.post(`/api/datasources/verify`) body: data,
.send(data) expectations,
.set(this.config.defaultHeaders()) })
.expect("Content-Type", /json/)
.expect(expectStatus)
return result
} }
} }

View File

@ -32,9 +32,7 @@ export interface FetchDatasourceInfoResponse {
tableNames: string[] tableNames: string[]
} }
export interface UpdateDatasourceRequest extends Datasource { export type UpdateDatasourceRequest = Datasource
datasource: Datasource
}
export interface BuildSchemaFromSourceRequest { export interface BuildSchemaFromSourceRequest {
tablesFilter?: string[] tablesFilter?: string[]