From 3eff4d85d03182f5d855eb41bd3cdd5575d0303f Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 26 Jun 2023 18:52:15 +0100 Subject: [PATCH 1/2] Adding a test case for the removal of automation logs from app sync, as well as adding to the publish/sync filter a check to not carry over automation logs. --- packages/backend-core/src/db/Replication.ts | 3 ++ .../src/api/routes/tests/application.spec.ts | 37 ++++++++++++++++++- .../src/tests/utilities/TestConfiguration.ts | 20 ++++++++++ .../server/src/tests/utilities/structures.ts | 17 +++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) diff --git a/packages/backend-core/src/db/Replication.ts b/packages/backend-core/src/db/Replication.ts index eb9d613a58..e813722d98 100644 --- a/packages/backend-core/src/db/Replication.ts +++ b/packages/backend-core/src/db/Replication.ts @@ -57,6 +57,9 @@ class Replication { appReplicateOpts() { return { filter: (doc: any) => { + if (doc._id && doc._id.startsWith(DocumentType.AUTOMATION_LOG)) { + return false + } return doc._id !== DocumentType.APP_METADATA }, } diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index 1f4f189724..207d25d508 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -14,7 +14,7 @@ jest.mock("../../../utilities/redis", () => ({ import { clearAllApps, checkBuilderEndpoint } from "./utilities/TestFunctions" import * as setup from "./utilities" import { AppStatus } from "../../../db/utils" -import { events, utils } from "@budibase/backend-core" +import { events, utils, context } from "@budibase/backend-core" import env from "../../../environment" jest.setTimeout(15000) @@ -324,4 +324,39 @@ describe("/applications", () => { expect(events.app.unpublished).toBeCalledTimes(1) }) }) + + describe("app sync", () => { + it("should not sync automation logs", async () => { + // setup the apps + await config.createApp("testing-auto-logs") + const automation = await config.createAutomation() + await config.publish() + await context.doInAppContext(config.getProdAppId(), () => { + return config.createAutomationLog(automation) + }) + + // do the sync + const appId = config.getAppId() + await request + .post(`/api/applications/${appId}/sync`) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + + // does exist in prod + await context.doInAppContext(config.getProdAppId(), async () => { + const logs = await config.getAutomationLogs() + expect(logs.data.length).toBe(1) + }) + + // delete prod app so we revert to dev log search + await config.unpublish() + + // doesn't exist in dev + await context.doInAppContext(config.getAppId(), async () => { + const logs = await config.getAutomationLogs() + expect(logs.data.length).toBe(0) + }) + }) + }) }) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 995035aa97..26f70ec845 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -21,6 +21,7 @@ import { basicScreen, basicLayout, basicWebhook, + basicAutomationLog, } from "./structures" import { constants, @@ -48,6 +49,7 @@ import { Table, SearchFilters, UserRoles, + Automation, } from "@budibase/types" import { BUILTIN_ROLE_IDS } from "@budibase/backend-core/src/security/roles" @@ -720,6 +722,24 @@ class TestConfiguration { return { datasource, query: basedOnQuery } } + // AUTOMATION LOG + + async createAutomationLog(automation: Automation) { + return await context.doInAppContext(this.getProdAppId(), async () => { + return await pro.sdk.automations.logs.storeLog( + automation, + basicAutomationLog(automation._id!) + ) + }) + } + + async getAutomationLogs() { + const now = new Date() + return await pro.sdk.automations.logs.logSearch({ + startDate: new Date(now.getTime() - 100000).toISOString(), + }) + } + // QUERY async previewQuery( diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 71f30f0074..02c62b85ba 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -9,6 +9,8 @@ import { import { Automation, AutomationActionStepId, + AutomationResults, + AutomationStatus, AutomationStep, AutomationStepType, AutomationTrigger, @@ -241,6 +243,21 @@ export function collectAutomation(tableId?: string): Automation { return automation as Automation } +export function basicAutomationLog(automationId: string): AutomationResults { + return { + automationId, + status: AutomationStatus.SUCCESS, + trigger: "trigger", + steps: [ + { + stepId: AutomationActionStepId.SERVER_LOG, + inputs: {}, + outputs: {}, + }, + ], + } +} + export function basicRow(tableId: string) { return { name: "Test Contact", From 47260f23bf8c0cf39f1a2ef3ae9440d0b61e2d2c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 27 Jun 2023 10:01:06 +0100 Subject: [PATCH 2/2] PR comments. --- .../src/api/routes/tests/application.spec.ts | 14 +++++--------- .../src/tests/utilities/TestConfiguration.ts | 12 +++++++----- packages/server/src/tests/utilities/structures.ts | 4 +++- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/server/src/api/routes/tests/application.spec.ts b/packages/server/src/api/routes/tests/application.spec.ts index 207d25d508..fa5cb0a983 100644 --- a/packages/server/src/api/routes/tests/application.spec.ts +++ b/packages/server/src/api/routes/tests/application.spec.ts @@ -325,7 +325,7 @@ describe("/applications", () => { }) }) - describe("app sync", () => { + describe("POST /api/applications/:appId/sync", () => { it("should not sync automation logs", async () => { // setup the apps await config.createApp("testing-auto-logs") @@ -344,19 +344,15 @@ describe("/applications", () => { .expect(200) // does exist in prod - await context.doInAppContext(config.getProdAppId(), async () => { - const logs = await config.getAutomationLogs() - expect(logs.data.length).toBe(1) - }) + const prodLogs = await config.getAutomationLogs() + expect(prodLogs.data.length).toBe(1) // delete prod app so we revert to dev log search await config.unpublish() // doesn't exist in dev - await context.doInAppContext(config.getAppId(), async () => { - const logs = await config.getAutomationLogs() - expect(logs.data.length).toBe(0) - }) + const devLogs = await config.getAutomationLogs() + expect(devLogs.data.length).toBe(0) }) }) }) diff --git a/packages/server/src/tests/utilities/TestConfiguration.ts b/packages/server/src/tests/utilities/TestConfiguration.ts index 26f70ec845..2078bf765a 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.ts +++ b/packages/server/src/tests/utilities/TestConfiguration.ts @@ -21,7 +21,7 @@ import { basicScreen, basicLayout, basicWebhook, - basicAutomationLog, + basicAutomationResults, } from "./structures" import { constants, @@ -728,15 +728,17 @@ class TestConfiguration { return await context.doInAppContext(this.getProdAppId(), async () => { return await pro.sdk.automations.logs.storeLog( automation, - basicAutomationLog(automation._id!) + basicAutomationResults(automation._id!) ) }) } async getAutomationLogs() { - const now = new Date() - return await pro.sdk.automations.logs.logSearch({ - startDate: new Date(now.getTime() - 100000).toISOString(), + return context.doInAppContext(this.appId, async () => { + const now = new Date() + return await pro.sdk.automations.logs.logSearch({ + startDate: new Date(now.getTime() - 100000).toISOString(), + }) }) } diff --git a/packages/server/src/tests/utilities/structures.ts b/packages/server/src/tests/utilities/structures.ts index 02c62b85ba..0a9a9d5a68 100644 --- a/packages/server/src/tests/utilities/structures.ts +++ b/packages/server/src/tests/utilities/structures.ts @@ -243,7 +243,9 @@ export function collectAutomation(tableId?: string): Automation { return automation as Automation } -export function basicAutomationLog(automationId: string): AutomationResults { +export function basicAutomationResults( + automationId: string +): AutomationResults { return { automationId, status: AutomationStatus.SUCCESS,