From e20838fd428d3b2a887531050b8a922a261336dc Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Fri, 14 Apr 2023 17:35:13 +0100 Subject: [PATCH] Add rest test and fix type --- .../types/src/documents/app/datasource.ts | 8 --- packages/types/src/documents/app/query.ts | 3 + .../src/internal-api/fixtures/datasources.ts | 18 ++++- qa-core/src/internal-api/fixtures/queries.ts | 48 +++++++++++++ .../tests/dataSources/restAPI.spec.ts | 69 +++++++++++++++++++ 5 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 qa-core/src/internal-api/tests/dataSources/restAPI.spec.ts diff --git a/packages/types/src/documents/app/datasource.ts b/packages/types/src/documents/app/datasource.ts index 9b498fb4b3..8dfdfe6d0f 100644 --- a/packages/types/src/documents/app/datasource.ts +++ b/packages/types/src/documents/app/datasource.ts @@ -56,11 +56,3 @@ export interface RestConfig { } ] } - -export interface CreateDatasourceResponse { - datasource: Datasource -} - -export interface UpdatedDatasourceResponse { - datasource: Datasource -} diff --git a/packages/types/src/documents/app/query.ts b/packages/types/src/documents/app/query.ts index d0bbf05b5c..31a3a3ba09 100644 --- a/packages/types/src/documents/app/query.ts +++ b/packages/types/src/documents/app/query.ts @@ -45,4 +45,7 @@ export interface PaginationValues { export interface PreviewQueryRequest extends Omit { parameters: {} + flags?: { + urlName?: boolean + } } diff --git a/qa-core/src/internal-api/fixtures/datasources.ts b/qa-core/src/internal-api/fixtures/datasources.ts index a8d59b88a1..dcf6af824a 100644 --- a/qa-core/src/internal-api/fixtures/datasources.ts +++ b/qa-core/src/internal-api/fixtures/datasources.ts @@ -34,8 +34,6 @@ export const postgresSQL = () => { fetchSchema: true, } } - -// Add the data source for MariaDB to the this file in the same style as above export const mariaDB = () => { return { datasource: { @@ -55,3 +53,19 @@ export const mariaDB = () => { fetchSchema: true, } } + +export const restAPI = () => { + return { + datasource: { + name: "RestAPI", + source: "REST", + type: "datasource", + config: { + defaultHeaders: {}, + rejectUnauthorized: true, + url: process.env.REST_API_BASE_URL, + }, + }, + fetchSchema: false, + } +} diff --git a/qa-core/src/internal-api/fixtures/queries.ts b/qa-core/src/internal-api/fixtures/queries.ts index 7ba81fc744..83839a0b41 100644 --- a/qa-core/src/internal-api/fixtures/queries.ts +++ b/qa-core/src/internal-api/fixtures/queries.ts @@ -72,4 +72,52 @@ export const expectedSchemaFields = { postal_code: "string", region: "string", }, + restAPI: { + abilities: "array", + base_experience: "number", + forms: "array", + game_indices: "array", + height: "number", + held_items: "array", + id: "number", + is_default: "string", + location_area_encounters: "string", + moves: "array", + name: "string", + order: "number", + past_types: "array", + species: "json", + sprites: "json", + stats: "array", + types: "array", + weight: "number", + }, +} + +const request = (datasourceId: string, fields: any, flags: any): any => { + return { + datasourceId: datasourceId, + fields: fields, + flags: flags, + name: "Query 1", + parameters: {}, + queryVerb: "read", + schema: {}, + transformer: "return data", + } +} +export const restAPI = (datasourceId: string): PreviewQueryRequest => { + const fields = { + authConfigId: null, + bodyType: "none", + disabledHeaders: {}, + headers: {}, + pagination: {}, + path: `${process.env.REST_API_BASE_URL}/pokemon/ditto`, + queryString: "", + } + const flags = { + urlName: true, + } + return request(datasourceId, fields, flags) } diff --git a/qa-core/src/internal-api/tests/dataSources/restAPI.spec.ts b/qa-core/src/internal-api/tests/dataSources/restAPI.spec.ts new file mode 100644 index 0000000000..d7b75db514 --- /dev/null +++ b/qa-core/src/internal-api/tests/dataSources/restAPI.spec.ts @@ -0,0 +1,69 @@ +import TestConfiguration from "../../config/TestConfiguration" +import * as fixtures from "../../fixtures" +import { Query } from "@budibase/types" + +describe("Internal API - Data Sources: REST API", () => { + const config = new TestConfiguration() + + beforeAll(async () => { + await config.beforeAll() + }) + + afterAll(async () => { + await config.afterAll() + }) + + it("Create an app with a data source - REST API", async () => { + // Create app + await config.createApp() + + // Get all integrations + await config.api.integrations.getAll() + + // Add data source + const [dataSourceResponse, dataSourceJson] = + await config.api.datasources.add(fixtures.datasources.restAPI()) + + // Update data source + const newDataSourceInfo = { + ...dataSourceJson.datasource, + name: "RestAPI - Updated", + } + const [updatedDataSourceResponse, updatedDataSourceJson] = + await config.api.datasources.update(newDataSourceInfo) + + // Query data source + const [queryResponse, queryJson] = await config.api.queries.preview( + fixtures.queries.restAPI(updatedDataSourceJson.datasource._id!) + ) + + expect(queryJson.rows.length).toEqual(1) + expect(queryJson.schemaFields).toEqual( + fixtures.queries.expectedSchemaFields.restAPI + ) + + // Save query + const datasourcetoSave: Query = { + ...fixtures.queries.postgres(updatedDataSourceJson.datasource._id!), + parameters: [], + } + + const [saveQueryResponse, saveQueryJson] = await config.api.queries.save( + datasourcetoSave + ) + // Get Query + const [getQueryResponse, getQueryJson] = await config.api.queries.get( + saveQueryJson._id! + ) + + // Get Query permissions + const [getQueryPermissionsResponse, getQueryPermissionsJson] = + await config.api.permissions.getAll(saveQueryJson._id!) + + // Delete data source + const deleteResponse = await config.api.datasources.delete( + updatedDataSourceJson.datasource._id!, + updatedDataSourceJson.datasource._rev! + ) + }) +})