Vitor-Avila commented on code in PR #33614:
URL: https://github.com/apache/superset/pull/33614#discussion_r2113143013
##########
tests/unit_tests/jinja_context_test.py:
##########
@@ -355,66 +361,104 @@ def func(input_: Any) -> Any:
safe_proxy(func, {"foo": lambda: "bar"})
-def test_user_macros(mocker: MockerFixture):
[email protected](
+ "add_to_cache_keys,mock_cache_key_wrapper_call_count",
+ [
+ (True, 5),
+ (False, 0),
+ ],
+)
+def test_user_macros(
+ mocker: MockerFixture,
+ add_to_cache_keys: bool,
+ mock_cache_key_wrapper_call_count: int,
+):
"""
Test all user macros:
- ``current_user_id``
- ``current_username``
- ``current_user_email``
- ``current_user_roles``
+ - ``current_user_rls_rules``
"""
mock_g = mocker.patch("superset.utils.core.g")
mock_get_user_roles =
mocker.patch("superset.security_manager.get_user_roles")
+ mock_get_user_rls =
mocker.patch("superset.security_manager.get_rls_filters")
mock_cache_key_wrapper = mocker.patch(
"superset.jinja_context.ExtraCache.cache_key_wrapper"
)
mock_g.user.id = 1
mock_g.user.username = "my_username"
mock_g.user.email = "[email protected]"
mock_get_user_roles.return_value = [Role(name="my_role1"),
Role(name="my_role2")]
- cache = ExtraCache()
- assert cache.current_user_id() == 1
- assert cache.current_username() == "my_username"
- assert cache.current_user_email() == "[email protected]"
- assert cache.current_user_roles() == ["my_role1", "my_role2"]
- assert mock_cache_key_wrapper.call_count == 4
+ mock_get_user_rls.return_value = [
+ RowLevelSecurityFilter(group_key="test", clause="1=1"),
+ RowLevelSecurityFilter(group_key="other_test", clause="product_id=1"),
+ ]
+ cache = ExtraCache(table=mocker.MagicMock())
+ assert cache.current_user_id(add_to_cache_keys) == 1
+ assert cache.current_username(add_to_cache_keys) == "my_username"
+ assert cache.current_user_email(add_to_cache_keys) == "[email protected]"
+ assert cache.current_user_roles(add_to_cache_keys) == ["my_role1",
"my_role2"]
+ assert cache.current_user_rls_rules(add_to_cache_keys) == ["1=1",
"product_id=1"]
+ assert mock_cache_key_wrapper.call_count ==
mock_cache_key_wrapper_call_count
+
- mock_get_user_roles.return_value = []
+def test_user_macros_without_user_info(mocker: MockerFixture):
+ """
+ Test all user macros when no user info is available.
+ """
+ mock_g = mocker.patch("superset.utils.core.g")
+ mock_g.user = None
+ cache = ExtraCache(table=mocker.MagicMock())
+ assert cache.current_user_id() is None
+ assert cache.current_username() is None
+ assert cache.current_user_email() is None
assert cache.current_user_roles() is None
+ assert cache.current_user_rls_rules() is None
-def test_user_macros_without_cache_key_inclusion(mocker: MockerFixture):
Review Comment:
This is now cover in the previous test with `parametrize`
--
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]