Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role to OrganizationMembership class #236

Merged
merged 9 commits into from
Mar 18, 2024
Merged
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
14 changes: 14 additions & 0 deletions tests/test_user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ def test_create_organization_membership(
assert organization_membership["user_id"] == user_id
assert organization_membership["organization_id"] == organization_id

def test_update_organization_membership(
self, capture_and_mock_request, mock_organization_membership
):
url, _ = capture_and_mock_request("put", mock_organization_membership, 201)

organization_membership = self.user_management.update_organization_membership(
organization_membership_id="om_ABCDE",
role_slug="member",
)

assert url[0].endswith("user_management/organization_memberships/om_ABCDE")
assert organization_membership["id"] == "om_ABCDE"
assert organization_membership["role"] == {"slug": "member"}

def test_get_organization_membership(
self, mock_organization_membership, capture_and_mock_request
):
Expand Down
2 changes: 2 additions & 0 deletions tests/utils/fixtures/mock_organization_membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def __init__(self, id):
self.user_id = "user_12345"
self.organization_id = "org_67890"
self.status = "active"
self.role = {"slug": "member"}
self.created_at = datetime.datetime.now()
self.updated_at = datetime.datetime.now()

Expand All @@ -16,6 +17,7 @@ def __init__(self, id):
"user_id",
"organization_id",
"status",
"role",
"created_at",
"updated_at",
]
2 changes: 1 addition & 1 deletion workos/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

__package_url__ = "https://github.com/workos-inc/workos-python"

__version__ = "3.0.0"
__version__ = "3.1.0"

__author__ = "WorkOS"

Expand Down
1 change: 1 addition & 0 deletions workos/resources/user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class WorkOSOrganizationMembership(WorkOSBaseResource):
"status",
"created_at",
"updated_at",
"role",
]


Expand Down
39 changes: 37 additions & 2 deletions workos/user_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,25 @@ def delete_user(self, user_id):
token=workos.api_key,
)

def create_organization_membership(self, user_id, organization_id):
def create_organization_membership(self, user_id, organization_id, role_slug=None):
"""Create a new OrganizationMembership for the given Organization and User.

Args:
user_id: The Unique ID of the User.
organization_id: The Unique ID of the Organization to which the user belongs to.
role_slug: The Unique Slug of the Role to which to grant to this membership.
If no slug is passed in, the default role will be granted.(Optional)

Returns:
dict: Created OrganizationMembership response from WorkOS.
"""
headers = {}

params = {"user_id": user_id, "organization_id": organization_id}
params = {
"user_id": user_id,
"organization_id": organization_id,
"role_slug": role_slug,
}

response = self.request_helper.request(
ORGANIZATION_MEMBERSHIP_PATH,
Expand All @@ -225,6 +231,35 @@ def create_organization_membership(self, user_id, organization_id):

return WorkOSOrganizationMembership.construct_from_response(response).to_dict()

def update_organization_membership(
self, organization_membership_id, role_slug=None
):
"""Updates an OrganizationMembership for the given id.

Args:
organization_membership_id (str) - The unique ID of the Organization Membership.
role_slug: The Unique Slug of the Role to which to grant to this membership.
If no slug is passed in, it will not be changed (Optional)

Returns:
dict: Created OrganizationMembership response from WorkOS.
"""
headers = {}

params = {
"role_slug": role_slug,
}

response = self.request_helper.request(
ORGANIZATION_MEMBERSHIP_DETAIL_PATH.format(organization_membership_id),
method=REQUEST_METHOD_PUT,
params=params,
headers=headers,
token=workos.api_key,
)

return WorkOSOrganizationMembership.construct_from_response(response).to_dict()

def get_organization_membership(self, organization_membership_id):
"""Get the details of an organization membership.

Expand Down
Loading