Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Sep 1, 2022
1 parent 5279d7f commit 7c5fe05
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 86 deletions.
2 changes: 1 addition & 1 deletion superset/databases/commands/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, data: Dict[str, Any]):
self._properties = data.copy()
self._model: Optional[Database] = None

def run(self) -> None:
def run(self) -> None: # pylint: disable=too-many-statements
self.validate()
uri = self._properties.get("sqlalchemy_uri", "")
if self._model and uri == self._model.safe_sqlalchemy_uri():
Expand Down
8 changes: 4 additions & 4 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ def parameters(self) -> Dict[str, Any]:
self.encrypted_extra
)
try:
config = json.loads(masked_encrypted_extra)
except json.JSONDecodeError:
config = {}
encrypted_config = json.loads(masked_encrypted_extra)
except (TypeError, json.JSONDecodeError):
encrypted_config = {}

try:
# pylint: disable=useless-suppression
parameters = db_engine_spec.get_parameters_from_uri( # type: ignore
masked_uri,
encrypted_extra=config,
encrypted_extra=encrypted_config,
)
except Exception: # pylint: disable=broad-except
parameters = {}
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def test_create_database_json_validate(self):
"database_name": "test-create-database-invalid-json",
"sqlalchemy_uri": example_db.sqlalchemy_uri_decrypted,
"configuration_method": ConfigurationMethod.SQLALCHEMY_FORM,
"encrypted_extra": '{"A": "a", "B", "C"}',
"masked_encrypted_extra": '{"A": "a", "B", "C"}',
"extra": '["A": "a", "B", "C"]',
}

Expand All @@ -383,7 +383,7 @@ def test_create_database_json_validate(self):
response = json.loads(rv.data.decode("utf-8"))
expected_response = {
"message": {
"encrypted_extra": [
"masked_encrypted_extra": [
"Field cannot be decoded by JSON. Expecting ':' "
"delimiter: line 1 column 15 (char 14)"
],
Expand Down Expand Up @@ -1353,7 +1353,7 @@ def test_test_connection(self):
# validate that the endpoint works with the password-masked sqlalchemy uri
data = {
"database_name": "examples",
"encrypted_extra": "{}",
"masked_encrypted_extra": "{}",
"extra": json.dumps(extra),
"impersonate_user": False,
"sqlalchemy_uri": example_db.safe_sqlalchemy_uri(),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests/db_engine_specs/postgres_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ def test_base_parameters_mixin():
parameters_from_uri = PostgresEngineSpec.get_parameters_from_uri(sqlalchemy_uri)
assert parameters_from_uri == {
"username": "username",
"password": "XXXXXXXXXX",
"password": "password",
"host": "localhost",
"port": 5432,
"database": "dbname",
Expand Down
39 changes: 0 additions & 39 deletions tests/unit_tests/db_engine_specs/test_bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,45 +163,6 @@ def test_get_parameters_from_uri_serializable() -> None:
assert json.loads(json.dumps(parameters)) == parameters


def test_get_parameters_from_uri() -> None:
"""
Test that the private key in the credentials is properly masked.
"""
from superset.db_engine_specs.bigquery import BigQueryEngineSpec

uri = "bigquery://"
encrypted_extra = {
"credentials_info": {
"type": "service_account",
"project_id": "black-sanctum-314419",
"private_key_id": "259b0d419a8f840056158763ff54d8b08f7b8173",
"private_key": "SECRET",
"client_email": "google-spreadsheets-demo-servi@black-sanctum-314419.iam.gserviceaccount.com",
"client_id": "114567578578109757129",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google-spreadsheets-demo-servi%40black-sanctum-314419.iam.gserviceaccount.com",
},
}

assert BigQueryEngineSpec.get_parameters_from_uri(uri, encrypted_extra) == {
"credentials_info": {
"type": "service_account",
"project_id": "black-sanctum-314419",
"private_key_id": "259b0d419a8f840056158763ff54d8b08f7b8173",
"private_key": "XXXXXXXXXX",
"client_email": "google-spreadsheets-demo-servi@black-sanctum-314419.iam.gserviceaccount.com",
"client_id": "114567578578109757129",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google-spreadsheets-demo-servi%40black-sanctum-314419.iam.gserviceaccount.com",
},
"query": {},
}


def test_unmask_encrypted_extra() -> None:
"""
Test that the private key can be reused from the previous ``encrypted_extra``.
Expand Down
38 changes: 0 additions & 38 deletions tests/unit_tests/db_engine_specs/test_gsheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,44 +205,6 @@ def test_validate_parameters_catalog_and_credentials(
)


def test_get_parameters_from_uri() -> None:
"""
Test that the private key in the credentials is properly masked.
"""
from superset.db_engine_specs.gsheets import GSheetsEngineSpec

uri = "gsheets://"
encrypted_extra = {
"service_account_info": {
"type": "service_account",
"project_id": "black-sanctum-314419",
"private_key_id": "259b0d419a8f840056158763ff54d8b08f7b8173",
"private_key": "SECRET",
"client_email": "google-spreadsheets-demo-servi@black-sanctum-314419.iam.gserviceaccount.com",
"client_id": "114567578578109757129",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google-spreadsheets-demo-servi%40black-sanctum-314419.iam.gserviceaccount.com",
},
}

assert GSheetsEngineSpec.get_parameters_from_uri(uri, encrypted_extra) == {
"service_account_info": {
"type": "service_account",
"project_id": "black-sanctum-314419",
"private_key_id": "259b0d419a8f840056158763ff54d8b08f7b8173",
"private_key": "XXXXXXXXXX",
"client_email": "google-spreadsheets-demo-servi@black-sanctum-314419.iam.gserviceaccount.com",
"client_id": "114567578578109757129",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google-spreadsheets-demo-servi%40black-sanctum-314419.iam.gserviceaccount.com",
},
}


def test_unmask_encrypted_extra() -> None:
"""
Test that the private key can be reused from the previous ``encrypted_extra``.
Expand Down

0 comments on commit 7c5fe05

Please sign in to comment.