gkneighb commented on code in PR #41843:
URL: https://github.com/apache/superset/pull/41843#discussion_r3541069039


##########
tests/unit_tests/security/test_permission_instructions_link.py:
##########
@@ -0,0 +1,133 @@
+# 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,
+)
+
+MANAGER = "superset.security.manager"
+
+
+def _render(template, *, username="alice", anonymous=False, **kwargs):
+    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():
+    assert _render("") is None
+    assert _render(None) is None
+
+
+def test_plain_link_without_placeholders_is_unchanged():
+    assert _render("https://wiki.example.com/data-access";) == (
+        "https://wiki.example.com/data-access";
+    )
+
+
+def test_datasource_placeholders_are_filled_and_url_encoded():
+    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():
+    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():
+    out = _render(
+        "https://acme.example.com/req?u={username}";,
+        anonymous=True,
+    )
+    assert out == "https://acme.example.com/req?u=";
+
+
+def test_unreferenced_placeholders_left_untouched():
+    # datasource link doesn't supply table_names; that token stays literal
+    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=";

Review Comment:
   Fixed in 33e2b572ee — renamed to test_unsupplied_placeholders_render_empty 
and aligned the comment with the helper's documented replace-with-empty 
behavior.



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