mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
fix: Refactor ownership checks and ensure consistency (#20499)
Co-authored-by: John Bodley <john.bodley@airbnb.com>
This commit is contained in:
@@ -70,10 +70,11 @@ class TestCreateDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
def test_create_duplicate_error(self, mock_logger):
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_create_duplicate_error(self, mock_g, mock_logger):
|
||||
example_db = get_example_database()
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
command = CreateDatabaseCommand(
|
||||
security_manager.find_user("admin"),
|
||||
{"database_name": example_db.database_name},
|
||||
)
|
||||
with pytest.raises(DatabaseInvalidError) as excinfo:
|
||||
@@ -90,8 +91,10 @@ class TestCreateDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
def test_multiple_error_logging(self, mock_logger):
|
||||
command = CreateDatabaseCommand(security_manager.find_user("admin"), {})
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_multiple_error_logging(self, mock_g, mock_logger):
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
command = CreateDatabaseCommand({})
|
||||
with pytest.raises(DatabaseInvalidError) as excinfo:
|
||||
command.run()
|
||||
assert str(excinfo.value) == ("Database parameters are invalid.")
|
||||
@@ -643,15 +646,17 @@ class TestTestConnectionDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
def test_connection_db_exception(self, mock_event_logger, mock_get_sqla_engine):
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_connection_db_exception(
|
||||
self, mock_g, mock_event_logger, mock_get_sqla_engine
|
||||
):
|
||||
"""Test to make sure event_logger is called when an exception is raised"""
|
||||
database = get_example_database()
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
mock_get_sqla_engine.side_effect = Exception("An error has occurred!")
|
||||
db_uri = database.sqlalchemy_uri_decrypted
|
||||
json_payload = {"sqlalchemy_uri": db_uri}
|
||||
command_without_db_name = TestConnectionDatabaseCommand(
|
||||
security_manager.find_user("admin"), json_payload
|
||||
)
|
||||
command_without_db_name = TestConnectionDatabaseCommand(json_payload)
|
||||
|
||||
with pytest.raises(DatabaseTestConnectionUnexpectedError) as excinfo:
|
||||
command_without_db_name.run()
|
||||
@@ -664,19 +669,19 @@ class TestTestConnectionDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_connection_do_ping_exception(
|
||||
self, mock_event_logger, mock_get_sqla_engine
|
||||
self, mock_g, mock_event_logger, mock_get_sqla_engine
|
||||
):
|
||||
"""Test to make sure do_ping exceptions gets captured"""
|
||||
database = get_example_database()
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
mock_get_sqla_engine.return_value.dialect.do_ping.side_effect = Exception(
|
||||
"An error has occurred!"
|
||||
)
|
||||
db_uri = database.sqlalchemy_uri_decrypted
|
||||
json_payload = {"sqlalchemy_uri": db_uri}
|
||||
command_without_db_name = TestConnectionDatabaseCommand(
|
||||
security_manager.find_user("admin"), json_payload
|
||||
)
|
||||
command_without_db_name = TestConnectionDatabaseCommand(json_payload)
|
||||
|
||||
with pytest.raises(DatabaseTestConnectionFailedError) as excinfo:
|
||||
command_without_db_name.run()
|
||||
@@ -689,15 +694,17 @@ class TestTestConnectionDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
def test_connection_do_ping_timeout(self, mock_event_logger, mock_func_timeout):
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_connection_do_ping_timeout(
|
||||
self, mock_g, mock_event_logger, mock_func_timeout
|
||||
):
|
||||
"""Test to make sure do_ping exceptions gets captured"""
|
||||
database = get_example_database()
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
mock_func_timeout.side_effect = FunctionTimedOut("Time out")
|
||||
db_uri = database.sqlalchemy_uri_decrypted
|
||||
json_payload = {"sqlalchemy_uri": db_uri}
|
||||
command_without_db_name = TestConnectionDatabaseCommand(
|
||||
security_manager.find_user("admin"), json_payload
|
||||
)
|
||||
command_without_db_name = TestConnectionDatabaseCommand(json_payload)
|
||||
|
||||
with pytest.raises(SupersetTimeoutException) as excinfo:
|
||||
command_without_db_name.run()
|
||||
@@ -711,20 +718,20 @@ class TestTestConnectionDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_connection_superset_security_connection(
|
||||
self, mock_event_logger, mock_get_sqla_engine
|
||||
self, mock_g, mock_event_logger, mock_get_sqla_engine
|
||||
):
|
||||
"""Test to make sure event_logger is called when security
|
||||
connection exc is raised"""
|
||||
database = get_example_database()
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
mock_get_sqla_engine.side_effect = SupersetSecurityException(
|
||||
SupersetError(error_type=500, message="test", level="info")
|
||||
)
|
||||
db_uri = database.sqlalchemy_uri_decrypted
|
||||
json_payload = {"sqlalchemy_uri": db_uri}
|
||||
command_without_db_name = TestConnectionDatabaseCommand(
|
||||
security_manager.find_user("admin"), json_payload
|
||||
)
|
||||
command_without_db_name = TestConnectionDatabaseCommand(json_payload)
|
||||
|
||||
with pytest.raises(DatabaseSecurityUnsafeError) as excinfo:
|
||||
command_without_db_name.run()
|
||||
@@ -736,17 +743,19 @@ class TestTestConnectionDatabaseCommand(SupersetTestCase):
|
||||
@mock.patch(
|
||||
"superset.databases.commands.test_connection.event_logger.log_with_context"
|
||||
)
|
||||
def test_connection_db_api_exc(self, mock_event_logger, mock_get_sqla_engine):
|
||||
@mock.patch("superset.utils.core.g")
|
||||
def test_connection_db_api_exc(
|
||||
self, mock_g, mock_event_logger, mock_get_sqla_engine
|
||||
):
|
||||
"""Test to make sure event_logger is called when DBAPIError is raised"""
|
||||
database = get_example_database()
|
||||
mock_g.user = security_manager.find_user("admin")
|
||||
mock_get_sqla_engine.side_effect = DBAPIError(
|
||||
statement="error", params={}, orig={}
|
||||
)
|
||||
db_uri = database.sqlalchemy_uri_decrypted
|
||||
json_payload = {"sqlalchemy_uri": db_uri}
|
||||
command_without_db_name = TestConnectionDatabaseCommand(
|
||||
security_manager.find_user("admin"), json_payload
|
||||
)
|
||||
command_without_db_name = TestConnectionDatabaseCommand(json_payload)
|
||||
|
||||
with pytest.raises(DatabaseTestConnectionFailedError) as excinfo:
|
||||
command_without_db_name.run()
|
||||
@@ -778,7 +787,7 @@ def test_validate(DatabaseDAO, is_port_open, is_hostname_valid, app_context):
|
||||
"query": {},
|
||||
},
|
||||
}
|
||||
command = ValidateDatabaseParametersCommand(None, payload)
|
||||
command = ValidateDatabaseParametersCommand(payload)
|
||||
command.run()
|
||||
|
||||
|
||||
@@ -802,7 +811,7 @@ def test_validate_partial(is_port_open, is_hostname_valid, app_context):
|
||||
"query": {},
|
||||
},
|
||||
}
|
||||
command = ValidateDatabaseParametersCommand(None, payload)
|
||||
command = ValidateDatabaseParametersCommand(payload)
|
||||
with pytest.raises(SupersetErrorsException) as excinfo:
|
||||
command.run()
|
||||
assert excinfo.value.errors == [
|
||||
@@ -841,7 +850,7 @@ def test_validate_partial_invalid_hostname(is_hostname_valid, app_context):
|
||||
"query": {},
|
||||
},
|
||||
}
|
||||
command = ValidateDatabaseParametersCommand(None, payload)
|
||||
command = ValidateDatabaseParametersCommand(payload)
|
||||
with pytest.raises(SupersetErrorsException) as excinfo:
|
||||
command.run()
|
||||
assert excinfo.value.errors == [
|
||||
|
||||
Reference in New Issue
Block a user