From 4cab97b8abea1245bf457dbbd30770c3a8813b2b Mon Sep 17 00:00:00 2001 From: adrinr Date: Fri, 3 Feb 2023 17:49:21 +0000 Subject: [PATCH] Improve automation tests --- .../src/api/routes/tests/application.spec.ts | 26 ++++++++++++++----- .../routes/tests/utilities/TestFunctions.ts | 7 +++-- .../src/tests/utilities/TestConfiguration.ts | 3 --- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index 3177f776c2..1f4f189724 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -14,18 +14,22 @@ jest.mock("../../../utilities/redis", () => ({ import { clearAllApps, checkBuilderEndpoint } from "./utilities/TestFunctions" import * as setup from "./utilities" import { AppStatus } from "../../../db/utils" -import { events } from "@budibase/backend-core" +import { events, utils } from "@budibase/backend-core" import env from "../../../environment" +jest.setTimeout(15000) + describe("/applications", () => { let request = setup.getRequest() let config = setup.getConfig() afterAll(setup.afterAll) - beforeEach(async () => { - await clearAllApps() + beforeAll(async () => { await config.init() + }) + + beforeEach(async () => { jest.clearAllMocks() }) @@ -33,7 +37,7 @@ describe("/applications", () => { it("creates empty app", async () => { const res = await request .post("/api/applications") - .field("name", "My App") + .field("name", utils.newid()) .set(config.defaultHeaders()) .expect("Content-Type", /json/) .expect(200) @@ -44,7 +48,7 @@ describe("/applications", () => { it("creates app from template", async () => { const res = await request .post("/api/applications") - .field("name", "My App") + .field("name", utils.newid()) .field("useTemplate", "true") .field("templateKey", "test") .field("templateString", "{}") // override the file download @@ -59,7 +63,7 @@ describe("/applications", () => { it("creates app from file", async () => { const res = await request .post("/api/applications") - .field("name", "My App") + .field("name", utils.newid()) .field("useTemplate", "true") .set(config.defaultHeaders()) .attach("templateFile", "src/api/routes/tests/data/export.txt") @@ -106,6 +110,11 @@ describe("/applications", () => { }) describe("fetch", () => { + beforeEach(async () => { + // Clean all apps but the onde from config + await clearAllApps(config.getTenantId(), [config.getAppId()!]) + }) + it("lists all applications", async () => { await config.createApp("app1") await config.createApp("app2") @@ -266,6 +275,11 @@ describe("/applications", () => { }) describe("unpublish", () => { + beforeEach(async () => { + // We want to republish as the unpublish will delete the prod app + await config.publish() + }) + it("should unpublish app with dev app ID", async () => { const appId = config.getAppId() await request diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts index ad65c705ac..3f57c1e8ef 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.ts +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.ts @@ -32,7 +32,10 @@ export const getAllTableRows = async (config: any) => { return req.body } -export const clearAllApps = async (tenantId = TENANT_ID) => { +export const clearAllApps = async ( + tenantId = TENANT_ID, + exceptions: Array = [] +) => { await tenancy.doInTenant(tenantId, async () => { const req: any = { query: { status: AppStatus.DEV }, user: { tenantId } } await appController.fetch(req) @@ -40,7 +43,7 @@ export const clearAllApps = async (tenantId = TENANT_ID) => { if (!apps || apps.length <= 0) { return } - for (let app of apps) { + for (let app of apps.filter((x: any) => !exceptions.includes(x.appId))) { const { appId } = app const req = new Request(null, { appId }) await runRequest(appId, appController.destroy, req) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 510cb1bf42..123a8047a8 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -363,9 +363,6 @@ class TestConfiguration { if (this.appId) { headers[constants.Header.APP_ID] = this.appId } - if (this.tenantId) { - headers[constants.Header.TENANT_ID] = this.tenantId - } return headers }