Skip to content

Commit

Permalink
Add add_user_to_organization and remove_user_to_organization methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatascastro12 committed Aug 21, 2023
1 parent a9df256 commit a78404d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
16 changes: 16 additions & 0 deletions tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,19 @@ def test_list_users_returns_metadata(
dict_users = users.to_dict()
assert dict_users["metadata"]["params"]["email"] == "[email protected]"
assert dict_users["metadata"]["params"]["organization"] == "foo-corp.com"

def test_add_user_to_organization(self, capture_and_mock_request, mock_user):
url, _ = capture_and_mock_request("post", mock_user, 200)

user = self.users.add_user_to_organization("user_123", "org_123")

assert url[0].endswith("users/user_123/organization/org_123")
assert user["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"

def test_remove_user_from_organization(self, capture_and_mock_request, mock_user):
url, _ = capture_and_mock_request("delete", mock_user, 200)

user = self.users.remove_user_from_organization("user_123", "org_123")

assert url[0].endswith("users/user_123/organization/org_123")
assert user["id"] == "user_01H7ZGXFP5C6BBQY6Z7277ZCT0"
51 changes: 48 additions & 3 deletions workos/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
RequestHelper,
REQUEST_METHOD_POST,
REQUEST_METHOD_GET,
REQUEST_METHOD_DELETE,
)
from workos.utils.validation import validate_settings, USERS_MODULE

USER_PATH = "users"
USER_DETAIL_PATH = "users/{0}"
USER_ORGANIZATION_PATH = "users/{0}/organization/{1}"

RESPONSE_LIMIT = 10

Expand Down Expand Up @@ -66,7 +69,7 @@ def get_user(self, user):
headers = {}

response = self.request_helper.request(
"{0}/{1}".format(USER_PATH, user),
USER_DETAIL_PATH.format(user),
method=REQUEST_METHOD_GET,
headers=headers,
token=workos.api_key,
Expand All @@ -85,8 +88,6 @@ def list_users(
):
"""Get a list of all of your existing users matching the criteria specified.
Kwargs:
email (str): Filter Users by their email. (Optional)
organization (list): Filter Users by the organization they are members of. (Optional)
Expand Down Expand Up @@ -140,3 +141,47 @@ def list_users(
response["metadata"] = {"params": {"default_limit": default_limit}}

return self.construct_from_response(response)

def add_user_to_organization(self, user, organization):
"""Adds a User as a member of the given Organization.
Kwargs:
user (str): The unique ID of the User.
organization (str): Unique ID of the Organization.
Returns:
dict: User response from WorkOS.
"""

headers = {}

response = self.request_helper.request(
USER_ORGANIZATION_PATH.format(user, organization),
method=REQUEST_METHOD_POST,
headers=headers,
token=workos.api_key,
)

return WorkOSUser.construct_from_response(response).to_dict()

def remove_user_from_organization(self, user, organization):
"""Removes a User from the given Organization.
Kwargs:
user (str): The unique ID of the User.
organization (str): Unique ID of the Organization.
Returns:
dict: User response from WorkOS.
"""

headers = {}

response = self.request_helper.request(
USER_ORGANIZATION_PATH.format(user, organization),
method=REQUEST_METHOD_DELETE,
headers=headers,
token=workos.api_key,
)

return WorkOSUser.construct_from_response(response).to_dict()

0 comments on commit a78404d

Please sign in to comment.