|
49 | 49 | from superset.db_engine_specs.base import TIME_LIMIT_EXCEPTIONS |
50 | 50 | from superset.models.core import Database |
51 | 51 | from superset.sql.parse import LimitMethod, SQLScript |
| 52 | +from tests.unit_tests.conftest import with_feature_flags |
52 | 53 |
|
53 | 54 | # Note: database, database_with_dml, mock_db_session fixtures and |
54 | 55 | # mock_query_execution helper are imported from conftest.py |
@@ -1156,6 +1157,42 @@ def test_execute_async_dml_without_permission_raises( |
1156 | 1157 | database.execute_async("INSERT INTO users (name) VALUES ('test')") |
1157 | 1158 |
|
1158 | 1159 |
|
| 1160 | +@with_feature_flags(ENABLE_TEMPLATE_PROCESSING=True) |
| 1161 | +def test_execute_async_undefined_template_var_raises_superset_template_exception( |
| 1162 | + mocker: MockerFixture, database: Database, app_context: None |
| 1163 | +) -> None: |
| 1164 | + """A Jinja template referencing an undefined variable (not called as a |
| 1165 | + function) must not leak a raw ``jinja2.exceptions.UndefinedError`` out of |
| 1166 | + ``execute_async`` - it should surface as ``SupersetTemplateException``.""" |
| 1167 | + from superset.exceptions import SupersetTemplateException |
| 1168 | + |
| 1169 | + mocker.patch.dict( |
| 1170 | + current_app.config, {"SQL_QUERY_MUTATOR": None, "SQLLAB_TIMEOUT": 30} |
| 1171 | + ) |
| 1172 | + |
| 1173 | + options = QueryOptions(template_params={"foo": "bar"}) |
| 1174 | + |
| 1175 | + with pytest.raises(SupersetTemplateException): |
| 1176 | + database.execute_async("SELECT {{ missing_var[0] }}", options=options) |
| 1177 | + |
| 1178 | + |
| 1179 | +@with_feature_flags(ENABLE_TEMPLATE_PROCESSING=True) |
| 1180 | +def test_execute_sync_undefined_template_var_returns_failed_result( |
| 1181 | + mocker: MockerFixture, database: Database, app_context: None |
| 1182 | +) -> None: |
| 1183 | + """The sync ``execute`` path's broad ``except Exception`` still catches the |
| 1184 | + template rendering failure and returns a FAILED ``QueryResult``, unchanged |
| 1185 | + by the ``_render_sql_template`` fix.""" |
| 1186 | + mocker.patch.dict( |
| 1187 | + current_app.config, {"SQL_QUERY_MUTATOR": None, "SQLLAB_TIMEOUT": 30} |
| 1188 | + ) |
| 1189 | + |
| 1190 | + options = QueryOptions(template_params={"foo": "bar"}) |
| 1191 | + result = database.execute("SELECT {{ missing_var[0] }}", options=options) |
| 1192 | + |
| 1193 | + assert result.status == QueryStatus.FAILED |
| 1194 | + |
| 1195 | + |
1159 | 1196 | def test_async_handle_get_status( |
1160 | 1197 | mocker: MockerFixture, |
1161 | 1198 | database: Database, |
|
0 commit comments