Skip to content

Commit f01d34f

Browse files
authored
Add GET /organization/:orgId/roles support (#392)
1 parent 22b22c3 commit f01d34f

File tree

7 files changed

+98
-12
lines changed

7 files changed

+98
-12
lines changed

tests/test_organizations.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from tests.types.test_auto_pagination_function import TestAutoPaginationFunction
55
from tests.utils.fixtures.mock_organization import MockOrganization
6+
from tests.utils.fixtures.mock_role import MockRole
67
from tests.utils.list_resource import list_response_of
78
from tests.utils.syncify import syncify
89
from workos.organizations import AsyncOrganizations, Organizations
@@ -67,6 +68,13 @@ def mock_organizations_multiple_data_pages(self):
6768
]
6869
return list_response_of(data=organizations_list)
6970

71+
@pytest.fixture
72+
def mock_organization_roles(self):
73+
return {
74+
"data": [MockRole(id=str(i)).dict() for i in range(10)],
75+
"object": "list",
76+
}
77+
7078
def test_list_organizations(
7179
self, mock_organizations, capture_and_mock_http_client_request
7280
):
@@ -227,3 +235,28 @@ def test_list_organizations_auto_pagination_for_multiple_pages(
227235
list_function=self.organizations.list_organizations,
228236
expected_all_page_data=mock_organizations_multiple_data_pages["data"],
229237
)
238+
239+
def test_list_organization_roles(
240+
self, mock_organization_roles, capture_and_mock_http_client_request
241+
):
242+
request_kwargs = capture_and_mock_http_client_request(
243+
self.http_client, mock_organization_roles, 200
244+
)
245+
246+
organization_roles_response = syncify(
247+
self.organizations.list_organization_roles(
248+
organization_id="org_01EHT88Z8J8795GZNQ4ZP1J81T"
249+
)
250+
)
251+
252+
def to_dict(x):
253+
return x.dict()
254+
255+
assert request_kwargs["method"] == "get"
256+
assert request_kwargs["url"].endswith(
257+
"/organizations/org_01EHT88Z8J8795GZNQ4ZP1J81T/roles"
258+
)
259+
assert (
260+
list(map(to_dict, organization_roles_response.data))
261+
== mock_organization_roles["data"]
262+
)

tests/utils/fixtures/mock_role.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import datetime
2+
3+
from workos.types.roles.role import Role
4+
5+
6+
class MockRole(Role):
7+
def __init__(self, id):
8+
now = datetime.datetime.now().isoformat()
9+
super().__init__(
10+
object="role",
11+
id=id,
12+
name="Member",
13+
slug="member",
14+
description="The default member role",
15+
type="EnvironmentRole",
16+
created_at=now,
17+
updated_at=now,
18+
)

workos/organizations.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from workos.types.organizations.domain_data_input import DomainDataInput
44
from workos.types.organizations.list_filters import OrganizationListFilters
5+
from workos.types.roles.role import RoleList
56
from workos.typing.sync_or_async import SyncOrAsync
67
from workos.utils.http_client import AsyncHTTPClient, SyncHTTPClient
78
from workos.utils.pagination_order import PaginationOrder
@@ -223,6 +224,14 @@ def delete_organization(self, organization_id: str) -> None:
223224
method=REQUEST_METHOD_DELETE,
224225
)
225226

227+
def list_organization_roles(self, organization_id: str) -> RoleList:
228+
response = self._http_client.request(
229+
f"organizations/{organization_id}/roles",
230+
method=REQUEST_METHOD_GET,
231+
)
232+
233+
return RoleList.model_validate(response)
234+
226235

227236
class AsyncOrganizations(OrganizationsModule):
228237

@@ -324,3 +333,11 @@ async def delete_organization(self, organization_id: str) -> None:
324333
f"organizations/{organization_id}",
325334
method=REQUEST_METHOD_DELETE,
326335
)
336+
337+
async def list_organization_roles(self, organization_id: str) -> RoleList:
338+
response = await self._http_client.request(
339+
f"organizations/{organization_id}/roles",
340+
method=REQUEST_METHOD_GET,
341+
)
342+
343+
return RoleList.model_validate(response)

