diff --git a/superset/databases/schemas.py b/superset/databases/schemas.py index 7dbaf0243a42b..5d44c2006c758 100644 --- a/superset/databases/schemas.py +++ b/superset/databases/schemas.py @@ -857,6 +857,7 @@ def fix_allow_csv_upload( allow_cvas = fields.Boolean() allow_dml = fields.Boolean(required=False) allow_csv_upload = fields.Boolean() + impersonate_user = fields.Boolean() extra = fields.Nested(ImportV1DatabaseExtraSchema) uuid = fields.UUID(required=True) version = fields.String(required=True) diff --git a/superset/models/core.py b/superset/models/core.py index 512c5a93e300e..3dbc2b1156f56 100755 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -171,6 +171,7 @@ class Database(Model, AuditMixinNullable, ImportExportMixin): # pylint: disable "allow_dml", "allow_file_upload", "extra", + "impersonate_user", ] extra_import_fields = [ "password", diff --git a/tests/integration_tests/databases/commands_tests.py b/tests/integration_tests/databases/commands_tests.py index bbf9b82592448..692c728a2bc3b 100644 --- a/tests/integration_tests/databases/commands_tests.py +++ b/tests/integration_tests/databases/commands_tests.py @@ -371,6 +371,7 @@ def test_export_database_command_key_order(self, mock_g): "allow_dml", "allow_csv_upload", "extra", + "impersonate_user", "uuid", "version", ] diff --git a/tests/unit_tests/databases/commands/importers/v1/import_test.py b/tests/unit_tests/databases/commands/importers/v1/import_test.py index 86d848ff2192d..5a0af24f57706 100644 --- a/tests/unit_tests/databases/commands/importers/v1/import_test.py +++ b/tests/unit_tests/databases/commands/importers/v1/import_test.py @@ -172,6 +172,30 @@ def test_import_database_with_version(mocker: MockerFixture, session: Session) - assert json.loads(database.extra)["version"] == "1.1.1" +def test_import_database_with_user_impersonation( + mocker: MockerFixture, + session: Session, +) -> None: + """ + Test importing a database that is managed externally. + """ + from superset import security_manager + from superset.commands.database.importers.v1.utils import import_database + from superset.models.core import Database + from tests.integration_tests.fixtures.importexport import database_config + + mocker.patch.object(security_manager, "can_access", return_value=True) + mocker.patch("superset.commands.database.importers.v1.utils.add_permissions") + engine = db.session.get_bind() + Database.metadata.create_all(engine) # pylint: disable=no-member + + config = copy.deepcopy(database_config) + config["impersonate_user"] = True + + database = import_database(config) + assert database.impersonate_user is True + + def test_add_permissions(mocker: MockerFixture) -> None: """ Test adding permissions to a database when it's imported. diff --git a/tests/unit_tests/datasets/commands/export_test.py b/tests/unit_tests/datasets/commands/export_test.py index 0dc01ed3e92ad..0b5bae310a410 100644 --- a/tests/unit_tests/datasets/commands/export_test.py +++ b/tests/unit_tests/datasets/commands/export_test.py @@ -220,6 +220,7 @@ def test_export(session: Session) -> None: engine_params: {{}} metadata_cache_timeout: {{}} schemas_allowed_for_file_upload: [] +impersonate_user: false uuid: {database.uuid} version: 1.0.0 """,