Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add organization membership events #218

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*` 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)
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 -> 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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}