mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
feat: export dashboards as ZIP files (#11351)
* Export datasets as ZIP files * Add logging when failing to parse extra * Export datasets as ZIP files * Export charts as Zip file * Export dashboards as a Zip file * Add logging
This commit is contained in:
@@ -17,7 +17,10 @@
|
||||
# isort:skip_file
|
||||
"""Unit tests for Superset"""
|
||||
import json
|
||||
from io import BytesIO
|
||||
from typing import List, Optional
|
||||
from unittest.mock import patch
|
||||
from zipfile import is_zipfile
|
||||
|
||||
import pytest
|
||||
import prison
|
||||
@@ -989,3 +992,59 @@ class TestDashboardApi(SupersetTestCase, ApiOwnersTestCaseMixin):
|
||||
self.assertEqual(rv.status_code, 404)
|
||||
db.session.delete(dashboard)
|
||||
db.session.commit()
|
||||
|
||||
@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()
|
||||
|
||||
190
tests/dashboards/commands_tests.py
Normal file
190
tests/dashboards/commands_tests.py
Normal file
@@ -0,0 +1,190 @@
|
||||
# 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.
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import yaml
|
||||
|
||||
from superset import db, security_manager
|
||||
from superset.dashboards.commands.exceptions import DashboardNotFoundError
|
||||
from superset.dashboards.commands.export import ExportDashboardsCommand
|
||||
from superset.models.dashboard import Dashboard
|
||||
from tests.base_tests import SupersetTestCase
|
||||
|
||||
|
||||
class TestExportDashboardsCommand(SupersetTestCase):
|
||||
@patch("superset.security.manager.g")
|
||||
@patch("superset.views.base.g")
|
||||
def test_export_dashboard_command(self, mock_g1, mock_g2):
|
||||
mock_g1.user = security_manager.find_user("admin")
|
||||
mock_g2.user = security_manager.find_user("admin")
|
||||
|
||||
example_dashboard = db.session.query(Dashboard).filter_by(id=1).one()
|
||||
command = ExportDashboardsCommand(dashboard_ids=[example_dashboard.id])
|
||||
contents = dict(command.run())
|
||||
|
||||
expected_paths = {
|
||||
"dashboards/world_banks_data.yaml",
|
||||
"charts/box_plot.yaml",
|
||||
"datasets/examples/wb_health_population.yaml",
|
||||
"databases/examples.yaml",
|
||||
"charts/treemap.yaml",
|
||||
"charts/region_filter.yaml",
|
||||
"charts/_rural.yaml",
|
||||
"charts/worlds_population.yaml",
|
||||
"charts/most_populated_countries.yaml",
|
||||
"charts/growth_rate.yaml",
|
||||
"charts/life_expectancy_vs_rural_.yaml",
|
||||
"charts/rural_breakdown.yaml",
|
||||
"charts/worlds_pop_growth.yaml",
|
||||
}
|
||||
|
||||
assert expected_paths == set(contents.keys())
|
||||
|
||||
metadata = yaml.safe_load(contents["dashboards/world_banks_data.yaml"])
|
||||
|
||||
# remove chart UUIDs from metadata so we can compare
|
||||
for chart_info in metadata["position"].values():
|
||||
if isinstance(chart_info, dict) and "uuid" in chart_info.get("meta", {}):
|
||||
del chart_info["meta"]["chartId"]
|
||||
del chart_info["meta"]["uuid"]
|
||||
|
||||
assert metadata == {
|
||||
"dashboard_title": "World Bank's Data",
|
||||
"description": None,
|
||||
"css": "",
|
||||
"slug": "world_health",
|
||||
"uuid": str(example_dashboard.uuid),
|
||||
"position": {
|
||||
"DASHBOARD_CHART_TYPE-0": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-0",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-1": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-1",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-2": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-2",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-3": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-3",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-4": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-4",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-5": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-5",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-6": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-6",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-7": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-7",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-8": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-8",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_CHART_TYPE-9": {
|
||||
"children": [],
|
||||
"id": "DASHBOARD_CHART_TYPE-9",
|
||||
"meta": {"height": 50, "width": 4},
|
||||
"type": "CHART",
|
||||
},
|
||||
"DASHBOARD_VERSION_KEY": "v2",
|
||||
},
|
||||
"metadata": {
|
||||
"timed_refresh_immune_slices": [],
|
||||
"expanded_slices": {},
|
||||
"refresh_frequency": 0,
|
||||
"default_filters": "{}",
|
||||
"color_scheme": None,
|
||||
},
|
||||
"version": "1.0.0",
|
||||
}
|
||||
|
||||
@patch("superset.security.manager.g")
|
||||
@patch("superset.views.base.g")
|
||||
def test_export_dashboard_command_no_access(self, mock_g1, mock_g2):
|
||||
"""Test that users can't export datasets they don't have access to"""
|
||||
mock_g1.user = security_manager.find_user("gamma")
|
||||
mock_g2.user = security_manager.find_user("gamma")
|
||||
|
||||
example_dashboard = db.session.query(Dashboard).filter_by(id=1).one()
|
||||
command = ExportDashboardsCommand(dashboard_ids=[example_dashboard.id])
|
||||
contents = command.run()
|
||||
with self.assertRaises(DashboardNotFoundError):
|
||||
next(contents)
|
||||
|
||||
@patch("superset.security.manager.g")
|
||||
@patch("superset.views.base.g")
|
||||
def test_export_dashboard_command_invalid_dataset(self, mock_g1, mock_g2):
|
||||
"""Test that an error is raised when exporting an invalid dataset"""
|
||||
mock_g1.user = security_manager.find_user("admin")
|
||||
mock_g2.user = security_manager.find_user("admin")
|
||||
command = ExportDashboardsCommand(dashboard_ids=[-1])
|
||||
contents = command.run()
|
||||
with self.assertRaises(DashboardNotFoundError):
|
||||
next(contents)
|
||||
|
||||
@patch("superset.security.manager.g")
|
||||
@patch("superset.views.base.g")
|
||||
def test_export_dashboard_command_key_order(self, mock_g1, mock_g2):
|
||||
"""Test that they keys in the YAML have the same order as export_fields"""
|
||||
mock_g1.user = security_manager.find_user("admin")
|
||||
mock_g2.user = security_manager.find_user("admin")
|
||||
|
||||
example_dashboard = db.session.query(Dashboard).filter_by(id=1).one()
|
||||
command = ExportDashboardsCommand(dashboard_ids=[example_dashboard.id])
|
||||
contents = dict(command.run())
|
||||
|
||||
metadata = yaml.safe_load(contents["dashboards/world_banks_data.yaml"])
|
||||
assert list(metadata.keys()) == [
|
||||
"dashboard_title",
|
||||
"description",
|
||||
"css",
|
||||
"slug",
|
||||
"uuid",
|
||||
"position",
|
||||
"metadata",
|
||||
"version",
|
||||
]
|
||||
Reference in New Issue
Block a user