Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/main/kotlin/com/workos/webhooks/models/EventType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ enum class EventType(
*/
OrganizationMembershipUpdated("organization_membership.updated"),

/**
* Triggers when an organization is created.
*/
OrganizationCreated("organization.created"),

/**
* Triggers when an organization is updated.
*/
OrganizationUpdated("organization.updated"),

/**
* Triggers when an organization is deleted.
*/
OrganizationDeleted("organization.deleted"),

/**
* Triggers when a user requests to reset their password.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.workos.webhooks.models

import com.workos.organizations.models.Organization

/**
* Webhook Event for `organization.created`.
*/
class OrganizationCreatedEvent(
@JvmField
override val id: String,

@JvmField
override val event: EventType,

@JvmField
override val data: Organization,

@JvmField
override val createdAt: String
) : WebhookEvent(id, event, data, createdAt)

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.workos.webhooks.models

import com.workos.organizations.models.Organization

/**
* Webhook Event for `organization.deleted`.
*/
class OrganizationDeletedEvent(
@JvmField
override val id: String,

@JvmField
override val event: EventType,

@JvmField
override val data: Organization,

@JvmField
override val createdAt: String
) : WebhookEvent(id, event, data, createdAt)

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.workos.webhooks.models

import com.workos.organizations.models.Organization

/**
* Webhook Event for `organization.updated`.
*/
class OrganizationUpdatedEvent(
@JvmField
override val id: String,

@JvmField
override val event: EventType,

@JvmField
override val data: Organization,

@JvmField
override val createdAt: String
) : WebhookEvent(id, event, data, createdAt)

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.workos.directorysync.models.Directory
import com.workos.directorysync.models.Group
import com.workos.directorysync.models.User
import com.workos.organizations.models.Organization
import com.workos.sso.models.Connection
import com.workos.usermanagement.models.AuthenticationEventData
import com.workos.usermanagement.models.EmailVerificationEventData
Expand Down Expand Up @@ -73,6 +74,9 @@ class WebhookJsonDeserializer : JsonDeserializer<WebhookEvent>() {
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)
EventType.OrganizationCreated -> OrganizationCreatedEvent(id, eventType, deserializeData(data, Organization::class.java), createdAt)
EventType.OrganizationUpdated -> OrganizationUpdatedEvent(id, eventType, deserializeData(data, Organization::class.java), createdAt)
EventType.OrganizationDeleted -> OrganizationDeletedEvent(id, eventType, deserializeData(data, Organization::class.java), createdAt)
EventType.PasswordResetCreated -> PasswordResetEvent(id, eventType, deserializeData(data, PasswordResetEventData::class.java), createdAt)
EventType.PasswordResetSucceeded -> PasswordResetEvent(id, eventType, deserializeData(data, PasswordResetEventData::class.java), createdAt)
EventType.UserCreated -> UserCreatedEvent(id, eventType, deserializeData(data, UserManagementUser::class.java), createdAt)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.workos.test.webhooks

import com.workos.test.TestBase
import com.workos.webhooks.models.EventType
import com.workos.webhooks.models.OrganizationCreatedEvent
import com.workos.webhooks.models.OrganizationUpdatedEvent
import com.workos.webhooks.models.OrganizationDeletedEvent
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertTrue
import kotlin.test.assertEquals

class OrganizationWebhookTests : TestBase() {

private val organizationId = "org_01EHWNCE74X7JSDV0X3SZ3KJNY"
private val webhookId = "wh_01FMXJ2W7T9VY7EAHHMBF2K07Y"

private fun generateWebhookEvent(eventType: EventType): String {
return """
{
"id": "$webhookId",
"event": "${eventType.value}",
"data": {
"object": "organization",
"id": "$organizationId",
"name": "Test Organization",
"allow_profiles_outside_organization": false,
"domains": [
{
"object": "organization_domain",
"id": "org_domain_01H7ZGXFP5C6BBQY6Z7277ZCT0",
"domain": "example.com",
"state": "verified",
"verification_strategy": "dns",
"verification_token": "abc123"
}
],
"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 constructOrganizationCreatedEvent() {
val workos = createWorkOSClient()
val webhookData = generateWebhookEvent(EventType.OrganizationCreated)
val testData = WebhooksApiTest.prepareTest(webhookData)

val webhook = workos.webhooks.constructEvent(
webhookData,
testData["signature"] as String,
testData["secret"] as String
)

assertTrue(webhook is OrganizationCreatedEvent)
assertEquals(webhook.id, webhookId)
assertEquals((webhook as OrganizationCreatedEvent).data.id, organizationId)
assertEquals(webhook.data.name, "Test Organization")
}

@Test
fun constructOrganizationUpdatedEvent() {
val workos = createWorkOSClient()
val webhookData = generateWebhookEvent(EventType.OrganizationUpdated)
val testData = WebhooksApiTest.prepareTest(webhookData)

val webhook = workos.webhooks.constructEvent(
webhookData,
testData["signature"] as String,
testData["secret"] as String
)

assertTrue(webhook is OrganizationUpdatedEvent)
assertEquals(webhook.id, webhookId)
assertEquals((webhook as OrganizationUpdatedEvent).data.id, organizationId)
assertEquals(webhook.data.name, "Test Organization")
}

@Test
fun constructOrganizationDeletedEvent() {
val workos = createWorkOSClient()
val webhookData = generateWebhookEvent(EventType.OrganizationDeleted)
val testData = WebhooksApiTest.prepareTest(webhookData)

val webhook = workos.webhooks.constructEvent(
webhookData,
testData["signature"] as String,
testData["secret"] as String
)

assertTrue(webhook is OrganizationDeletedEvent)
assertEquals(webhook.id, webhookId)
assertEquals((webhook as OrganizationDeletedEvent).data.id, organizationId)
assertEquals(webhook.data.name, "Test Organization")
}
}

Loading