refactor(sqllab): migrate share queries via kv by permalink (#29163)

This commit is contained in:
JUST.in DO IT
2025-01-17 16:16:02 -08:00
committed by GitHub
parent b86572b084
commit 2874096e27
27 changed files with 682 additions and 229 deletions

View File

@@ -53,7 +53,6 @@ from superset.utils import core as utils, json
from superset.utils.core import backend
from superset.utils.database import get_example_database
from superset.views.database.views import DatabaseView
from tests.integration_tests.conftest import with_feature_flags
from tests.integration_tests.constants import ADMIN_USERNAME, GAMMA_USERNAME
from tests.integration_tests.fixtures.birth_names_dashboard import (
load_birth_names_dashboard_with_slices, # noqa: F401
@@ -371,35 +370,6 @@ class TestCore(SupersetTestCase):
}
]
@with_feature_flags(KV_STORE=False)
def test_kv_disabled(self):
self.login(ADMIN_USERNAME)
resp = self.client.get("/kv/10001/")
assert 404 == resp.status_code
value = json.dumps({"data": "this is a test"})
resp = self.client.post("/kv/store/", data=dict(data=value)) # noqa: C408
assert resp.status_code == 404
@with_feature_flags(KV_STORE=True)
def test_kv_enabled(self):
self.login(ADMIN_USERNAME)
resp = self.client.get("/kv/10001/")
assert 404 == resp.status_code
value = json.dumps({"data": "this is a test"})
resp = self.client.post("/kv/store/", data=dict(data=value)) # noqa: C408
assert resp.status_code == 200
kv = db.session.query(models.KeyValue).first()
kv_value = kv.value
assert json.loads(value) == json.loads(kv_value)
resp = self.client.get(f"/kv/{kv.id}/")
assert resp.status_code == 200
assert json.loads(value) == json.loads(resp.data.decode("utf-8"))
def test_gamma(self):
self.login(GAMMA_USERNAME)
assert "Charts" in self.get_resp("/chart/list/")

View File

@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

View File

@@ -0,0 +1,102 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
from collections.abc import Iterator
from typing import Any
from uuid import uuid3
import pytest
from superset import db
from superset.key_value.models import KeyValueEntry
from superset.key_value.types import KeyValueResource
from superset.key_value.utils import decode_permalink_id
from tests.integration_tests.constants import (
GAMMA_SQLLAB_USERNAME,
)
@pytest.fixture
def tab_state_data() -> dict[str, Any]:
return {
"dbId": 1,
"name": "Untitled Query 1",
"schema": "main",
"sql": "SELECT 'foo' as bar",
"autorun": False,
"templateParams": '{"param1": "value1"}',
}
@pytest.fixture
def permalink_salt(app_context) -> Iterator[str]:
from superset.key_value.shared_entries import get_permalink_salt, get_uuid_namespace
from superset.key_value.types import SharedKey
key = SharedKey.SQLLAB_PERMALINK_SALT
salt = get_permalink_salt(key)
yield salt
namespace = get_uuid_namespace(salt)
db.session.query(KeyValueEntry).filter_by(
resource=KeyValueResource.APP,
uuid=uuid3(namespace, key),
)
db.session.commit()
def test_post(
tab_state_data: dict[str, Any], permalink_salt: str, test_client, login_as
):
login_as(GAMMA_SQLLAB_USERNAME)
resp = test_client.post("api/v1/sqllab/permalink", json=tab_state_data)
assert resp.status_code == 201
data = resp.json
key = data["key"]
url = data["url"]
assert key in url
id_ = decode_permalink_id(key, permalink_salt)
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
db.session.commit()
def test_post_access_denied(tab_state_data: dict[str, Any], test_client, login_as):
resp = test_client.post("api/v1/sqllab/permalink", json=tab_state_data)
assert resp.status_code == 401
def test_post_invalid_schema(test_client, login_as):
login_as(GAMMA_SQLLAB_USERNAME)
resp = test_client.post(
"api/v1/sqllab/permalink", json={"name": "Untitled Query 1", "sql": "Test"}
)
assert resp.status_code == 400
def test_get(
tab_state_data: dict[str, Any], permalink_salt: str, test_client, login_as
):
login_as(GAMMA_SQLLAB_USERNAME)
resp = test_client.post("api/v1/sqllab/permalink", json=tab_state_data)
data = resp.json
key = data["key"]
resp = test_client.get(f"api/v1/sqllab/permalink/{key}")
assert resp.status_code == 200
result = json.loads(resp.data.decode("utf-8"))
assert result == tab_state_data
id_ = decode_permalink_id(key, permalink_salt)
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
db.session.commit()

View File

@@ -68,8 +68,6 @@ SQLLAB_CTAS_NO_LIMIT = True # SQL_MAX_ROW will not take effect for the CTA quer
FEATURE_FLAGS = {
**FEATURE_FLAGS, # noqa: F405
"foo": "bar",
"KV_STORE": True,
"SHARE_QUERIES_VIA_KV_STORE": True,
"ENABLE_TEMPLATE_PROCESSING": True,
"ALERT_REPORTS": True,
"AVOID_COLORS_COLLISION": True,

View File

@@ -74,8 +74,6 @@ CELERY_CONFIG = CeleryConfig
FEATURE_FLAGS = {
"foo": "bar",
"KV_STORE": False,
"SHARE_QUERIES_VIA_KV_STORE": False,
"THUMBNAILS": True,
"THUMBNAILS_SQLA_LISTENERS": False,
}