Skip to content

Commit

Permalink
tests: added fixtures to created dashboards and updated test with pro…
Browse files Browse the repository at this point in the history
…per value (#11290)

* 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.

* Changed number of dashboards in database api tests after cleanup of dashboards in dashboards_tests
  • Loading branch information
kkucharc authored Oct 19, 2020
1 parent 55c2892 commit 0e97c4f
Showing 3 changed files with 69 additions and 35 deletions.
99 changes: 67 additions & 32 deletions tests/dashboard_tests.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion tests/databases/api_tests.py
Original file line number Diff line number Diff line change
@@ -784,7 +784,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):
"""
3 changes: 1 addition & 2 deletions tests/datasets/api_tests.py
Original file line number Diff line number Diff line change
@@ -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,
@@ -1019,7 +1018,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):
"""

0 comments on commit 0e97c4f

Please sign in to comment.