-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(database): Database Filtering via custom configuration #24580
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3632,3 +3632,77 @@ def test_validate_sql_endpoint_failure(self, get_validator_by_name): | |
return | ||
self.assertEqual(rv.status_code, 422) | ||
self.assertIn("Kaboom!", response["errors"][0]["message"]) | ||
|
||
def test_get_databases_with_extra_filters(self): | ||
""" | ||
API: Test get database with extra query filter | ||
""" | ||
self.login(username="admin") | ||
extra = { | ||
"metadata_params": {}, | ||
"engine_params": {}, | ||
"metadata_cache_timeout": {}, | ||
"schemas_allowed_for_file_upload": [], | ||
} | ||
example_db = get_example_database() | ||
|
||
if example_db.backend == "sqlite": | ||
return | ||
database_data = { | ||
"sqlalchemy_uri": example_db.sqlalchemy_uri_decrypted, | ||
"configuration_method": ConfigurationMethod.SQLALCHEMY_FORM, | ||
"server_cert": None, | ||
"extra": json.dumps(extra), | ||
} | ||
|
||
uri = "api/v1/database/" | ||
rv = self.client.post( | ||
uri, json={**database_data, "database_name": "dyntest-create-database-1"} | ||
) | ||
first_response = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual(rv.status_code, 201) | ||
|
||
uri = "api/v1/database/" | ||
rv = self.client.post( | ||
uri, json={**database_data, "database_name": "create-database-2"} | ||
) | ||
second_response = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual(rv.status_code, 201) | ||
dbs = db.session.query(Database).all() | ||
expected_names = [db.database_name for db in dbs] | ||
expected_names.sort() | ||
|
||
uri = f"api/v1/database/" | ||
rv = self.client.get(uri) | ||
data = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual(data["count"], len(dbs)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hughhhh here I am testing our current behavior (default) where all databases must be returned if nothing is being set in the config, so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added explicit assertions to check whether the filter method has been called when defined. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Can you write that down in the function docstring? :) |
||
database_names = [item["database_name"] for item in data["result"]] | ||
database_names.sort() | ||
# All Databases because we are an admin | ||
self.assertEqual(database_names, expected_names) | ||
assert rv.status_code == 200 | ||
|
||
def _base_filter(query): | ||
from superset.models.core import Database | ||
|
||
return query.filter(Database.database_name.startswith("dyntest")) | ||
|
||
with patch.dict( | ||
"superset.views.filters.current_app.config", | ||
{"EXTRA_DYNAMIC_DATABASE_FILTER": _base_filter}, | ||
): | ||
uri = f"api/v1/database/" | ||
rv = self.client.get(uri) | ||
data = json.loads(rv.data.decode("utf-8")) | ||
self.assertEqual(data["count"], 1) | ||
database_names = [item["database_name"] for item in data["result"]] | ||
# Only the database that starts with tests, even if we are an admin | ||
self.assertEqual(database_names, ["dyntest-create-database-1"]) | ||
assert rv.status_code == 200 | ||
|
||
# Cleanup | ||
first_model = db.session.query(Database).get(first_response.get("id")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to figure out how to do this via fixture or after every test so we can always land back in a normal state |
||
second_model = db.session.query(Database).get(second_response.get("id")) | ||
db.session.delete(first_model) | ||
db.session.delete(second_model) | ||
db.session.commit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like a good place for a docstring comment.