2022-03-16 19:15:52 -04:00
|
|
|
# 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.
|
2023-06-01 15:01:10 -04:00
|
|
|
from collections.abc import Iterator
|
2022-03-16 19:15:52 -04:00
|
|
|
from unittest.mock import patch
|
2022-03-24 15:53:09 -04:00
|
|
|
from uuid import uuid3
|
2022-03-16 19:15:52 -04:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from flask_appbuilder.security.sqla.models import User
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
from superset import db
|
2023-11-22 14:55:54 -05:00
|
|
|
from superset.commands.dashboard.exceptions import DashboardAccessDeniedError
|
2022-03-16 19:15:52 -04:00
|
|
|
from superset.key_value.models import KeyValueEntry
|
2022-03-24 15:53:09 -04:00
|
|
|
from superset.key_value.types import KeyValueResource
|
|
|
|
from superset.key_value.utils import decode_permalink_id
|
2022-03-16 19:15:52 -04:00
|
|
|
from superset.models.dashboard import Dashboard
|
|
|
|
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
|
|
|
|
|
|
|
|
STATE = {
|
2022-06-29 12:43:52 -04:00
|
|
|
"dataMask": {"FILTER_1": "foo"},
|
|
|
|
"activeTabs": ["my-anchor"],
|
2022-03-16 19:15:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def dashboard_id(load_world_bank_dashboard_with_slices) -> int:
|
|
|
|
with app.app_context() as ctx:
|
|
|
|
session: Session = ctx.app.appbuilder.get_session
|
|
|
|
dashboard = session.query(Dashboard).filter_by(slug="world_health").one()
|
|
|
|
return dashboard.id
|
|
|
|
|
|
|
|
|
2022-03-24 15:53:09 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def permalink_salt() -> Iterator[str]:
|
|
|
|
from superset.key_value.shared_entries import get_permalink_salt, get_uuid_namespace
|
|
|
|
from superset.key_value.types import SharedKey
|
|
|
|
|
|
|
|
key = SharedKey.DASHBOARD_PERMALINK_SALT
|
|
|
|
salt = get_permalink_salt(key)
|
|
|
|
yield salt
|
|
|
|
namespace = get_uuid_namespace(salt)
|
|
|
|
db.session.query(KeyValueEntry).filter_by(
|
2022-03-29 13:03:09 -04:00
|
|
|
resource=KeyValueResource.APP,
|
|
|
|
uuid=uuid3(namespace, key),
|
2022-03-24 15:53:09 -04:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2022-07-14 21:10:31 -04:00
|
|
|
def test_post(
|
2023-05-12 10:01:30 -04:00
|
|
|
dashboard_id: int, permalink_salt: str, test_client, login_as_admin
|
2022-07-14 21:10:31 -04:00
|
|
|
) -> None:
|
|
|
|
resp = test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE)
|
2022-03-16 19:15:52 -04:00
|
|
|
assert resp.status_code == 201
|
2022-07-12 19:33:18 -04:00
|
|
|
data = resp.json
|
2022-03-16 19:15:52 -04:00
|
|
|
key = data["key"]
|
|
|
|
url = data["url"]
|
|
|
|
assert key in url
|
2022-03-24 15:53:09 -04:00
|
|
|
id_ = decode_permalink_id(key, permalink_salt)
|
2022-07-12 19:33:18 -04:00
|
|
|
|
|
|
|
assert (
|
|
|
|
data
|
2022-07-14 21:10:31 -04:00
|
|
|
== test_client.post(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE
|
|
|
|
).json
|
2022-07-12 19:33:18 -04:00
|
|
|
), "Should always return the same permalink key for the same payload"
|
|
|
|
|
2022-03-24 15:53:09 -04:00
|
|
|
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
|
2022-03-16 19:15:52 -04:00
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
2023-01-05 12:10:40 -05:00
|
|
|
def test_post_access_denied(test_client, login_as, dashboard_id: int):
|
|
|
|
login_as("gamma")
|
2022-07-14 21:10:31 -04:00
|
|
|
resp = test_client.post(f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE)
|
2023-01-05 12:10:40 -05:00
|
|
|
assert resp.status_code == 404
|
2022-03-16 19:15:52 -04:00
|
|
|
|
|
|
|
|
2023-05-12 10:01:30 -04:00
|
|
|
def test_post_invalid_schema(dashboard_id: int, test_client, login_as_admin):
|
2022-07-14 21:10:31 -04:00
|
|
|
resp = test_client.post(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/permalink", json={"foo": "bar"}
|
|
|
|
)
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
|
|
|
2023-05-12 10:01:30 -04:00
|
|
|
def test_get(dashboard_id: int, permalink_salt: str, test_client, login_as_admin):
|
2022-07-14 21:10:31 -04:00
|
|
|
key = test_client.post(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/permalink", json=STATE
|
|
|
|
).json["key"]
|
|
|
|
resp = test_client.get(f"api/v1/dashboard/permalink/{key}")
|
2022-03-16 19:15:52 -04:00
|
|
|
assert resp.status_code == 200
|
2022-07-12 19:33:18 -04:00
|
|
|
result = resp.json
|
2023-03-16 11:27:02 -04:00
|
|
|
dashboard_uuid = result["dashboardId"]
|
|
|
|
assert Dashboard.get(dashboard_uuid).id == dashboard_id
|
2022-03-16 19:15:52 -04:00
|
|
|
assert result["state"] == STATE
|
2022-03-24 15:53:09 -04:00
|
|
|
id_ = decode_permalink_id(key, permalink_salt)
|
|
|
|
db.session.query(KeyValueEntry).filter_by(id=id_).delete()
|
2022-03-16 19:15:52 -04:00
|
|
|
db.session.commit()
|