From 4f6a09fb023b9b017863e9da96277f58bde9a977 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 6 Nov 2024 17:14:03 +0100 Subject: [PATCH] Add utils --- .../src/events/processors/AuditLogsProcessor.ts | 4 ++++ packages/backend-core/src/events/processors/index.ts | 4 ++-- packages/backend-core/tests/core/utilities/index.ts | 1 + packages/backend-core/tests/core/utilities/queue.ts | 9 +++++++++ .../worker/src/api/routes/global/tests/auditLogs.spec.ts | 7 ++++++- 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 packages/backend-core/tests/core/utilities/queue.ts diff --git a/packages/backend-core/src/events/processors/AuditLogsProcessor.ts b/packages/backend-core/src/events/processors/AuditLogsProcessor.ts index 3dd2ab9d10..8dadf7b7a5 100644 --- a/packages/backend-core/src/events/processors/AuditLogsProcessor.ts +++ b/packages/backend-core/src/events/processors/AuditLogsProcessor.ts @@ -17,6 +17,10 @@ export default class AuditLogsProcessor implements EventProcessor { static auditLogsEnabled = false static auditLogQueue: BullQueue.Queue + get queue() { + return AuditLogsProcessor.auditLogQueue + } + // can't use constructor as need to return promise static init(fn: AuditLogFn) { AuditLogsProcessor.auditLogsEnabled = true diff --git a/packages/backend-core/src/events/processors/index.ts b/packages/backend-core/src/events/processors/index.ts index 6646764e47..f88cf43c97 100644 --- a/packages/backend-core/src/events/processors/index.ts +++ b/packages/backend-core/src/events/processors/index.ts @@ -5,8 +5,8 @@ import Processors from "./Processors" import { AuditLogFn } from "@budibase/types" export const analyticsProcessor = new AnalyticsProcessor() -const loggingProcessor = new LoggingProcessor() -const auditLogsProcessor = new AuditLogsProcessor() +export const loggingProcessor = new LoggingProcessor() +export const auditLogsProcessor = new AuditLogsProcessor() export function init(auditingFn: AuditLogFn) { return AuditLogsProcessor.init(auditingFn) diff --git a/packages/backend-core/tests/core/utilities/index.ts b/packages/backend-core/tests/core/utilities/index.ts index 787d69be2c..c3d81784c8 100644 --- a/packages/backend-core/tests/core/utilities/index.ts +++ b/packages/backend-core/tests/core/utilities/index.ts @@ -4,3 +4,4 @@ export { generator } from "./structures" export * as testContainerUtils from "./testContainerUtils" export * as utils from "./utils" export * from "./jestUtils" +export * as queue from "./queue" diff --git a/packages/backend-core/tests/core/utilities/queue.ts b/packages/backend-core/tests/core/utilities/queue.ts new file mode 100644 index 0000000000..49dd33ca29 --- /dev/null +++ b/packages/backend-core/tests/core/utilities/queue.ts @@ -0,0 +1,9 @@ +import { Queue } from "bull" + +export async function processMessages(queue: Queue) { + do { + await queue.whenCurrentJobsFinished() + } while (await queue.count()) + + await queue.whenCurrentJobsFinished() +} diff --git a/packages/worker/src/api/routes/global/tests/auditLogs.spec.ts b/packages/worker/src/api/routes/global/tests/auditLogs.spec.ts index 55664f9893..601c3ffe62 100644 --- a/packages/worker/src/api/routes/global/tests/auditLogs.spec.ts +++ b/packages/worker/src/api/routes/global/tests/auditLogs.spec.ts @@ -1,4 +1,4 @@ -import { mocks, structures } from "@budibase/backend-core/tests" +import { mocks, structures, queue } from "@budibase/backend-core/tests" import { context, events, features } from "@budibase/backend-core" import { Event, IdentityType } from "@budibase/types" import { TestConfiguration } from "../../../../tests" @@ -54,6 +54,11 @@ describe("/api/global/auditlogs (%s)", () => { await context.doInAppContext(APP_ID, async () => { await events.app.created(structures.apps.app(APP_ID)) }) + + await queue.processMessages( + events.processors.auditLogsProcessor.queue + ) + // fetch the user created events const response = await config.api.auditLogs.search({ events: [Event.USER_CREATED],