diff --git a/src/main/kotlin/com/workos/webhooks/models/EventType.kt b/src/main/kotlin/com/workos/webhooks/models/EventType.kt index 1f388f51..1e900ce8 100644 --- a/src/main/kotlin/com/workos/webhooks/models/EventType.kt +++ b/src/main/kotlin/com/workos/webhooks/models/EventType.kt @@ -79,5 +79,20 @@ enum class EventType( /** * Triggers when a group is removed from a directory. */ - DirectoryGroupDeleted("dsync.group.deleted") + DirectoryGroupDeleted("dsync.group.deleted"), + + /** + * Triggers when an organization membership is created. + */ + OrganizationMembershipCreated("organization_membership.created"), + + /** + * Triggers when an organization membership is deleted. + */ + OrganizationMembershipDeleted("organization_membership.deleted"), + + /** + * Triggers when an organization membership is updated. + */ + OrganizationMembershipUpdated("organization_membership.updated") } diff --git a/src/main/kotlin/com/workos/webhooks/models/OrganizationMembershipEvent.kt b/src/main/kotlin/com/workos/webhooks/models/OrganizationMembershipEvent.kt new file mode 100644 index 00000000..d20ad68a --- /dev/null +++ b/src/main/kotlin/com/workos/webhooks/models/OrganizationMembershipEvent.kt @@ -0,0 +1,20 @@ +package com.workos.webhooks.models + +import com.workos.usermanagement.models.OrganizationMembership + +/** + * Webhook Event for `organization_membership.*` events. + */ +class OrganizationMembershipEvent( + @JvmField + override val id: String, + + @JvmField + override val event: EventType, + + @JvmField + override val data: OrganizationMembership, + + @JvmField + override val createdAt: String +) : WebhookEvent(id, event, data, createdAt) diff --git a/src/main/kotlin/com/workos/webhooks/models/WebhookJsonDeserializer.kt b/src/main/kotlin/com/workos/webhooks/models/WebhookJsonDeserializer.kt index 9feb6861..9a60726e 100644 --- a/src/main/kotlin/com/workos/webhooks/models/WebhookJsonDeserializer.kt +++ b/src/main/kotlin/com/workos/webhooks/models/WebhookJsonDeserializer.kt @@ -11,6 +11,7 @@ import com.workos.directorysync.models.Directory import com.workos.directorysync.models.Group import com.workos.directorysync.models.User import com.workos.sso.models.Connection +import com.workos.usermanagement.models.OrganizationMembership /** * Custom JSON deserializer for [com.workos.webhooks.models.WebhookEvent] events. @@ -48,6 +49,9 @@ class WebhookJsonDeserializer : JsonDeserializer() { EventType.DirectoryGroupDeleted -> DirectoryGroupDeletedEvent(id, eventType, deserializeData(data, Group::class.java), createdAt) EventType.DirectoryGroupUserAdded -> DirectoryGroupUserAddedEvent(id, eventType, deserializeData(data, DirectoryGroupUserEvent::class.java), createdAt) EventType.DirectoryGroupUserRemoved -> DirectoryGroupUserRemovedEvent(id, eventType, deserializeData(data, DirectoryGroupUserEvent::class.java), createdAt) + EventType.OrganizationMembershipCreated -> OrganizationMembershipEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt) + EventType.OrganizationMembershipDeleted -> OrganizationMembershipEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt) + EventType.OrganizationMembershipUpdated -> OrganizationMembershipEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt) } } diff --git a/src/test/kotlin/com/workos/test/webhooks/OrganizationMembershipWebhookTests.kt b/src/test/kotlin/com/workos/test/webhooks/OrganizationMembershipWebhookTests.kt new file mode 100644 index 00000000..ab539b26 --- /dev/null +++ b/src/test/kotlin/com/workos/test/webhooks/OrganizationMembershipWebhookTests.kt @@ -0,0 +1,84 @@ +package com.workos.test.webhooks + +import com.workos.test.TestBase +import com.workos.webhooks.models.EventType +import com.workos.webhooks.models.OrganizationMembershipEvent +import org.junit.Test +import org.junit.jupiter.api.Assertions.assertTrue +import kotlin.test.assertEquals + +class OrganizationMembershipWebhookTests : TestBase() { + + private val organizationMembershipId = "om_01EHWNC0FCBHZ3BJ7EGKYXK0E7" + private val webhookId = "wh_01FMXJ2W7T9VY7EAHHMBF2K07Y" + + private fun generateWebhookEvent(eventType: EventType): String { + return """ + { + "id": "$webhookId", + "event": "${eventType.value}", + "data": { + "object": "organization_membership", + "id": "$organizationMembershipId", + "user_id": "user_01EHWNC0FCBHZ3BJ7EGKYXK0E6", + "organization_id": "org_01EHWNCE74X7JSDV0X3SZ3KJNY", + "status": "active", + "created_at": "2023-11-27T19:07:33.155Z", + "updated_at": "2023-11-27T19:07:33.155Z" + }, + "created_at": "2023-11-27T19:07:33.155Z" + } + """ + } + + @Test + fun constructOrganizationMembershipCreatedEvent() { + val workos = createWorkOSClient() + val webhookData = generateWebhookEvent(EventType.OrganizationMembershipCreated) + val testData = WebhooksApiTest.prepareTest(webhookData) + + val webhook = workos.webhooks.constructEvent( + webhookData, + testData["signature"] as String, + testData["secret"] as String + ) + + assertTrue(webhook is OrganizationMembershipEvent) + assertEquals(webhook.id, webhookId) + assertEquals((webhook as OrganizationMembershipEvent).data.id, organizationMembershipId) + } + + @Test + fun constructOrganizationMembershipDeletedEvent() { + val workos = createWorkOSClient() + val webhookData = generateWebhookEvent(EventType.OrganizationMembershipDeleted) + val testData = WebhooksApiTest.prepareTest(webhookData) + + val webhook = workos.webhooks.constructEvent( + webhookData, + testData["signature"] as String, + testData["secret"] as String + ) + + assertTrue(webhook is OrganizationMembershipEvent) + assertEquals(webhook.id, webhookId) + assertEquals((webhook as OrganizationMembershipEvent).data.id, organizationMembershipId) + } + + @Test + fun constructOrganizationMembershipUpdatedEvent() { + val workos = createWorkOSClient() + val webhookData = generateWebhookEvent(EventType.OrganizationMembershipUpdated) + val testData = WebhooksApiTest.prepareTest(webhookData) + + val webhook = workos.webhooks.constructEvent( + webhookData, + testData["signature"] as String, + testData["secret"] as String + ) + + assertTrue(webhook is OrganizationMembershipEvent) + assertEquals(webhook.id, webhookId) + assertEquals((webhook as OrganizationMembershipEvent).data.id, organizationMembershipId) + } +}