mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
fix: Validate required fields in sql_json API (#21003)
* fix: Validate required params for sql_json API * Test required params in sql_json API * Refactoring: use marshmallow Schema for validation sql_json API * Update SqlJsonPayloadSchema * Update SqlJsonPayloadSchema * Refactoring * Refactoring * Refactoring
This commit is contained in:
@@ -763,6 +763,52 @@ class TestCore(SupersetTestCase):
|
||||
f"/superset/extra_table_metadata/{example_db.id}/birth_names/{schema}/"
|
||||
)
|
||||
|
||||
def test_required_params_in_sql_json(self):
|
||||
self.login()
|
||||
client_id = "{}".format(random.getrandbits(64))[:10]
|
||||
|
||||
data = {"client_id": client_id}
|
||||
rv = self.client.post(
|
||||
"/superset/sql_json/",
|
||||
json=data,
|
||||
)
|
||||
failed_resp = {
|
||||
"sql": ["Missing data for required field."],
|
||||
"database_id": ["Missing data for required field."],
|
||||
}
|
||||
resp_data = json.loads(rv.data.decode("utf-8"))
|
||||
self.assertDictEqual(resp_data, failed_resp)
|
||||
self.assertEqual(rv.status_code, 400)
|
||||
|
||||
data = {"sql": "SELECT 1", "client_id": client_id}
|
||||
rv = self.client.post(
|
||||
"/superset/sql_json/",
|
||||
json=data,
|
||||
)
|
||||
failed_resp = {"database_id": ["Missing data for required field."]}
|
||||
resp_data = json.loads(rv.data.decode("utf-8"))
|
||||
self.assertDictEqual(resp_data, failed_resp)
|
||||
self.assertEqual(rv.status_code, 400)
|
||||
|
||||
data = {"database_id": 1, "client_id": client_id}
|
||||
rv = self.client.post(
|
||||
"/superset/sql_json/",
|
||||
json=data,
|
||||
)
|
||||
failed_resp = {"sql": ["Missing data for required field."]}
|
||||
resp_data = json.loads(rv.data.decode("utf-8"))
|
||||
self.assertDictEqual(resp_data, failed_resp)
|
||||
self.assertEqual(rv.status_code, 400)
|
||||
|
||||
data = {"sql": "SELECT 1", "database_id": 1, "client_id": client_id}
|
||||
rv = self.client.post(
|
||||
"/superset/sql_json/",
|
||||
json=data,
|
||||
)
|
||||
resp_data = json.loads(rv.data.decode("utf-8"))
|
||||
self.assertEqual(resp_data.get("status"), "success")
|
||||
self.assertEqual(rv.status_code, 200)
|
||||
|
||||
def test_templated_sql_json(self):
|
||||
if superset.utils.database.get_example_database().backend == "presto":
|
||||
# TODO: make it work for presto
|
||||
|
||||
Reference in New Issue
Block a user