From 751c9192d1736f24b432ac7e22d6d906aab76fd4 Mon Sep 17 00:00:00 2001 From: Lily Kuang Date: Wed, 9 Sep 2020 19:22:47 -0700 Subject: [PATCH 1/3] fix test connection with extra --- superset/databases/commands/test_connection.py | 4 ++-- superset/databases/schemas.py | 4 +++- tests/databases/api_tests.py | 11 ++++++++++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/superset/databases/commands/test_connection.py b/superset/databases/commands/test_connection.py index 3bcd5b09237cc..0fbfb313144b6 100644 --- a/superset/databases/commands/test_connection.py +++ b/superset/databases/commands/test_connection.py @@ -46,9 +46,9 @@ def run(self) -> None: database = DatabaseDAO.build_db_for_connection_test( server_cert=self._properties.get("server_cert", ""), - extra=json.dumps(self._properties.get("extra", {})), + extra=self._properties.get("extra", "{}"), impersonate_user=self._properties.get("impersonate_user", False), - encrypted_extra=json.dumps(self._properties.get("encrypted_extra", {})), + encrypted_extra=self._properties.get("encrypted_extra", "{}"), ) if database is not None: database.set_sqlalchemy_uri(uri) diff --git a/superset/databases/schemas.py b/superset/databases/schemas.py index 859eebb9290d6..d3755148bfdf1 100644 --- a/superset/databases/schemas.py +++ b/superset/databases/schemas.py @@ -298,7 +298,9 @@ class DatabaseTestConnectionSchema(Schema): impersonate_user = fields.Boolean(description=impersonate_user_description) extra = fields.String(description=extra_description, validate=extra_validator) encrypted_extra = fields.String( - description=encrypted_extra_description, validate=encrypted_extra_validator + description=encrypted_extra_description, + validate=encrypted_extra_validator, + allow_none=True, ) server_cert = fields.String( description=server_cert_description, validate=server_cert_validator diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index e07b7ac8a072f..e58e1689a3896 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -656,15 +656,23 @@ def test_test_connection(self): """ Database API: Test test connection """ + extra = { + "metadata_params": {}, + "engine_params": {}, + "metadata_cache_timeout": {}, + "schemas_allowed_for_csv_upload": [], + } # need to temporarily allow sqlite dbs, teardown will undo this app.config["PREVENT_UNSAFE_DB_CONNECTIONS"] = False self.login("admin") example_db = get_example_database() # validate that the endpoint works with the password-masked sqlalchemy uri data = { - "sqlalchemy_uri": example_db.safe_sqlalchemy_uri(), "database_name": "examples", + "encrypted_extra": "{}", + "extra": json.dumps(extra), "impersonate_user": False, + "sqlalchemy_uri": example_db.safe_sqlalchemy_uri(), } url = f"api/v1/database/test_connection" rv = self.post_assert_metric(url, data, "test_connection") @@ -676,6 +684,7 @@ def test_test_connection(self): "sqlalchemy_uri": example_db.sqlalchemy_uri_decrypted, "database_name": "examples", "impersonate_user": False, + "extra": json.dumps(extra), } rv = self.post_assert_metric(url, data, "test_connection") self.assertEqual(rv.status_code, 200) From d37181fc95c526b80dec4bb1e1c6ef75763af6ed Mon Sep 17 00:00:00 2001 From: Lily Kuang Date: Thu, 10 Sep 2020 09:18:06 -0700 Subject: [PATCH 2/3] fix lint and allow_none server_cert --- superset/databases/commands/test_connection.py | 1 - superset/databases/schemas.py | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/superset/databases/commands/test_connection.py b/superset/databases/commands/test_connection.py index 0fbfb313144b6..99e6f98721705 100644 --- a/superset/databases/commands/test_connection.py +++ b/superset/databases/commands/test_connection.py @@ -18,7 +18,6 @@ from contextlib import closing from typing import Any, Dict, Optional -import simplejson as json from flask_appbuilder.security.sqla.models import User from sqlalchemy import select diff --git a/superset/databases/schemas.py b/superset/databases/schemas.py index d3755148bfdf1..7dd7bc41f0ed1 100644 --- a/superset/databases/schemas.py +++ b/superset/databases/schemas.py @@ -303,7 +303,9 @@ class DatabaseTestConnectionSchema(Schema): allow_none=True, ) server_cert = fields.String( - description=server_cert_description, validate=server_cert_validator + description=server_cert_description, + allow_none=True, + validate=server_cert_validator, ) sqlalchemy_uri = fields.String( description=sqlalchemy_uri_description, From 2e11d0aed407274f64535e71d04079387f08c54c Mon Sep 17 00:00:00 2001 From: Lily Kuang Date: Thu, 10 Sep 2020 11:59:03 -0700 Subject: [PATCH 3/3] update test connection tests --- tests/databases/api_tests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index e58e1689a3896..9dafb87f2f7ac 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -673,6 +673,7 @@ def test_test_connection(self): "extra": json.dumps(extra), "impersonate_user": False, "sqlalchemy_uri": example_db.safe_sqlalchemy_uri(), + "server_cert": ssl_certificate, } url = f"api/v1/database/test_connection" rv = self.post_assert_metric(url, data, "test_connection") @@ -685,6 +686,7 @@ def test_test_connection(self): "database_name": "examples", "impersonate_user": False, "extra": json.dumps(extra), + "server_cert": None, } rv = self.post_assert_metric(url, data, "test_connection") self.assertEqual(rv.status_code, 200) @@ -700,6 +702,7 @@ def test_test_connection_failed(self): "sqlalchemy_uri": "broken://url", "database_name": "examples", "impersonate_user": False, + "server_cert": None, } url = f"api/v1/database/test_connection" rv = self.post_assert_metric(url, data, "test_connection") @@ -716,6 +719,7 @@ def test_test_connection_failed(self): "sqlalchemy_uri": "mssql+pymssql://url", "database_name": "examples", "impersonate_user": False, + "server_cert": None, } rv = self.post_assert_metric(url, data, "test_connection") self.assertEqual(rv.status_code, 400) @@ -738,6 +742,7 @@ def test_test_connection_unsafe_uri(self): "sqlalchemy_uri": "sqlite:///home/superset/unsafe.db", "database_name": "unsafe", "impersonate_user": False, + "server_cert": None, } url = f"api/v1/database/test_connection" rv = self.post_assert_metric(url, data, "test_connection")