mirror of
https://github.com/apache/superset.git
synced 2026-08-12 11:11:01 +00:00
chore: simplify user impersonation (#32485)
This commit is contained in:
@@ -27,7 +27,7 @@ import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
from sqlalchemy import types
|
||||
from sqlalchemy.dialects import sqlite
|
||||
from sqlalchemy.engine.url import URL
|
||||
from sqlalchemy.engine.url import make_url, URL
|
||||
from sqlalchemy.sql import sqltypes
|
||||
|
||||
from superset.sql_parse import Table
|
||||
@@ -382,3 +382,85 @@ def test_unmask_encrypted_extra() -> None:
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_impersonate_user_backwards_compatible(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
Test that the `impersonate_user` method calls the original methods it replaced.
|
||||
"""
|
||||
from superset.db_engine_specs.base import BaseEngineSpec
|
||||
|
||||
database = mocker.MagicMock()
|
||||
url = make_url("sqlite://foo.db")
|
||||
new_url = make_url("sqlite://bar.db")
|
||||
engine_kwargs = {"connect_args": {"user": "alice"}}
|
||||
|
||||
get_url_for_impersonation = mocker.patch.object(
|
||||
BaseEngineSpec,
|
||||
"get_url_for_impersonation",
|
||||
return_value=new_url,
|
||||
)
|
||||
update_impersonation_config = mocker.patch.object(
|
||||
BaseEngineSpec,
|
||||
"update_impersonation_config",
|
||||
)
|
||||
signature = mocker.patch("superset.db_engine_specs.base.signature")
|
||||
signature().parameters = [
|
||||
"cls",
|
||||
"database",
|
||||
"connect_args",
|
||||
"uri",
|
||||
"username",
|
||||
"access_token",
|
||||
]
|
||||
|
||||
BaseEngineSpec.impersonate_user(database, "alice", "SECRET", url, engine_kwargs)
|
||||
|
||||
get_url_for_impersonation.assert_called_once_with(url, True, "alice", "SECRET")
|
||||
update_impersonation_config.assert_called_once_with(
|
||||
database,
|
||||
{"user": "alice"},
|
||||
new_url,
|
||||
"alice",
|
||||
"SECRET",
|
||||
)
|
||||
|
||||
|
||||
def test_impersonate_user_no_database(mocker: MockerFixture) -> None:
|
||||
"""
|
||||
Test `impersonate_user` when `update_impersonation_config` has an old signature.
|
||||
"""
|
||||
from superset.db_engine_specs.base import BaseEngineSpec
|
||||
|
||||
database = mocker.MagicMock()
|
||||
url = make_url("sqlite://foo.db")
|
||||
new_url = make_url("sqlite://bar.db")
|
||||
engine_kwargs = {"connect_args": {"user": "alice"}}
|
||||
|
||||
get_url_for_impersonation = mocker.patch.object(
|
||||
BaseEngineSpec,
|
||||
"get_url_for_impersonation",
|
||||
return_value=new_url,
|
||||
)
|
||||
update_impersonation_config = mocker.patch.object(
|
||||
BaseEngineSpec,
|
||||
"update_impersonation_config",
|
||||
)
|
||||
signature = mocker.patch("superset.db_engine_specs.base.signature")
|
||||
signature().parameters = [
|
||||
"cls",
|
||||
"connect_args",
|
||||
"uri",
|
||||
"username",
|
||||
"access_token",
|
||||
]
|
||||
|
||||
BaseEngineSpec.impersonate_user(database, "alice", "SECRET", url, engine_kwargs)
|
||||
|
||||
get_url_for_impersonation.assert_called_once_with(url, True, "alice", "SECRET")
|
||||
update_impersonation_config.assert_called_once_with(
|
||||
{"user": "alice"},
|
||||
new_url,
|
||||
"alice",
|
||||
"SECRET",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user