Skip to content

Commit b21fce1

Browse files
authored
Fix dsync.activated event parsing for Event API when domains present (#369)
1 parent a1b6c5a commit b21fce1

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

tests/test_webhooks.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import pytest
3+
from workos.typing.webhooks import WebhookTypeAdapter
34
from workos.webhooks import Webhooks
45

56

@@ -122,3 +123,34 @@ def test_unrecognized_webhook_type_returns_untyped_webhook(
122123
)
123124
assert type(result).__name__ == "UntypedWebhook"
124125
assert result.dict() == json.loads(mock_unknown_webhook_body)
126+
127+
# TODO: This test should be updated in the next major version to expect
128+
# a DirectoryActivatedWebhook return type.
129+
def test_validate_dsync_activated_event(self):
130+
event_body = {
131+
"id": "event_01J8SX5FTXYD2YFWVTGJY49EM6",
132+
"data": {
133+
"id": "directory_01EHWNC0FCBHZ3BJ7EGKYXK0E6",
134+
"name": "Foo Corp's Directory",
135+
"type": "generic scim v2.0",
136+
"state": "active",
137+
"object": "directory",
138+
"domains": [
139+
{
140+
"id": "org_domain_01EZTR5N6Y9RQKHK2E9F31KZX6",
141+
"domain": "foo-corp.com",
142+
"object": "organization_domain",
143+
}
144+
],
145+
"created_at": "2021-06-25T19:07:33.155Z",
146+
"updated_at": "2021-06-25T19:07:33.155Z",
147+
"external_key": "UWuccu6o1E0GqkYs",
148+
"organization_id": "org_01EZTR6WYX1A0DSE2CYMGXQ24Y",
149+
},
150+
"event": "dsync.activated",
151+
"created_at": "2021-06-25T19:07:33.155Z",
152+
}
153+
154+
result = WebhookTypeAdapter.validate_json(json.dumps(event_body))
155+
assert type(result).__name__ == "UntypedWebhook"
156+
assert result.dict() == event_body

tests/utils/fixtures/mock_event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from workos.types.events import DirectoryActivatedEvent
44
from workos.types.events.directory_payload_with_legacy_fields import (
55
DirectoryPayloadWithLegacyFields,
6+
DirectoryPayloadWithLegacyFieldsForEventsApi,
67
)
78

89

@@ -13,7 +14,7 @@ def __init__(self, id):
1314
object="event",
1415
id=id,
1516
event="dsync.activated",
16-
data=DirectoryPayloadWithLegacyFields(
17+
data=DirectoryPayloadWithLegacyFieldsForEventsApi(
1718
object="directory",
1819
id="dir_1234",
1920
organization_id="organization_id",

workos/types/events/directory_payload_with_legacy_fields.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
55

66
class MinimalOrganizationDomain(WorkOSModel):
77
id: str
8+
# TODO: This should be domain: str in the
9+
# next major version to fix object parsing.
810
organization_id: str
911
object: Literal["organization_domain"]
1012

1113

14+
# TODO: This class should be removed in the next major version once MinimalOrganizationDomain is updated.
15+
class MinimalOrganizationDomainForEventsApi(WorkOSModel):
16+
id: str
17+
domain: str
18+
object: Literal["organization_domain"]
19+
20+
1221
class DirectoryPayloadWithLegacyFields(DirectoryPayload):
1322
domains: Sequence[MinimalOrganizationDomain]
1423
external_key: str
24+
25+
26+
# TODO: This class should be removed in the next major version once MinimalOrganizationDomain is updated.
27+
class DirectoryPayloadWithLegacyFieldsForEventsApi(DirectoryPayload):
28+
domains: Sequence[MinimalOrganizationDomainForEventsApi]
29+
external_key: str

workos/types/events/event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from workos.types.events.directory_payload import DirectoryPayload
2929
from workos.types.events.directory_payload_with_legacy_fields import (
3030
DirectoryPayloadWithLegacyFields,
31+
DirectoryPayloadWithLegacyFieldsForEventsApi,
3132
)
3233
from workos.types.events.directory_user_with_previous_attributes import (
3334
DirectoryUserWithPreviousAttributes,
@@ -119,7 +120,7 @@ class ConnectionDeletedEvent(EventModel[Connection]):
119120
event: Literal["connection.deleted"]
120121

121122

122-
class DirectoryActivatedEvent(EventModel[DirectoryPayloadWithLegacyFields]):
123+
class DirectoryActivatedEvent(EventModel[DirectoryPayloadWithLegacyFieldsForEventsApi]):
123124
event: Literal["dsync.activated"]
124125

125126

workos/types/events/event_model.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from workos.types.events.directory_payload import DirectoryPayload
2828
from workos.types.events.directory_payload_with_legacy_fields import (
2929
DirectoryPayloadWithLegacyFields,
30+
DirectoryPayloadWithLegacyFieldsForEventsApi,
3031
)
3132
from workos.types.events.directory_user_with_previous_attributes import (
3233
DirectoryUserWithPreviousAttributes,
@@ -63,6 +64,8 @@
6364
ConnectionPayloadWithLegacyFields,
6465
DirectoryPayload,
6566
DirectoryPayloadWithLegacyFields,
67+
# TODO: Remove once merged with DirectoryPayloadWithLegacyFields in next major release.
68+
DirectoryPayloadWithLegacyFieldsForEventsApi,
6669
DirectoryGroup,
6770
DirectoryGroupWithPreviousAttributes,
6871
DirectoryUser,

0 commit comments

Comments
 (0)