rusackas commented on code in PR #42122:
URL: https://github.com/apache/superset/pull/42122#discussion_r3606002170
##########
tests/unit_tests/jinja_context_test.py:
##########
@@ -684,6 +684,117 @@ def test_user_macros_without_user_info(mocker:
MockerFixture):
assert cache.current_user_rls_rules() is None
+def _user_metadata_cache_keys(
+ mocker: MockerFixture,
+ *,
+ user_id: int | None,
+ username: str | None,
+ email: str | None,
+ roles: list[str],
+) -> list[Any]:
+ """
+ Render the user-metadata macros for a given user and return the values they
+ contributed to the query cache key.
+ """
+ mock_g = mocker.patch("superset.utils.core.g")
+ if user_id is None:
+ mock_g.user = None
+ else:
+ mock_g.user.id = user_id
+ mock_g.user.username = username
+ mock_g.user.email = email
+ mocker.patch(
+ "superset.security_manager.get_user_roles",
+ return_value=[Role(name=name) for name in roles],
+ )
+ keys: list[Any] = []
+ cache = ExtraCache(extra_cache_keys=keys, table=mocker.MagicMock())
+ cache.current_user_id()
+ cache.current_username()
+ cache.current_user_email()
+ cache.current_user_roles()
+ return keys
+
+
+def test_user_metadata_cache_keys_isolate_distinct_users(
+ mocker: MockerFixture,
+) -> None:
+ """
+ Two different users contribute disjoint values to the cache key, so neither
+ can be served the other's cached result. This is the property that keeps
the
+ ``current_user_*`` macro family safe for per-user (and multi-tenant)
queries.
+ """
+ alice = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Admin"]
+ )
+ bob = _user_metadata_cache_keys(
+ mocker, user_id=2, username="bob", email="[email protected]",
roles=["Gamma"]
+ )
+ assert alice
+ assert bob
+ assert set(alice).isdisjoint(set(bob))
+
+
+def test_user_metadata_cache_keys_match_for_identical_users(
+ mocker: MockerFixture,
+) -> None:
+ """
+ The same user always contributes the same values, so identical renders
+ correctly share a cache entry (no needless fragmentation).
+ """
+ first = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Admin"]
+ )
+ second = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Admin"]
+ )
+ assert first == second
+
+
+def test_anonymous_user_never_collides_with_a_logged_in_user(
+ mocker: MockerFixture,
+) -> None:
+ """
+ Refutes the "skip cache key when the value is absent" collision concern. A
+ real anonymous request has no user object, so ``current_user_id`` /
+ ``current_username`` / ``current_user_email`` return ``None`` and add
nothing
+ to the key, while ``current_user_roles`` still contributes the Public role.
+ Two anonymous requests therefore render identically and correctly share one
+ cache entry, and neither can be served a logged-in user's cached result.
+ """
+ logged_in = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Admin"]
+ )
+ anon_first = _user_metadata_cache_keys(
+ mocker, user_id=None, username=None, email=None, roles=["Public"]
+ )
+ anon_second = _user_metadata_cache_keys(
+ mocker, user_id=None, username=None, email=None, roles=["Public"]
+ )
+ # The absent id/username/email contribute nothing, so an anonymous
request's
+ # key carries only its role, never a stray value for the missing fields.
+ assert anon_first == [json.dumps(["Public"])]
+ # Two anonymous requests share a cache entry; neither collides with a user.
+ assert anon_first == anon_second
+ assert set(anon_first).isdisjoint(set(logged_in))
+
+
+def test_user_metadata_cache_keys_track_each_field_independently(
+ mocker: MockerFixture,
+) -> None:
+ """
+ Two users who differ in a single field (here only the roles) still get
+ distinct cache keys, so a change in any one metadata field is reflected.
+ """
+ admin = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Admin"]
+ )
+ gamma = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Gamma"]
+ )
+ assert admin != gamma
Review Comment:
Good call. Parametrized it across `user_id`, `username`, `email`, and
`roles`, so each field is guarded on its own now.
##########
tests/unit_tests/jinja_context_test.py:
##########
@@ -684,6 +684,117 @@ def test_user_macros_without_user_info(mocker:
MockerFixture):
assert cache.current_user_rls_rules() is None
+def _user_metadata_cache_keys(
+ mocker: MockerFixture,
+ *,
+ user_id: int | None,
+ username: str | None,
+ email: str | None,
+ roles: list[str],
+) -> list[Any]:
+ """
+ Render the user-metadata macros for a given user and return the values they
+ contributed to the query cache key.
+ """
+ mock_g = mocker.patch("superset.utils.core.g")
+ if user_id is None:
+ mock_g.user = None
+ else:
+ mock_g.user.id = user_id
+ mock_g.user.username = username
+ mock_g.user.email = email
+ mocker.patch(
+ "superset.security_manager.get_user_roles",
+ return_value=[Role(name=name) for name in roles],
+ )
+ keys: list[Any] = []
+ cache = ExtraCache(extra_cache_keys=keys, table=mocker.MagicMock())
+ cache.current_user_id()
+ cache.current_username()
+ cache.current_user_email()
+ cache.current_user_roles()
+ return keys
+
+
+def test_user_metadata_cache_keys_isolate_distinct_users(
+ mocker: MockerFixture,
+) -> None:
+ """
+ Two different users contribute disjoint values to the cache key, so neither
+ can be served the other's cached result. This is the property that keeps
the
+ ``current_user_*`` macro family safe for per-user (and multi-tenant)
queries.
+ """
+ alice = _user_metadata_cache_keys(
+ mocker, user_id=1, username="alice", email="[email protected]",
roles=["Admin"]
+ )
+ bob = _user_metadata_cache_keys(
+ mocker, user_id=2, username="bob", email="[email protected]",
roles=["Gamma"]
+ )
+ assert alice
+ assert bob
+ assert set(alice).isdisjoint(set(bob))
Review Comment:
Right, the real key is `list(set(extra_cache_keys))`. Switched the
assertions to compare on the normalized set so a difference that collapses
under de-dup can't sneak through.
--
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]