-
Notifications
You must be signed in to change notification settings - Fork 53
feature_get_users_groups #59
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # The Okta software accompanied by this notice is provided pursuant to the following terms: | ||
| # Copyright © 2025-Present, Okta, Inc. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and limitations under the License. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # The Okta software accompanied by this notice is provided pursuant to the following terms: | ||
| # Copyright © 2025-Present, Okta, Inc. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and limitations under the License. | ||
|
|
||
| from loguru import logger | ||
| from mcp.server.fastmcp import Context | ||
|
|
||
| from okta_mcp_server.server import mcp | ||
| from okta_mcp_server.utils.client import get_okta_client | ||
| from okta_mcp_server.utils.scope_guard import require_scopes | ||
| from okta_mcp_server.utils.validation import validate_ids | ||
|
|
||
|
|
||
| @mcp.tool() | ||
| @require_scopes("okta.users.read", error_return_type="list") | ||
| @validate_ids("user_id") | ||
| async def list_user_groups(user_id: str, ctx: Context = None) -> list: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: most tools in this codebase take |
||
| """List all groups that a user is a member of. | ||
|
|
||
| This tool retrieves all groups of which the specified user is a member. | ||
| To list all groups in your org, use list_groups() from the Groups tools instead. | ||
|
|
||
| Parameters: | ||
| user_id (str, required): The ID, login, or login shortname of the user. | ||
|
|
||
| Returns: | ||
| List of group objects the user belongs to. | ||
| """ | ||
| logger.info(f"Listing groups for user: {user_id}") | ||
|
|
||
| manager = ctx.request_context.lifespan_context.okta_auth_manager | ||
|
|
||
| try: | ||
| client = await get_okta_client(manager) | ||
| logger.debug(f"Calling Okta API to list groups for user {user_id}") | ||
|
|
||
| groups, _, err = await client.list_user_groups(user_id) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This call isn't paginated. |
||
|
|
||
| if err: | ||
| logger.error(f"Okta API error while listing groups for user {user_id}: {err}") | ||
| return [f"Error: {err}"] | ||
|
|
||
| if not groups: | ||
| logger.info(f"No groups found for user {user_id}") | ||
| return [] | ||
|
|
||
| logger.info(f"Successfully retrieved {len(groups)} groups for user {user_id}") | ||
| return groups | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns a list of raw Okta SDK |
||
|
|
||
| except Exception as e: | ||
| logger.error(f"Exception while listing groups for user {user_id}: {type(e).__name__}: {e}") | ||
| return [f"Exception: {e}"] | ||
|
Comment on lines
+53
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| # The Okta software accompanied by this notice is provided pursuant to the following terms: | ||
| # Copyright © 2025-Present, Okta, Inc. | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and limitations under the License. | ||
|
|
||
| """Tests for user_resources tools — list_user_groups.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from okta_mcp_server.tools.user_resources.user_resources import list_user_groups | ||
|
|
||
|
|
||
| USER_ID = "00uTEST000000001" | ||
|
|
||
|
|
||
| def _make_ctx(): | ||
| """Build a minimal fake Context (no elicitation needed for read-only tools).""" | ||
| from tests.conftest import FakeLifespanContext, FakeOktaAuthManager | ||
|
|
||
| request_context = MagicMock() | ||
| request_context.lifespan_context = FakeLifespanContext( | ||
| okta_auth_manager=FakeOktaAuthManager() | ||
| ) | ||
| ctx = MagicMock() | ||
| ctx.request_context = request_context | ||
| return ctx | ||
|
|
||
|
|
||
| def _make_group_mock(group_id: str, name: str): | ||
| group = MagicMock() | ||
| group.id = group_id | ||
| group.profile = MagicMock() | ||
| group.profile.name = name | ||
| return group | ||
|
|
||
|
|
||
| class TestListUserGroups: | ||
| """Tests for list_user_groups tool.""" | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") | ||
| async def test_returns_groups_for_user(self, mock_get_client): | ||
| """A valid user_id should return the list of groups the user belongs to.""" | ||
| client = AsyncMock() | ||
| groups = [ | ||
| _make_group_mock("00gTEST0000001", "Engineering"), | ||
| _make_group_mock("00gTEST0000002", "Everyone"), | ||
| ] | ||
| client.list_user_groups.return_value = (groups, MagicMock(), None) | ||
| mock_get_client.return_value = client | ||
|
|
||
| result = await list_user_groups(user_id=USER_ID, ctx=_make_ctx()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test (and its assertions) will keep passing even after |
||
|
|
||
| client.list_user_groups.assert_called_once_with(USER_ID) | ||
| assert len(result) == 2 | ||
| assert result[0].profile.name == "Engineering" | ||
| assert result[1].profile.name == "Everyone" | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") | ||
| async def test_returns_empty_list_when_user_has_no_groups(self, mock_get_client): | ||
| """A user with no group memberships should return an empty list.""" | ||
| client = AsyncMock() | ||
| client.list_user_groups.return_value = ([], MagicMock(), None) | ||
| mock_get_client.return_value = client | ||
|
|
||
| result = await list_user_groups(user_id=USER_ID, ctx=_make_ctx()) | ||
|
|
||
| assert result == [] | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") | ||
| async def test_returns_empty_list_when_api_returns_none(self, mock_get_client): | ||
| """A None groups response (no memberships) should return an empty list.""" | ||
| client = AsyncMock() | ||
| client.list_user_groups.return_value = (None, MagicMock(), None) | ||
| mock_get_client.return_value = client | ||
|
|
||
| result = await list_user_groups(user_id=USER_ID, ctx=_make_ctx()) | ||
|
|
||
| assert result == [] | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") | ||
| async def test_okta_api_error_is_surfaced(self, mock_get_client): | ||
| """An Okta API error should be returned as an error string in the list.""" | ||
| client = AsyncMock() | ||
| client.list_user_groups.return_value = (None, None, "Error: user not found") | ||
| mock_get_client.return_value = client | ||
|
|
||
| result = await list_user_groups(user_id=USER_ID, ctx=_make_ctx()) | ||
|
|
||
| assert len(result) == 1 | ||
| assert "Error" in result[0] | ||
|
|
||
| @pytest.mark.asyncio | ||
| @patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") | ||
| async def test_exception_is_surfaced(self, mock_get_client): | ||
| """An unexpected exception should be returned as an Exception string.""" | ||
| mock_get_client.side_effect = Exception("Connection refused") | ||
|
|
||
| result = await list_user_groups(user_id=USER_ID, ctx=_make_ctx()) | ||
|
|
||
| assert len(result) == 1 | ||
| assert "Exception" in result[0] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_invalid_user_id_rejected_before_api_call(self): | ||
| """A user_id with path traversal characters should be rejected without hitting the API.""" | ||
| result = await list_user_groups(user_id="../admin", ctx=_make_ctx()) | ||
|
|
||
| assert len(result) == 1 | ||
| assert "Error" in result[0] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_login_shortname_accepted(self): | ||
| """A login shortname (valid Okta ID format) should pass validation.""" | ||
| with patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") as mock_get_client: | ||
| client = AsyncMock() | ||
| client.list_user_groups.return_value = ([], MagicMock(), None) | ||
| mock_get_client.return_value = client | ||
|
|
||
| result = await list_user_groups(user_id="jdoe", ctx=_make_ctx()) | ||
|
|
||
| client.list_user_groups.assert_called_once_with("jdoe") | ||
| assert result == [] | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_email_login_accepted(self): | ||
| """An email address used as login should pass validation.""" | ||
| with patch("okta_mcp_server.tools.user_resources.user_resources.get_okta_client") as mock_get_client: | ||
| client = AsyncMock() | ||
| client.list_user_groups.return_value = ([], MagicMock(), None) | ||
| mock_get_client.return_value = client | ||
|
|
||
| result = await list_user_groups(user_id="jdoe@example.com", ctx=_make_ctx()) | ||
|
|
||
| client.list_user_groups.assert_called_once_with("jdoe@example.com") | ||
| assert result == [] | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
@json_responsehere as the innermost decorator (should sit between this andasync def). Without it, this tool returns raw Okta SDK objects instead of JSON — see the top-level comment for details.