aminghadersohi commented on code in PR #40746: URL: https://github.com/apache/superset/pull/40746#discussion_r3368408240
########## tests/unit_tests/mcp_service/user/test_schemas.py: ########## @@ -0,0 +1,101 @@ +# 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 UserInfo, serialize_user_object + + +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" + role_detached = MagicMock() + role_detached.name = MagicMock(side_effect=DetachedInstanceError()) Review Comment: MEDIUM — `MagicMock(side_effect=DetachedInstanceError())` fires only when the mock is *called* (`item.name()`), not when it is *accessed* (`item.name`). In the validator, `str(item.name)` evaluates `item.name` first (returns the MagicMock object with no exception), then calls `str()` on it — the `side_effect` never triggers. SQLAlchemy raises `DetachedInstanceError` via its attribute descriptor, so the `except DetachedInstanceError:` branch may not be exercised by this test. Use `PropertyMock` to simulate attribute-access exceptions (also add `PropertyMock` to the import on line 20): ```suggestion type(role_detached).name = PropertyMock(side_effect=DetachedInstanceError) ``` ########## superset/mcp_service/user/schemas.py: ########## @@ -104,6 +104,27 @@ class UserInfo(BaseModel): "access via get_user_info; not available in list_users because roles " "is a relationship, not a selectable column)", ) + + @field_validator("roles", mode="before") + @classmethod + def _extract_role_names(cls, v: Any) -> list[str] | None: + """Coerce Role ORM objects to their .name strings.""" + if v is None: + return None + if isinstance(v, str): + # Preserve Pydantic's default rejection of bare strings for list[str]. + raise ValueError("roles must be a list, not a string") + result: list[str] = [] + for item in v: + if isinstance(item, str): + result.append(item) + elif hasattr(item, "name"): + try: + result.append(str(item.name)) + except DetachedInstanceError: Review Comment: MEDIUM — Silent suppression with no diagnostic output. When a session closes before roles are loaded, the role is dropped from the list with no log line. `roles == []` from a user with three roles and a closed session is indistinguishable from `roles == []` from a user with no roles. A `logger.debug(...)` before `continue` would make session-management issues diagnosable. (Prefer debug over warning here since the validator runs per-request.) ########## superset/mcp_service/user/schemas.py: ########## @@ -104,6 +104,27 @@ class UserInfo(BaseModel): "access via get_user_info; not available in list_users because roles " "is a relationship, not a selectable column)", ) + + @field_validator("roles", mode="before") + @classmethod + def _extract_role_names(cls, v: Any) -> list[str] | None: + """Coerce Role ORM objects to their .name strings.""" + if v is None: + return None + if isinstance(v, str): + # Preserve Pydantic's default rejection of bare strings for list[str]. + raise ValueError("roles must be a list, not a string") + result: list[str] = [] + for item in v: + if isinstance(item, str): + result.append(item) + elif hasattr(item, "name"): Review Comment: NIT — `hasattr(item, "name")` matches any object with a `.name` attribute (Python module, `enum` member, `namedtuple`). Low risk in practice since `roles` is only populated from the ORM relationship, but an `isinstance(name, str)` check inside the `try` block after `name = item.name` would make the intent explicit and avoid silently coercing unexpected types. -- 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]
