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 b3717439e7..d4d4fed120 100644 --- a/packages/worker/src/api/routes/global/tests/auditLogs.spec.ts +++ b/packages/worker/src/api/routes/global/tests/auditLogs.spec.ts @@ -12,121 +12,111 @@ const BASE_IDENTITY = { const USER_AUDIT_LOG_COUNT = 3 const APP_ID = "app_1" -describe("/api/global/auditlogs (%s)", () => { +describe.each(["lucene", "sql"])("/api/global/auditlogs (%s)", method => { const config = new TestConfiguration() + let envCleanup: (() => void) | undefined beforeAll(async () => { + envCleanup = features.testutils.setFeatureFlags("*", { + SQS: method === "sql", + }) await config.beforeAll() + await config.useNewTenant() }) afterAll(async () => { + envCleanup?.() await config.afterAll() }) - describe.each(["lucene", "sql"])( - "POST /api/global/auditlogs/search", - method => { - let envCleanup: (() => void) | undefined - - beforeAll(async () => { - envCleanup = features.testutils.setFeatureFlags("*", { - SQS: method === "sql", - }) - await config.useNewTenant() - }) - - afterAll(() => { - envCleanup?.() - }) - - it("should be able to fire some events (create audit logs)", async () => { - await context.doInTenant(config.tenantId, async () => { - const userId = config.user!._id! - const identity = { - ...BASE_IDENTITY, - _id: userId, - tenantId: config.tenantId, - } - await context.doInIdentityContext(identity, async () => { - for (let i = 0; i < USER_AUDIT_LOG_COUNT; i++) { - await events.user.created(structures.users.user()) - } - await context.doInAppContext(APP_ID, async () => { - await events.app.created(structures.apps.app(APP_ID)) - }) - - await utils.queue.processMessages( - events.processors.auditLogsProcessor.queue - ) - - // fetch the user created events - const response = await config.api.auditLogs.search({ - events: [Event.USER_CREATED], - }) - expect(response.data).toBeDefined() - // there will be an initial event which comes from the default user creation - expect(response.data.length).toBe(USER_AUDIT_LOG_COUNT + 1) - }) - }) - }) - - it("should be able to search by event", async () => { - const response = await config.api.auditLogs.search({ - events: [Event.USER_CREATED], - }) - expect(response.data.length).toBeGreaterThan(0) - for (let log of response.data) { - expect(log.event).toBe(Event.USER_CREATED) - } - }) - - it("should be able to search by time range (frozen)", async () => { - // this is frozen, only need to add 1 and minus 1 - const now = new Date() - const start = new Date() - start.setSeconds(now.getSeconds() - 1) - const end = new Date() - end.setSeconds(now.getSeconds() + 1) - const response = await config.api.auditLogs.search({ - startDate: start.toISOString(), - endDate: end.toISOString(), - }) - expect(response.data.length).toBeGreaterThan(0) - for (let log of response.data) { - expect(log.timestamp).toBe(now.toISOString()) - } - }) - - it("should be able to search by user ID", async () => { + describe("POST /api/global/auditlogs/search", () => { + it("should be able to fire some events (create audit logs)", async () => { + await context.doInTenant(config.tenantId, async () => { const userId = config.user!._id! - const response = await config.api.auditLogs.search({ - userIds: [userId], - }) - expect(response.data.length).toBeGreaterThan(0) - for (let log of response.data) { - expect(log.user._id).toBe(userId) + const identity = { + ...BASE_IDENTITY, + _id: userId, + tenantId: config.tenantId, } - }) + await context.doInIdentityContext(identity, async () => { + for (let i = 0; i < USER_AUDIT_LOG_COUNT; i++) { + await events.user.created(structures.users.user()) + } + await context.doInAppContext(APP_ID, async () => { + await events.app.created(structures.apps.app(APP_ID)) + }) - it("should be able to search by app ID", async () => { - const response = await config.api.auditLogs.search({ - appIds: [APP_ID], - }) - expect(response.data.length).toBeGreaterThan(0) - for (let log of response.data) { - expect(log.app?._id).toBe(APP_ID) - } - }) + await utils.queue.processMessages( + events.processors.auditLogsProcessor.queue + ) - it("should be able to search by full string", async () => { - const response = await config.api.auditLogs.search({ - fullSearch: "User", + // fetch the user created events + const response = await config.api.auditLogs.search({ + events: [Event.USER_CREATED], + }) + expect(response.data).toBeDefined() + // there will be an initial event which comes from the default user creation + expect(response.data.length).toBe(USER_AUDIT_LOG_COUNT + 1) }) - expect(response.data.length).toBeGreaterThan(0) - for (let log of response.data) { - expect(log.name.includes("User")).toBe(true) - } }) - } - ) + }) + + it("should be able to search by event", async () => { + const response = await config.api.auditLogs.search({ + events: [Event.USER_CREATED], + }) + expect(response.data.length).toBeGreaterThan(0) + for (let log of response.data) { + expect(log.event).toBe(Event.USER_CREATED) + } + }) + + it("should be able to search by time range (frozen)", async () => { + // this is frozen, only need to add 1 and minus 1 + const now = new Date() + const start = new Date() + start.setSeconds(now.getSeconds() - 1) + const end = new Date() + end.setSeconds(now.getSeconds() + 1) + const response = await config.api.auditLogs.search({ + startDate: start.toISOString(), + endDate: end.toISOString(), + }) + expect(response.data.length).toBeGreaterThan(0) + for (let log of response.data) { + expect(log.timestamp).toBe(now.toISOString()) + } + }) + + it("should be able to search by user ID", async () => { + const userId = config.user!._id! + const response = await config.api.auditLogs.search({ + userIds: [userId], + }) + expect(response.data.length).toBeGreaterThan(0) + for (let log of response.data) { + expect(log.user._id).toBe(userId) + } + }) + + it("should be able to search by app ID", async () => { + const response = await config.api.auditLogs.search({ + appIds: [APP_ID], + }) + expect(response.data.length).toBeGreaterThan(0) + for (let log of response.data) { + expect(log.app?._id).toBe(APP_ID) + } + }) + + it("should be able to search by full string", async () => { + const response = await config.api.auditLogs.search({ + fullSearch: "User", + }) + expect(response.data.length).toBeGreaterThan(0) + for (let log of response.data) { + expect(log.name.includes("User")).toBe(true) + } + }) + }) }) diff --git a/packages/worker/src/tests/TestConfiguration.ts b/packages/worker/src/tests/TestConfiguration.ts index c2cf005308..f93de73916 100644 --- a/packages/worker/src/tests/TestConfiguration.ts +++ b/packages/worker/src/tests/TestConfiguration.ts @@ -123,11 +123,11 @@ class TestConfiguration { } async afterAll() { - if (this.server) { - await this.server.close() - } else { - await require("../index").default.close() - } + // if (this.server) { + // await this.server.close() + // } else { + // await require("../index").default.close() + // } } // TENANCY