Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion superset/charts/data/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,13 @@ def _process_data(query_data: Any) -> Any:
)

if result_format == ChartDataResultFormat.JSON:
queries = result["queries"]
if security_manager.is_guest_user():
for query in queries:
with contextlib.suppress(KeyError):
del query["query"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, I think you could do:

if security_manager.is_guest_user():
    for query in queries:
        query.pop("query", None)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great minds think alike! 😉

response_data = json.dumps(
{"result": result["queries"]},
{"result": queries},
default=json.json_int_dttm_ser,
ignore_nan=True,
)
Expand Down
56 changes: 56 additions & 0 deletions tests/integration_tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import prison
import pytest
import yaml
from flask import g
from flask_babel import lazy_gettext as _
from parameterized import parameterized
from sqlalchemy import and_
Expand Down Expand Up @@ -62,6 +63,7 @@
dataset_config,
dataset_metadata_config,
)
from tests.integration_tests.fixtures.query_context import get_query_context
from tests.integration_tests.fixtures.tags import (
create_custom_tags, # noqa: F401
get_filter_params,
Expand Down Expand Up @@ -2327,3 +2329,57 @@ def test_update_chart_no_tag_changes(self):

security_manager.add_permission_role(alpha_role, write_tags_perm)
security_manager.add_permission_role(alpha_role, tag_charts_perm)

@patch("superset.security.manager.SupersetSecurityManager.has_guest_access")
@patch("superset.security.manager.SupersetSecurityManager.is_guest_user")
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_get_chart_data_as_guest_user(
self, is_guest_user, has_guest_access
): # get_guest_rls_filters
"""
Chart API: Test create simple chart
"""
self.login(ADMIN_USERNAME)
g.user.rls = []
is_guest_user.return_value = True
has_guest_access.return_value = True

with mock.patch.object(Slice, "get_query_context") as mock_get_query_context:
mock_get_query_context.return_value = get_query_context("birth_names")
rv = self.client.post(
"api/v1/chart/data", # noqa: F541
json={
"datasource": {"id": 2, "type": "table"},
"queries": [
{
"extras": {"where": "", "time_grain_sqla": "P1D"},
"columns": ["name"],
"metrics": [{"label": "sum__num"}],
"orderby": [("sum__num", False)],
"row_limit": 100,
"granularity": "ds",
"time_range": "100 years ago : now",
"timeseries_limit": 0,
"timeseries_limit_metric": None,
"order_desc": True,
"filters": [
{"col": "gender", "op": "==", "val": "boy"},
{"col": "num", "op": "IS NOT NULL"},
{
"col": "name",
"op": "NOT IN",
"val": ["<NULL>", '"abc"'],
},
],
"having": "",
"where": "",
}
],
"result_format": "json",
"result_type": "full",
},
)
data = json.loads(rv.data.decode("utf-8"))
result = data["result"]
excluded_key = "query"
assert all([excluded_key not in query for query in result])
Loading