mirror of
https://github.com/apache/superset.git
synced 2026-04-10 11:55:24 +00:00
feat(sqllab): Add /sqllab endpoint to the v1 api (#24983)
This commit is contained in:
@@ -28,6 +28,7 @@ import prison
|
||||
from sqlalchemy.sql import func
|
||||
from unittest import mock
|
||||
|
||||
from flask_appbuilder.security.sqla.models import Role
|
||||
from tests.integration_tests.test_app import app
|
||||
from superset import db, sql_lab
|
||||
from superset.common.db_query_status import QueryStatus
|
||||
@@ -37,11 +38,95 @@ from superset.utils import core as utils
|
||||
from superset.models.sql_lab import Query
|
||||
|
||||
from tests.integration_tests.base_tests import SupersetTestCase
|
||||
from tests.integration_tests.fixtures.users import create_gamma_sqllab_no_data
|
||||
|
||||
QUERIES_FIXTURE_COUNT = 10
|
||||
|
||||
|
||||
class TestSqlLabApi(SupersetTestCase):
|
||||
@pytest.mark.usefixtures("create_gamma_sqllab_no_data")
|
||||
@mock.patch.dict(
|
||||
"superset.extensions.feature_flag_manager._feature_flags",
|
||||
{"SQLLAB_BACKEND_PERSISTENCE": False},
|
||||
clear=True,
|
||||
)
|
||||
def test_get_from_empty_bootsrap_data(self):
|
||||
self.login(username="gamma_sqllab_no_data")
|
||||
resp = self.client.get("/api/v1/sqllab/")
|
||||
assert resp.status_code == 200
|
||||
data = json.loads(resp.data.decode("utf-8"))
|
||||
result = data.get("result")
|
||||
assert result["active_tab"] == None
|
||||
assert result["queries"] == {}
|
||||
assert result["tab_state_ids"] == []
|
||||
self.assertEqual(len(result["databases"]), 0)
|
||||
|
||||
@mock.patch.dict(
|
||||
"superset.extensions.feature_flag_manager._feature_flags",
|
||||
{"SQLLAB_BACKEND_PERSISTENCE": True},
|
||||
clear=True,
|
||||
)
|
||||
def test_get_from_bootstrap_data_with_queries(self):
|
||||
username = "admin"
|
||||
self.login(username)
|
||||
|
||||
# create a tab
|
||||
data = {
|
||||
"queryEditor": json.dumps(
|
||||
{
|
||||
"title": "Untitled Query 1",
|
||||
"dbId": 1,
|
||||
"schema": None,
|
||||
"autorun": False,
|
||||
"sql": "SELECT ...",
|
||||
"queryLimit": 1000,
|
||||
}
|
||||
)
|
||||
}
|
||||
resp = self.get_json_resp("/tabstateview/", data=data)
|
||||
tab_state_id = resp["id"]
|
||||
|
||||
# run a query in the created tab
|
||||
self.run_sql(
|
||||
"SELECT name FROM birth_names",
|
||||
"client_id_1",
|
||||
username=username,
|
||||
raise_on_error=True,
|
||||
sql_editor_id=str(tab_state_id),
|
||||
)
|
||||
# run an orphan query (no tab)
|
||||
self.run_sql(
|
||||
"SELECT name FROM birth_names",
|
||||
"client_id_2",
|
||||
username=username,
|
||||
raise_on_error=True,
|
||||
)
|
||||
|
||||
# we should have only 1 query returned, since the second one is not
|
||||
# associated with any tabs
|
||||
resp = self.get_json_resp("/api/v1/sqllab/")
|
||||
result = resp["result"]
|
||||
self.assertEqual(len(result["queries"]), 1)
|
||||
|
||||
def test_get_access_denied(self):
|
||||
new_role = Role(name="Dummy Role", permissions=[])
|
||||
db.session.add(new_role)
|
||||
db.session.commit()
|
||||
unauth_user = self.create_user(
|
||||
"unauth_user1",
|
||||
"password",
|
||||
"Dummy Role",
|
||||
email=f"unauth_user1@superset.org",
|
||||
)
|
||||
self.login(username="unauth_user1", password="password")
|
||||
rv = self.client.get("/api/v1/sqllab/")
|
||||
|
||||
assert rv.status_code == 403
|
||||
|
||||
db.session.delete(unauth_user)
|
||||
db.session.delete(new_role)
|
||||
db.session.commit()
|
||||
|
||||
def test_estimate_required_params(self):
|
||||
self.login()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user