From 17d1c4dbf38ad6b9b6b0fab25db118cc8c351e4c Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Thu, 15 Oct 2020 20:26:13 +0200 Subject: [PATCH 1/2] Added fixtures for hidden and published dashboards. Added fixture to restore copied dashboard in dashboard tests. Changed number of dashboards in datasets/api_tests.py because copied dashboard is removed. --- tests/dashboard_tests.py | 99 +++++++++++++++++++++++++------------ tests/datasets/api_tests.py | 3 +- 2 files changed, 68 insertions(+), 34 deletions(-) diff --git a/tests/dashboard_tests.py b/tests/dashboard_tests.py index 3888cf359d350..56119c9e77fc4 100644 --- a/tests/dashboard_tests.py +++ b/tests/dashboard_tests.py @@ -21,10 +21,12 @@ import unittest from random import random +import pytest from flask import escape, url_for from sqlalchemy import func -import tests.test_app +from tests.fixtures.unicode_dashboard import load_unicode_dashboard_with_position +from tests.test_app import app from superset import db, security_manager from superset.connectors.sqla.models import SqlaTable from superset.models import core as models @@ -35,6 +37,64 @@ class TestDashboard(SupersetTestCase): + @pytest.fixture + def cleanup_copied_dash(self): + with app.app_context(): + original_dashboard = ( + db.session.query(Dashboard).filter_by(slug="births").first() + ) + original_dashboard_id = original_dashboard.id + yield + copied_dashboard = ( + db.session.query(Dashboard) + .filter( + Dashboard.dashboard_title == "Copy Of Births", + Dashboard.id != original_dashboard_id, + ) + .first() + ) + + db.session.merge(original_dashboard) + if copied_dashboard: + db.session.delete(copied_dashboard) + db.session.commit() + + @pytest.fixture + def load_dashboard(self): + with app.app_context(): + table = ( + db.session.query(SqlaTable).filter_by(table_name="energy_usage").one() + ) + # get a slice from the allowed table + slice = db.session.query(Slice).filter_by(slice_name="Energy Sankey").one() + + self.grant_public_access_to_table(table) + + pytest.hidden_dash_slug = f"hidden_dash_{random()}" + pytest.published_dash_slug = f"published_dash_{random()}" + + # Create a published and hidden dashboard and add them to the database + published_dash = Dashboard() + published_dash.dashboard_title = "Published Dashboard" + published_dash.slug = pytest.published_dash_slug + published_dash.slices = [slice] + published_dash.published = True + + hidden_dash = Dashboard() + hidden_dash.dashboard_title = "Hidden Dashboard" + hidden_dash.slug = pytest.hidden_dash_slug + hidden_dash.slices = [slice] + hidden_dash.published = False + + db.session.merge(published_dash) + db.session.merge(hidden_dash) + yield db.session.commit() + + self.revoke_public_access_to_table(table) + db.session.delete(published_dash) + db.session.delete(hidden_dash) + db.session.commit() + def get_mock_positions(self, dash): positions = {"DASHBOARD_VERSION_KEY": "v2"} for i, slc in enumerate(dash.slices): @@ -200,6 +260,9 @@ def test_save_dash_with_colors(self, username="admin"): del data["label_colors"] self.get_resp(url, data=dict(data=json.dumps(data))) + @pytest.mark.usefixtures( + "cleanup_copied_dash", "load_unicode_dashboard_with_position" + ) def test_copy_dash(self, username="admin"): self.login(username=username) dash = db.session.query(Dashboard).filter_by(slug="births").first() @@ -389,39 +452,11 @@ def test_owners_can_view_empty_dashboard(self): resp = self.get_resp("/api/v1/dashboard/") self.assertNotIn("/superset/dashboard/empty_dashboard/", resp) + @pytest.mark.usefixtures("load_dashboard") def test_users_can_view_published_dashboard(self): - table = db.session.query(SqlaTable).filter_by(table_name="energy_usage").one() - # get a slice from the allowed table - slice = db.session.query(Slice).filter_by(slice_name="Energy Sankey").one() - - self.grant_public_access_to_table(table) - - hidden_dash_slug = f"hidden_dash_{random()}" - published_dash_slug = f"published_dash_{random()}" - - # Create a published and hidden dashboard and add them to the database - published_dash = Dashboard() - published_dash.dashboard_title = "Published Dashboard" - published_dash.slug = published_dash_slug - published_dash.slices = [slice] - published_dash.published = True - - hidden_dash = Dashboard() - hidden_dash.dashboard_title = "Hidden Dashboard" - hidden_dash.slug = hidden_dash_slug - hidden_dash.slices = [slice] - hidden_dash.published = False - - db.session.merge(published_dash) - db.session.merge(hidden_dash) - db.session.commit() - resp = self.get_resp("/api/v1/dashboard/") - self.assertNotIn(f"/superset/dashboard/{hidden_dash_slug}/", resp) - self.assertIn(f"/superset/dashboard/{published_dash_slug}/", resp) - - # Cleanup - self.revoke_public_access_to_table(table) + self.assertNotIn(f"/superset/dashboard/{pytest.hidden_dash_slug}/", resp) + self.assertIn(f"/superset/dashboard/{pytest.published_dash_slug}/", resp) def test_users_can_view_own_dashboard(self): user = security_manager.find_user("gamma") diff --git a/tests/datasets/api_tests.py b/tests/datasets/api_tests.py index a2ff652fa5a1c..53265cfc37eae 100644 --- a/tests/datasets/api_tests.py +++ b/tests/datasets/api_tests.py @@ -24,7 +24,6 @@ import yaml from sqlalchemy.sql import func -import tests.test_app from superset.connectors.sqla.models import SqlaTable, SqlMetric, TableColumn from superset.dao.exceptions import ( DAOCreateFailedError, @@ -1020,7 +1019,7 @@ def test_get_dataset_related_objects(self): self.assertEqual(rv.status_code, 200) response = json.loads(rv.data.decode("utf-8")) self.assertEqual(response["charts"]["count"], 18) - self.assertEqual(response["dashboards"]["count"], 2) + self.assertEqual(response["dashboards"]["count"], 1) def test_get_dataset_related_objects_not_found(self): """ From d44aaf8ce649d8e905c64408ae1571dadb1765f4 Mon Sep 17 00:00:00 2001 From: Kasia Kucharczyk Date: Thu, 15 Oct 2020 20:35:35 +0200 Subject: [PATCH 2/2] Changed number of dashboards in database api tests after cleanup of dashboards in dashboards_tests --- tests/databases/api_tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index a5ce0b408021f..b04640a18c54f 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -782,7 +782,7 @@ def test_get_database_related_objects(self): self.assertEqual(rv.status_code, 200) response = json.loads(rv.data.decode("utf-8")) self.assertEqual(response["charts"]["count"], 33) - self.assertEqual(response["dashboards"]["count"], 6) + self.assertEqual(response["dashboards"]["count"], 3) def test_get_database_related_objects_not_found(self): """