workos/types/events/event.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
)
2828
from workos.types.events.directory_payload import DirectoryPayload
2929
from workos.types.events.directory_payload_with_legacy_fields import (
30-
DirectoryPayloadWithLegacyFields,
3130
DirectoryPayloadWithLegacyFieldsForEventsApi,
3231
)
3332
from workos.types.events.directory_user_with_previous_attributes import (
@@ -40,7 +39,7 @@
4039
from workos.types.events.session_created_payload import SessionCreatedPayload
4140
from workos.types.organizations.organization_common import OrganizationCommon
4241
from workos.types.organizations.organization_domain import OrganizationDomain
43-
from workos.types.roles.role import Role
42+
from workos.types.roles.role import EventRole
4443
from workos.types.sso.connection import Connection
4544
from workos.types.user_management.email_verification import (
4645
EmailVerificationCommon,
@@ -210,15 +209,15 @@ class PasswordResetCreatedEvent(EventModel[PasswordResetCommon]):
210209
event: Literal["password_reset.created"]
211210

212211

213-
class RoleCreatedEvent(EventModel[Role]):
212+
class RoleCreatedEvent(EventModel[EventRole]):
214213
event: Literal["role.created"]
215214

216215

217-
class RoleDeletedEvent(EventModel[Role]):
216+
class RoleDeletedEvent(EventModel[EventRole]):
218217
event: Literal["role.deleted"]
219218

220219

221-
class RoleUpdatedEvent(EventModel[Role]):
220+
class RoleUpdatedEvent(EventModel[EventRole]):
222221
event: Literal["role.updated"]
223222

224223

workos/types/events/event_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from workos.types.events.session_created_payload import SessionCreatedPayload
3939
from workos.types.organizations.organization_common import OrganizationCommon
4040
from workos.types.organizations.organization_domain import OrganizationDomain
41-
from workos.types.roles.role import Role
41+
from workos.types.roles.role import EventRole
4242
from workos.types.sso.connection import Connection
4343
from workos.types.user_management.email_verification import (
4444
EmailVerificationCommon,
@@ -72,14 +72,14 @@
7272
DirectoryUserWithPreviousAttributes,
7373
DirectoryGroupMembershipPayload,
7474
EmailVerificationCommon,
75+
EventRole,
7576
InvitationCommon,
7677
MagicAuthCommon,
7778
OrganizationCommon,
7879
OrganizationDomain,
7980
OrganizationDomainVerificationFailedPayload,
8081
OrganizationMembership,
8182
PasswordResetCommon,
82-
Role,
8383
SessionCreatedPayload,
8484
User,
8585
)

workos/types/roles/role.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
from typing import Literal, Optional, Sequence
22
from workos.types.workos_model import WorkOSModel
33

4+
RoleType = Literal["EnvironmentRole", "OrganizationRole"]
45

5-
class Role(WorkOSModel):
6+
7+
class RoleCommon(WorkOSModel):
68
object: Literal["role"]
79
slug: str
10+
11+
12+
class EventRole(RoleCommon):
813
permissions: Optional[Sequence[str]] = None
14+
15+
16+
class Role(RoleCommon):
17+
id: str
18+
name: str
19+
description: Optional[str] = None
20+
type: RoleType
21+
created_at: str
22+
updated_at: str
23+
24+
25+
class RoleList(WorkOSModel):
26+
object: Literal["list"]
27+
data: Sequence[Role]

workos/types/webhooks/webhook.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from workos.types.events.session_created_payload import SessionCreatedPayload
4040
from workos.types.organizations.organization_common import OrganizationCommon
4141
from workos.types.organizations.organization_domain import OrganizationDomain
42-
from workos.types.roles.role import Role
42+
from workos.types.roles.role import EventRole
4343
from workos.types.sso.connection import Connection
4444
from workos.types.user_management.email_verification import (
4545
EmailVerificationCommon,
@@ -213,15 +213,15 @@ class PasswordResetCreatedWebhook(WebhookModel[PasswordResetCommon]):
213213
event: Literal["password_reset.created"]
214214

215215

216-
class RoleCreatedWebhook(WebhookModel[Role]):
216+
class RoleCreatedWebhook(WebhookModel[EventRole]):
217217
event: Literal["role.created"]
218218

219219

220-
class RoleDeletedWebhook(WebhookModel[Role]):
220+
class RoleDeletedWebhook(WebhookModel[EventRole]):
221221
event: Literal["role.deleted"]
222222

223223

224-
class RoleUpdatedWebhook(WebhookModel[Role]):
224+
class RoleUpdatedWebhook(WebhookModel[EventRole]):
225225
event: Literal["role.updated"]
226226

227227

0 commit comments

Comments
 (0)