From 26f077cc1cd872f59e8731c29206a4f07cb56db2 Mon Sep 17 00:00:00 2001 From: adrinr Date: Fri, 24 Mar 2023 14:31:12 +0000 Subject: [PATCH] Event on delete --- packages/backend-core/src/events/publishers/scim.ts | 9 +++++++++ packages/backend-core/tests/utilities/mocks/events.ts | 1 + packages/types/src/sdk/events/event.ts | 2 ++ packages/types/src/sdk/events/scim.ts | 3 +++ .../src/api/routes/global/tests/scim/users.spec.ts | 11 ++++++++++- 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/backend-core/src/events/publishers/scim.ts b/packages/backend-core/src/events/publishers/scim.ts index e35cc52730..6339f81953 100644 --- a/packages/backend-core/src/events/publishers/scim.ts +++ b/packages/backend-core/src/events/publishers/scim.ts @@ -2,6 +2,7 @@ import { publishEvent } from "../events" import { Event, ScimUserCreatedEvent, + ScimUserDeletedEvent, ScimUserUpdatedEvent, } from "@budibase/types" @@ -25,7 +26,15 @@ async function SCIMUserUpdated(props: { await publishEvent(Event.SCIM_USER_UPDATED, properties, props.timestamp) } +async function SCIMUserDeleted(props: { userId: string }) { + const properties: ScimUserDeletedEvent = { + userId: props.userId, + } + await publishEvent(Event.SCIM_USER_DELETED, properties) +} + export default { SCIMUserCreated, SCIMUserUpdated, + SCIMUserDeleted, } diff --git a/packages/backend-core/tests/utilities/mocks/events.ts b/packages/backend-core/tests/utilities/mocks/events.ts index 5626fd9a01..28e37a8d62 100644 --- a/packages/backend-core/tests/utilities/mocks/events.ts +++ b/packages/backend-core/tests/utilities/mocks/events.ts @@ -123,3 +123,4 @@ jest.spyOn(events.plugin, "deleted") jest.spyOn(events.scim, "SCIMUserCreated") jest.spyOn(events.scim, "SCIMUserUpdated") +jest.spyOn(events.scim, "SCIMUserDeleted") diff --git a/packages/types/src/sdk/events/event.ts b/packages/types/src/sdk/events/event.ts index 79361d2b18..7f0c171af5 100644 --- a/packages/types/src/sdk/events/event.ts +++ b/packages/types/src/sdk/events/event.ts @@ -188,6 +188,7 @@ export enum Event { // SCIM SCIM_USER_CREATED = "scim:user:created", SCIM_USER_UPDATED = "scim:user:updated", + SCIM_USER_DELETED = "scim:user:deleted", } // all events that are not audited have been added to this record as undefined, this means @@ -372,6 +373,7 @@ export const AuditedEventFriendlyName: Record = { // SCIM [Event.SCIM_USER_CREATED]: `SCIM user "{{ email }}" created`, [Event.SCIM_USER_UPDATED]: `SCIM user "{{ email }}" updated`, + [Event.SCIM_USER_DELETED]: `SCIM user "{{ email }}" deleted`, } // properties added at the final stage of the event pipeline diff --git a/packages/types/src/sdk/events/scim.ts b/packages/types/src/sdk/events/scim.ts index e8e34f1e04..2bc348558b 100644 --- a/packages/types/src/sdk/events/scim.ts +++ b/packages/types/src/sdk/events/scim.ts @@ -7,3 +7,6 @@ export interface ScimUserCreatedEvent extends BaseEvent { export interface ScimUserUpdatedEvent extends BaseEvent { email: string } +export interface ScimUserDeletedEvent extends BaseEvent { + userId: string +} diff --git a/packages/worker/src/api/routes/global/tests/scim/users.spec.ts b/packages/worker/src/api/routes/global/tests/scim/users.spec.ts index 4437ee77c6..32b20c7c5e 100644 --- a/packages/worker/src/api/routes/global/tests/scim/users.spec.ts +++ b/packages/worker/src/api/routes/global/tests/scim/users.spec.ts @@ -438,7 +438,7 @@ describe("/api/global/scim/v2/users", () => { expect(persistedUser).toEqual(expectedScimUser) }) - it.only("an event is dispatched", async () => { + it("an event is dispatched", async () => { const body: ScimUpdateRequest = { schemas: ["urn:ietf:params:scim:api:messages:2.0:PatchOp"], Operations: [ @@ -498,5 +498,14 @@ describe("/api/global/scim/v2/users", () => { it("an non existing user can not be deleted", async () => { await deleteScimUser(structures.uuid(), { expect: 404 }) }) + + it("an event is dispatched", async () => { + await deleteScimUser(user.id, { expect: 204 }) + + expect(events.scim.SCIMUserDeleted).toBeCalledTimes(1) + expect(events.scim.SCIMUserDeleted).toBeCalledWith({ + userId: user.id, + }) + }) }) })