Skip to content

Commit 47b34e9

Browse files
authored
feat: Add ability to accept invitations (#552)
1 parent a806a31 commit 47b34e9

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/workos/user_management.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
INVITATION_DETAIL_BY_TOKEN_PATH = "user_management/invitations/by_token/{0}"
100100
INVITATION_REVOKE_PATH = "user_management/invitations/{0}/revoke"
101101
INVITATION_RESEND_PATH = "user_management/invitations/{0}/resend"
102+
INVITATION_ACCEPT_PATH = "user_management/invitations/{0}/accept"
102103
PASSWORD_RESET_PATH = "user_management/password_reset"
103104
PASSWORD_RESET_DETAIL_PATH = "user_management/password_reset/{0}"
104105
USER_FEATURE_FLAGS_PATH = "user_management/users/{0}/feature-flags"
@@ -915,6 +916,17 @@ def resend_invitation(self, invitation_id: str) -> SyncOrAsync[Invitation]:
915916
"""
916917
...
917918

919+
def accept_invitation(self, invitation_id: str) -> SyncOrAsync[Invitation]:
920+
"""Accepts an existing Invitation.
921+
922+
Args:
923+
invitation_id (str): The unique ID of the Invitation.
924+
925+
Returns:
926+
Invitation: Invitation response from WorkOS.
927+
"""
928+
...
929+
918930
def list_feature_flags(
919931
self,
920932
user_id: str,
@@ -1634,6 +1646,13 @@ def resend_invitation(self, invitation_id: str) -> Invitation:
16341646

16351647
return Invitation.model_validate(response)
16361648

1649+
def accept_invitation(self, invitation_id: str) -> Invitation:
1650+
response = self._http_client.request(
1651+
INVITATION_ACCEPT_PATH.format(invitation_id), method=REQUEST_METHOD_POST
1652+
)
1653+
1654+
return Invitation.model_validate(response)
1655+
16371656
def list_feature_flags(
16381657
self,
16391658
user_id: str,
@@ -2373,6 +2392,13 @@ async def resend_invitation(self, invitation_id: str) -> Invitation:
23732392

23742393
return Invitation.model_validate(response)
23752394

2395+
async def accept_invitation(self, invitation_id: str) -> Invitation:
2396+
response = await self._http_client.request(
2397+
INVITATION_ACCEPT_PATH.format(invitation_id), method=REQUEST_METHOD_POST
2398+
)
2399+
2400+
return Invitation.model_validate(response)
2401+
23762402
async def list_feature_flags(
23772403
self,
23782404
user_id: str,

tests/test_user_management.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,44 @@ def test_resend_invitation_accepted(self, capture_and_mock_http_client_request):
12601260
with pytest.raises(Exception):
12611261
syncify(self.user_management.resend_invitation("invitation_accepted"))
12621262

1263+
def test_accept_invitation(
1264+
self, capture_and_mock_http_client_request, mock_invitation
1265+
):
1266+
request_kwargs = capture_and_mock_http_client_request(
1267+
self.http_client, mock_invitation, 200
1268+
)
1269+
1270+
invitation = syncify(self.user_management.accept_invitation("invitation_ABCDE"))
1271+
1272+
assert request_kwargs["url"].endswith(
1273+
"user_management/invitations/invitation_ABCDE/accept"
1274+
)
1275+
assert request_kwargs["method"] == "post"
1276+
assert isinstance(invitation, Invitation)
1277+
assert invitation.id == "invitation_ABCDE"
1278+
1279+
def test_accept_invitation_not_found(self, capture_and_mock_http_client_request):
1280+
error_response = {
1281+
"message": "Invitation not found",
1282+
"code": "not_found",
1283+
}
1284+
capture_and_mock_http_client_request(self.http_client, error_response, 404)
1285+
1286+
with pytest.raises(Exception):
1287+
syncify(self.user_management.accept_invitation("invitation_nonexistent"))
1288+
1289+
def test_accept_invitation_already_accepted(
1290+
self, capture_and_mock_http_client_request
1291+
):
1292+
error_response = {
1293+
"message": "Invite has already been accepted.",
1294+
"code": "invite_accepted",
1295+
}
1296+
capture_and_mock_http_client_request(self.http_client, error_response, 400)
1297+
1298+
with pytest.raises(Exception):
1299+
syncify(self.user_management.accept_invitation("invitation_accepted"))
1300+
12631301
def test_list_feature_flags(
12641302
self, mock_feature_flags, capture_and_mock_http_client_request
12651303
):

0 commit comments

Comments
 (0)