mirror of
https://github.com/apache/superset.git
synced 2026-04-20 00:24:38 +00:00
feat(SIP-95): permissions for catalogs (#28317)
This commit is contained in:
54
tests/unit_tests/utils/filters_test.py
Normal file
54
tests/unit_tests/utils/filters_test.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from pytest_mock import MockerFixture
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from superset.utils.filters import get_dataset_access_filters
|
||||
|
||||
|
||||
def test_get_dataset_access_filters(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
Test the `get_dataset_access_filters` function.
|
||||
"""
|
||||
from superset.connectors.sqla.models import SqlaTable
|
||||
from superset.extensions import security_manager
|
||||
|
||||
mocker.patch.object(
|
||||
security_manager,
|
||||
"get_accessible_databases",
|
||||
return_value=[1, 3],
|
||||
)
|
||||
mocker.patch.object(
|
||||
security_manager,
|
||||
"user_view_menu_names",
|
||||
side_effect=[
|
||||
{"[db].[catalog1].[schema1].[table1](id:1)"},
|
||||
{"[db].[catalog1].[schema2]"},
|
||||
{"[db].[catalog2]"},
|
||||
],
|
||||
)
|
||||
|
||||
clause = get_dataset_access_filters(SqlaTable)
|
||||
engine = create_engine("sqlite://")
|
||||
compiled_query = clause.compile(engine, compile_kwargs={"literal_binds": True})
|
||||
assert str(compiled_query) == (
|
||||
"dbs.id IN (1, 3) "
|
||||
"OR tables.perm IN ('[db].[catalog1].[schema1].[table1](id:1)') "
|
||||
"OR tables.catalog_perm IN ('[db].[catalog2]') OR "
|
||||
"tables.schema_perm IN ('[db].[catalog1].[schema2]')"
|
||||
)
|
||||
@@ -29,6 +29,7 @@ from superset.utils.core import (
|
||||
DateColumn,
|
||||
generic_find_constraint_name,
|
||||
generic_find_fk_constraint_name,
|
||||
get_datasource_full_name,
|
||||
is_test,
|
||||
normalize_dttm_col,
|
||||
parse_boolean_string,
|
||||
@@ -369,3 +370,29 @@ def test_generic_find_fk_constraint_none_exist():
|
||||
)
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_get_datasource_full_name():
|
||||
"""
|
||||
Test the `get_datasource_full_name` function.
|
||||
|
||||
This is used to build permissions, so it doesn't really return the datasource full
|
||||
name. Instead, it returns a fully qualified table name that includes the database
|
||||
name and schema, with each part wrapped in square brackets.
|
||||
"""
|
||||
assert (
|
||||
get_datasource_full_name("db", "table", "catalog", "schema")
|
||||
== "[db].[catalog].[schema].[table]"
|
||||
)
|
||||
|
||||
assert get_datasource_full_name("db", "table", None, None) == "[db].[table]"
|
||||
|
||||
assert (
|
||||
get_datasource_full_name("db", "table", None, "schema")
|
||||
== "[db].[schema].[table]"
|
||||
)
|
||||
|
||||
assert (
|
||||
get_datasource_full_name("db", "table", "catalog", None)
|
||||
== "[db].[catalog].[table]"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user