configpanels: fix inconsistent return format for boolean, sometimes 1/0, sometimes True/False -> force normalization of values when calling get() for a single setting from a config panel

This commit is contained in:
Alexandre Aubin 2022-12-20 19:51:21 +01:00
parent 80a060dd94
commit 47b9b8b520
8 changed files with 68 additions and 29 deletions

View file

@ -81,7 +81,7 @@ alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases alias_database = hash:/etc/aliases
mydomain = {{ main_domain }} mydomain = {{ main_domain }}
mydestination = localhost mydestination = localhost
{% if relay_enabled != "1" %} {% if relay_enabled != "True" %}
relayhost = relayhost =
{% else %} {% else %}
relayhost = [{{ relay_host }}]:{{ relay_port }} relayhost = [{{ relay_host }}]:{{ relay_port }}
@ -198,7 +198,7 @@ smtpd_client_recipient_rate_limit=150
# and after to send spam # and after to send spam
disable_vrfy_command = yes disable_vrfy_command = yes
{% if relay_enabled == "1" %} {% if relay_enabled == "True" %}
# Relay email through an other smtp account # Relay email through an other smtp account
# enable SASL authentication # enable SASL authentication
smtp_sasl_auth_enable = yes smtp_sasl_auth_enable = yes

View file

@ -957,3 +957,7 @@ _ynh_apply_default_permissions() {
chown root:root $target chown root:root $target
fi fi
} }
int_to_bool() {
sed -e 's/^1$/True/g' -e 's/^0$/False/g'
}

View file

