goingforstudying-ctrl commented on code in PR #40746: URL: https://github.com/apache/superset/pull/40746#discussion_r3448851299
########## tests/unit_tests/mcp_service/user/test_schemas.py: ########## @@ -0,0 +1,177 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +"""Unit tests for user-related MCP schemas.""" + +from unittest.mock import MagicMock + +import pytest +from pydantic import ValidationError +from sqlalchemy.orm.exc import DetachedInstanceError + +from superset.mcp_service.user.schemas import ( + sanitize_for_llm_context, + serialize_user_object, + UserInfo, +) + + +def test_user_info_rejects_bare_string_for_roles() -> None: + """A plain string must not be silently split into individual characters.""" + with pytest.raises(ValidationError): + UserInfo(roles="Admin") + + +def test_user_info_preserves_empty_roles_list() -> None: + """Empty roles should remain [] so callers can distinguish it from None.""" + info = UserInfo(roles=[]) + assert info.roles == [] + + +def test_user_info_coerces_role_objects_to_names() -> None: + """Role-like ORM objects must be converted to their .name strings.""" + role_admin = MagicMock() + role_admin.name = "Admin" + role_alpha = MagicMock() + role_alpha.name = "Alpha" + + info = UserInfo(roles=[role_admin, role_alpha]) + + assert info.roles == ["Admin", "Alpha"] + + +def test_user_info_ignores_role_with_detached_instance() -> None: + """Detached ORM roles must not blow up serialization.""" + role_good = MagicMock() + role_good.name = "Admin" + + class DetachedRole: + """Mock ORM role that raises DetachedInstanceError on name access.""" + + @property + def name(self) -> str: + raise DetachedInstanceError() Review Comment: Fixed in latest commit. The empty roles list is now preserved as [] instead of being collapsed to None, matching the test expectations and the pre-PR behavior. ########## superset/mcp_service/user/schemas.py: ########## @@ -277,9 +305,17 @@ def serialize_user_object( if include_sensitive and include_roles: user_roles = getattr(user, "roles", None) if user_roles is not None: - try: - roles = [r.name for r in user_roles if hasattr(r, "name")] - except (AttributeError, DetachedInstanceError): + roles = [] + for r in user_roles: + try: + if hasattr(r, "name") and isinstance(r.name, str): + roles.append(escape_llm_context_delimiters(r.name)) + except (AttributeError, DetachedInstanceError): + logger.debug( + "Skipping role that raised exception in serialize_user_object" + ) + continue + if not roles: roles = None Review Comment: Fixed in latest commit. The empty roles list is now preserved as [] instead of being collapsed to None, matching the test expectations and the pre-PR behavior. ########## superset/mcp_service/user/schemas.py: ########## @@ -277,9 +305,17 @@ def serialize_user_object( if include_sensitive and include_roles: user_roles = getattr(user, "roles", None) if user_roles is not None: - try: - roles = [r.name for r in user_roles if hasattr(r, "name")] - except (AttributeError, DetachedInstanceError): + roles = [] + for r in user_roles: + try: + if hasattr(r, "name") and isinstance(r.name, str): + roles.append(escape_llm_context_delimiters(r.name)) + except (AttributeError, DetachedInstanceError): + logger.debug( + "Skipping role that raised exception in serialize_user_object" + ) + continue + if not roles: roles = None Review Comment: Fixed in latest commit. The empty roles list is now preserved as [] instead of being collapsed to None, matching the test expectations and the pre-PR behavior. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
