codeant-ai-for-open-source[bot] commented on code in PR #41753: URL: https://github.com/apache/superset/pull/41753#discussion_r3537711810
########## tests/unit_tests/mcp_service/test_guest_scope.py: ########## @@ -0,0 +1,120 @@ +# 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 the embedded-guest data-query authorization helpers.""" + +from types import SimpleNamespace + +from pytest_mock import MockerFixture + +from superset.extensions import security_manager +from superset.mcp_service import guest_scope + + +def test_is_guest_read_true_for_guest(mocker: MockerFixture) -> None: + """The skip predicate is True for an embedded guest.""" + mocker.patch.object(security_manager, "is_guest_user", return_value=True) + assert guest_scope.is_guest_read() is True + + +def test_is_guest_read_false_for_non_guest(mocker: MockerFixture) -> None: + """The skip predicate is False for a regular user.""" + mocker.patch.object(security_manager, "is_guest_user", return_value=False) + assert guest_scope.is_guest_read() is False + + +def test_guest_dashboard_id_non_guest_short_circuits(mocker: MockerFixture) -> None: + """Non-guests return None without ever inspecting the chart's dashboards.""" + mocker.patch.object(security_manager, "is_guest_user", return_value=False) + has_access = mocker.patch.object(security_manager, "has_guest_access") + + chart = SimpleNamespace(dashboards=[SimpleNamespace(id=7)]) + + assert guest_scope.guest_dashboard_id(chart) is None + has_access.assert_not_called() + + +def test_guest_dashboard_id_returns_accessible_dashboard( + mocker: MockerFixture, +) -> None: + """A guest gets the id of the first dashboard it may access.""" + mocker.patch.object(security_manager, "is_guest_user", return_value=True) + mocker.patch.object( + security_manager, + "has_guest_access", + side_effect=lambda dashboard: dashboard.id == 7, + ) Review Comment: **Suggestion:** Replace the untyped lambda callback with a named helper function that includes explicit type hints for its parameter and return type. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The new test code introduces an untyped callback via `lambda dashboard: ...` where the parameter could be annotated by replacing it with a named helper function. This matches the rule requiring type hints for new or modified Python code where annotations are applicable. </details> <details> <summary><b>Rule source 📖 </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=109f2d8cdd494eebb446436ddd0f4aba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=109f2d8cdd494eebb446436ddd0f4aba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <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_guest_scope.py **Line:** 56:60 **Comment:** *Custom Rule: Replace the untyped lambda callback with a named helper function that includes explicit type hints for its parameter and return type. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=8e8f96ce6baab1bfbf7450f48896b3139d66e2e49396db960252fca0f8de8ff6&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=8e8f96ce6baab1bfbf7450f48896b3139d66e2e49396db960252fca0f8de8ff6&reaction=dislike'>👎</a> ########## superset/utils/filters.py: ########## @@ -41,3 +41,43 @@ def get_dataset_access_filters( base_model.schema_perm.in_(schema_perms), *args, ) + + +def guest_embedded_dashboard_filter() -> Optional[Any]: + """SQLAlchemy condition matching dashboards embedded via the current guest + token's resources, or None when the user is not an embedded guest. + + Used by the chart access filter to scope an embedded guest to its token's + dashboards (and their charts), mirroring how DashboardAccessFilter already + scopes dashboards. + """ + # pylint: disable=import-outside-toplevel + from superset import is_feature_enabled, security_manager + from superset.models.dashboard import Dashboard, is_uuid + from superset.models.embedded_dashboard import EmbeddedDashboard + from superset.security.guest_token import GuestTokenResourceType + + if not is_feature_enabled("EMBEDDED_SUPERSET"): + return None + guest = security_manager.get_current_guest_user_if_guest() + if guest is None: + return None + ids: list[Any] = [ + r["id"] + for r in guest.resources + if r["type"] == GuestTokenResourceType.DASHBOARD.value + ] + if not ids: + return None + # TODO (embedded): only use the uuid filter once uuids are rolled out + # A guest token may mix uuid and int dashboard ids during the uuid rollout. + # Route each id kind to its own column and OR them — a plain int sent to the + # uuid-typed column would raise a bind/type error. + uuid_ids = [id_ for id_ in ids if is_uuid(id_)] + int_ids = [id_ for id_ in ids if not is_uuid(id_)] + conditions = [] Review Comment: **Suggestion:** Add an explicit type annotation to this list variable so it does not remain untyped in new code. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The new local variable `conditions` is introduced without a type annotation even though its contents are later used as a list of SQLAlchemy conditions. This matches the rule requiring type hints for relevant variables that can be annotated. </details> <details> <summary><b>Rule source 📖 </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b3e94b06f86042c9ae7fe29569445fc6&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b3e94b06f86042c9ae7fe29569445fc6&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/utils/filters.py **Line:** 78:78 **Comment:** *Custom Rule: Add an explicit type annotation to this list variable so it does not remain untyped in new code. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=4a98dea2e3a4b32404bd848e73be59865db72869b1a442257b3f94b2329aa156&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=4a98dea2e3a4b32404bd848e73be59865db72869b1a442257b3f94b2329aa156&reaction=dislike'>👎</a> ########## tests/unit_tests/charts/test_filters.py: ########## @@ -0,0 +1,119 @@ +# 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 the ``ChartFilter`` embedded-guest scoping branch.""" + +from typing import Any +from unittest.mock import MagicMock + +import pytest +from pytest_mock import MockerFixture +from sqlalchemy import create_engine + + +def test_chart_filter_scopes_guest_to_token_dashboards(mocker: MockerFixture) -> None: + """A guest's chart list is scoped to the dashboards in its token.""" + from superset.charts.filters import ChartFilter + from superset.extensions import security_manager + from superset.models.dashboard import Dashboard + from superset.models.slice import Slice + + mocker.patch.object( + security_manager, "can_access_all_datasources", return_value=False + ) + mocker.patch( + "superset.charts.filters.guest_embedded_dashboard_filter", + return_value=Dashboard.id.in_([1, 2]), + ) + + captured: dict[str, Any] = {} + query: MagicMock = MagicMock() + + def _capture_filter(clause: object) -> MagicMock: + captured["clause"] = clause + return query + + query.filter.side_effect = _capture_filter + + filt = ChartFilter.__new__(ChartFilter) + filt.model = Slice + result = filt.apply(query, None) + + assert result is query + query.filter.assert_called_once() + # Guest branch returns early: it must NOT take the datasource-access join path. + query.join.assert_not_called() + + compiled = str( + captured["clause"].compile( + create_engine("sqlite://"), compile_kwargs={"literal_binds": True} + ) + ) Review Comment: **Suggestion:** Add an explicit type annotation for this compiled SQL string variable to comply with the type-hint rule for relevant locals. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The `compiled` local is a newly added derived string value in Python code and can be annotated, but no type hint is provided, so this matches the custom type-hint rule. </details> <details> <summary><b>Rule source 📖 </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1412eeb99070483f89a793737a31e755&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=1412eeb99070483f89a793737a31e755&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/charts/test_filters.py **Line:** 61:65 **Comment:** *Custom Rule: Add an explicit type annotation for this compiled SQL string variable to comply with the type-hint rule for relevant locals. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=eb77b59e123479e96eeb37b0d1d5793dd33fa3ad0c333bebae546fa0d92f6f21&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=eb77b59e123479e96eeb37b0d1d5793dd33fa3ad0c333bebae546fa0d92f6f21&reaction=dislike'>👎</a> ########## tests/unit_tests/charts/test_filters.py: ########## @@ -0,0 +1,119 @@ +# 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 the ``ChartFilter`` embedded-guest scoping branch.""" + +from typing import Any +from unittest.mock import MagicMock + +import pytest +from pytest_mock import MockerFixture +from sqlalchemy import create_engine + + +def test_chart_filter_scopes_guest_to_token_dashboards(mocker: MockerFixture) -> None: + """A guest's chart list is scoped to the dashboards in its token.""" + from superset.charts.filters import ChartFilter + from superset.extensions import security_manager + from superset.models.dashboard import Dashboard + from superset.models.slice import Slice + + mocker.patch.object( + security_manager, "can_access_all_datasources", return_value=False + ) + mocker.patch( + "superset.charts.filters.guest_embedded_dashboard_filter", + return_value=Dashboard.id.in_([1, 2]), + ) + + captured: dict[str, Any] = {} + query: MagicMock = MagicMock() + + def _capture_filter(clause: object) -> MagicMock: + captured["clause"] = clause + return query + + query.filter.side_effect = _capture_filter + + filt = ChartFilter.__new__(ChartFilter) Review Comment: **Suggestion:** Add a type annotation for this locally created filter object so the variable is explicitly typed. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This local variable is introduced in new test code and is a relevant object assignment that can be annotated, so omitting a type hint violates the rule. </details> <details> <summary><b>Rule source 📖 </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=2f5e4a95199a413a94b5a53e54c42ef7&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=2f5e4a95199a413a94b5a53e54c42ef7&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/charts/test_filters.py **Line:** 52:52 **Comment:** *Custom Rule: Add a type annotation for this locally created filter object so the variable is explicitly typed. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=2ac9e8c2b064a8e71a1b600954228ee42e78b3a1ffc6c999db74f8d040770364&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=2ac9e8c2b064a8e71a1b600954228ee42e78b3a1ffc6c999db74f8d040770364&reaction=dislike'>👎</a> ########## tests/unit_tests/charts/test_filters.py: ########## @@ -0,0 +1,119 @@ +# 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 the ``ChartFilter`` embedded-guest scoping branch.""" + +from typing import Any +from unittest.mock import MagicMock + +import pytest +from pytest_mock import MockerFixture +from sqlalchemy import create_engine + + +def test_chart_filter_scopes_guest_to_token_dashboards(mocker: MockerFixture) -> None: + """A guest's chart list is scoped to the dashboards in its token.""" + from superset.charts.filters import ChartFilter + from superset.extensions import security_manager + from superset.models.dashboard import Dashboard + from superset.models.slice import Slice + + mocker.patch.object( + security_manager, "can_access_all_datasources", return_value=False + ) + mocker.patch( + "superset.charts.filters.guest_embedded_dashboard_filter", + return_value=Dashboard.id.in_([1, 2]), + ) + + captured: dict[str, Any] = {} + query: MagicMock = MagicMock() + + def _capture_filter(clause: object) -> MagicMock: + captured["clause"] = clause + return query + + query.filter.side_effect = _capture_filter + + filt = ChartFilter.__new__(ChartFilter) + filt.model = Slice + result = filt.apply(query, None) + + assert result is query + query.filter.assert_called_once() + # Guest branch returns early: it must NOT take the datasource-access join path. + query.join.assert_not_called() + + compiled = str( + captured["clause"].compile( + create_engine("sqlite://"), compile_kwargs={"literal_binds": True} + ) + ) + assert "EXISTS" in compiled # scoped via the m2m dashboards relationship + assert "IN (1, 2)" in compiled + + +def test_chart_filter_non_guest_skips_guest_branch(mocker: MockerFixture) -> None: + """With no embedded guest, ChartFilter does not take the guest branch (it + proceeds to the datasource-access join instead of filtering on dashboards).""" + from superset.charts.filters import ChartFilter + from superset.extensions import security_manager + from superset.models.slice import Slice + + mocker.patch.object( + security_manager, "can_access_all_datasources", return_value=False + ) + guest_filter: MagicMock = mocker.patch( + "superset.charts.filters.guest_embedded_dashboard_filter", return_value=None + ) + # Stop apply right after the guest check so we don't exercise the (heavily + # env-dependent) datasource-access path — reaching the join proves the guest + # branch was skipped. + sentinel = RuntimeError("reached datasource-access join") Review Comment: **Suggestion:** Add an explicit type annotation for this exception instance variable to satisfy the type-hint requirement for relevant local variables. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is new Python code in a test file and the local variable `sentinel` is clearly type-annotatable but lacks an explicit type hint, matching the type-hint rule. </details> <details> <summary><b>Rule source 📖 </b></summary> .cursor/rules/dev-standard.mdc (line 28) </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=109c313ae7a345fbbbf842f5c88934dd&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=109c313ae7a345fbbbf842f5c88934dd&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/charts/test_filters.py **Line:** 86:86 **Comment:** *Custom Rule: Add an explicit type annotation for this exception instance variable to satisfy the type-hint requirement for relevant local variables. 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. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=a26ec1232936766446db4175162b18f15781bf772e89b8f285ec99f449703d6b&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41753&comment_hash=a26ec1232936766446db4175162b18f15781bf772e89b8f285ec99f449703d6b&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]
