mirror of
https://github.com/apache/superset.git
synced 2026-04-14 05:34:38 +00:00
feat: add permalink to dashboard and explore (#19078)
* rename key_value to temporary_cache * add migration * create new key_value package * add commands * lots of new stuff * fix schema reference * remove redundant filter state from bootstrap data * add missing license headers * fix pylint * fix dashboard permalink access * use valid json mocks for filter state tests * fix temporary cache tests * add anchors to dashboard state * lint * fix util test * fix url shortlink button tests * remove legacy shortner * remove unused imports * fix js tests * fix test * add native filter state to anchor link * add UPDATING.md section * address comments * address comments * lint * fix test * add utils tests + other test stubs * add key_value integration tests * add filter box state to permalink state * fully support persisting url parameters * lint, add redirects and a few integration tests * fix test + clean up trailing comma * fix anchor bug * change value to LargeBinary to support persisting binary values * fix urlParams type and simplify urlencode * lint * add optional entry expiration * fix incorrect chart id + add test
This commit is contained in:
117
tests/integration_tests/explore/permalink/api_tests.py
Normal file
117
tests/integration_tests/explore/permalink/api_tests.py
Normal file
@@ -0,0 +1,117 @@
|
||||
# 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
|
||||
import pickle
|
||||
from typing import Any, Dict
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from superset import db
|
||||
from superset.key_value.models import KeyValueEntry
|
||||
from superset.models.slice import Slice
|
||||
from tests.integration_tests.base_tests import login
|
||||
from tests.integration_tests.fixtures.client import client
|
||||
from tests.integration_tests.fixtures.world_bank_dashboard import (
|
||||
load_world_bank_dashboard_with_slices,
|
||||
load_world_bank_data,
|
||||
)
|
||||
from tests.integration_tests.test_app import app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def chart(load_world_bank_dashboard_with_slices) -> Slice:
|
||||
with app.app_context() as ctx:
|
||||
session: Session = ctx.app.appbuilder.get_session
|
||||
chart = session.query(Slice).filter_by(slice_name="World's Population").one()
|
||||
return chart
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def form_data(chart) -> Dict[str, Any]:
|
||||
datasource = f"{chart.datasource.id}__{chart.datasource.type}"
|
||||
return {
|
||||
"chart_id": chart.id,
|
||||
"datasource": datasource,
|
||||
}
|
||||
|
||||
|
||||
def test_post(client, form_data):
|
||||
login(client, "admin")
|
||||
resp = client.post(f"api/v1/explore/permalink", json={"formData": form_data})
|
||||
assert resp.status_code == 201
|
||||
data = json.loads(resp.data.decode("utf-8"))
|
||||
key = data["key"]
|
||||
url = data["url"]
|
||||
assert key in url
|
||||
db.session.query(KeyValueEntry).filter_by(uuid=key).delete()
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def test_post_access_denied(client, form_data):
|
||||
login(client, "gamma")
|
||||
resp = client.post(f"api/v1/explore/permalink", json={"formData": form_data})
|
||||
assert resp.status_code == 404
|
||||
|
||||
|
||||
def test_get_missing_chart(client, chart):
|
||||
from superset.key_value.models import KeyValueEntry
|
||||
|
||||
key = 1234
|
||||
uuid_key = "e2ea9d19-7988-4862-aa69-c3a1a7628cb9"
|
||||
entry = KeyValueEntry(
|
||||
id=int(key),
|
||||
uuid=UUID("e2ea9d19-7988-4862-aa69-c3a1a7628cb9"),
|
||||
resource="explore_permalink",
|
||||
value=pickle.dumps(
|
||||
{
|
||||
"chartId": key,
|
||||
"datasetId": chart.datasource.id,
|
||||
"formData": {
|
||||
"slice_id": key,
|
||||
"datasource": f"{chart.datasource.id}__{chart.datasource.type}",
|
||||
},
|
||||
}
|
||||
),
|
||||
)
|
||||
db.session.add(entry)
|
||||
db.session.commit()
|
||||
login(client, "admin")
|
||||
resp = client.get(f"api/v1/explore/permalink/{uuid_key}")
|
||||
assert resp.status_code == 404
|
||||
db.session.delete(entry)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
def test_post_invalid_schema(client):
|
||||
login(client, "admin")
|
||||
resp = client.post(f"api/v1/explore/permalink", json={"abc": 123})
|
||||
assert resp.status_code == 400
|
||||
|
||||
|
||||
def test_get(client, form_data):
|
||||
login(client, "admin")
|
||||
resp = client.post(f"api/v1/explore/permalink", json={"formData": form_data})
|
||||
data = json.loads(resp.data.decode("utf-8"))
|
||||
key = data["key"]
|
||||
resp = client.get(f"api/v1/explore/permalink/{key}")
|
||||
assert resp.status_code == 200
|
||||
result = json.loads(resp.data.decode("utf-8"))
|
||||
assert result["state"]["formData"] == form_data
|
||||
db.session.query(KeyValueEntry).filter_by(uuid=key).delete()
|
||||
db.session.commit()
|
||||
Reference in New Issue
Block a user