codeant-ai-for-open-source[bot] commented on code in PR #38407: URL: https://github.com/apache/superset/pull/38407#discussion_r2885589825
########## tests/unit_tests/mcp_service/test_auth_rbac.py: ########## @@ -0,0 +1,197 @@ +# 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. + +"""Tests for MCP RBAC permission checking (auth.py).""" + +from unittest.mock import MagicMock, patch + +import pytest +from flask import g + +from superset.mcp_service.auth import ( + check_tool_permission, + CLASS_PERMISSION_ATTR, + MCPPermissionDeniedError, + METHOD_PERMISSION_ATTR, + PERMISSION_PREFIX, +) + + +class _ToolFunc: + """Minimal callable stand-in for a tool function in tests. + + Unlike MagicMock, this does NOT auto-create attributes on access, + so ``getattr(func, ATTR, None)`` properly returns None when the + attribute hasn't been set. + """ + + def __init__(self, name: str = "test_tool"): + self.__name__ = name + + def __call__(self, *args, **kwargs): + pass + + +def _make_tool_func( + class_perm: str | None = None, + method_perm: str | None = None, +) -> _ToolFunc: + """Create a tool function stub with optional permission attributes.""" + func = _ToolFunc() + if class_perm is not None: + setattr(func, CLASS_PERMISSION_ATTR, class_perm) + if method_perm is not None: + setattr(func, METHOD_PERMISSION_ATTR, method_perm) + return func + + +# -- MCPPermissionDeniedError -- + + +def test_permission_denied_error_message_basic(): + err = MCPPermissionDeniedError( + permission_name="can_read", + view_name="Chart", + ) + assert "can_read" in str(err) + assert "Chart" in str(err) + assert err.permission_name == "can_read" + assert err.view_name == "Chart" + + +def test_permission_denied_error_message_with_user_and_tool(): + err = MCPPermissionDeniedError( + permission_name="can_write", + view_name="Dashboard", + user="alice", + tool_name="generate_dashboard", + ) + assert "alice" in str(err) + assert "generate_dashboard" in str(err) + assert "Dashboard" in str(err) + + +# -- check_tool_permission -- + + +def test_check_tool_permission_no_class_permission_allows(app_context): + """Tools without class_permission_name should be allowed by default.""" + g.user = MagicMock(username="admin") + func = _make_tool_func() # no class_permission_name + assert check_tool_permission(func) is True + + +def test_check_tool_permission_no_user_denies(app_context): + """If no g.user, permission check should deny.""" + g.user = None + func = _make_tool_func(class_perm="Chart") + assert check_tool_permission(func) is False + + +def test_check_tool_permission_granted(app_context): + """When security_manager.can_access returns True, permission is granted.""" + g.user = MagicMock(username="admin") + func = _make_tool_func(class_perm="Chart", method_perm="read") + + with patch("superset.extensions.appbuilder") as mock_appbuilder: + mock_appbuilder.sm.can_access.return_value = True + result = check_tool_permission(func) + + assert result is True + mock_appbuilder.sm.can_access.assert_called_once_with("can_read", "Chart") + + +def test_check_tool_permission_denied(app_context): + """When security_manager.can_access returns False, permission is denied.""" + g.user = MagicMock(username="viewer") + func = _make_tool_func(class_perm="Dashboard", method_perm="write") + + with patch("superset.extensions.appbuilder") as mock_appbuilder: + mock_appbuilder.sm.can_access.return_value = False + result = check_tool_permission(func) + + assert result is False + mock_appbuilder.sm.can_access.assert_called_once_with("can_write", "Dashboard") + + +def test_check_tool_permission_default_method_is_read(app_context): + """When no method_permission_name is set, defaults to 'read'.""" + g.user = MagicMock(username="admin") + func = _make_tool_func(class_perm="Dataset") + # No method_perm set - should default to "read" + + with patch("superset.extensions.appbuilder") as mock_appbuilder: + mock_appbuilder.sm.can_access.return_value = True + check_tool_permission(func) + + mock_appbuilder.sm.can_access.assert_called_once_with("can_read", "Dataset") Review Comment: **Suggestion:** The tests mock the wrong object when stubbing RBAC checks: they patch `superset.extensions.appbuilder.sm.can_access`, but `check_tool_permission` actually imports and calls `superset.security_manager.can_access`, so the real security manager is used in tests, which can cause unintended side effects or flaky tests instead of exercising the mocked behavior. [logic error] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ RBAC unit tests hit real security backend. - ⚠️ Tests may be flaky with different security setups. - ⚠️ Tests do not truly mock permission checks. ``` </details> ```suggestion def test_check_tool_permission_granted(app_context): """When security_manager.can_access returns True, permission is granted.""" g.user = MagicMock(username="admin") func = _make_tool_func(class_perm="Chart", method_perm="read") with patch("superset.security_manager") as mock_security_manager: mock_security_manager.can_access.return_value = True result = check_tool_permission(func) assert result is True mock_security_manager.can_access.assert_called_once_with("can_read", "Chart") def test_check_tool_permission_denied(app_context): """When security_manager.can_access returns False, permission is denied.""" g.user = MagicMock(username="viewer") func = _make_tool_func(class_perm="Dashboard", method_perm="write") with patch("superset.security_manager") as mock_security_manager: mock_security_manager.can_access.return_value = False result = check_tool_permission(func) assert result is False mock_security_manager.can_access.assert_called_once_with("can_write", "Dashboard") def test_check_tool_permission_default_method_is_read(app_context): """When no method_permission_name is set, defaults to 'read'.""" g.user = MagicMock(username="admin") func = _make_tool_func(class_perm="Dataset") # No method_perm set - should default to "read" with patch("superset.security_manager") as mock_security_manager: mock_security_manager.can_access.return_value = True check_tool_permission(func) mock_security_manager.can_access.assert_called_once_with("can_read", "Dataset") ``` <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. Run `pytest tests/unit_tests/mcp_service/test_auth_rbac.py::test_check_tool_permission_granted -vv`. 2. Observe in `tests/unit_tests/mcp_service/test_auth_rbac.py:106-115` that the test patches `"superset.extensions.appbuilder"` and sets `mock_appbuilder.sm.can_access.return_value = True` before calling `check_tool_permission(func)`. 3. Inside `check_tool_permission` at `superset/mcp_service/auth.py:74-120`, the function executes `from superset import security_manager` and calls `security_manager.can_access(permission_str, class_permission_name)`; it never references `superset.extensions.appbuilder`. 4. Because only `superset.extensions.appbuilder` is patched, `superset.security_manager` remains the real security manager, so `can_access()` hits the actual RBAC backend instead of the mock, meaning these tests don't control or isolate the permission check and can behave differently depending on global security configuration. ``` </details> <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/mcp_service/test_auth_rbac.py **Line:** 106:141 **Comment:** *Logic Error: The tests mock the wrong object when stubbing RBAC checks: they patch `superset.extensions.appbuilder.sm.can_access`, but `check_tool_permission` actually imports and calls `superset.security_manager.can_access`, so the real security manager is used in tests, which can cause unintended side effects or flaky tests instead of exercising the mocked behavior. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38407&comment_hash=831ca2b61847cf987e08ad3aaedbbb261e951099bc805ce164bdfb0e7bdc155c&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F38407&comment_hash=831ca2b61847cf987e08ad3aaedbbb261e951099bc805ce164bdfb0e7bdc155c&reaction=dislike'>👎</a> -- 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]
