spec update by fixture

This commit is contained in:
justinpark 2024-06-10 14:58:24 -07:00
parent 836a60ccc2
commit 26036ce3cb
1 changed files with 66 additions and 33 deletions

View File

@ -15,17 +15,24 @@
# specific language governing permissions and limitations # specific language governing permissions and limitations
# under the License. # under the License.
import json import json
from collections.abc import Iterator
from typing import Any
from uuid import uuid3
import pytest
from superset import db from superset import db
from superset.key_value.models import KeyValueEntry from superset.key_value.models import KeyValueEntry
from tests.integration_tests.base_tests import SupersetTestCase from superset.key_value.types import KeyValueResource
from superset.key_value.utils import decode_permalink_id
from tests.integration_tests.constants import ( from tests.integration_tests.constants import (
GAMMA_SQLLAB_USERNAME, GAMMA_SQLLAB_USERNAME,
) )
class TestSqlLabPermalinkApi(SupersetTestCase): @pytest.fixture()
data = { def tab_state_data() -> dict[str, Any]:
return {
"dbId": 1, "dbId": 1,
"name": "Untitled Query 1", "name": "Untitled Query 1",
"schema": "main", "schema": "main",
@ -34,36 +41,62 @@ class TestSqlLabPermalinkApi(SupersetTestCase):
"templateParams": '{"param1": "value1"}', "templateParams": '{"param1": "value1"}',
} }
def test_post(self):
self.login(GAMMA_SQLLAB_USERNAME)
resp = self.client.post("api/v1/sqllab/permalink", json=self.data)
assert resp.status_code == 201
data = resp.json
key = data["key"]
url = data["url"]
assert key in url
db.session.query(KeyValueEntry).filter_by(id=key).delete()
db.session.commit()
def test_post_access_denied(self): @pytest.fixture()
resp = self.client.post("api/v1/sqllab/permalink", json=self.data) def permalink_salt(app_context) -> Iterator[str]:
assert resp.status_code == 401 from superset.key_value.shared_entries import get_permalink_salt, get_uuid_namespace
from superset.key_value.types import SharedKey
def test_post_invalid_schema(self): key = SharedKey.SQLLAB_PERMALINK_SALT
self.login(GAMMA_SQLLAB_USERNAME) salt = get_permalink_salt(key)
resp = self.client.post( yield salt
"api/v1/sqllab/permalink", json={"name": "Untitled Query 1", "sql": "Test"} namespace = get_uuid_namespace(salt)
) db.session.query(KeyValueEntry).filter_by(
assert resp.status_code == 400 resource=KeyValueResource.APP,
uuid=uuid3(namespace, key),
)
db.session.commit()
def test_get(self):
self.login(GAMMA_SQLLAB_USERNAME) def test_post(
resp = self.client.post("api/v1/sqllab/permalink", json=self.data) tab_state_data: dict[str, Any], permalink_salt: str, test_client, login_as
data = resp.json ):
key = data["key"] login_as(GAMMA_SQLLAB_USERNAME)
resp = self.client.get(f"api/v1/sqllab/permalink/{key}") resp = test_client.post("api/v1/sqllab/permalink", json=tab_state_data)
assert resp.status_code == 200 assert resp.status_code == 201
result = json.loads(resp.data.decode("utf-8")) data = resp.json
assert result == self.data key = data["key"]
db.session.query(KeyValueEntry).filter_by(id=key).delete() url = data["url"]
db.session.commit() 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()