Skip to content

Commit

Permalink
fix: set importer as owner (#16656)
Browse files Browse the repository at this point in the history
* fix: set importer as owner

* Fix tests
  • Loading branch information
betodealmeida authored Sep 15, 2021
1 parent 86290cc commit 092ef5b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions superset/charts/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 4 additions & 0 deletions superset/dashboards/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import logging
from typing import Any, Dict, Set

from flask import g
from sqlalchemy.orm import Session

from superset.models.dashboard import Dashboard
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion superset/datasets/commands/importers/v1/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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
Expand Down Expand Up @@ -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


Expand Down
6 changes: 6 additions & 0 deletions tests/integration_tests/charts/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,9 @@ def test_import_chart(self):
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)
Expand Down Expand Up @@ -1784,6 +1787,9 @@ def test_import_chart_overwrite(self):
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)
Expand Down
11 changes: 9 additions & 2 deletions tests/integration_tests/charts/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ def test_export_chart_command_key_order(self, mock_g):


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),
Expand Down Expand Up @@ -224,6 +226,11 @@ def test_import_v1_chart(self):
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)
Expand Down Expand Up @@ -332,7 +339,7 @@ def test_update_v1_response(self, mock_sm_g, mock_g):
@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
"""
Expand Down
10 changes: 9 additions & 1 deletion tests/integration_tests/dashboards/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ def test_import_v0_dashboard_cli_export(self):
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),
Expand Down Expand Up @@ -544,6 +546,12 @@ def test_import_v1_dashboard(self):
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)
Expand Down
4 changes: 4 additions & 0 deletions tests/integration_tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,8 @@ def test_import_database(self):
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()
Expand Down Expand Up @@ -1257,6 +1259,8 @@ def test_import_database_overwrite(self):
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()
Expand Down
4 changes: 4 additions & 0 deletions tests/integration_tests/datasets/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,8 @@ def test_import_dataset(self):
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()
Expand Down Expand Up @@ -1626,6 +1628,8 @@ def test_import_dataset_overwrite(self):
)
dataset = database.tables[0]

dataset.owners = []
database.owners = []
db.session.delete(dataset)
db.session.delete(database)
db.session.commit()
Expand Down
12 changes: 11 additions & 1 deletion tests/integration_tests/datasets/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,11 @@ def test_import_v0_dataset_ui_export(self):
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),
Expand All @@ -319,6 +322,9 @@ def test_import_v1_dataset(self):
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"

Expand Down Expand Up @@ -346,6 +352,8 @@ def test_import_v1_dataset(self):
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()
Expand Down Expand Up @@ -467,6 +475,8 @@ def test_import_v1_dataset_existing_database(self):
)
assert len(database.tables) == 1

database.tables[0].owners = []
database.owners = []
db.session.delete(database.tables[0])
db.session.delete(database)
db.session.commit()
Expand Down

0 comments on commit 092ef5b

Please sign in to comment.