|
25 | 25 | """ |
26 | 26 |
|
27 | 27 | from typing import Any |
28 | | -from unittest.mock import MagicMock, patch |
| 28 | +from unittest.mock import MagicMock, patch, PropertyMock |
| 29 | + |
| 30 | +from pytest_mock import MockerFixture |
| 31 | + |
| 32 | +from superset.commands.explore.get import GetExploreCommand |
| 33 | +from superset.commands.explore.parameters import CommandParameters |
| 34 | +from superset.connectors.sqla.models import SqlaTable |
| 35 | +from superset.semantic_layers.models import SemanticView |
29 | 36 |
|
30 | 37 | _FULL_FILTER: dict[str, Any] = { |
31 | 38 | "id": 1, |
@@ -121,3 +128,59 @@ def test_rls_empty_list_on_dao_exception( |
121 | 128 | datasource_data["rls_filters"] = [] |
122 | 129 |
|
123 | 130 | assert datasource_data["rls_filters"] == [] |
| 131 | + |
| 132 | + |
| 133 | +def test_explore_rls_lookup_uses_sql_dataset_identity(mocker: MockerFixture) -> None: |
| 134 | + """A semantic view cannot inherit SQL RLS metadata from a matching numeric id.""" |
| 135 | + view: SemanticView = SemanticView(id=42, name="orders_view") |
| 136 | + dataset: SqlaTable = SqlaTable(id=42, table_name="orders") |
| 137 | + mocker.patch.object( |
| 138 | + SemanticView, |
| 139 | + "data", |
| 140 | + new_callable=PropertyMock, |
| 141 | + return_value={"name": "orders_view"}, |
| 142 | + ) |
| 143 | + mocker.patch.object( |
| 144 | + SqlaTable, "data", new_callable=PropertyMock, return_value={"name": "orders"} |
| 145 | + ) |
| 146 | + mocker.patch("superset.commands.explore.get.get_form_data", return_value=({}, None)) |
| 147 | + mocker.patch( |
| 148 | + "superset.commands.explore.get.get_datasource_info", |
| 149 | + side_effect=lambda id, kind, form: (id, kind), |
| 150 | + ) |
| 151 | + mocker.patch( |
| 152 | + "superset.commands.explore.get.utils.convert_legacy_filters_into_adhoc" |
| 153 | + ) |
| 154 | + mocker.patch("superset.commands.explore.get.utils.merge_extra_filters") |
| 155 | + mocker.patch("superset.commands.explore.get.utils.merge_request_params") |
| 156 | + mocker.patch("superset.commands.explore.get.request", new=MagicMock()) |
| 157 | + mocker.patch( |
| 158 | + "superset.commands.explore.get.sanitize_datasource_data", |
| 159 | + side_effect=lambda data: data, |
| 160 | + ) |
| 161 | + mocker.patch( |
| 162 | + "superset.commands.explore.get.security_manager" |
| 163 | + ).can_access.return_value = True |
| 164 | + datasource_lookup: MagicMock = mocker.patch( |
| 165 | + "superset.commands.explore.get.DatasourceDAO.get_datasource", |
| 166 | + side_effect=lambda kind, id: view if kind == "semantic_view" else dataset, |
| 167 | + ) |
| 168 | + rls_lookup: MagicMock = mocker.patch( |
| 169 | + "superset.commands.explore.get.DatasetDAO.get_rls_filters_for_dataset", |
| 170 | + return_value=[_FULL_FILTER], |
| 171 | + ) |
| 172 | + |
| 173 | + view_result: dict[str, Any] | None = GetExploreCommand( |
| 174 | + CommandParameters(None, None, 42, "semantic_view", None) |
| 175 | + ).run() |
| 176 | + assert view_result is not None |
| 177 | + assert view_result["dataset"].get("rls_filters", []) == [] |
| 178 | + rls_lookup.assert_not_called() |
| 179 | + |
| 180 | + dataset_result: dict[str, Any] | None = GetExploreCommand( |
| 181 | + CommandParameters(None, None, 42, "table", None) |
| 182 | + ).run() |
| 183 | + assert dataset_result is not None |
| 184 | + assert dataset_result["dataset"]["rls_filters"] == [_FULL_FILTER] |
| 185 | + rls_lookup.assert_called_once_with(42) |
| 186 | + assert datasource_lookup.call_count == 2 |
0 commit comments