2019-12-16 16:10:33 -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.
|
2020-02-06 19:45:37 -05:00
|
|
|
# isort:skip_file
|
2020-11-25 01:45:35 -05:00
|
|
|
# pylint: disable=too-many-public-methods, no-self-use, invalid-name, too-many-arguments
|
2019-12-16 16:10:33 -05:00
|
|
|
"""Unit tests for Superset"""
|
|
|
|
import json
|
2020-10-22 23:02:40 -04:00
|
|
|
from io import BytesIO
|
2020-02-17 01:34:16 -05:00
|
|
|
from typing import List, Optional
|
2020-10-22 23:02:40 -04:00
|
|
|
from unittest.mock import patch
|
2020-11-25 01:45:35 -05:00
|
|
|
from zipfile import is_zipfile, ZipFile
|
2019-12-16 16:10:33 -05:00
|
|
|
|
2020-10-01 18:08:40 -04:00
|
|
|
import pytest
|
2019-12-20 05:16:57 -05:00
|
|
|
import prison
|
2020-11-25 01:45:35 -05:00
|
|
|
import yaml
|
2020-03-20 12:32:03 -04:00
|
|
|
from sqlalchemy.sql import func
|
2019-12-16 16:10:33 -05:00
|
|
|
|
2020-10-16 14:10:39 -04:00
|
|
|
from freezegun import freeze_time
|
2020-10-01 18:08:40 -04:00
|
|
|
from sqlalchemy import and_
|
2019-12-16 16:10:33 -05:00
|
|
|
from superset import db, security_manager
|
2020-03-20 12:32:03 -04:00
|
|
|
from superset.models.dashboard import Dashboard
|
2020-11-03 00:26:14 -05:00
|
|
|
from superset.models.core import FavStar, FavStarClassName
|
2020-11-26 03:45:49 -05:00
|
|
|
from superset.models.reports import ReportSchedule, ReportScheduleType
|
2020-01-06 10:29:39 -05:00
|
|
|
from superset.models.slice import Slice
|
2020-01-15 13:09:55 -05:00
|
|
|
from superset.views.base import generate_download_headers
|
2019-12-16 16:10:33 -05:00
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
from tests.base_api_tests import ApiOwnersTestCaseMixin
|
|
|
|
from tests.base_tests import SupersetTestCase
|
2020-11-25 01:45:35 -05:00
|
|
|
from tests.fixtures.importexport import (
|
|
|
|
chart_config,
|
|
|
|
database_config,
|
|
|
|
dashboard_config,
|
|
|
|
dashboard_metadata_config,
|
|
|
|
dataset_config,
|
|
|
|
dataset_metadata_config,
|
|
|
|
)
|
2019-12-16 16:10:33 -05:00
|
|
|
|
|
|
|
|
2020-10-01 18:08:40 -04:00
|
|
|
DASHBOARDS_FIXTURE_COUNT = 10
|
|
|
|
|
|
|
|
|
2020-06-29 18:36:06 -04:00
|
|
|
class TestDashboardApi(SupersetTestCase, ApiOwnersTestCaseMixin):
|
2020-01-21 13:04:52 -05:00
|
|
|
resource_name = "dashboard"
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
dashboard_data = {
|
|
|
|
"dashboard_title": "title1_changed",
|
|
|
|
"slug": "slug1_changed",
|
|
|
|
"position_json": '{"b": "B"}',
|
|
|
|
"css": "css_changed",
|
2020-07-07 08:26:54 -04:00
|
|
|
"json_metadata": '{"refresh_frequency": 30}',
|
2020-03-20 12:32:03 -04:00
|
|
|
"published": False,
|
|
|
|
}
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def insert_dashboard(
|
|
|
|
self,
|
|
|
|
dashboard_title: str,
|
2020-03-30 11:10:08 -04:00
|
|
|
slug: Optional[str],
|
2019-12-16 16:10:33 -05:00
|
|
|
owners: List[int],
|
2020-09-24 17:33:55 -04:00
|
|
|
created_by=None,
|
2020-02-17 01:34:16 -05:00
|
|
|
slices: Optional[List[Slice]] = None,
|
2019-12-16 16:10:33 -05:00
|
|
|
position_json: str = "",
|
|
|
|
css: str = "",
|
|
|
|
json_metadata: str = "",
|
|
|
|
published: bool = False,
|
2020-03-20 12:32:03 -04:00
|
|
|
) -> Dashboard:
|
2019-12-16 16:10:33 -05:00
|
|
|
obj_owners = list()
|
2020-01-06 10:29:39 -05:00
|
|
|
slices = slices or []
|
2019-12-16 16:10:33 -05:00
|
|
|
for owner in owners:
|
|
|
|
user = db.session.query(security_manager.user_model).get(owner)
|
|
|
|
obj_owners.append(user)
|
2020-03-20 12:32:03 -04:00
|
|
|
dashboard = Dashboard(
|
2019-12-16 16:10:33 -05:00
|
|
|
dashboard_title=dashboard_title,
|
|
|
|
slug=slug,
|
|
|
|
owners=obj_owners,
|
|
|
|
position_json=position_json,
|
|
|
|
css=css,
|
|
|
|
json_metadata=json_metadata,
|
2020-01-06 10:29:39 -05:00
|
|
|
slices=slices,
|
2019-12-16 16:10:33 -05:00
|
|
|
published=published,
|
2020-09-24 17:33:55 -04:00
|
|
|
created_by=created_by,
|
2019-12-16 16:10:33 -05:00
|
|
|
)
|
|
|
|
db.session.add(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
return dashboard
|
|
|
|
|
2020-10-01 18:08:40 -04:00
|
|
|
@pytest.fixture()
|
|
|
|
def create_dashboards(self):
|
|
|
|
with self.create_app().app_context():
|
|
|
|
dashboards = []
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
for cx in range(DASHBOARDS_FIXTURE_COUNT - 1):
|
|
|
|
dashboards.append(
|
|
|
|
self.insert_dashboard(f"title{cx}", f"slug{cx}", [admin.id])
|
|
|
|
)
|
|
|
|
fav_dashboards = []
|
|
|
|
for cx in range(round(DASHBOARDS_FIXTURE_COUNT / 2)):
|
|
|
|
fav_star = FavStar(
|
|
|
|
user_id=admin.id, class_name="Dashboard", obj_id=dashboards[cx].id
|
|
|
|
)
|
|
|
|
db.session.add(fav_star)
|
|
|
|
db.session.commit()
|
|
|
|
fav_dashboards.append(fav_star)
|
|
|
|
yield dashboards
|
|
|
|
|
|
|
|
# rollback changes
|
|
|
|
for dashboard in dashboards:
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
for fav_dashboard in fav_dashboards:
|
|
|
|
db.session.delete(fav_dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-11-26 03:45:49 -05:00
|
|
|
@pytest.fixture()
|
|
|
|
def create_dashboard_with_report(self):
|
|
|
|
with self.create_app().app_context():
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
dashboard = self.insert_dashboard(
|
|
|
|
f"dashboard_report", "dashboard_report", [admin.id]
|
|
|
|
)
|
|
|
|
report_schedule = ReportSchedule(
|
|
|
|
type=ReportScheduleType.REPORT,
|
|
|
|
name="report_with_dashboard",
|
|
|
|
crontab="* * * * *",
|
|
|
|
dashboard=dashboard,
|
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
yield dashboard
|
|
|
|
|
|
|
|
# rollback changes
|
|
|
|
db.session.delete(report_schedule)
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
def test_get_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test get dashboard
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
2020-09-24 17:33:55 -04:00
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin.id], admin)
|
2020-03-20 12:32:03 -04:00
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.get_assert_metric(uri, "get")
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
expected_result = {
|
|
|
|
"changed_by": None,
|
|
|
|
"changed_by_name": "",
|
|
|
|
"changed_by_url": "",
|
|
|
|
"charts": [],
|
2020-09-24 17:33:55 -04:00
|
|
|
"created_by": {"id": 1, "first_name": "admin", "last_name": "user",},
|
2020-03-20 12:32:03 -04:00
|
|
|
"id": dashboard.id,
|
|
|
|
"css": "",
|
|
|
|
"dashboard_title": "title",
|
|
|
|
"json_metadata": "",
|
2020-04-07 12:09:02 -04:00
|
|
|
"owners": [
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"username": "admin",
|
|
|
|
"first_name": "admin",
|
|
|
|
"last_name": "user",
|
|
|
|
}
|
|
|
|
],
|
2020-03-20 12:32:03 -04:00
|
|
|
"position_json": "",
|
|
|
|
"published": False,
|
2020-11-25 01:45:35 -05:00
|
|
|
"url": "/superset/dashboard/slug1/",
|
2020-03-20 12:32:03 -04:00
|
|
|
"slug": "slug1",
|
|
|
|
"table_names": "",
|
2020-04-15 04:40:14 -04:00
|
|
|
"thumbnail_url": dashboard.thumbnail_url,
|
2020-03-20 12:32:03 -04:00
|
|
|
}
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertIn("changed_on", data["result"])
|
|
|
|
for key, value in data["result"].items():
|
2020-07-15 14:09:32 -04:00
|
|
|
# We can't assert timestamp values
|
2020-03-20 12:32:03 -04:00
|
|
|
if key != "changed_on":
|
|
|
|
self.assertEqual(value, expected_result[key])
|
|
|
|
# rollback changes
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-04-16 05:54:45 -04:00
|
|
|
def test_info_dashboard(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test info
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
2020-11-25 01:45:35 -05:00
|
|
|
uri = "api/v1/dashboard/_info"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.get_assert_metric(uri, "info")
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
def test_get_dashboard_not_found(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test get dashboard not found
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
|
|
|
max_id = db.session.query(func.max(Dashboard.id)).scalar()
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{max_id + 1}"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.get_assert_metric(uri, "get")
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
|
|
|
def test_get_dashboard_no_data_access(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test get dashboard without data access
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin.id])
|
|
|
|
|
|
|
|
self.login(username="gamma")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
# rollback changes
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-07-15 14:09:32 -04:00
|
|
|
def test_get_dashboards_changed_on(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test get dashboards changed on
|
|
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
import humanize
|
|
|
|
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
start_changed_on = datetime.now()
|
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin.id])
|
|
|
|
|
|
|
|
self.login(username="admin")
|
|
|
|
|
|
|
|
arguments = {
|
|
|
|
"order_column": "changed_on_delta_humanized",
|
|
|
|
"order_direction": "desc",
|
|
|
|
}
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
|
|
|
|
rv = self.get_assert_metric(uri, "get_list")
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(
|
|
|
|
data["result"][0]["changed_on_delta_humanized"],
|
|
|
|
humanize.naturaltime(datetime.now() - start_changed_on),
|
|
|
|
)
|
|
|
|
|
|
|
|
# rollback changes
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
def test_get_dashboards_filter(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test get dashboards filter
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
gamma = self.get_user("gamma")
|
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin.id, gamma.id])
|
|
|
|
|
|
|
|
self.login(username="admin")
|
|
|
|
|
|
|
|
arguments = {
|
|
|
|
"filters": [{"col": "dashboard_title", "opr": "sw", "value": "ti"}]
|
|
|
|
}
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
2020-04-16 05:54:45 -04:00
|
|
|
|
|
|
|
rv = self.get_assert_metric(uri, "get_list")
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(data["count"], 1)
|
|
|
|
|
|
|
|
arguments = {
|
|
|
|
"filters": [
|
|
|
|
{"col": "owners", "opr": "rel_m_m", "value": [admin.id, gamma.id]}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(data["count"], 1)
|
|
|
|
|
|
|
|
# rollback changes
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-10-01 18:08:40 -04:00
|
|
|
@pytest.mark.usefixtures("create_dashboards")
|
|
|
|
def test_get_dashboards_title_or_slug_filter(self):
|
2020-04-09 06:14:28 -04:00
|
|
|
"""
|
2020-10-01 18:08:40 -04:00
|
|
|
Dashboard API: Test get dashboards title or slug filter
|
2020-04-09 06:14:28 -04:00
|
|
|
"""
|
2020-10-01 18:08:40 -04:00
|
|
|
# Test title filter with ilike
|
2020-04-09 06:14:28 -04:00
|
|
|
arguments = {
|
|
|
|
"filters": [
|
2020-10-01 18:08:40 -04:00
|
|
|
{"col": "dashboard_title", "opr": "title_or_slug", "value": "title1"}
|
2020-04-14 05:10:14 -04:00
|
|
|
],
|
|
|
|
"order_column": "dashboard_title",
|
|
|
|
"order_direction": "asc",
|
2020-04-29 03:36:11 -04:00
|
|
|
"keys": ["none"],
|
|
|
|
"columns": ["dashboard_title", "slug"],
|
2020-04-09 06:14:28 -04:00
|
|
|
}
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
2020-10-01 18:08:40 -04:00
|
|
|
self.assertEqual(data["count"], 1)
|
2020-04-09 06:14:28 -04:00
|
|
|
|
2020-04-14 05:10:14 -04:00
|
|
|
expected_response = [
|
2020-10-01 18:08:40 -04:00
|
|
|
{"slug": "slug1", "dashboard_title": "title1"},
|
2020-04-14 05:10:14 -04:00
|
|
|
]
|
2020-10-01 18:08:40 -04:00
|
|
|
assert data["result"] == expected_response
|
|
|
|
|
|
|
|
# Test slug filter with ilike
|
|
|
|
arguments["filters"][0]["value"] = "slug2"
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(data["count"], 1)
|
|
|
|
|
|
|
|
expected_response = [
|
|
|
|
{"slug": "slug2", "dashboard_title": "title2"},
|
|
|
|
]
|
|
|
|
assert data["result"] == expected_response
|
2020-04-14 05:10:14 -04:00
|
|
|
|
2020-04-09 06:14:28 -04:00
|
|
|
self.logout()
|
|
|
|
self.login(username="gamma")
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(data["count"], 0)
|
|
|
|
|
2020-10-01 18:08:40 -04:00
|
|
|
@pytest.mark.usefixtures("create_dashboards")
|
|
|
|
def test_get_dashboards_favorite_filter(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test get dashboards favorite filter
|
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
users_favorite_query = db.session.query(FavStar.obj_id).filter(
|
|
|
|
and_(FavStar.user_id == admin.id, FavStar.class_name == "Dashboard")
|
|
|
|
)
|
|
|
|
expected_models = (
|
|
|
|
db.session.query(Dashboard)
|
|
|
|
.filter(and_(Dashboard.id.in_(users_favorite_query)))
|
|
|
|
.order_by(Dashboard.dashboard_title.asc())
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
arguments = {
|
|
|
|
"filters": [{"col": "id", "opr": "dashboard_is_fav", "value": True}],
|
|
|
|
"order_column": "dashboard_title",
|
|
|
|
"order_direction": "asc",
|
|
|
|
"keys": ["none"],
|
|
|
|
"columns": ["dashboard_title"],
|
|
|
|
}
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
assert rv.status_code == 200
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
assert len(expected_models) == data["count"]
|
|
|
|
|
|
|
|
for i, expected_model in enumerate(expected_models):
|
|
|
|
assert (
|
|
|
|
expected_model.dashboard_title == data["result"][i]["dashboard_title"]
|
|
|
|
)
|
|
|
|
|
2020-11-03 00:26:14 -05:00
|
|
|
@pytest.mark.usefixtures("create_dashboards")
|
|
|
|
def test_get_current_user_favorite_status(self):
|
|
|
|
"""
|
|
|
|
Dataset API: Test get current user favorite stars
|
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
users_favorite_ids = [
|
|
|
|
star.obj_id
|
|
|
|
for star in db.session.query(FavStar.obj_id)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
FavStar.user_id == admin.id,
|
|
|
|
FavStar.class_name == FavStarClassName.DASHBOARD,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.all()
|
|
|
|
]
|
|
|
|
|
|
|
|
assert users_favorite_ids
|
|
|
|
arguments = [dash.id for dash in db.session.query(Dashboard.id).all()]
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/favorite_status/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
assert rv.status_code == 200
|
|
|
|
for res in data["result"]:
|
|
|
|
if res["id"] in users_favorite_ids:
|
|
|
|
assert res["value"]
|
|
|
|
|
2020-10-01 18:08:40 -04:00
|
|
|
@pytest.mark.usefixtures("create_dashboards")
|
|
|
|
def test_get_dashboards_not_favorite_filter(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test get dashboards not favorite filter
|
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
users_favorite_query = db.session.query(FavStar.obj_id).filter(
|
|
|
|
and_(FavStar.user_id == admin.id, FavStar.class_name == "Dashboard")
|
|
|
|
)
|
|
|
|
expected_models = (
|
|
|
|
db.session.query(Dashboard)
|
|
|
|
.filter(and_(~Dashboard.id.in_(users_favorite_query)))
|
|
|
|
.order_by(Dashboard.dashboard_title.asc())
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
arguments = {
|
|
|
|
"filters": [{"col": "id", "opr": "dashboard_is_fav", "value": False}],
|
|
|
|
"order_column": "dashboard_title",
|
|
|
|
"order_direction": "asc",
|
|
|
|
"keys": ["none"],
|
|
|
|
"columns": ["dashboard_title"],
|
|
|
|
}
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
self.login(username="admin")
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
assert rv.status_code == 200
|
|
|
|
assert len(expected_models) == data["count"]
|
|
|
|
for i, expected_model in enumerate(expected_models):
|
|
|
|
assert (
|
|
|
|
expected_model.dashboard_title == data["result"][i]["dashboard_title"]
|
|
|
|
)
|
2020-04-09 06:14:28 -04:00
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
def test_get_dashboards_no_data_access(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test get dashboards no data access
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin.id])
|
|
|
|
|
|
|
|
self.login(username="gamma")
|
|
|
|
arguments = {
|
|
|
|
"filters": [{"col": "dashboard_title", "opr": "sw", "value": "ti"}]
|
|
|
|
}
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(data["count"], 0)
|
|
|
|
|
|
|
|
# rollback changes
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def test_delete_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test delete
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard_id = self.insert_dashboard("title", "slug1", [admin_id]).id
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.delete_assert_metric(uri, "delete")
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(model, None)
|
|
|
|
|
2020-01-21 17:14:53 -05:00
|
|
|
def test_delete_bulk_dashboards(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test delete bulk
|
2020-01-21 17:14:53 -05:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard_count = 4
|
|
|
|
dashboard_ids = list()
|
|
|
|
for dashboard_name_index in range(dashboard_count):
|
|
|
|
dashboard_ids.append(
|
|
|
|
self.insert_dashboard(
|
|
|
|
f"title{dashboard_name_index}",
|
|
|
|
f"slug{dashboard_name_index}",
|
|
|
|
[admin_id],
|
|
|
|
).id
|
|
|
|
)
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = dashboard_ids
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(argument)}"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.delete_assert_metric(uri, "bulk_delete")
|
2020-01-21 17:14:53 -05:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
expected_response = {"message": f"Deleted {dashboard_count} dashboards"}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
for dashboard_id in dashboard_ids:
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
2020-01-21 17:14:53 -05:00
|
|
|
self.assertEqual(model, None)
|
|
|
|
|
|
|
|
def test_delete_bulk_dashboards_bad_request(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test delete bulk bad request
|
2020-01-21 17:14:53 -05:00
|
|
|
"""
|
|
|
|
dashboard_ids = [1, "a"]
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = dashboard_ids
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
self.assertEqual(rv.status_code, 400)
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def test_delete_not_found_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test not found delete
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
dashboard_id = 1000
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2020-11-26 03:45:49 -05:00
|
|
|
@pytest.mark.usefixtures("create_dashboard_with_report")
|
|
|
|
def test_delete_dashboard_with_report(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test delete with associated report
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
dashboard = (
|
|
|
|
db.session.query(Dashboard.id)
|
|
|
|
.filter(Dashboard.dashboard_title == "dashboard_report")
|
|
|
|
.one_or_none()
|
|
|
|
)
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(rv.status_code, 422)
|
|
|
|
expected_response = {
|
|
|
|
"message": "There are associated alerts or reports: report_with_dashboard"
|
|
|
|
}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
2020-01-21 17:14:53 -05:00
|
|
|
def test_delete_bulk_dashboards_not_found(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test delete bulk not found
|
2020-01-21 17:14:53 -05:00
|
|
|
"""
|
|
|
|
dashboard_ids = [1001, 1002]
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = dashboard_ids
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
2020-11-26 03:45:49 -05:00
|
|
|
@pytest.mark.usefixtures("create_dashboard_with_report", "create_dashboards")
|
|
|
|
def test_bulk_delete_dashboard_with_report(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test bulk delete with associated report
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
dashboard_with_report = (
|
|
|
|
db.session.query(Dashboard.id)
|
|
|
|
.filter(Dashboard.dashboard_title == "dashboard_report")
|
|
|
|
.one_or_none()
|
|
|
|
)
|
|
|
|
dashboards = (
|
|
|
|
db.session.query(Dashboard)
|
|
|
|
.filter(Dashboard.dashboard_title.like("title%"))
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
|
|
|
|
dashboard_ids = [dashboard.id for dashboard in dashboards]
|
|
|
|
dashboard_ids.append(dashboard_with_report.id)
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(dashboard_ids)}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(rv.status_code, 422)
|
|
|
|
expected_response = {
|
|
|
|
"message": "There are associated alerts or reports: report_with_dashboard"
|
|
|
|
}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def test_delete_dashboard_admin_not_owned(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test admin delete not owned
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
gamma_id = self.get_user("gamma").id
|
|
|
|
dashboard_id = self.insert_dashboard("title", "slug1", [gamma_id]).id
|
|
|
|
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(model, None)
|
|
|
|
|
2020-01-21 17:14:53 -05:00
|
|
|
def test_delete_bulk_dashboard_admin_not_owned(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test admin delete bulk not owned
|
2020-01-21 17:14:53 -05:00
|
|
|
"""
|
|
|
|
gamma_id = self.get_user("gamma").id
|
|
|
|
dashboard_count = 4
|
|
|
|
dashboard_ids = list()
|
|
|
|
for dashboard_name_index in range(dashboard_count):
|
|
|
|
dashboard_ids.append(
|
|
|
|
self.insert_dashboard(
|
|
|
|
f"title{dashboard_name_index}",
|
|
|
|
f"slug{dashboard_name_index}",
|
|
|
|
[gamma_id],
|
|
|
|
).id
|
|
|
|
)
|
|
|
|
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = dashboard_ids
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
expected_response = {"message": f"Deleted {dashboard_count} dashboards"}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
for dashboard_id in dashboard_ids:
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
2020-01-21 17:14:53 -05:00
|
|
|
self.assertEqual(model, None)
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def test_delete_dashboard_not_owned(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test delete try not owned
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
user_alpha1 = self.create_user(
|
|
|
|
"alpha1", "password", "Alpha", email="alpha1@superset.org"
|
|
|
|
)
|
|
|
|
user_alpha2 = self.create_user(
|
|
|
|
"alpha2", "password", "Alpha", email="alpha2@superset.org"
|
|
|
|
)
|
2020-01-06 10:29:39 -05:00
|
|
|
existing_slice = (
|
|
|
|
db.session.query(Slice).filter_by(slice_name="Girl Name Cloud").first()
|
|
|
|
)
|
|
|
|
dashboard = self.insert_dashboard(
|
|
|
|
"title", "slug1", [user_alpha1.id], slices=[existing_slice], published=True
|
|
|
|
)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.login(username="alpha2", password="password")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
|
|
|
rv = self.client.delete(uri)
|
2020-01-06 10:29:39 -05:00
|
|
|
self.assertEqual(rv.status_code, 403)
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.delete(user_alpha1)
|
|
|
|
db.session.delete(user_alpha2)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-01-21 17:14:53 -05:00
|
|
|
def test_delete_bulk_dashboard_not_owned(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test delete bulk try not owned
|
2020-01-21 17:14:53 -05:00
|
|
|
"""
|
|
|
|
user_alpha1 = self.create_user(
|
|
|
|
"alpha1", "password", "Alpha", email="alpha1@superset.org"
|
|
|
|
)
|
|
|
|
user_alpha2 = self.create_user(
|
|
|
|
"alpha2", "password", "Alpha", email="alpha2@superset.org"
|
|
|
|
)
|
|
|
|
existing_slice = (
|
|
|
|
db.session.query(Slice).filter_by(slice_name="Girl Name Cloud").first()
|
|
|
|
)
|
|
|
|
|
|
|
|
dashboard_count = 4
|
|
|
|
dashboards = list()
|
|
|
|
for dashboard_name_index in range(dashboard_count):
|
|
|
|
dashboards.append(
|
|
|
|
self.insert_dashboard(
|
|
|
|
f"title{dashboard_name_index}",
|
|
|
|
f"slug{dashboard_name_index}",
|
|
|
|
[user_alpha1.id],
|
|
|
|
slices=[existing_slice],
|
|
|
|
published=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
owned_dashboard = self.insert_dashboard(
|
|
|
|
"title_owned",
|
|
|
|
"slug_owned",
|
|
|
|
[user_alpha2.id],
|
|
|
|
slices=[existing_slice],
|
|
|
|
published=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.login(username="alpha2", password="password")
|
|
|
|
|
|
|
|
# verify we can't delete not owned dashboards
|
|
|
|
arguments = [dashboard.id for dashboard in dashboards]
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
self.assertEqual(rv.status_code, 403)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
expected_response = {"message": "Forbidden"}
|
2020-01-21 17:14:53 -05:00
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
2020-03-27 05:31:01 -04:00
|
|
|
# nothing is deleted in bulk with a list of owned and not owned dashboards
|
2020-01-21 17:14:53 -05:00
|
|
|
arguments = [dashboard.id for dashboard in dashboards] + [owned_dashboard.id]
|
|
|
|
uri = f"api/v1/dashboard/?q={prison.dumps(arguments)}"
|
|
|
|
rv = self.client.delete(uri)
|
|
|
|
self.assertEqual(rv.status_code, 403)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
expected_response = {"message": "Forbidden"}
|
2020-01-21 17:14:53 -05:00
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
for dashboard in dashboards:
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.delete(owned_dashboard)
|
|
|
|
db.session.delete(user_alpha1)
|
|
|
|
db.session.delete(user_alpha2)
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def test_create_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create dashboard
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard_data = {
|
|
|
|
"dashboard_title": "title1",
|
|
|
|
"slug": "slug1",
|
|
|
|
"owners": [admin_id],
|
|
|
|
"position_json": '{"a": "A"}',
|
|
|
|
"css": "css",
|
2020-07-07 08:26:54 -04:00
|
|
|
"json_metadata": '{"refresh_frequency": 30}',
|
2019-12-16 16:10:33 -05:00
|
|
|
"published": True,
|
|
|
|
}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.post_assert_metric(uri, dashboard_data, "post")
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(data.get("id"))
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_create_simple_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create simple dashboard
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
dashboard_data = {"dashboard_title": "title1"}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(data.get("id"))
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_create_dashboard_empty(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create empty
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
dashboard_data = {}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(data.get("id"))
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
dashboard_data = {"dashboard_title": ""}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 201)
|
|
|
|
data = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(data.get("id"))
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_create_dashboard_validate_title(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create dashboard validate title
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
dashboard_data = {"dashboard_title": "a" * 600}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.post_assert_metric(uri, dashboard_data, "post")
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 400)
|
2019-12-16 16:10:33 -05:00
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
expected_response = {
|
|
|
|
"message": {"dashboard_title": ["Length must be between 0 and 500."]}
|
|
|
|
}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
def test_create_dashboard_validate_slug(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create validate slug
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard = self.insert_dashboard("title1", "slug1", [admin_id])
|
|
|
|
self.login(username="admin")
|
|
|
|
|
|
|
|
# Check for slug uniqueness
|
|
|
|
dashboard_data = {"dashboard_title": "title2", "slug": "slug1"}
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 422)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
expected_response = {"message": {"slug": ["Must be unique"]}}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
# Check for slug max size
|
|
|
|
dashboard_data = {"dashboard_title": "title2", "slug": "a" * 256}
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 400)
|
2019-12-16 16:10:33 -05:00
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
expected_response = {"message": {"slug": ["Length must be between 1 and 255."]}}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_create_dashboard_validate_owners(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create validate owners
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
dashboard_data = {"dashboard_title": "title1", "owners": [1000]}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 422)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
2020-03-20 12:32:03 -04:00
|
|
|
expected_response = {"message": {"owners": ["Owners are invalid"]}}
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
def test_create_dashboard_validate_json(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test create validate json
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
dashboard_data = {"dashboard_title": "title1", "position_json": '{"A:"a"}'}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 400)
|
2019-12-16 16:10:33 -05:00
|
|
|
|
|
|
|
dashboard_data = {"dashboard_title": "title1", "json_metadata": '{"A:"a"}'}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 400)
|
2019-12-16 16:10:33 -05:00
|
|
|
|
|
|
|
dashboard_data = {
|
|
|
|
"dashboard_title": "title1",
|
|
|
|
"json_metadata": '{"refresh_frequency": "A"}',
|
|
|
|
}
|
|
|
|
self.login(username="admin")
|
2020-01-29 04:41:40 -05:00
|
|
|
uri = "api/v1/dashboard/"
|
2019-12-16 16:10:33 -05:00
|
|
|
rv = self.client.post(uri, json=dashboard_data)
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 400)
|
2019-12-16 16:10:33 -05:00
|
|
|
|
|
|
|
def test_update_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
2020-03-20 12:32:03 -04:00
|
|
|
admin = self.get_user("admin")
|
|
|
|
dashboard_id = self.insert_dashboard("title1", "slug1", [admin.id]).id
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.put_assert_metric(uri, self.dashboard_data, "put")
|
2020-03-20 12:32:03 -04:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
|
|
|
self.assertEqual(model.dashboard_title, self.dashboard_data["dashboard_title"])
|
|
|
|
self.assertEqual(model.slug, self.dashboard_data["slug"])
|
|
|
|
self.assertEqual(model.position_json, self.dashboard_data["position_json"])
|
|
|
|
self.assertEqual(model.css, self.dashboard_data["css"])
|
|
|
|
self.assertEqual(model.json_metadata, self.dashboard_data["json_metadata"])
|
|
|
|
self.assertEqual(model.published, self.dashboard_data["published"])
|
|
|
|
self.assertEqual(model.owners, [admin])
|
|
|
|
|
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-04-09 04:37:37 -04:00
|
|
|
def test_update_dashboard_chart_owners(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update chart owners
|
2020-04-09 04:37:37 -04:00
|
|
|
"""
|
|
|
|
user_alpha1 = self.create_user(
|
|
|
|
"alpha1", "password", "Alpha", email="alpha1@superset.org"
|
|
|
|
)
|
|
|
|
user_alpha2 = self.create_user(
|
|
|
|
"alpha2", "password", "Alpha", email="alpha2@superset.org"
|
|
|
|
)
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
slices = []
|
|
|
|
slices.append(
|
|
|
|
db.session.query(Slice).filter_by(slice_name="Girl Name Cloud").first()
|
|
|
|
)
|
|
|
|
slices.append(db.session.query(Slice).filter_by(slice_name="Trends").first())
|
|
|
|
slices.append(db.session.query(Slice).filter_by(slice_name="Boys").first())
|
|
|
|
|
|
|
|
dashboard = self.insert_dashboard("title1", "slug1", [admin.id], slices=slices,)
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
|
|
|
dashboard_data = {"owners": [user_alpha1.id, user_alpha2.id]}
|
|
|
|
rv = self.client.put(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
|
|
|
# verify slices owners include alpha1 and alpha2 users
|
|
|
|
slices_ids = [slice.id for slice in slices]
|
|
|
|
# Refetch Slices
|
|
|
|
slices = db.session.query(Slice).filter(Slice.id.in_(slices_ids)).all()
|
|
|
|
for slice in slices:
|
|
|
|
self.assertIn(user_alpha1, slice.owners)
|
|
|
|
self.assertIn(user_alpha2, slice.owners)
|
|
|
|
self.assertIn(admin, slice.owners)
|
|
|
|
# Revert owners on slice
|
|
|
|
slice.owners = []
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# Rollback changes
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.delete(user_alpha1)
|
|
|
|
db.session.delete(user_alpha2)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
def test_update_partial_dashboard(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update partial
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
2019-12-16 16:10:33 -05:00
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard_id = self.insert_dashboard("title1", "slug1", [admin_id]).id
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
2020-03-20 12:32:03 -04:00
|
|
|
rv = self.client.put(
|
|
|
|
uri, json={"json_metadata": self.dashboard_data["json_metadata"]}
|
|
|
|
)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(rv.status_code, 200)
|
2020-03-20 12:32:03 -04:00
|
|
|
|
|
|
|
rv = self.client.put(
|
|
|
|
uri, json={"dashboard_title": self.dashboard_data["dashboard_title"]}
|
|
|
|
)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
|
|
|
rv = self.client.put(uri, json={"slug": self.dashboard_data["slug"]})
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
|
|
|
self.assertEqual(model.json_metadata, self.dashboard_data["json_metadata"])
|
|
|
|
self.assertEqual(model.dashboard_title, self.dashboard_data["dashboard_title"])
|
|
|
|
self.assertEqual(model.slug, self.dashboard_data["slug"])
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_update_dashboard_new_owner(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update set new owner to current user
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
gamma_id = self.get_user("gamma").id
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
dashboard_id = self.insert_dashboard("title1", "slug1", [gamma_id]).id
|
|
|
|
dashboard_data = {"dashboard_title": "title1_changed"}
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
|
|
|
rv = self.client.put(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertIn(admin, model.owners)
|
|
|
|
for slc in model.slices:
|
|
|
|
self.assertIn(admin, slc.owners)
|
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_update_dashboard_slug_formatting(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update slug formatting
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard_id = self.insert_dashboard("title1", "slug1", [admin_id]).id
|
|
|
|
dashboard_data = {"dashboard_title": "title1_changed", "slug": "slug1 changed"}
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard_id}"
|
|
|
|
rv = self.client.put(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard_id)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.assertEqual(model.dashboard_title, "title1_changed")
|
|
|
|
self.assertEqual(model.slug, "slug1-changed")
|
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
def test_update_dashboard_validate_slug(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update validate slug
|
2020-03-20 12:32:03 -04:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard1 = self.insert_dashboard("title1", "slug-1", [admin_id])
|
|
|
|
dashboard2 = self.insert_dashboard("title2", "slug-2", [admin_id])
|
|
|
|
|
|
|
|
self.login(username="admin")
|
|
|
|
# Check for slug uniqueness
|
|
|
|
dashboard_data = {"dashboard_title": "title2", "slug": "slug 1"}
|
|
|
|
uri = f"api/v1/dashboard/{dashboard2.id}"
|
|
|
|
rv = self.client.put(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 422)
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
expected_response = {"message": {"slug": ["Must be unique"]}}
|
|
|
|
self.assertEqual(response, expected_response)
|
|
|
|
|
|
|
|
db.session.delete(dashboard1)
|
|
|
|
db.session.delete(dashboard2)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-30 11:10:08 -04:00
|
|
|
dashboard1 = self.insert_dashboard("title1", None, [admin_id])
|
|
|
|
dashboard2 = self.insert_dashboard("title2", None, [admin_id])
|
|
|
|
self.login(username="admin")
|
|
|
|
# Accept empty slugs and don't validate them has unique
|
|
|
|
dashboard_data = {"dashboard_title": "title2_changed", "slug": ""}
|
|
|
|
uri = f"api/v1/dashboard/{dashboard2.id}"
|
|
|
|
rv = self.client.put(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
|
|
|
db.session.delete(dashboard1)
|
|
|
|
db.session.delete(dashboard2)
|
|
|
|
db.session.commit()
|
|
|
|
|
2020-03-12 11:35:27 -04:00
|
|
|
def test_update_published(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update published patch
|
2020-03-12 11:35:27 -04:00
|
|
|
"""
|
|
|
|
admin = self.get_user("admin")
|
|
|
|
gamma = self.get_user("gamma")
|
|
|
|
|
|
|
|
dashboard = self.insert_dashboard("title1", "slug1", [admin.id, gamma.id])
|
|
|
|
dashboard_data = {"published": True}
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
|
|
|
rv = self.client.put(uri, json=dashboard_data)
|
|
|
|
self.assertEqual(rv.status_code, 200)
|
|
|
|
|
2020-03-20 12:32:03 -04:00
|
|
|
model = db.session.query(Dashboard).get(dashboard.id)
|
2020-03-12 11:35:27 -04:00
|
|
|
self.assertEqual(model.published, True)
|
|
|
|
self.assertEqual(model.slug, "slug1")
|
|
|
|
self.assertIn(admin, model.owners)
|
|
|
|
self.assertIn(gamma, model.owners)
|
|
|
|
db.session.delete(model)
|
|
|
|
db.session.commit()
|
|
|
|
|
2019-12-16 16:10:33 -05:00
|
|
|
def test_update_dashboard_not_owned(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test update dashboard not owned
|
2019-12-16 16:10:33 -05:00
|
|
|
"""
|
|
|
|
user_alpha1 = self.create_user(
|
|
|
|
"alpha1", "password", "Alpha", email="alpha1@superset.org"
|
|
|
|
)
|
|
|
|
user_alpha2 = self.create_user(
|
|
|
|
"alpha2", "password", "Alpha", email="alpha2@superset.org"
|
|
|
|
)
|
2020-01-06 10:29:39 -05:00
|
|
|
existing_slice = (
|
|
|
|
db.session.query(Slice).filter_by(slice_name="Girl Name Cloud").first()
|
|
|
|
)
|
|
|
|
dashboard = self.insert_dashboard(
|
|
|
|
"title", "slug1", [user_alpha1.id], slices=[existing_slice], published=True
|
|
|
|
)
|
2019-12-16 16:10:33 -05:00
|
|
|
self.login(username="alpha2", password="password")
|
|
|
|
dashboard_data = {"dashboard_title": "title1_changed", "slug": "slug1 changed"}
|
|
|
|
uri = f"api/v1/dashboard/{dashboard.id}"
|
2020-04-16 05:54:45 -04:00
|
|
|
rv = self.put_assert_metric(uri, dashboard_data, "put")
|
2020-01-06 10:29:39 -05:00
|
|
|
self.assertEqual(rv.status_code, 403)
|
2019-12-16 16:10:33 -05:00
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.delete(user_alpha1)
|
|
|
|
db.session.delete(user_alpha2)
|
|
|
|
db.session.commit()
|
2019-12-20 05:16:57 -05:00
|
|
|
|
2020-01-15 13:09:55 -05:00
|
|
|
def test_export(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test dashboard export
|
2020-01-15 13:09:55 -05:00
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = [1, 2]
|
|
|
|
uri = f"api/v1/dashboard/export/?q={prison.dumps(argument)}"
|
2020-10-16 14:10:39 -04:00
|
|
|
|
|
|
|
# freeze time to ensure filename is deterministic
|
|
|
|
with freeze_time("2020-01-01T00:00:00Z"):
|
|
|
|
rv = self.get_assert_metric(uri, "export")
|
|
|
|
headers = generate_download_headers("json")["Content-Disposition"]
|
|
|
|
|
|
|
|
assert rv.status_code == 200
|
|
|
|
assert rv.headers["Content-Disposition"] == headers
|
2020-01-15 13:09:55 -05:00
|
|
|
|
|
|
|
def test_export_not_found(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test dashboard export not found
|
2020-01-15 13:09:55 -05:00
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = [1000]
|
|
|
|
uri = f"api/v1/dashboard/export/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
|
|
|
|
def test_export_not_allowed(self):
|
|
|
|
"""
|
2020-04-16 05:54:45 -04:00
|
|
|
Dashboard API: Test dashboard export not allowed
|
2020-01-15 13:09:55 -05:00
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin_id], published=False)
|
|
|
|
|
|
|
|
self.login(username="gamma")
|
|
|
|
argument = [dashboard.id]
|
|
|
|
uri = f"api/v1/dashboard/export/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
self.assertEqual(rv.status_code, 404)
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
2020-10-22 23:02:40 -04:00
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
"superset.extensions.feature_flag_manager._feature_flags",
|
|
|
|
{"VERSIONED_EXPORT": True},
|
|
|
|
clear=True,
|
|
|
|
)
|
|
|
|
def test_export_bundle(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test dashboard export
|
|
|
|
"""
|
|
|
|
argument = [1, 2]
|
|
|
|
uri = f"api/v1/dashboard/export/?q={prison.dumps(argument)}"
|
|
|
|
|
|
|
|
self.login(username="admin")
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
|
|
|
|
assert rv.status_code == 200
|
|
|
|
|
|
|
|
buf = BytesIO(rv.data)
|
|
|
|
assert is_zipfile(buf)
|
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
"superset.extensions.feature_flag_manager._feature_flags",
|
|
|
|
{"VERSIONED_EXPORT": True},
|
|
|
|
clear=True,
|
|
|
|
)
|
|
|
|
def test_export_bundle_not_found(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test dashboard export not found
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
argument = [1000]
|
|
|
|
uri = f"api/v1/dashboard/export/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
assert rv.status_code == 404
|
|
|
|
|
|
|
|
@patch.dict(
|
|
|
|
"superset.extensions.feature_flag_manager._feature_flags",
|
|
|
|
{"VERSIONED_EXPORT": True},
|
|
|
|
clear=True,
|
|
|
|
)
|
|
|
|
def test_export_bundle_not_allowed(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test dashboard export not allowed
|
|
|
|
"""
|
|
|
|
admin_id = self.get_user("admin").id
|
|
|
|
dashboard = self.insert_dashboard("title", "slug1", [admin_id], published=False)
|
|
|
|
|
|
|
|
self.login(username="gamma")
|
|
|
|
argument = [dashboard.id]
|
|
|
|
uri = f"api/v1/dashboard/export/?q={prison.dumps(argument)}"
|
|
|
|
rv = self.client.get(uri)
|
|
|
|
assert rv.status_code == 404
|
|
|
|
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.commit()
|
2020-11-25 01:45:35 -05:00
|
|
|
|
|
|
|
def test_import_dashboard(self):
|
|
|
|
"""
|
|
|
|
Dashboard API: Test import dashboard
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = "api/v1/dashboard/import/"
|
|
|
|
|
|
|
|
buf = BytesIO()
|
|
|
|
with ZipFile(buf, "w") as bundle:
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open("dashboard_export/metadata.yaml", "w") as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(dashboard_metadata_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open(
|
|
|
|
"dashboard_export/databases/imported_database.yaml", "w"
|
|
|
|
) as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(database_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open(
|
|
|
|
"dashboard_export/datasets/imported_dataset.yaml", "w"
|
|
|
|
) as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(dataset_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open("dashboard_export/charts/imported_chart.yaml", "w") as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(chart_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open(
|
|
|
|
"dashboard_export/dashboards/imported_dashboard.yaml", "w"
|
|
|
|
) as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(dashboard_config).encode())
|
|
|
|
buf.seek(0)
|
|
|
|
|
|
|
|
form_data = {
|
2020-11-25 14:47:48 -05:00
|
|
|
"formData": (buf, "dashboard_export.zip"),
|
2020-11-25 01:45:35 -05:00
|
|
|
}
|
|
|
|
rv = self.client.post(uri, data=form_data, content_type="multipart/form-data")
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
|
|
|
|
assert rv.status_code == 200
|
|
|
|
assert response == {"message": "OK"}
|
|
|
|
|
|
|
|
dashboard = (
|
|
|
|
db.session.query(Dashboard).filter_by(uuid=dashboard_config["uuid"]).one()
|
|
|
|
)
|
|
|
|
assert dashboard.dashboard_title == "Test dash"
|
|
|
|
|
|
|
|
assert len(dashboard.slices) == 1
|
|
|
|
chart = dashboard.slices[0]
|
|
|
|
assert str(chart.uuid) == chart_config["uuid"]
|
|
|
|
|
|
|
|
dataset = chart.table
|
|
|
|
assert str(dataset.uuid) == dataset_config["uuid"]
|
|
|
|
|
|
|
|
database = dataset.database
|
|
|
|
assert str(database.uuid) == database_config["uuid"]
|
|
|
|
|
|
|
|
db.session.delete(dashboard)
|
|
|
|
db.session.delete(chart)
|
|
|
|
db.session.delete(dataset)
|
|
|
|
db.session.delete(database)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
def test_import_dashboard_invalid(self):
|
|
|
|
"""
|
|
|
|
Dataset API: Test import invalid dashboard
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
uri = "api/v1/dashboard/import/"
|
|
|
|
|
|
|
|
buf = BytesIO()
|
|
|
|
with ZipFile(buf, "w") as bundle:
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open("dashboard_export/metadata.yaml", "w") as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(dataset_metadata_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open(
|
|
|
|
"dashboard_export/databases/imported_database.yaml", "w"
|
|
|
|
) as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(database_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open(
|
|
|
|
"dashboard_export/datasets/imported_dataset.yaml", "w"
|
|
|
|
) as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(dataset_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open("dashboard_export/charts/imported_chart.yaml", "w") as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(chart_config).encode())
|
2020-11-25 14:47:48 -05:00
|
|
|
with bundle.open(
|
|
|
|
"dashboard_export/dashboards/imported_dashboard.yaml", "w"
|
|
|
|
) as fp:
|
2020-11-25 01:45:35 -05:00
|
|
|
fp.write(yaml.safe_dump(dashboard_config).encode())
|
|
|
|
buf.seek(0)
|
|
|
|
|
|
|
|
form_data = {
|
2020-11-25 14:47:48 -05:00
|
|
|
"formData": (buf, "dashboard_export.zip"),
|
2020-11-25 01:45:35 -05:00
|
|
|
}
|
|
|
|
rv = self.client.post(uri, data=form_data, content_type="multipart/form-data")
|
|
|
|
response = json.loads(rv.data.decode("utf-8"))
|
|
|
|
|
|
|
|
assert rv.status_code == 422
|
|
|
|
assert response == {
|
|
|
|
"message": {"metadata.yaml": {"type": ["Must be equal to Dashboard."]}}
|
|
|
|
}
|