From bfb0064289a24561db92d46a73f9702997ebe27e Mon Sep 17 00:00:00 2001 From: Sam Rose Date: Wed, 28 Feb 2024 11:46:58 +0000 Subject: [PATCH] More types. --- .../server/src/api/controllers/automation.ts | 4 +- .../src/api/routes/tests/automation.spec.ts | 4 +- .../src/api/routes/tests/backup.spec.ts | 2 +- .../src/api/routes/tests/webhook.spec.ts | 4 +- .../src/tests/utilities/TestConfiguration.ts | 60 ++++++++++++------- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/packages/server/src/api/controllers/automation.ts b/packages/server/src/api/controllers/automation.ts index 186b68f3b7..b7c29efa6f 100644 --- a/packages/server/src/api/controllers/automation.ts +++ b/packages/server/src/api/controllers/automation.ts @@ -72,7 +72,9 @@ function cleanAutomationInputs(automation: Automation) { return automation } -export async function create(ctx: UserCtx) { +export async function create( + ctx: UserCtx +) { const db = context.getAppDB() let automation = ctx.request.body automation.appId = ctx.appId diff --git a/packages/server/src/api/routes/tests/automation.spec.ts b/packages/server/src/api/routes/tests/automation.spec.ts index 178189555d..ee8fc7d544 100644 --- a/packages/server/src/api/routes/tests/automation.spec.ts +++ b/packages/server/src/api/routes/tests/automation.spec.ts @@ -394,7 +394,7 @@ describe("/automations", () => { it("deletes a automation by its ID", async () => { const automation = await config.createAutomation() const res = await request - .delete(`/api/automations/${automation.id}/${automation.rev}`) + .delete(`/api/automations/${automation._id}/${automation._rev}`) .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -408,7 +408,7 @@ describe("/automations", () => { await checkBuilderEndpoint({ config, method: "DELETE", - url: `/api/automations/${automation.id}/${automation._rev}`, + url: `/api/automations/${automation._id}/${automation._rev}`, }) }) }) diff --git a/packages/server/src/api/routes/tests/backup.spec.ts b/packages/server/src/api/routes/tests/backup.spec.ts index acfac783db..becbeb5480 100644 --- a/packages/server/src/api/routes/tests/backup.spec.ts +++ b/packages/server/src/api/routes/tests/backup.spec.ts @@ -44,7 +44,7 @@ describe("/backups", () => { expect(headers["content-disposition"]).toEqual( `attachment; filename="${ - config.getApp()!.name + config.getApp().name }-export-${mocks.date.MOCK_DATE.getTime()}.tar.gz"` ) }) diff --git a/packages/server/src/api/routes/tests/webhook.spec.ts b/packages/server/src/api/routes/tests/webhook.spec.ts index 38f84852b4..48a6da38bf 100644 --- a/packages/server/src/api/routes/tests/webhook.spec.ts +++ b/packages/server/src/api/routes/tests/webhook.spec.ts @@ -36,7 +36,7 @@ describe("/webhooks", () => { const automation = await config.createAutomation() const res = await request .put(`/api/webhooks`) - .send(basicWebhook(automation._id)) + .send(basicWebhook(automation._id!)) .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -145,7 +145,7 @@ describe("/webhooks", () => { let automation = collectAutomation() let newAutomation = await config.createAutomation(automation) let syncWebhook = await config.createWebhook( - basicWebhook(newAutomation._id) + basicWebhook(newAutomation._id!) ) // replicate changes before checking webhook diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index f6f0992585..599675bd4e 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -61,7 +61,7 @@ import { Table, TableSourceType, User, - UserRoles, + UserCtx, View, WithRequired, } from "@budibase/types" @@ -70,7 +70,6 @@ import API from "./api" import { cloneDeep } from "lodash" import jwt, { Secret } from "jsonwebtoken" import { Server } from "http" -import { userDetailListType } from "aws-sdk/clients/iam" mocks.licenses.init(pro) @@ -89,14 +88,14 @@ export default class TestConfiguration { request?: supertest.SuperTest started: boolean appId?: string - allApps: any[] + allApps: App[] app?: App prodApp?: App prodAppId?: string user?: User userMetadataId?: string table?: Table - automation: any + automation?: Automation datasource?: Datasource tenantId?: string api: API @@ -124,16 +123,26 @@ export default class TestConfiguration { } getApp() { + if (!this.app) { + throw new Error("app has not been initialised, call config.init() first") + } return this.app } getProdApp() { + if (!this.prodApp) { + throw new Error( + "prodApp has not been initialised, call config.init() first" + ) + } return this.prodApp } getAppId() { if (!this.appId) { - throw new Error("appId has not been initialised properly") + throw new Error( + "appId has not been initialised, call config.init() first" + ) } return this.appId } @@ -164,6 +173,15 @@ export default class TestConfiguration { } } + getAutomation() { + if (!this.automation) { + throw new Error( + "automation has not been initialised, call config.init() first" + ) + } + return this.automation + } + async doInContext( appId: string | undefined, task: () => Promise @@ -270,11 +288,11 @@ export default class TestConfiguration { // UTILS - _req, Res, Context extends Ctx>( - handler: (ctx: Context) => Promise, + _req, Res>( + handler: (ctx: UserCtx) => Promise, body?: Req, params?: Record - ) { + ): Promise { // create a fake request ctx const request: any = {} const appId = this.appId @@ -539,19 +557,20 @@ export default class TestConfiguration { // create dev app // clear any old app this.appId = undefined - this.app = await context.doInTenant(this.tenantId!, async () => { - const app = (await this._req(appController.create, { - name: appName, - })) as App - this.appId = app.appId! - return app - }) + this.app = await context.doInTenant( + this.tenantId!, + async () => + (await this._req(appController.create, { + name: appName, + })) as App + ) + this.appId = this.app.appId return await context.doInAppContext(this.app.appId!, async () => { // create production app this.prodApp = await this.publish() this.allApps.push(this.prodApp) - this.allApps.push(this.app) + this.allApps.push(this.app!) return this.app! }) @@ -739,14 +758,13 @@ export default class TestConfiguration { // AUTOMATION - async createAutomation(config?: any) { + async createAutomation(config?: Automation) { config = config || basicAutomation() if (config._rev) { delete config._rev } - this.automation = ( - await this._req(automationController.create, config) - ).automation + const res = await this._req(automationController.create, config) + this.automation = res.automation return this.automation } @@ -769,7 +787,7 @@ export default class TestConfiguration { if (!this.automation) { throw "Must create an automation before creating webhook." } - config = config || basicWebhook(this.automation._id) + config = config || basicWebhook(this.automation._id!) return (await this._req(webhookController.save, config)).webhook }