2021-12-01 07:06:49 -05: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.
|
|
|
|
import json
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import pytest
|
2021-12-05 20:13:09 -05:00
|
|
|
from flask_appbuilder.security.sqla.models import User
|
2021-12-01 07:06:49 -05:00
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
|
|
from superset.dashboards.commands.exceptions import DashboardAccessDeniedError
|
|
|
|
from superset.extensions import cache_manager
|
|
|
|
from superset.models.dashboard import Dashboard
|
2022-03-16 19:15:52 -04:00
|
|
|
from superset.temporary_cache.commands.entry import Entry
|
|
|
|
from superset.temporary_cache.utils import cache_key
|
2021-12-05 20:13:09 -05:00
|
|
|
from tests.integration_tests.base_tests import login
|
2022-03-16 19:15:52 -04:00
|
|
|
from tests.integration_tests.fixtures.client import client
|
2021-12-01 07:06:49 -05:00
|
|
|
from tests.integration_tests.fixtures.world_bank_dashboard import (
|
|
|
|
load_world_bank_dashboard_with_slices,
|
2021-12-16 19:11:47 -05:00
|
|
|
load_world_bank_data,
|
2021-12-01 07:06:49 -05:00
|
|
|
)
|
|
|
|
from tests.integration_tests.test_app import app
|
|
|
|
|
2022-03-16 19:15:52 -04:00
|
|
|
KEY = "test-key"
|
|
|
|
INITIAL_VALUE = json.dumps({"test": "initial value"})
|
|
|
|
UPDATED_VALUE = json.dumps({"test": "updated value"})
|
2021-12-05 20:13:09 -05: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
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def admin_id() -> int:
|
|
|
|
with app.app_context() as ctx:
|
|
|
|
session: Session = ctx.app.appbuilder.get_session
|
|
|
|
admin = session.query(User).filter_by(username="admin").one_or_none()
|
|
|
|
return admin.id
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def cache(dashboard_id, admin_id):
|
2022-03-16 19:15:52 -04:00
|
|
|
entry: Entry = {"owner": admin_id, "value": INITIAL_VALUE}
|
|
|
|
cache_manager.filter_state_cache.set(cache_key(dashboard_id, KEY), entry)
|
2021-12-05 20:13:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_post(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2021-12-05 20:13:09 -05:00
|
|
|
}
|
|
|
|
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
|
|
|
|
assert resp.status_code == 201
|
|
|
|
|
|
|
|
|
2022-03-16 19:15:52 -04:00
|
|
|
def test_post_bad_request_non_string(client, dashboard_id: int):
|
2021-12-05 20:13:09 -05:00
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
|
|
|
"value": 1234,
|
|
|
|
}
|
2022-02-14 15:09:06 -05:00
|
|
|
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
|
|
|
2022-03-16 19:15:52 -04:00
|
|
|
def test_post_bad_request_non_json_string(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
|
|
|
"value": "foo",
|
|
|
|
}
|
|
|
|
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
|
|
|
2021-12-05 20:13:09 -05:00
|
|
|
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
|
|
|
|
def test_post_access_denied(mock_raise_for_dashboard_access, client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2021-12-05 20:13:09 -05:00
|
|
|
}
|
|
|
|
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
|
|
|
|
assert resp.status_code == 403
|
|
|
|
|
|
|
|
|
2022-02-14 15:09:06 -05:00
|
|
|
def test_post_same_key_for_same_tab_id(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2022-02-14 15:09:06 -05:00
|
|
|
}
|
|
|
|
resp = client.post(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=1", json=payload
|
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
first_key = data.get("key")
|
|
|
|
resp = client.post(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=1", json=payload
|
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
second_key = data.get("key")
|
|
|
|
assert first_key == second_key
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_different_key_for_different_tab_id(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2022-02-14 15:09:06 -05:00
|
|
|
}
|
|
|
|
resp = client.post(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=1", json=payload
|
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
first_key = data.get("key")
|
|
|
|
resp = client.post(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state?tab_id=2", json=payload
|
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
second_key = data.get("key")
|
|
|
|
assert first_key != second_key
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_different_key_for_no_tab_id(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2022-02-14 15:09:06 -05:00
|
|
|
}
|
|
|
|
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
first_key = data.get("key")
|
|
|
|
resp = client.post(f"api/v1/dashboard/{dashboard_id}/filter_state", json=payload)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
second_key = data.get("key")
|
|
|
|
assert first_key != second_key
|
|
|
|
|
|
|
|
|
2021-12-05 20:13:09 -05:00
|
|
|
def test_put(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": UPDATED_VALUE,
|
2021-12-05 20:13:09 -05:00
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
2021-12-05 20:13:09 -05:00
|
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
|
2022-02-14 15:09:06 -05:00
|
|
|
def test_put_same_key_for_same_tab_id(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2022-02-14 15:09:06 -05:00
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=1", json=payload
|
2022-02-14 15:09:06 -05:00
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
first_key = data.get("key")
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=1", json=payload
|
2022-02-14 15:09:06 -05:00
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
second_key = data.get("key")
|
|
|
|
assert first_key == second_key
|
|
|
|
|
|
|
|
|
|
|
|
def test_put_different_key_for_different_tab_id(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2022-02-14 15:09:06 -05:00
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=1", json=payload
|
2022-02-14 15:09:06 -05:00
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
first_key = data.get("key")
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}?tab_id=2", json=payload
|
2022-02-14 15:09:06 -05:00
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
second_key = data.get("key")
|
|
|
|
assert first_key != second_key
|
|
|
|
|
|
|
|
|
|
|
|
def test_put_different_key_for_no_tab_id(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": INITIAL_VALUE,
|
2022-02-14 15:09:06 -05:00
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
2022-02-14 15:09:06 -05:00
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
first_key = data.get("key")
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
2022-02-14 15:09:06 -05:00
|
|
|
)
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
|
|
|
second_key = data.get("key")
|
|
|
|
assert first_key != second_key
|
|
|
|
|
|
|
|
|
2022-03-16 19:15:52 -04:00
|
|
|
def test_put_bad_request_non_string(client, dashboard_id: int):
|
2021-12-05 20:13:09 -05:00
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
|
|
|
"value": 1234,
|
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
|
|
|
)
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
def test_put_bad_request_non_json_string(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
payload = {
|
|
|
|
"value": "foo",
|
|
|
|
}
|
|
|
|
resp = client.put(
|
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
2021-12-05 20:13:09 -05:00
|
|
|
)
|
|
|
|
assert resp.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
|
|
|
|
def test_put_access_denied(mock_raise_for_dashboard_access, client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
|
|
|
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": UPDATED_VALUE,
|
2021-12-05 20:13:09 -05:00
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
2021-12-05 20:13:09 -05:00
|
|
|
)
|
|
|
|
assert resp.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
def test_put_not_owner(client, dashboard_id: int):
|
|
|
|
login(client, "gamma")
|
|
|
|
payload = {
|
2022-03-16 19:15:52 -04:00
|
|
|
"value": UPDATED_VALUE,
|
2021-12-05 20:13:09 -05:00
|
|
|
}
|
|
|
|
resp = client.put(
|
2022-03-16 19:15:52 -04:00
|
|
|
f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}", json=payload
|
2021-12-05 20:13:09 -05:00
|
|
|
)
|
|
|
|
assert resp.status_code == 403
|
|
|
|
|
|
|
|
|
2022-01-10 12:24:22 -05:00
|
|
|
def test_get_key_not_found(client, dashboard_id: int):
|
2021-12-05 20:13:09 -05:00
|
|
|
login(client, "admin")
|
2022-01-10 12:24:22 -05:00
|
|
|
resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/unknown-key/")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_dashboard_not_found(client):
|
|
|
|
login(client, "admin")
|
2022-03-16 19:15:52 -04:00
|
|
|
resp = client.get(f"api/v1/dashboard/{-1}/filter_state/{KEY}")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_get(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
2022-03-16 19:15:52 -04:00
|
|
|
resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 200
|
|
|
|
data = json.loads(resp.data.decode("utf-8"))
|
2022-03-16 19:15:52 -04:00
|
|
|
assert INITIAL_VALUE == data.get("value")
|
2021-12-05 20:13:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
|
|
|
|
def test_get_access_denied(mock_raise_for_dashboard_access, client, dashboard_id):
|
|
|
|
login(client, "admin")
|
|
|
|
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
|
2022-03-16 19:15:52 -04:00
|
|
|
resp = client.get(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete(client, dashboard_id: int):
|
|
|
|
login(client, "admin")
|
2022-03-16 19:15:52 -04:00
|
|
|
resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
|
|
|
|
@patch("superset.security.SupersetSecurityManager.raise_for_dashboard_access")
|
|
|
|
def test_delete_access_denied(
|
|
|
|
mock_raise_for_dashboard_access, client, dashboard_id: int
|
|
|
|
):
|
|
|
|
login(client, "admin")
|
|
|
|
mock_raise_for_dashboard_access.side_effect = DashboardAccessDeniedError()
|
2022-03-16 19:15:52 -04:00
|
|
|
resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 403
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete_not_owner(client, dashboard_id: int):
|
|
|
|
login(client, "gamma")
|
2022-03-16 19:15:52 -04:00
|
|
|
resp = client.delete(f"api/v1/dashboard/{dashboard_id}/filter_state/{KEY}")
|
2021-12-05 20:13:09 -05:00
|
|
|
assert resp.status_code == 403
|