Add utils

This commit is contained in:
Adria Navarro 2024-11-06 18:33:06 +01:00
parent f7f056ef76
commit 505146f4b3
4 changed files with 106 additions and 106 deletions

View File

@ -4,3 +4,4 @@ export { generator } from "./structures"
export * as testContainerUtils from "./testContainerUtils" export * as testContainerUtils from "./testContainerUtils"
export * as utils from "./utils" export * as utils from "./utils"
export * from "./jestUtils" export * from "./jestUtils"
export * as queue from "./queue"

View File

@ -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()
}

View File

@ -12,121 +12,111 @@ const BASE_IDENTITY = {
const USER_AUDIT_LOG_COUNT = 3 const USER_AUDIT_LOG_COUNT = 3
const APP_ID = "app_1" const APP_ID = "app_1"
describe("/api/global/auditlogs (%s)", () => { describe.each(["lucene", "sql"])("/api/global/auditlogs (%s)", method => {
const config = new TestConfiguration() const config = new TestConfiguration()
let envCleanup: (() => void) | undefined
beforeAll(async () => { beforeAll(async () => {
envCleanup = features.testutils.setFeatureFlags("*", {
SQS: method === "sql",
})
await config.beforeAll() await config.beforeAll()
await config.useNewTenant()
}) })
afterAll(async () => { afterAll(async () => {
envCleanup?.()
await config.afterAll() await config.afterAll()
}) })
describe.each(["lucene", "sql"])( describe("POST /api/global/auditlogs/search", () => {
"POST /api/global/auditlogs/search", it("should be able to fire some events (create audit logs)", async () => {
method => { await context.doInTenant(config.tenantId, async () => {
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 () => {
const userId = config.user!._id! const userId = config.user!._id!
const response = await config.api.auditLogs.search({ const identity = {
userIds: [userId], ...BASE_IDENTITY,
}) _id: userId,
expect(response.data.length).toBeGreaterThan(0) tenantId: config.tenantId,
for (let log of response.data) {
expect(log.user._id).toBe(userId)
} }
}) 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 () => { await utils.queue.processMessages(
const response = await config.api.auditLogs.search({ events.processors.auditLogsProcessor.queue
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 () => { // fetch the user created events
const response = await config.api.auditLogs.search({ const response = await config.api.auditLogs.search({
fullSearch: "User", 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)
}
})
})
}) })

View File

@ -123,11 +123,11 @@ class TestConfiguration {
} }
async afterAll() { async afterAll() {
if (this.server) { // if (this.server) {
await this.server.close() // await this.server.close()
} else { // } else {
await require("../index").default.close() // await require("../index").default.close()
} // }
} }
// TENANCY // TENANCY