2021-04-23 13:51:47 -04:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
# distributed with this work for additional information
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
# pylint: disable=import-outside-toplevel, invalid-name, unused-argument, redefined-outer-name
|
2021-04-23 13:51:47 -04:00
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
import pytest
|
2021-04-23 13:51:47 -04:00
|
|
|
from marshmallow import fields, Schema, ValidationError
|
2022-08-29 14:42:42 -04:00
|
|
|
from pytest_mock import MockFixture
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from superset.databases.schemas import DatabaseParametersSchemaMixin
|
|
|
|
from superset.db_engine_specs.base import BasicParametersMixin
|
2021-04-23 13:51:47 -04:00
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class InvalidEngine:
|
|
|
|
"""
|
|
|
|
An invalid DB engine spec.
|
|
|
|
"""
|
2021-04-23 13:51:47 -04:00
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
@pytest.fixture
|
|
|
|
def dummy_schema() -> "DatabaseParametersSchemaMixin":
|
|
|
|
"""
|
|
|
|
Fixture providing a dummy schema.
|
|
|
|
"""
|
|
|
|
from superset.databases.schemas import DatabaseParametersSchemaMixin
|
2021-04-23 13:51:47 -04:00
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
class DummySchema(Schema, DatabaseParametersSchemaMixin):
|
|
|
|
sqlalchemy_uri = fields.String()
|
2021-04-23 13:51:47 -04:00
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
return DummySchema()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def dummy_engine(mocker: MockFixture) -> None:
|
|
|
|
"""
|
|
|
|
Fixture proving a dummy DB engine spec.
|
|
|
|
"""
|
|
|
|
from superset.db_engine_specs.base import BasicParametersMixin
|
|
|
|
|
|
|
|
class DummyEngine(BasicParametersMixin):
|
|
|
|
engine = "dummy"
|
|
|
|
default_driver = "dummy"
|
|
|
|
|
|
|
|
mocker.patch("superset.databases.schemas.get_engine_spec", return_value=DummyEngine)
|
2021-04-23 13:51:47 -04:00
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
def test_database_parameters_schema_mixin(
|
|
|
|
dummy_engine: None,
|
|
|
|
dummy_schema: "Schema",
|
|
|
|
) -> None:
|
|
|
|
from superset.models.core import ConfigurationMethod
|
2021-04-23 13:51:47 -04:00
|
|
|
|
|
|
|
payload = {
|
2021-05-25 17:13:17 -04:00
|
|
|
"engine": "dummy_engine",
|
|
|
|
"configuration_method": ConfigurationMethod.DYNAMIC_FORM,
|
2021-04-23 13:51:47 -04:00
|
|
|
"parameters": {
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 12345,
|
|
|
|
"database": "dbname",
|
2021-05-25 17:13:17 -04:00
|
|
|
},
|
2021-04-23 13:51:47 -04:00
|
|
|
}
|
2022-08-29 14:42:42 -04:00
|
|
|
result = dummy_schema.load(payload)
|
2021-04-23 13:51:47 -04:00
|
|
|
assert result == {
|
2021-05-25 17:13:17 -04:00
|
|
|
"configuration_method": ConfigurationMethod.DYNAMIC_FORM,
|
2021-05-25 17:46:54 -04:00
|
|
|
"sqlalchemy_uri": "dummy+dummy://username:password@localhost:12345/dbname",
|
2021-04-23 13:51:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
def test_database_parameters_schema_mixin_no_engine(
|
|
|
|
dummy_schema: "Schema",
|
|
|
|
) -> None:
|
|
|
|
from superset.models.core import ConfigurationMethod
|
|
|
|
|
2021-04-23 13:51:47 -04:00
|
|
|
payload = {
|
2021-05-25 17:13:17 -04:00
|
|
|
"configuration_method": ConfigurationMethod.DYNAMIC_FORM,
|
2021-04-23 13:51:47 -04:00
|
|
|
"parameters": {
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 12345,
|
2022-08-29 14:42:42 -04:00
|
|
|
"database": "dbname",
|
2021-05-25 17:13:17 -04:00
|
|
|
},
|
2021-04-23 13:51:47 -04:00
|
|
|
}
|
|
|
|
try:
|
2022-08-29 14:42:42 -04:00
|
|
|
dummy_schema.load(payload)
|
2021-04-23 13:51:47 -04:00
|
|
|
except ValidationError as err:
|
|
|
|
assert err.messages == {
|
|
|
|
"_schema": [
|
2022-08-29 14:42:42 -04:00
|
|
|
(
|
|
|
|
"An engine must be specified when passing individual parameters to "
|
|
|
|
"a database."
|
|
|
|
),
|
2021-04-23 13:51:47 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
def test_database_parameters_schema_mixin_invalid_engine(
|
|
|
|
dummy_engine: None,
|
|
|
|
dummy_schema: "Schema",
|
|
|
|
) -> None:
|
|
|
|
from superset.models.core import ConfigurationMethod
|
|
|
|
|
2021-04-23 13:51:47 -04:00
|
|
|
payload = {
|
2021-05-25 17:13:17 -04:00
|
|
|
"engine": "dummy_engine",
|
|
|
|
"configuration_method": ConfigurationMethod.DYNAMIC_FORM,
|
2021-04-23 13:51:47 -04:00
|
|
|
"parameters": {
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 12345,
|
2022-08-29 14:42:42 -04:00
|
|
|
"database": "dbname",
|
2021-05-25 17:13:17 -04:00
|
|
|
},
|
2021-04-23 13:51:47 -04:00
|
|
|
}
|
|
|
|
try:
|
2022-08-29 14:42:42 -04:00
|
|
|
dummy_schema.load(payload)
|
2021-04-23 13:51:47 -04:00
|
|
|
except ValidationError as err:
|
|
|
|
assert err.messages == {
|
|
|
|
"_schema": ['Engine "dummy_engine" is not a valid engine.']
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
def test_database_parameters_schema_no_mixin(
|
|
|
|
dummy_engine: None,
|
|
|
|
dummy_schema: "Schema",
|
|
|
|
) -> None:
|
|
|
|
from superset.models.core import ConfigurationMethod
|
|
|
|
|
2021-04-23 13:51:47 -04:00
|
|
|
payload = {
|
2021-05-25 17:13:17 -04:00
|
|
|
"engine": "invalid_engine",
|
|
|
|
"configuration_method": ConfigurationMethod.DYNAMIC_FORM,
|
2021-04-23 13:51:47 -04:00
|
|
|
"parameters": {
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": 12345,
|
|
|
|
"database": "dbname",
|
2021-05-25 17:13:17 -04:00
|
|
|
},
|
2021-04-23 13:51:47 -04:00
|
|
|
}
|
|
|
|
try:
|
2022-08-29 14:42:42 -04:00
|
|
|
dummy_schema.load(payload)
|
2021-04-23 13:51:47 -04:00
|
|
|
except ValidationError as err:
|
|
|
|
assert err.messages == {
|
|
|
|
"_schema": [
|
|
|
|
(
|
|
|
|
'Engine spec "InvalidEngine" does not support '
|
|
|
|
"being configured via individual parameters."
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
2021-05-27 17:46:28 -04:00
|
|
|
|
|
|
|
|
2022-08-29 14:42:42 -04:00
|
|
|
def test_database_parameters_schema_mixin_invalid_type(
|
|
|
|
dummy_engine: None,
|
|
|
|
dummy_schema: "Schema",
|
|
|
|
) -> None:
|
|
|
|
from superset.models.core import ConfigurationMethod
|
|
|
|
|
2021-05-27 17:46:28 -04:00
|
|
|
payload = {
|
|
|
|
"engine": "dummy_engine",
|
|
|
|
"configuration_method": ConfigurationMethod.DYNAMIC_FORM,
|
|
|
|
"parameters": {
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"host": "localhost",
|
|
|
|
"port": "badport",
|
|
|
|
"database": "dbname",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
try:
|
2022-08-29 14:42:42 -04:00
|
|
|
dummy_schema.load(payload)
|
2021-05-27 17:46:28 -04:00
|
|
|
except ValidationError as err:
|
|
|
|
assert err.messages == {"port": ["Not a valid integer."]}
|
2022-10-25 13:29:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_rename_encrypted_extra() -> None:
|
|
|
|
"""
|
|
|
|
Test that ``encrypted_extra`` gets renamed to ``masked_encrypted_extra``.
|
|
|
|
"""
|
|
|
|
from superset.databases.schemas import ConfigurationMethod, DatabasePostSchema
|
|
|
|
|
|
|
|
schema = DatabasePostSchema()
|
|
|
|
|
|
|
|
# current schema
|
|
|
|
payload = schema.load(
|
|
|
|
{
|
|
|
|
"database_name": "My database",
|
|
|
|
"masked_encrypted_extra": "{}",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert payload == {
|
|
|
|
"database_name": "My database",
|
|
|
|
"configuration_method": ConfigurationMethod.SQLALCHEMY_FORM,
|
|
|
|
"masked_encrypted_extra": "{}",
|
|
|
|
}
|
|
|
|
|
|
|
|
# previous schema
|
|
|
|
payload = schema.load(
|
|
|
|
{
|
|
|
|
"database_name": "My database",
|
|
|
|
"encrypted_extra": "{}",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert payload == {
|
|
|
|
"database_name": "My database",
|
|
|
|
"configuration_method": ConfigurationMethod.SQLALCHEMY_FORM,
|
|
|
|
"masked_encrypted_extra": "{}",
|
|
|
|
}
|