Skip to content

Commit

Permalink
fix(databases): test connection api endpoint (apache#10824)
Browse files Browse the repository at this point in the history
* fix test connection with extra

* fix lint and allow_none server_cert

* update test connection tests
  • Loading branch information
Lily Kuang authored and auxten committed Nov 20, 2020
1 parent 3c37b6d commit 1fb00bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 2 additions & 3 deletions superset/databases/commands/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -46,9 +45,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)
Expand Down
8 changes: 6 additions & 2 deletions superset/databases/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,14 @@ 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
description=server_cert_description,
allow_none=True,
validate=server_cert_validator,
)
sqlalchemy_uri = fields.String(
description=sqlalchemy_uri_description,
Expand Down
16 changes: 15 additions & 1 deletion tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,15 +656,24 @@ 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(),
"server_cert": ssl_certificate,
}
url = f"api/v1/database/test_connection"
rv = self.post_assert_metric(url, data, "test_connection")
Expand All @@ -676,6 +685,8 @@ def test_test_connection(self):
"sqlalchemy_uri": example_db.sqlalchemy_uri_decrypted,
"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)
Expand All @@ -691,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")
Expand All @@ -707,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)
Expand All @@ -729,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")
Expand Down

0 comments on commit 1fb00bc

Please sign in to comment.