bito-code-review[bot] commented on code in PR #38647: URL: https://github.com/apache/superset/pull/38647#discussion_r2969443446
########## tests/unit_tests/views/datasource/views_test.py: ########## @@ -0,0 +1,310 @@ +# 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 resource-level authorization in superset/views/datasource/views.py. + +Tests use ``inspect.unwrap`` to call the underlying view logic directly, +bypassing the Flask-AppBuilder permission decorator machinery. +""" + +import inspect +from unittest.mock import MagicMock, patch + +import pytest + +from superset.errors import ErrorLevel, SupersetError, SupersetErrorType +from superset.exceptions import SupersetSecurityException +from superset.utils import json as superset_json + + +def _security_exception() -> SupersetSecurityException: + return SupersetSecurityException( + SupersetError( + message="Access denied", + error_type=SupersetErrorType.DATASOURCE_SECURITY_ACCESS_ERROR, + level=ErrorLevel.WARNING, + ) + ) + + +def _get_view_func(name: str): + """Return the unwrapped body of a Datasource view method.""" + from superset.views.datasource.views import Datasource + + return inspect.unwrap(getattr(Datasource, name)) + + +def _view_self() -> MagicMock: + """Create a minimal stand-in for a Datasource view instance.""" + self = MagicMock() + self.json_response = MagicMock(return_value="ok") + return self + + +# --------------------------------------------------------------------------- +# Datasource.get +# --------------------------------------------------------------------------- + + +@patch("superset.views.datasource.views.security_manager", new_callable=MagicMock) +@patch("superset.views.datasource.views.DatasourceDAO.get_datasource") +def test_get_raises_when_access_denied( + mock_get_datasource: MagicMock, + mock_security_manager: MagicMock, +) -> None: + """raise_for_access is called and propagates for unauthorised callers.""" + mock_datasource = MagicMock() + mock_get_datasource.return_value = mock_datasource + mock_security_manager.raise_for_access.side_effect = _security_exception() + + raw_get = _get_view_func("get") + with pytest.raises(SupersetSecurityException): + raw_get(_view_self(), "table", 1) + + mock_security_manager.raise_for_access.assert_called_once_with( + datasource=mock_datasource + ) + + +@patch("superset.views.datasource.views.sanitize_datasource_data") +@patch("superset.views.datasource.views.security_manager", new_callable=MagicMock) +@patch("superset.views.datasource.views.DatasourceDAO.get_datasource") +def test_get_succeeds_for_authorised_user( + mock_get_datasource: MagicMock, + mock_security_manager: MagicMock, + mock_sanitize: MagicMock, +) -> None: + """raise_for_access is called without raising for authorised callers.""" + mock_datasource = MagicMock() + mock_datasource.data = {"id": 1} + mock_get_datasource.return_value = mock_datasource + mock_security_manager.raise_for_access.return_value = None + mock_sanitize.return_value = {"id": 1} + + raw_get = _get_view_func("get") + raw_get(_view_self(), "table", 1) + + mock_security_manager.raise_for_access.assert_called_once_with( + datasource=mock_datasource + ) + + +# --------------------------------------------------------------------------- +# Datasource.external_metadata +# --------------------------------------------------------------------------- + + +@patch("superset.views.datasource.views.security_manager", new_callable=MagicMock) +@patch("superset.views.datasource.views.DatasourceDAO.get_datasource") +def test_external_metadata_raises_when_access_denied( + mock_get_datasource: MagicMock, + mock_security_manager: MagicMock, +) -> None: + mock_datasource = MagicMock() + mock_get_datasource.return_value = mock_datasource + mock_security_manager.raise_for_access.side_effect = _security_exception() + + raw_fn = _get_view_func("external_metadata") + with pytest.raises(SupersetSecurityException): + raw_fn(_view_self(), "table", 1) + + mock_security_manager.raise_for_access.assert_called_once_with( + datasource=mock_datasource + ) + + +@patch("superset.views.datasource.views.security_manager", new_callable=MagicMock) +@patch("superset.views.datasource.views.DatasourceDAO.get_datasource") +def test_external_metadata_succeeds_for_authorised_user( + mock_get_datasource: MagicMock, + mock_security_manager: MagicMock, +) -> None: + mock_datasource = MagicMock() + mock_datasource.external_metadata.return_value = [{"name": "col1"}] + mock_get_datasource.return_value = mock_datasource + mock_security_manager.raise_for_access.return_value = None + + raw_fn = _get_view_func("external_metadata") + raw_fn(_view_self(), "table", 1) + + mock_security_manager.raise_for_access.assert_called_once_with( + datasource=mock_datasource + ) Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Missing behavior assertion</b></div> <div id="fix"> Per BITO rule [6262], assert the method's output to verify external_metadata logic, not just security. </div> </div> <small><i>Code Review Run #cbe1f3</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/views/datasource/views_test.py: ########## @@ -0,0 +1,310 @@ +# 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 resource-level authorization in superset/views/datasource/views.py. + +Tests use ``inspect.unwrap`` to call the underlying view logic directly, +bypassing the Flask-AppBuilder permission decorator machinery. +""" + +import inspect +from unittest.mock import MagicMock, patch + +import pytest + +from superset.errors import ErrorLevel, SupersetError, SupersetErrorType +from superset.exceptions import SupersetSecurityException +from superset.utils import json as superset_json + + +def _security_exception() -> SupersetSecurityException: + return SupersetSecurityException( + SupersetError( + message="Access denied", + error_type=SupersetErrorType.DATASOURCE_SECURITY_ACCESS_ERROR, + level=ErrorLevel.WARNING, + ) + ) + + +def _get_view_func(name: str): + """Return the unwrapped body of a Datasource view method.""" + from superset.views.datasource.views import Datasource + + return inspect.unwrap(getattr(Datasource, name)) + + +def _view_self() -> MagicMock: + """Create a minimal stand-in for a Datasource view instance.""" + self = MagicMock() + self.json_response = MagicMock(return_value="ok") + return self + + +# --------------------------------------------------------------------------- +# Datasource.get +# --------------------------------------------------------------------------- + + +@patch("superset.views.datasource.views.security_manager", new_callable=MagicMock) +@patch("superset.views.datasource.views.DatasourceDAO.get_datasource") +def test_get_raises_when_access_denied( + mock_get_datasource: MagicMock, + mock_security_manager: MagicMock, +) -> None: + """raise_for_access is called and propagates for unauthorised callers.""" + mock_datasource = MagicMock() + mock_get_datasource.return_value = mock_datasource + mock_security_manager.raise_for_access.side_effect = _security_exception() + + raw_get = _get_view_func("get") + with pytest.raises(SupersetSecurityException): + raw_get(_view_self(), "table", 1) + + mock_security_manager.raise_for_access.assert_called_once_with( + datasource=mock_datasource + ) + + +@patch("superset.views.datasource.views.sanitize_datasource_data") +@patch("superset.views.datasource.views.security_manager", new_callable=MagicMock) +@patch("superset.views.datasource.views.DatasourceDAO.get_datasource") +def test_get_succeeds_for_authorised_user( + mock_get_datasource: MagicMock, + mock_security_manager: MagicMock, + mock_sanitize: MagicMock, +) -> None: + """raise_for_access is called without raising for authorised callers.""" + mock_datasource = MagicMock() + mock_datasource.data = {"id": 1} + mock_get_datasource.return_value = mock_datasource + mock_security_manager.raise_for_access.return_value = None + mock_sanitize.return_value = {"id": 1} + + raw_get = _get_view_func("get") + raw_get(_view_self(), "table", 1) + + mock_security_manager.raise_for_access.assert_called_once_with( + datasource=mock_datasource + ) + Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Missing behavior assertion</b></div> <div id="fix"> Per BITO rule [6262], tests should assert actual behavior logic, not just security calls. Here, verify the method returns sanitized data via json_response. </div> </div> <small><i>Code Review Run #cbe1f3</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]
