fix: set importer as owner (#16656)

* fix: set importer as owner

* Fix tests
This commit is contained in:
Beto Dealmeida 2021-09-15 12:27:02 -07:00 committed by GitHub
parent 86290cc0e5
commit 092ef5bdfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 5 deletions

View File

@ -18,6 +18,7 @@
import json
from typing import Any, Dict
from flask import g
from sqlalchemy.orm import Session
from superset.models.slice import Slice
@ -39,4 +40,7 @@ def import_chart(
if chart.id is None:
session.flush()
if hasattr(g, "user") and g.user:
chart.owners.append(g.user)
return chart

View File

@ -19,6 +19,7 @@ import json
import logging
from typing import Any, Dict, Set
from flask import g
from sqlalchemy.orm import Session
from superset.models.dashboard import Dashboard
@ -155,4 +156,7 @@ def import_dashboard(
if dashboard.id is None:
session.flush()
if hasattr(g, "user") and g.user:
dashboard.owners.append(g.user)
return dashboard

View File

@ -22,7 +22,7 @@ from typing import Any, Dict
from urllib import request
import pandas as pd
from flask import current_app
from flask import current_app, g
from sqlalchemy import BigInteger, Boolean, Date, DateTime, Float, String, Text
from sqlalchemy.orm import Session
from sqlalchemy.sql.visitors import VisitableType
@ -127,6 +127,9 @@ def import_dataset(
if data_uri and (not table_exists or force_data):
load_data(data_uri, dataset, example_database, session)
if hasattr(g, "user") and g.user:
dataset.owners.append(g.user)
return dataset

View File

@ -1715,6 +1715,9 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin, InsertChartMixin):
chart = db.session.query(Slice).filter_by(uuid=chart_config["uuid"]).one()
assert chart.table == dataset
chart.owners = []
dataset.owners = []
database.owners = []
db.session.delete(chart)
db.session.delete(dataset)
db.session.delete(database)
@ -1784,6 +1787,9 @@ class TestChartApi(SupersetTestCase, ApiOwnersTestCaseMixin, InsertChartMixin):
dataset = database.tables[0]
chart = db.session.query(Slice).filter_by(uuid=chart_config["uuid"]).one()
chart.owners = []
dataset.owners = []
database.owners = []
db.session.delete(chart)
db.session.delete(dataset)
db.session.delete(database)

View File

@ -134,8 +134,10 @@ class TestExportChartsCommand(SupersetTestCase):
class TestImportChartsCommand(SupersetTestCase):
def test_import_v1_chart(self):
@patch("superset.charts.commands.importers.v1.utils.g")
def test_import_v1_chart(self, mock_g):
"""Test that we can import a chart"""
mock_g.user = security_manager.find_user("admin")
contents = {
"metadata.yaml": yaml.safe_dump(chart_metadata_config),
"databases/imported_database.yaml": yaml.safe_dump(database_config),
@ -224,6 +226,11 @@ class TestImportChartsCommand(SupersetTestCase):
assert database.database_name == "imported_database"
assert chart.table.database == database
assert chart.owners == [mock_g.user]
chart.owners = []
dataset.owners = []
database.owners = []
db.session.delete(chart)
db.session.delete(dataset)
db.session.delete(database)
@ -332,7 +339,7 @@ class TestChartsUpdateCommand(SupersetTestCase):
@patch("superset.security.manager.g")
@pytest.mark.usefixtures("load_energy_table_with_slice")
def test_query_context_update_command(self, mock_sm_g, mock_g):
""" "
"""
Test that a user can generate the chart query context
payloadwithout affecting owners
"""

View File

@ -463,8 +463,10 @@ class TestImportDashboardsCommand(SupersetTestCase):
db.session.delete(dataset)
db.session.commit()
def test_import_v1_dashboard(self):
@patch("superset.dashboards.commands.importers.v1.utils.g")
def test_import_v1_dashboard(self, mock_g):
"""Test that we can import a dashboard"""
mock_g.user = security_manager.find_user("admin")
contents = {
"metadata.yaml": yaml.safe_dump(dashboard_metadata_config),
"databases/imported_database.yaml": yaml.safe_dump(database_config),
@ -544,6 +546,12 @@ class TestImportDashboardsCommand(SupersetTestCase):
database = dataset.database
assert str(database.uuid) == database_config["uuid"]
assert dashboard.owners == [mock_g.user]
dashboard.owners = []
chart.owners = []
dataset.owners = []
database.owners = []
db.session.delete(dashboard)
db.session.delete(chart)
db.session.delete(dataset)

View File

@ -1188,6 +1188,8 @@ class TestDatabaseApi(SupersetTestCase):
assert dataset.table_name == "imported_dataset"
assert str(dataset.uuid) == dataset_config["uuid"]
dataset.owners = []
database.owners = []
db.session.delete(dataset)
db.session.delete(database)
db.session.commit()
@ -1257,6 +1259,8 @@ class TestDatabaseApi(SupersetTestCase):
db.session.query(Database).filter_by(uuid=database_config["uuid"]).one()
)
dataset = database.tables[0]
dataset.owners = []
database.owners = []
db.session.delete(dataset)
db.session.delete(database)
db.session.commit()

View File

@ -1534,6 +1534,8 @@ class TestDatasetApi(SupersetTestCase):
assert dataset.table_name == "imported_dataset"
assert str(dataset.uuid) == dataset_config["uuid"]
dataset.owners = []
database.owners = []
db.session.delete(dataset)
db.session.delete(database)
db.session.commit()
@ -1626,6 +1628,8 @@ class TestDatasetApi(SupersetTestCase):
)
dataset = database.tables[0]
dataset.owners = []
database.owners = []
db.session.delete(dataset)
db.session.delete(database)
db.session.commit()

View File

@ -292,8 +292,11 @@ class TestImportDatasetsCommand(SupersetTestCase):
db.session.delete(dataset)
db.session.commit()
def test_import_v1_dataset(self):
@patch("superset.datasets.commands.importers.v1.utils.g")
@pytest.mark.usefixtures("load_energy_table_with_slice")
def test_import_v1_dataset(self, mock_g):
"""Test that we can import a dataset"""
mock_g.user = security_manager.find_user("admin")
contents = {
"metadata.yaml": yaml.safe_dump(dataset_metadata_config),
"databases/imported_database.yaml": yaml.safe_dump(database_config),
@ -319,6 +322,9 @@ class TestImportDatasetsCommand(SupersetTestCase):
assert dataset.fetch_values_predicate is None
assert dataset.extra == "dttm > sysdate() -10 "
# user should be included as one of the owners
assert dataset.owners == [mock_g.user]
# database is also imported
assert str(dataset.database.uuid) == "b8a1ccd3-779d-4ab7-8ad8-9ab119d7fe89"
@ -346,6 +352,8 @@ class TestImportDatasetsCommand(SupersetTestCase):
assert column.description is None
assert column.python_date_format is None
dataset.owners = []
dataset.database.owners = []
db.session.delete(dataset)
db.session.delete(dataset.database)
db.session.commit()
@ -467,6 +475,8 @@ class TestImportDatasetsCommand(SupersetTestCase):
)
assert len(database.tables) == 1
database.tables[0].owners = []
database.owners = []
db.session.delete(database.tables[0])
db.session.delete(database)
db.session.commit()