kopia lustrzana https://github.com/simonw/datasette
Migrate allow from metadata to config if necessary, closes #2249
rodzic
60c6692f68
commit
9ac9f0152f
|
@ -74,7 +74,7 @@ from .utils import (
|
||||||
find_spatialite,
|
find_spatialite,
|
||||||
format_bytes,
|
format_bytes,
|
||||||
module_from_path,
|
module_from_path,
|
||||||
move_plugins,
|
move_plugins_and_allow,
|
||||||
move_table_config,
|
move_table_config,
|
||||||
parse_metadata,
|
parse_metadata,
|
||||||
resolve_env_secrets,
|
resolve_env_secrets,
|
||||||
|
@ -344,10 +344,10 @@ class Datasette:
|
||||||
with config_files[0].open() as fp:
|
with config_files[0].open() as fp:
|
||||||
config = parse_metadata(fp.read())
|
config = parse_metadata(fp.read())
|
||||||
|
|
||||||
# Move any "plugins" settings from metadata to config - updates them in place
|
# Move any "plugins" and "allow" settings from metadata to config - updates them in place
|
||||||
metadata = metadata or {}
|
metadata = metadata or {}
|
||||||
config = config or {}
|
config = config or {}
|
||||||
metadata, config = move_plugins(metadata, config)
|
metadata, config = move_plugins_and_allow(metadata, config)
|
||||||
# Now migrate any known table configuration settings over as well
|
# Now migrate any known table configuration settings over as well
|
||||||
metadata, config = move_table_config(metadata, config)
|
metadata, config = move_table_config(metadata, config)
|
||||||
|
|
||||||
|
|
|
@ -1302,10 +1302,11 @@ def prune_empty_dicts(d: dict):
|
||||||
d.pop(key, None)
|
d.pop(key, None)
|
||||||
|
|
||||||
|
|
||||||
def move_plugins(source: dict, destination: dict) -> Tuple[dict, dict]:
|
def move_plugins_and_allow(source: dict, destination: dict) -> Tuple[dict, dict]:
|
||||||
"""
|
"""
|
||||||
Move 'plugins' keys from source to destination dictionary. Creates hierarchy in destination if needed.
|
Move 'plugins' and 'allow' keys from source to destination dictionary. Creates
|
||||||
After moving, recursively remove any keys in the source that are left empty.
|
hierarchy in destination if needed. After moving, recursively remove any keys
|
||||||
|
in the source that are left empty.
|
||||||
"""
|
"""
|
||||||
source = copy.deepcopy(source)
|
source = copy.deepcopy(source)
|
||||||
destination = copy.deepcopy(destination)
|
destination = copy.deepcopy(destination)
|
||||||
|
@ -1315,7 +1316,7 @@ def move_plugins(source: dict, destination: dict) -> Tuple[dict, dict]:
|
||||||
path = []
|
path = []
|
||||||
for key, value in list(src.items()):
|
for key, value in list(src.items()):
|
||||||
new_path = path + [key]
|
new_path = path + [key]
|
||||||
if key == "plugins":
|
if key in ("plugins", "allow"):
|
||||||
# Navigate and create the hierarchy in destination if needed
|
# Navigate and create the hierarchy in destination if needed
|
||||||
d = dest
|
d = dest
|
||||||
for step in path:
|
for step in path:
|
||||||
|
|
|
@ -89,10 +89,11 @@ def test_view_padlock(allow, expected_anon, expected_auth, path, padlock_client)
|
||||||
({"id": "root"}, 403, 200),
|
({"id": "root"}, 403, 200),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_view_database(allow, expected_anon, expected_auth):
|
@pytest.mark.parametrize("use_metadata", (True, False))
|
||||||
with make_app_client(
|
def test_view_database(allow, expected_anon, expected_auth, use_metadata):
|
||||||
config={"databases": {"fixtures": {"allow": allow}}}
|
key = "metadata" if use_metadata else "config"
|
||||||
) as client:
|
kwargs = {key: {"databases": {"fixtures": {"allow": allow}}}}
|
||||||
|
with make_app_client(**kwargs) as client:
|
||||||
for path in (
|
for path in (
|
||||||
"/fixtures",
|
"/fixtures",
|
||||||
"/fixtures/compound_three_primary_keys",
|
"/fixtures/compound_three_primary_keys",
|
||||||
|
@ -173,16 +174,19 @@ def test_database_list_respects_view_table():
|
||||||
({"id": "root"}, 403, 200),
|
({"id": "root"}, 403, 200),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_view_table(allow, expected_anon, expected_auth):
|
@pytest.mark.parametrize("use_metadata", (True, False))
|
||||||
with make_app_client(
|
def test_view_table(allow, expected_anon, expected_auth, use_metadata):
|
||||||
config={
|
key = "metadata" if use_metadata else "config"
|
||||||
|
kwargs = {
|
||||||
|
key: {
|
||||||
"databases": {
|
"databases": {
|
||||||
"fixtures": {
|
"fixtures": {
|
||||||
"tables": {"compound_three_primary_keys": {"allow": allow}}
|
"tables": {"compound_three_primary_keys": {"allow": allow}}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
) as client:
|
}
|
||||||
|
with make_app_client(**kwargs) as client:
|
||||||
anon_response = client.get("/fixtures/compound_three_primary_keys")
|
anon_response = client.get("/fixtures/compound_three_primary_keys")
|
||||||
assert expected_anon == anon_response.status
|
assert expected_anon == anon_response.status
|
||||||
if allow and anon_response.status == 200:
|
if allow and anon_response.status == 200:
|
||||||
|
|
Ładowanie…
Reference in New Issue