@ -17,7 +17,7 @@ do_pre_regen() {
# Support different strategy for security configurations # Support different strategy for security configurations
export compatibility="$(yunohost settings get 'security.ssh.ssh_compatibility')" export compatibility="$(yunohost settings get 'security.ssh.ssh_compatibility')"
export port="$(yunohost settings get 'security.ssh.ssh_port')" export port="$(yunohost settings get 'security.ssh.ssh_port')"
export password_authentication="$(yunohost settings get 'security.ssh.ssh_password_authentication')" export password_authentication="$(yunohost settings get 'security.ssh.ssh_password_authentication' | int_to_bool)"
export ssh_keys export ssh_keys
export ipv6_enabled export ipv6_enabled
ynh_render_template "sshd_config" "${pending_dir}/etc/ssh/sshd_config" ynh_render_template "sshd_config" "${pending_dir}/etc/ssh/sshd_config"

View file

@ -56,8 +56,8 @@ do_pre_regen() {
# install / update plain conf files # install / update plain conf files
cp plain/* "$nginx_conf_dir" cp plain/* "$nginx_conf_dir"
# remove the panel overlay if this is specified in settings # remove the panel overlay if this is specified in settings
panel_overlay=$(yunohost settings get 'misc.portal.ssowat_panel_overlay_enabled') panel_overlay=$(yunohost settings get 'misc.portal.ssowat_panel_overlay_enabled' | int_to_bool)
if [ "$panel_overlay" == "false" ] || [ "$panel_overlay" == "False" ]; then if [ "$panel_overlay" == "False" ]; then
echo "#" >"${nginx_conf_dir}/yunohost_panel.conf.inc" echo "#" >"${nginx_conf_dir}/yunohost_panel.conf.inc"
fi fi
@ -65,9 +65,9 @@ do_pre_regen() {
main_domain=$(cat /etc/yunohost/current_host) main_domain=$(cat /etc/yunohost/current_host)
# Support different strategy for security configurations # Support different strategy for security configurations
export redirect_to_https="$(yunohost settings get 'security.nginx.nginx_redirect_to_https')" export redirect_to_https="$(yunohost settings get 'security.nginx.nginx_redirect_to_https' | int_to_bool)"
export compatibility="$(yunohost settings get 'security.nginx.nginx_compatibility')" export compatibility="$(yunohost settings get 'security.nginx.nginx_compatibility')"
export experimental="$(yunohost settings get 'security.experimental.security_experimental_enabled')" export experimental="$(yunohost settings get 'security.experimental.security_experimental_enabled' | int_to_bool)"
ynh_render_template "security.conf.inc" "${nginx_conf_dir}/security.conf.inc" ynh_render_template "security.conf.inc" "${nginx_conf_dir}/security.conf.inc"
cert_status=$(yunohost domain cert status --json) cert_status=$(yunohost domain cert status --json)
@ -109,7 +109,7 @@ do_pre_regen() {
done done
export webadmin_allowlist_enabled=$(yunohost settings get security.webadmin.webadmin_allowlist_enabled) export webadmin_allowlist_enabled=$(yunohost settings get security.webadmin.webadmin_allowlist_enabled | int_to_bool)
if [ "$webadmin_allowlist_enabled" == "True" ]; then if [ "$webadmin_allowlist_enabled" == "True" ]; then
export webadmin_allowlist=$(yunohost settings get security.webadmin.webadmin_allowlist) export webadmin_allowlist=$(yunohost settings get security.webadmin.webadmin_allowlist)
fi fi

View file

@ -29,8 +29,8 @@ do_pre_regen() {
export relay_port="" export relay_port=""
export relay_user="" export relay_user=""
export relay_host="" export relay_host=""
export relay_enabled="$(yunohost settings get 'email.smtp.smtp_relay_enabled')" export relay_enabled="$(yunohost settings get 'email.smtp.smtp_relay_enabled' | int_to_bool)"
if [ "${relay_enabled}" == "1" ]; then if [ "${relay_enabled}" == "True" ]; then
relay_host="$(yunohost settings get 'email.smtp.smtp_relay_host')" relay_host="$(yunohost settings get 'email.smtp.smtp_relay_host')"
relay_port="$(yunohost settings get 'email.smtp.smtp_relay_port')" relay_port="$(yunohost settings get 'email.smtp.smtp_relay_port')"
relay_user="$(yunohost settings get 'email.smtp.smtp_relay_user')" relay_user="$(yunohost settings get 'email.smtp.smtp_relay_user')"
@ -56,7 +56,7 @@ do_pre_regen() {
>"${default_dir}/postsrsd" >"${default_dir}/postsrsd"
# adapt it for IPv4-only hosts # adapt it for IPv4-only hosts
ipv6="$(yunohost settings get 'email.smtp.smtp_allow_ipv6')" ipv6="$(yunohost settings get 'email.smtp.smtp_allow_ipv6' | int_to_bool)"
if [ "$ipv6" == "False" ] || [ ! -f /proc/net/if_inet6 ]; then if [ "$ipv6" == "False" ] || [ ! -f /proc/net/if_inet6 ]; then
sed -i \ sed -i \
's/ \[::ffff:127.0.0.0\]\/104 \[::1\]\/128//g' \ 's/ \[::ffff:127.0.0.0\]\/104 \[::1\]\/128//g' \

View file

@ -160,3 +160,24 @@ name = "Other"
[misc.backup.backup_compress_tar_archives] [misc.backup.backup_compress_tar_archives]
type = "boolean" type = "boolean"
default = false default = false
[example]
[example.example]
[example.example.boolean]
type = "boolean"
yes = "True"
no = "False"
default = "True"
[example.example.number]
type = "number"
default = 42
[example.example.string]
type = "string"
default = "yolo swag"
[example.example.select]
type = "select"
choices = ["a", "b", "c"]
default = "a"

View file

@ -1,6 +1,7 @@
import os import os
import pytest import pytest
import yaml import yaml
from mock import patch
import moulinette import moulinette
from yunohost.utils.error import YunohostError, YunohostValidationError from yunohost.utils.error import YunohostError, YunohostValidationError
@ -152,10 +153,10 @@ def test_settings_get_doesnt_exists():
def test_settings_set(): def test_settings_set():
settings_set("example.example.boolean", False) settings_set("example.example.boolean", False)
assert settings_get("example.example.boolean") is False assert settings_get("example.example.boolean") == 0
settings_set("example.example.boolean", "on") settings_set("example.example.boolean", "on")
assert settings_get("example.example.boolean") is True assert settings_get("example.example.boolean") == 1
def test_settings_set_int(): def test_settings_set_int():
@ -174,35 +175,39 @@ def test_settings_set_doesexit():
def test_settings_set_bad_type_bool(): def test_settings_set_bad_type_bool():
with pytest.raises(YunohostError):
settings_set("example.example.boolean", 42) with patch.object(os, "isatty", return_value=False):
with pytest.raises(YunohostError): with pytest.raises(YunohostError):
settings_set("example.example.boolean", "pouet") settings_set("example.example.boolean", 42)
with pytest.raises(YunohostError):
settings_set("example.example.boolean", "pouet")
def test_settings_set_bad_type_int(): def test_settings_set_bad_type_int():
# with pytest.raises(YunohostError): # with pytest.raises(YunohostError):
# settings_set("example.example.number", True) # settings_set("example.example.number", True)
with pytest.raises(YunohostError): with patch.object(os, "isatty", return_value=False):
settings_set("example.example.number", "pouet") with pytest.raises(YunohostError):
settings_set("example.example.number", "pouet")
# def test_settings_set_bad_type_string(): # def test_settings_set_bad_type_string():
# with pytest.raises(YunohostError): # with pytest.raises(YunohostError):
# settings_set("example.example.string", True) # settings_set(eexample.example.string", True)
# with pytest.raises(YunohostError): # with pytest.raises(YunohostError):
# settings_set("example.example.string", 42) # settings_set("example.example.string", 42)
def test_settings_set_bad_value_select(): def test_settings_set_bad_value_select():
with pytest.raises(YunohostError): with patch.object(os, "isatty", return_value=False):
settings_set("example.example.select", True) with pytest.raises(YunohostError):
with pytest.raises(YunohostError): settings_set("example.example.select", True)
settings_set("example.example.select", "e") with pytest.raises(YunohostError):
with pytest.raises(YunohostError): settings_set("example.example.select", "e")
settings_set("example.example.select", 42) with pytest.raises(YunohostError):
with pytest.raises(YunohostError): settings_set("example.example.select", 42)
settings_set("example.example.select", "pouet") with pytest.raises(YunohostError):
settings_set("example.example.select", "pouet")
def test_settings_list_modified(): def test_settings_list_modified():

View file

@ -264,8 +264,17 @@ class ConfigPanel:
# In 'classic' mode, we display the current value if key refer to an option # In 'classic' mode, we display the current value if key refer to an option
if self.filter_key.count(".") == 2 and mode == "classic": if self.filter_key.count(".") == 2 and mode == "classic":
option = self.filter_key.split(".")[-1] option = self.filter_key.split(".")[-1]
return self.values.get(option, None) value = self.values.get(option, None)
option_type = None
for _, _, option_ in self._iterate():
if option_["id"] == option:
option_type = ARGUMENTS_TYPE_PARSERS[option_["type"]]
break
return option_type.normalize(value) if option_type else value
# Format result in 'classic' or 'export' mode # Format result in 'classic' or 'export' mode
logger.debug(f"Formating result in '{mode}' mode") logger.debug(f"Formating result in '{mode}' mode")