mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Error on startup if invalid setting types
This commit is contained in:
parent
58ac5ccd6e
commit
23715d6c00
5 changed files with 96 additions and 15 deletions
|
|
@ -307,7 +307,57 @@ def test_setting_type_validation():
|
|||
runner = CliRunner()
|
||||
result = runner.invoke(cli, ["--setting", "default_page_size", "dog"])
|
||||
assert result.exit_code == 2
|
||||
assert '"settings.default_page_size" should be an integer' in result.stderr
|
||||
assert '"settings.default_page_size" should be an integer' in result.output
|
||||
|
||||
|
||||
def test_setting_boolean_validation_invalid():
|
||||
"""Test that invalid boolean values are rejected"""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli, ["--setting", "default_allow_sql", "invalid", "--get", "/-/settings.json"]
|
||||
)
|
||||
assert result.exit_code == 2
|
||||
assert (
|
||||
'"settings.default_allow_sql" should be on/off/true/false/1/0' in result.output
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ("off", "false", "0"))
|
||||
def test_setting_boolean_validation_false_values(value):
|
||||
"""Test that 'off', 'false', '0' work for boolean settings"""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"--setting",
|
||||
"default_allow_sql",
|
||||
value,
|
||||
"--get",
|
||||
"/_memory/-/query.json?sql=select+1",
|
||||
],
|
||||
)
|
||||
# Should be forbidden (setting is false)
|
||||
assert result.exit_code == 1, result.output
|
||||
assert "Forbidden" in result.output
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ("on", "true", "1"))
|
||||
def test_setting_boolean_validation_true_values(value):
|
||||
"""Test that 'on', 'true', '1' work for boolean settings"""
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(
|
||||
cli,
|
||||
[
|
||||
"--setting",
|
||||
"default_allow_sql",
|
||||
value,
|
||||
"--get",
|
||||
"/_memory/-/query.json?sql=select+1&_shape=objects",
|
||||
],
|
||||
)
|
||||
# Should succeed (setting is true)
|
||||
assert result.exit_code == 0, result.output
|
||||
assert json.loads(result.output)["rows"][0] == {"1": 1}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("default_allow_sql", (True, False))
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ def test_invalid_settings(config_dir):
|
|||
try:
|
||||
with pytest.raises(StartupError) as ex:
|
||||
ds = Datasette([], config_dir=config_dir)
|
||||
assert ex.value.args[0] == "Invalid setting 'invalid' in datasette.json"
|
||||
assert ex.value.args[0] == "Invalid setting 'invalid' in config file"
|
||||
finally:
|
||||
(config_dir / "datasette.json").write_text(previous, "utf-8")
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,9 @@ def plugin_allow_all_for_user(user: str) -> Callable[[str], PermissionSQL]:
|
|||
return provider
|
||||
|
||||
|
||||
def plugin_deny_specific_table(user: str, parent: str, child: str) -> Callable[[str], PermissionSQL]:
|
||||
def plugin_deny_specific_table(
|
||||
user: str, parent: str, child: str
|
||||
) -> Callable[[str], PermissionSQL]:
|
||||
def provider(action: str) -> PermissionSQL:
|
||||
return PermissionSQL(
|
||||
"deny_specific_table",
|
||||
|
|
@ -66,7 +68,9 @@ def plugin_org_policy_deny_parent(parent: str) -> Callable[[str], PermissionSQL]
|
|||
return provider
|
||||
|
||||
|
||||
def plugin_allow_parent_for_user(user: str, parent: str) -> Callable[[str], PermissionSQL]:
|
||||
def plugin_allow_parent_for_user(
|
||||
user: str, parent: str
|
||||
) -> Callable[[str], PermissionSQL]:
|
||||
def provider(action: str) -> PermissionSQL:
|
||||
return PermissionSQL(
|
||||
"allow_parent",
|
||||
|
|
@ -81,7 +85,9 @@ def plugin_allow_parent_for_user(user: str, parent: str) -> Callable[[str], Perm
|
|||
return provider
|
||||
|
||||
|
||||
def plugin_child_allow_for_user(user: str, parent: str, child: str) -> Callable[[str], PermissionSQL]:
|
||||
def plugin_child_allow_for_user(
|
||||
user: str, parent: str, child: str
|
||||
) -> Callable[[str], PermissionSQL]:
|
||||
def provider(action: str) -> PermissionSQL:
|
||||
return PermissionSQL(
|
||||
"allow_child",
|
||||
|
|
@ -137,7 +143,9 @@ def plugin_conflicting_same_child_rules(
|
|||
return [allow_provider, deny_provider]
|
||||
|
||||
|
||||
def plugin_allow_all_for_action(user: str, allowed_action: str) -> Callable[[str], PermissionSQL]:
|
||||
def plugin_allow_all_for_action(
|
||||
user: str, allowed_action: str
|
||||
) -> Callable[[str], PermissionSQL]:
|
||||
def provider(action: str) -> PermissionSQL:
|
||||
if action != allowed_action:
|
||||
return PermissionSQL(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue