tests: added fixtures to created dashboards and updated test with proper 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
This commit is contained in:
Kasia Kucharczyk 2020-10-20 00:07:26 +02:00 committed by GitHub
parent 55c2892e9b
commit 0e97c4f66c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 35 deletions

View File

@ -21,10 +21,12 @@ import json
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 @@ from .base_tests import SupersetTestCase
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 @@ class TestDashboard(SupersetTestCase):
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 @@ class TestDashboard(SupersetTestCase):
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")

View File

@ -784,7 +784,7 @@ class TestDatabaseApi(SupersetTestCase):
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):
"""

View File

@ -24,7 +24,6 @@ import pytest
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 @@ class TestDatasetApi(SupersetTestCase):
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):
"""