codeant-ai-for-open-source[bot] commented on code in PR #41843: URL: https://github.com/apache/superset/pull/41843#discussion_r3565416495
########## tests/unit_tests/security/test_permission_instructions_link.py: ########## @@ -0,0 +1,140 @@ +# 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 templated PERMISSION_INSTRUCTIONS_LINK rendering.""" + +from unittest.mock import MagicMock, patch + +from superset.security.manager import ( + _render_permission_instructions_link, + SupersetSecurityManager, +) +from superset.sql.parse import Table + +MANAGER = "superset.security.manager" + + +def _render( + template: str | None, + *, + username: str = "alice", + anonymous: bool = False, + **kwargs: str, +) -> str | None: + with ( + patch( + f"{MANAGER}.get_conf", + return_value={"PERMISSION_INSTRUCTIONS_LINK": template}, + ), + patch(f"{MANAGER}.g") as g_mock, + ): + g_mock.user.is_anonymous = anonymous + g_mock.user.username = username + return _render_permission_instructions_link(**kwargs) + + +def test_empty_or_unset_link_returns_none() -> None: + assert _render("") is None + assert _render(None) is None + + +def test_plain_link_without_placeholders_is_unchanged() -> None: + assert _render("https://wiki.example.com/data-access") == ( + "https://wiki.example.com/data-access" + ) + + +def test_datasource_placeholders_are_filled_and_url_encoded() -> None: + out = _render( + "https://acme.example.com/req?id={datasource_id}" + "&name={datasource_name}&u={username}", + datasource_id="12", + datasource_name="Quarterly Sales", + ) + # space in the dataset name is URL-encoded; username injected from g.user + assert out == ("https://acme.example.com/req?id=12&name=Quarterly%20Sales&u=alice") + + +def test_table_names_filled_and_encoded() -> None: + out = _render( + "https://acme.example.com/req?tables={table_names}", + table_names="public.sales,public.users", + ) + assert out == ("https://acme.example.com/req?tables=public.sales%2Cpublic.users") + + +def test_anonymous_user_renders_empty_username() -> None: + out = _render( + "https://acme.example.com/req?u={username}", + anonymous=True, + ) + assert out == "https://acme.example.com/req?u=" + + +def test_unsupplied_placeholders_render_empty() -> None: + # datasource link doesn't supply table_names; that token renders as + # an empty value (matching the helper's documented behavior) + out = _render( + "https://acme.example.com/req?id={datasource_id}&t={table_names}", + datasource_id="9", + ) + assert out == "https://acme.example.com/req?id=9&t=" + + +def test_get_datasource_access_link_pulls_from_datasource_data() -> None: + ds = MagicMock() Review Comment: **Suggestion:** Add a type annotation to this local test fixture variable so the new code remains fully typed. [custom_rule] **Severity Level:** Minor ๐งน <details> <summary><b>Why it matters? โญ </b></summary> This test introduces a local variable without a type hint even though it is clearly a mock object and can be annotated. That matches the custom rule requiring type hints on relevant variables. </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=ecd1cba2f9194c2493aed97337065c46&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=ecd1cba2f9194c2493aed97337065c46&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/security/test_permission_instructions_link.py **Line:** 99:99 **Comment:** *Custom Rule: Add a type annotation to this local test fixture variable so the new code remains fully 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%2F41843&comment_hash=b53a1cf9859a66861cd77a718fe80b6c48434ae23d2e78c6573886d9712cfe94&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41843&comment_hash=b53a1cf9859a66861cd77a718fe80b6c48434ae23d2e78c6573886d9712cfe94&reaction=dislike'>๐</a> ########## tests/unit_tests/security/test_permission_instructions_link.py: ########## @@ -0,0 +1,140 @@ +# 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 templated PERMISSION_INSTRUCTIONS_LINK rendering.""" + +from unittest.mock import MagicMock, patch + +from superset.security.manager import ( + _render_permission_instructions_link, + SupersetSecurityManager, +) +from superset.sql.parse import Table + +MANAGER = "superset.security.manager" + + +def _render( + template: str | None, + *, + username: str = "alice", + anonymous: bool = False, + **kwargs: str, +) -> str | None: + with ( + patch( + f"{MANAGER}.get_conf", + return_value={"PERMISSION_INSTRUCTIONS_LINK": template}, + ), + patch(f"{MANAGER}.g") as g_mock, + ): + g_mock.user.is_anonymous = anonymous + g_mock.user.username = username + return _render_permission_instructions_link(**kwargs) + + +def test_empty_or_unset_link_returns_none() -> None: + assert _render("") is None + assert _render(None) is None + + +def test_plain_link_without_placeholders_is_unchanged() -> None: + assert _render("https://wiki.example.com/data-access") == ( + "https://wiki.example.com/data-access" + ) + + +def test_datasource_placeholders_are_filled_and_url_encoded() -> None: + out = _render( + "https://acme.example.com/req?id={datasource_id}" + "&name={datasource_name}&u={username}", + datasource_id="12", + datasource_name="Quarterly Sales", + ) + # space in the dataset name is URL-encoded; username injected from g.user + assert out == ("https://acme.example.com/req?id=12&name=Quarterly%20Sales&u=alice") + + +def test_table_names_filled_and_encoded() -> None: + out = _render( + "https://acme.example.com/req?tables={table_names}", + table_names="public.sales,public.users", + ) + assert out == ("https://acme.example.com/req?tables=public.sales%2Cpublic.users") + + +def test_anonymous_user_renders_empty_username() -> None: + out = _render( + "https://acme.example.com/req?u={username}", + anonymous=True, + ) + assert out == "https://acme.example.com/req?u=" + + +def test_unsupplied_placeholders_render_empty() -> None: + # datasource link doesn't supply table_names; that token renders as + # an empty value (matching the helper's documented behavior) + out = _render( + "https://acme.example.com/req?id={datasource_id}&t={table_names}", + datasource_id="9", + ) + assert out == "https://acme.example.com/req?id=9&t=" + + +def test_get_datasource_access_link_pulls_from_datasource_data() -> None: + ds = MagicMock() + ds.data = {"id": 12, "name": "Quarterly Sales"} + with ( + patch( + f"{MANAGER}.get_conf", + return_value={ + "PERMISSION_INSTRUCTIONS_LINK": ( + "https://acme.example.com/req?id={datasource_id}" + "&name={datasource_name}" + ) + }, + ), + patch(f"{MANAGER}.g") as g_mock, + ): + g_mock.user.is_anonymous = False + g_mock.user.username = "alice" + out = SupersetSecurityManager.get_datasource_access_link(ds) + assert out == "https://acme.example.com/req?id=12&name=Quarterly%20Sales" + + +def test_get_table_access_link_joins_table_names() -> None: + sm = SupersetSecurityManager.__new__(SupersetSecurityManager) Review Comment: **Suggestion:** Add an explicit type annotation for this instantiated security manager variable to comply with the type-hint rule. [custom_rule] **Severity Level:** Minor ๐งน <details> <summary><b>Why it matters? โญ </b></summary> This is a newly added local variable whose type is apparent and can be annotated, but no type hint is present. That is a valid instance of the type-hint rule violation. </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=9df77d02f2cb4bf5b9185b54c109b51b&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=9df77d02f2cb4bf5b9185b54c109b51b&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/security/test_permission_instructions_link.py **Line:** 120:120 **Comment:** *Custom Rule: Add an explicit type annotation for this instantiated security manager variable to comply with the type-hint rule. 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%2F41843&comment_hash=27586512d442cc7caf66a21d26401448b0e06df5bd9552c272a4b62f2b42c41b&reaction=like'>๐</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41843&comment_hash=27586512d442cc7caf66a21d26401448b0e06df5bd9552c272a4b62f2b42c41b&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]
