From 99c3395cb9d2c970e2db4c22b7eddc35b60583c9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 25 Oct 2024 13:13:10 +0100 Subject: [PATCH] Some fixes to Public API test request as per PR comments, as well as removing AI formula type from open API spec (this is used internally, not supposed to be exposed). --- packages/server/specs/openapi.json | 9 +-- packages/server/specs/openapi.yaml | 3 - packages/server/specs/resources/table.ts | 2 +- .../src/api/routes/public/tests/Request.ts | 58 ++++++++----------- .../api/routes/public/tests/security.spec.ts | 4 +- packages/server/src/definitions/openapi.ts | 6 +- 6 files changed, 34 insertions(+), 48 deletions(-) diff --git a/packages/server/specs/openapi.json b/packages/server/specs/openapi.json index f3091a1fc7..685e168c66 100644 --- a/packages/server/specs/openapi.json +++ b/packages/server/specs/openapi.json @@ -831,8 +831,7 @@ "type": "string", "enum": [ "static", - "dynamic", - "ai" + "dynamic" ], "description": "Defines whether this is a static or dynamic formula." } @@ -1042,8 +1041,7 @@ "type": "string", "enum": [ "static", - "dynamic", - "ai" + "dynamic" ], "description": "Defines whether this is a static or dynamic formula." } @@ -1264,8 +1262,7 @@ "type": "string", "enum": [ "static", - "dynamic", - "ai" + "dynamic" ], "description": "Defines whether this is a static or dynamic formula." } diff --git a/packages/server/specs/openapi.yaml b/packages/server/specs/openapi.yaml index 1e9b9921cf..61a5d0b57f 100644 --- a/packages/server/specs/openapi.yaml +++ b/packages/server/specs/openapi.yaml @@ -761,7 +761,6 @@ components: enum: - static - dynamic - - ai description: Defines whether this is a static or dynamic formula. - type: object properties: @@ -931,7 +930,6 @@ components: enum: - static - dynamic - - ai description: Defines whether this is a static or dynamic formula. - type: object properties: @@ -1108,7 +1106,6 @@ components: enum: - static - dynamic - - ai description: Defines whether this is a static or dynamic formula. - type: object properties: diff --git a/packages/server/specs/resources/table.ts b/packages/server/specs/resources/table.ts index 7f90769b07..c068dbd48b 100644 --- a/packages/server/specs/resources/table.ts +++ b/packages/server/specs/resources/table.ts @@ -138,7 +138,7 @@ const tableSchema = { }, formulaType: { type: "string", - enum: Object.values(FormulaType), + enum: [FormulaType.STATIC, FormulaType.DYNAMIC], description: "Defines whether this is a static or dynamic formula.", }, diff --git a/packages/server/src/api/routes/public/tests/Request.ts b/packages/server/src/api/routes/public/tests/Request.ts index 70e83d711d..92a4996124 100644 --- a/packages/server/src/api/routes/public/tests/Request.ts +++ b/packages/server/src/api/routes/public/tests/Request.ts @@ -5,22 +5,35 @@ import { Expectations } from "../../../../tests/utilities/api/base" type RequestOpts = { internal?: boolean; appId?: string } -type PublicAPIExpectations = Omit +export interface PublicAPIExpectations { + status?: number + body?: Record +} export class PublicAPIRequest { - private makeRequest: MakeRequestResponse | undefined + private makeRequest: MakeRequestResponse private appId: string | undefined - private _tables: PublicTableAPI | undefined - private _rows: PublicRowAPI | undefined - private _apiKey: string | undefined - async init(config: TestConfiguration, user: User, opts?: RequestOpts) { - this._apiKey = await config.generateApiKey(user._id) - this.makeRequest = generateMakeRequest(this.apiKey, opts) - this.appId = opts?.appId - this._tables = new PublicTableAPI(this) - this._rows = new PublicRowAPI(this) - return this + tables: PublicTableAPI + rows: PublicRowAPI + apiKey: string + + private constructor( + apiKey: string, + makeRequest: MakeRequestResponse, + appId?: string + ) { + this.apiKey = apiKey + this.makeRequest = makeRequest + this.appId = appId + this.tables = new PublicTableAPI(this) + this.rows = new PublicRowAPI(this) + } + + static async init(config: TestConfiguration, user: User, opts?: RequestOpts) { + const apiKey = await config.generateApiKey(user._id) + const makeRequest = generateMakeRequest(apiKey, opts) + return new this(apiKey, makeRequest, opts?.appId) } opts(opts: RequestOpts) { @@ -48,27 +61,6 @@ export class PublicAPIRequest { } return res.body } - - get apiKey(): string { - if (!this._apiKey) { - throw new Error("Init has not been called") - } - return this._apiKey - } - - get tables(): PublicTableAPI { - if (!this._tables) { - throw new Error("Init has not been called") - } - return this._tables - } - - get rows(): PublicRowAPI { - if (!this._rows) { - throw new Error("Init has not been called") - } - return this._rows - } } export class PublicTableAPI { diff --git a/packages/server/src/api/routes/public/tests/security.spec.ts b/packages/server/src/api/routes/public/tests/security.spec.ts index a285bc7736..21b4d66843 100644 --- a/packages/server/src/api/routes/public/tests/security.spec.ts +++ b/packages/server/src/api/routes/public/tests/security.spec.ts @@ -20,8 +20,8 @@ describe("check public API security", () => { [config.getProdAppId()]: roles.BUILTIN_ROLE_IDS.BASIC, }, }) - builderRequest = await new PublicAPIRequest().init(config, builderUser) - appUserRequest = await new PublicAPIRequest().init(config, appUser) + builderRequest = await PublicAPIRequest.init(config, builderUser) + appUserRequest = await PublicAPIRequest.init(config, appUser) table = (await builderRequest.tables.create(basicTable())).data }) diff --git a/packages/server/src/definitions/openapi.ts b/packages/server/src/definitions/openapi.ts index 3abf7e6fa7..4479c89d9d 100644 --- a/packages/server/src/definitions/openapi.ts +++ b/packages/server/src/definitions/openapi.ts @@ -257,7 +257,7 @@ export interface components { * @description Defines whether this is a static or dynamic formula. * @enum {string} */ - formulaType?: "static" | "dynamic" | "ai"; + formulaType?: "static" | "dynamic"; } | { /** @@ -369,7 +369,7 @@ export interface components { * @description Defines whether this is a static or dynamic formula. * @enum {string} */ - formulaType?: "static" | "dynamic" | "ai"; + formulaType?: "static" | "dynamic"; } | { /** @@ -483,7 +483,7 @@ export interface components { * @description Defines whether this is a static or dynamic formula. * @enum {string} */ - formulaType?: "static" | "dynamic" | "ai"; + formulaType?: "static" | "dynamic"; } | { /**