Skip to content

Commit

Permalink
Add organization membership events.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgd committed Apr 26, 2024
1 parent ff3e9f2 commit 91f8bc2
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/kotlin/com/workos/webhooks/models/EventType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.workos.webhooks.models

import com.workos.usermanagement.models.OrganizationMembership

/**
* Webhook Event for `organization_membership.created`.
*/
class OrganizationMembershipCreatedEvent(
@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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.workos.webhooks.models

import com.workos.usermanagement.models.OrganizationMembership

/**
* Webhook Event for `organization_membership.deleted`.
*/
class OrganizationMembershipDeletedEvent(
@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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.workos.webhooks.models

import com.workos.usermanagement.models.OrganizationMembership

/**
* Webhook Event for `organization_membership.updated`.
*/
class OrganizationMembershipUpdatedEvent(
@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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -48,6 +49,9 @@ class WebhookJsonDeserializer : JsonDeserializer<WebhookEvent>() {
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 -> OrganizationMembershipCreatedEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt)
EventType.OrganizationMembershipDeleted -> OrganizationMembershipDeletedEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt)
EventType.OrganizationMembershipUpdated -> OrganizationMembershipUpdatedEvent(id, eventType, deserializeData(data, OrganizationMembership::class.java), createdAt)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.workos.test.webhooks

import com.workos.test.TestBase
import com.workos.webhooks.models.EventType
import com.workos.webhooks.models.OrganizationMembershipCreatedEvent
import com.workos.webhooks.models.OrganizationMembershipDeletedEvent
import com.workos.webhooks.models.OrganizationMembershipUpdatedEvent
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 OrganizationMembershipCreatedEvent)
assertEquals(webhook.id, webhookId)
assertEquals((webhook as OrganizationMembershipCreatedEvent).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 OrganizationMembershipDeletedEvent)
assertEquals(webhook.id, webhookId)
assertEquals((webhook as OrganizationMembershipDeletedEvent).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 OrganizationMembershipUpdatedEvent)
assertEquals(webhook.id, webhookId)
assertEquals((webhook as OrganizationMembershipUpdatedEvent).data.id, organizationMembershipId)
}
}

0 comments on commit 91f8bc2

Please sign in to comment.