|
18 | 18 | from typing import Any, Optional
|
19 | 19 |
|
20 | 20 | import pytest
|
| 21 | +from pytest_mock import MockerFixture |
21 | 22 | from sqlalchemy import JSON, types
|
22 | 23 | from sqlalchemy.engine.url import make_url
|
23 | 24 |
|
@@ -124,3 +125,47 @@ def test_get_schema_from_engine_params() -> None:
|
124 | 125 | )
|
125 | 126 | is None
|
126 | 127 | )
|
| 128 | + |
| 129 | + |
| 130 | +def test_impersonation_username(mocker: MockerFixture) -> None: |
| 131 | + """ |
| 132 | + Test impersonation and make sure that `get_url_for_impersonation` leaves the URL |
| 133 | + unchanged and that `get_prequeries` returns the appropriate impersonation query. |
| 134 | + """ |
| 135 | + from superset.db_engine_specs.starrocks import StarRocksEngineSpec |
| 136 | + |
| 137 | + database = mocker.MagicMock() |
| 138 | + database.impersonate_user = True |
| 139 | + database.get_effective_user.return_value = "alice" |
| 140 | + |
| 141 | + assert StarRocksEngineSpec.get_url_for_impersonation( |
| 142 | + url=make_url("starrocks://service_user@localhost:9030/hive.default"), |
| 143 | + impersonate_user=True, |
| 144 | + username="alice", |
| 145 | + access_token=None, |
| 146 | + ) == make_url("starrocks://service_user@localhost:9030/hive.default") |
| 147 | + |
| 148 | + assert StarRocksEngineSpec.get_prequeries(database) == [ |
| 149 | + 'EXECUTE AS "alice" WITH NO REVERT;' |
| 150 | + ] |
| 151 | + |
| 152 | + |
| 153 | +def test_impersonation_disabled(mocker: MockerFixture) -> None: |
| 154 | + """ |
| 155 | + Test that impersonation is not applied when the feature is disabled in |
| 156 | + `get_url_for_impersonation` and `get_prequeries`. |
| 157 | + """ |
| 158 | + from superset.db_engine_specs.starrocks import StarRocksEngineSpec |
| 159 | + |
| 160 | + database = mocker.MagicMock() |
| 161 | + database.impersonate_user = False |
| 162 | + database.get_effective_user.return_value = "alice" |
| 163 | + |
| 164 | + assert StarRocksEngineSpec.get_url_for_impersonation( |
| 165 | + url=make_url("starrocks://service_user@localhost:9030/hive.default"), |
| 166 | + impersonate_user=False, |
| 167 | + username="alice", |
| 168 | + access_token=None, |
| 169 | + ) == make_url("starrocks://service_user@localhost:9030/hive.default") |
| 170 | + |
| 171 | + assert StarRocksEngineSpec.get_prequeries(database) == [] |
0 commit comments