bito-code-review[bot] commented on code in PR #40746:
URL: https://github.com/apache/superset/pull/40746#discussion_r3375377006


##########
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())
+
+    info = UserInfo(roles=[role_good, role_detached])
+
+    assert info.roles == ["Admin"]
+
+
+def test_serialize_user_object_round_trip_with_empty_roles() -> None:
+    """serialize_user_object must produce UserInfo.roles == [] for empty 
roles."""
+    user = MagicMock()
+    user.id = 1
+    user.username = "admin"
+    user.first_name = "Admin"
+    user.last_name = "User"
+    user.active = True
+    user.email = "[email protected]"
+    user.changed_on = None
+    user.roles = []
+
+    info = serialize_user_object(user, include_sensitive=True, 
include_roles=True)
+
+    assert info is not None
+    assert info.roles == []

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Incomplete test assertions</b></div>
   <div id="fix">
   
   Per rule [6262], this test claims to verify 'serialize_user_object round 
trip' but only asserts `info.roles == []`. The function transforms `username`, 
`first_name`, `last_name`, `email`, and `changed_on` via 
`escape_llm_context_delimiters` and `sanitize_for_llm_context` (schemas.py 
lines 314-328), which can alter string values. Missing assertions allow bugs in 
those transformations to go undetected.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ```
    --- a/tests/unit_tests/mcp_service/user/test_schemas.py
    +++ b/tests/unit_tests/mcp_service/user/test_schemas.py
    @@ -74,9 +74,18 @@ def 
test_serialize_user_object_round_trip_with_empty_roles() -> None:
         user.changed_on = None
         user.roles = []
    
         info = serialize_user_object(user, include_sensitive=True, 
include_roles=True)
    
         assert info is not None
         assert info.roles == []
    +    assert info.username == "admin"
    +    assert info.first_name == "Admin"
    +    assert info.last_name == "User"
    +    assert info.active is True
    +    assert info.email == "[email protected]"
    +    assert info.changed_on is None
    
    
     def test_serialize_user_object_round_trip_with_role_objects() -> None:
         """Full from_attributes path through serialize_user_object -> 
UserInfo."""
    @@ -96,6 +105,11 @@ def 
test_serialize_user_object_round_trip_with_role_objects() -> None:
         info = serialize_user_object(user, include_sensitive=True, 
include_roles=True)
    
         assert info is not None
         assert info.roles == ["Admin"]
    +    assert info.username == "admin"
    +    assert info.first_name == "Admin"
    +    assert info.last_name == "User"
    +    assert info.active is True
    +    assert info.email == "[email protected]"
    +    assert info.changed_on is None
   ```
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #64cd4a</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



##########
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())
+
+    info = UserInfo(roles=[role_good, role_detached])
+
+    assert info.roles == ["Admin"]
+
+
+def test_serialize_user_object_round_trip_with_empty_roles() -> None:
+    """serialize_user_object must produce UserInfo.roles == [] for empty 
roles."""
+    user = MagicMock()
+    user.id = 1
+    user.username = "admin"
+    user.first_name = "Admin"
+    user.last_name = "User"
+    user.active = True
+    user.email = "[email protected]"
+    user.changed_on = None
+    user.roles = []
+
+    info = serialize_user_object(user, include_sensitive=True, 
include_roles=True)
+
+    assert info is not None
+    assert info.roles == []
+
+
+def test_serialize_user_object_round_trip_with_role_objects() -> None:
+    """Full from_attributes path through serialize_user_object -> UserInfo."""
+    role_admin = MagicMock()
+    role_admin.name = "Admin"
+
+    user = MagicMock()
+    user.id = 1
+    user.username = "admin"
+    user.first_name = "Admin"
+    user.last_name = "User"
+    user.active = True
+    user.email = "[email protected]"
+    user.changed_on = None
+    user.roles = [role_admin]
+
+    info = serialize_user_object(user, include_sensitive=True, 
include_roles=True)
+
+    assert info is not None
+    assert info.roles == ["Admin"]

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Incomplete test assertions</b></div>
   <div id="fix">
   
   Per rule [6262], the test name 'round_trip_with_role_objects' promises full 
round-trip verification, but only `roles` is asserted. This mirrors the gap in 
test 5 and should be fixed simultaneously for consistency.
   </div>
   
   
   <details>
   <summary>
   <b>Code suggestion</b>
   </summary>
   <blockquote>Check the AI-generated fix before applying</blockquote>
   <div id="code">
   
   
   ```
    --- a/tests/unit_tests/mcp_service/user/test_schemas.py
    +++ b/tests/unit_tests/mcp_service/user/test_schemas.py
    @@ -96,6 +96,11 @@ def 
test_serialize_user_object_round_trip_with_role_objects() -> None:
         info = serialize_user_object(user, include_sensitive=True, 
include_roles=True)
    
         assert info is not None
         assert info.roles == ["Admin"]
    +    assert info.username == "admin"
    +    assert info.first_name == "Admin"
    +    assert info.last_name == "User"
    +    assert info.active is True
    +    assert info.email == "[email protected]"
    +    assert info.changed_on is None
   ```
   
   </div>
   </details>
   
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #64cd4a</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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]

Reply via email to