forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jinja): current_user_email macro (apache#27197)
- Loading branch information
1 parent
11760d3
commit 1d571ec
Showing
5 changed files
with
116 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,14 +132,15 @@ def test_db_column_types(self): | |
col = TableColumn(column_name="foo", type=str_type, table=tbl, is_dttm=True) | ||
self.assertTrue(col.is_temporal) | ||
|
||
@patch("superset.jinja_context.g") | ||
def test_extra_cache_keys(self, flask_g): | ||
flask_g.user.username = "abc" | ||
@patch("superset.jinja_context.get_user_id", return_value=1) | ||
@patch("superset.jinja_context.get_username", return_value="abc") | ||
@patch("superset.jinja_context.get_user_email", return_value="[email protected]") | ||
def test_extra_cache_keys(self, mock_user_email, mock_username, mock_user_id): | ||
base_query_obj = { | ||
"granularity": None, | ||
"from_dttm": None, | ||
"to_dttm": None, | ||
"groupby": ["user"], | ||
"groupby": ["id", "username", "email"], | ||
"metrics": [], | ||
"is_timeseries": False, | ||
"filter": [], | ||
|
@@ -148,19 +149,27 @@ def test_extra_cache_keys(self, flask_g): | |
# Table with Jinja callable. | ||
table1 = SqlaTable( | ||
table_name="test_has_extra_cache_keys_table", | ||
sql="SELECT '{{ current_username() }}' as user", | ||
sql=""" | ||
SELECT '{{ current_user_id() }}' as id, | ||
SELECT '{{ current_username() }}' as username, | ||
SELECT '{{ current_user_email() }}' as email, | ||
""", | ||
database=get_example_database(), | ||
) | ||
|
||
query_obj = dict(**base_query_obj, extras={}) | ||
extra_cache_keys = table1.get_extra_cache_keys(query_obj) | ||
self.assertTrue(table1.has_extra_cache_key_calls(query_obj)) | ||
assert extra_cache_keys == ["abc"] | ||
assert extra_cache_keys == [1, "abc", "[email protected]"] | ||
|
||
# Table with Jinja callable disabled. | ||
table2 = SqlaTable( | ||
table_name="test_has_extra_cache_keys_disabled_table", | ||
sql="SELECT '{{ current_username(False) }}' as user", | ||
sql=""" | ||
SELECT '{{ current_user_id(False) }}' as id, | ||
SELECT '{{ current_username(False) }}' as username, | ||
SELECT '{{ current_user_email(False) }}' as email, | ||
""", | ||
database=get_example_database(), | ||
) | ||
query_obj = dict(**base_query_obj, extras={}) | ||
|
@@ -189,9 +198,8 @@ def test_extra_cache_keys(self, flask_g): | |
self.assertTrue(table3.has_extra_cache_key_calls(query_obj)) | ||
assert extra_cache_keys == ["abc"] | ||
|
||
@patch("superset.jinja_context.g") | ||
def test_jinja_metrics_and_calc_columns(self, flask_g): | ||
flask_g.user.username = "abc" | ||
@patch("superset.jinja_context.get_username", return_value="abc") | ||
def test_jinja_metrics_and_calc_columns(self, mock_username): | ||
base_query_obj = { | ||
"granularity": None, | ||
"from_dttm": None, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
# under the License. | ||
import json | ||
from typing import Any | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from sqlalchemy.dialects.postgresql import dialect | ||
|
@@ -265,3 +266,40 @@ def func(input_: Any) -> Any: | |
|
||
with pytest.raises(SupersetTemplateException): | ||
safe_proxy(func, {"foo": lambda: "bar"}) | ||
|
||
|
||
@patch("superset.jinja_context.ExtraCache.cache_key_wrapper") | ||
@patch("superset.utils.core.g") | ||
def test_user_macros(mock_flask_user, mock_cache_key_wrapper): | ||
mock_flask_user.user.id = 1 | ||
mock_flask_user.user.username = "my_username" | ||
mock_flask_user.user.email = "[email protected]" | ||
cache = ExtraCache() | ||
assert cache.current_user_id() == 1 | ||
assert cache.current_username() == "my_username" | ||
assert cache.current_user_email() == "[email protected]" | ||
assert mock_cache_key_wrapper.call_count == 3 | ||
|
||
|
||
@patch("superset.jinja_context.ExtraCache.cache_key_wrapper") | ||
@patch("superset.utils.core.g") | ||
def test_user_macros_without_cache_key_inclusion( | ||
mock_flask_user, mock_cache_key_wrapper | ||
): | ||
mock_flask_user.user.id = 1 | ||
mock_flask_user.user.username = "my_username" | ||
mock_flask_user.user.email = "[email protected]" | ||
cache = ExtraCache() | ||
assert cache.current_user_id(False) == 1 | ||
assert cache.current_username(False) == "my_username" | ||
assert cache.current_user_email(False) == "[email protected]" | ||
assert mock_cache_key_wrapper.call_count == 0 | ||
|
||
|
||
@patch("superset.utils.core.g") | ||
def test_user_macros_without_user_info(mock_flask_user): | ||
mock_flask_user.user = None | ||
cache = ExtraCache() | ||
assert cache.current_user_id() == None | ||
assert cache.current_username() == None | ||
assert cache.current_user_email() == None |