mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
wip tests
This commit is contained in:
parent
607a22de00
commit
c4d188200c
4 changed files with 62 additions and 35 deletions
|
@ -367,6 +367,7 @@
|
||||||
"firewall_reload_failed": "Could not reload the firewall",
|
"firewall_reload_failed": "Could not reload the firewall",
|
||||||
"firewall_reloaded": "Firewall reloaded",
|
"firewall_reloaded": "Firewall reloaded",
|
||||||
"firewall_rules_cmd_failed": "Some firewall rule commands have failed. More info in log.",
|
"firewall_rules_cmd_failed": "Some firewall rule commands have failed. More info in log.",
|
||||||
|
"global_settings_reset_success": "Reset global settings",
|
||||||
"global_settings_setting_admin_strength": "Admin password strength",
|
"global_settings_setting_admin_strength": "Admin password strength",
|
||||||
"global_settings_setting_backup_compress_tar_archives": "Compress backups",
|
"global_settings_setting_backup_compress_tar_archives": "Compress backups",
|
||||||
"global_settings_setting_backup_compress_tar_archives_help": "When creating new backups, compress the archives (.tar.gz) instead of uncompressed archives (.tar). N.B. : enabling this option means create lighter backup archives, but the initial backup procedure will be significantly longer and heavy on CPU.",
|
"global_settings_setting_backup_compress_tar_archives_help": "When creating new backups, compress the archives (.tar.gz) instead of uncompressed archives (.tar). N.B. : enabling this option means create lighter backup archives, but the initial backup procedure will be significantly longer and heavy on CPU.",
|
||||||
|
|
|
@ -28,13 +28,13 @@ name = "Security"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
||||||
[security.ssh.ssh_allow_deprecated_dsa_hostkey]
|
[security.ssh.ssh_allow_deprecated_dsa_hostkey]
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
||||||
[security.nginx]
|
[security.nginx]
|
||||||
name = "NGINX"
|
name = "NGINX"
|
||||||
|
@ -42,7 +42,7 @@ name = "Security"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "true"
|
default = "True"
|
||||||
|
|
||||||
[security.nginx.nginx_compatibility]
|
[security.nginx.nginx_compatibility]
|
||||||
type = "select"
|
type = "select"
|
||||||
|
@ -62,7 +62,7 @@ name = "Security"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
||||||
[security.webadmin.webadmin_allowlist]
|
[security.webadmin.webadmin_allowlist]
|
||||||
type = "tags"
|
type = "tags"
|
||||||
|
@ -76,7 +76,7 @@ name = "Security"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
||||||
|
|
||||||
[email]
|
[email]
|
||||||
|
@ -87,7 +87,7 @@ name = "Email"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
||||||
[email.smtp]
|
[email.smtp]
|
||||||
name = "SMTP"
|
name = "SMTP"
|
||||||
|
@ -95,13 +95,13 @@ name = "Email"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "true"
|
default = "True"
|
||||||
|
|
||||||
[email.smtp.smtp_relay_enabled]
|
[email.smtp.smtp_relay_enabled]
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
||||||
[email.smtp.smtp_relay_host]
|
[email.smtp.smtp_relay_host]
|
||||||
type = "string"
|
type = "string"
|
||||||
|
@ -134,7 +134,7 @@ name = "Other"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "true"
|
default = "True"
|
||||||
|
|
||||||
[misc.backup]
|
[misc.backup]
|
||||||
name = "Backup"
|
name = "Backup"
|
||||||
|
@ -142,4 +142,4 @@ name = "Other"
|
||||||
type = "boolean"
|
type = "boolean"
|
||||||
yes = "True"
|
yes = "True"
|
||||||
no = "False"
|
no = "False"
|
||||||
default = "false"
|
default = "False"
|
||||||
|
|
|
@ -15,6 +15,11 @@ logger = getActionLogger("yunohost.settings")
|
||||||
|
|
||||||
SETTINGS_PATH = "/etc/yunohost/settings.yml"
|
SETTINGS_PATH = "/etc/yunohost/settings.yml"
|
||||||
|
|
||||||
|
BOOLEANS = {
|
||||||
|
"True": True,
|
||||||
|
"False": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def settings_get(key="", full=False, export=False):
|
def settings_get(key="", full=False, export=False):
|
||||||
"""
|
"""
|
||||||
|
@ -37,9 +42,7 @@ def settings_get(key="", full=False, export=False):
|
||||||
mode = "classic"
|
mode = "classic"
|
||||||
|
|
||||||
if mode == "classic" and key == "":
|
if mode == "classic" and key == "":
|
||||||
raise YunohostValidationError(
|
raise YunohostValidationError("Missing key", raw_msg=True)
|
||||||
"Missing key"
|
|
||||||
)
|
|
||||||
|
|
||||||
settings = SettingsConfigPanel()
|
settings = SettingsConfigPanel()
|
||||||
key = translate_legacy_settings_to_configpanel_settings(key)
|
key = translate_legacy_settings_to_configpanel_settings(key)
|
||||||
|
@ -132,6 +135,10 @@ class SettingsConfigPanel(ConfigPanel):
|
||||||
option["help"] = m18n.n(self.config["i18n"] + "_" + option["id"] + "_help")
|
option["help"] = m18n.n(self.config["i18n"] + "_" + option["id"] + "_help")
|
||||||
return self.config
|
return self.config
|
||||||
|
|
||||||
|
# Dirty hack to let settings_get() to work from a python script
|
||||||
|
if isinstance(result, str) and result in BOOLEANS:
|
||||||
|
result = BOOLEANS[result]
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def reset(self, key = "", operation_logger=None):
|
def reset(self, key = "", operation_logger=None):
|
||||||
|
|
|
@ -3,7 +3,8 @@ import json
|
||||||
import glob
|
import glob
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from yunohost.utils.error import YunohostError
|
import moulinette
|
||||||
|
from yunohost.utils.error import YunohostError, YunohostValidationError
|
||||||
|
|
||||||
import yunohost.settings as settings
|
import yunohost.settings as settings
|
||||||
|
|
||||||
|
@ -27,7 +28,7 @@ EXAMPLE_SETTINGS = """
|
||||||
|
|
||||||
[example.example.number]
|
[example.example.number]
|
||||||
type = "number"
|
type = "number"
|
||||||
default = "42"
|
default = 42
|
||||||
|
|
||||||
[example.example.string]
|
[example.example.string]
|
||||||
type = "string"
|
type = "string"
|
||||||
|
@ -40,17 +41,35 @@ EXAMPLE_SETTINGS = """
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def setup_function(function):
|
def setup_function(function):
|
||||||
os.system("mv /etc/yunohost/settings.yml /etc/yunohost/settings.yml.saved")
|
# Backup settings
|
||||||
|
if os.path.exists(SETTINGS_PATH):
|
||||||
|
os.system(f"mv {SETTINGS_PATH} {SETTINGS_PATH}.saved")
|
||||||
|
# Add example settings to config panel
|
||||||
os.system("cp /usr/share/yunohost/config_settings.toml /usr/share/yunohost/config_settings.toml.saved")
|
os.system("cp /usr/share/yunohost/config_settings.toml /usr/share/yunohost/config_settings.toml.saved")
|
||||||
with open("/usr/share/yunohost/config_settings.py", "a") as file:
|
with open("/usr/share/yunohost/config_settings.toml", "a") as file:
|
||||||
file.write(EXAMPLE_SETTINGS)
|
file.write(EXAMPLE_SETTINGS)
|
||||||
|
|
||||||
|
|
||||||
def teardown_function(function):
|
def teardown_function(function):
|
||||||
os.system("mv /etc/yunohost/settings.yml.saved /etc/yunohost/settings.yml")
|
if os.path.exists("/etc/yunohost/settings.yml.saved"):
|
||||||
|
os.system(f"mv {SETTINGS_PATH}.saved {SETTINGS_PATH}")
|
||||||
|
elif os.path.exists(SETTINGS_PATH):
|
||||||
|
os.remove(SETTINGS_PATH)
|
||||||
os.system("mv /usr/share/yunohost/config_settings.toml.saved /usr/share/yunohost/config_settings.toml")
|
os.system("mv /usr/share/yunohost/config_settings.toml.saved /usr/share/yunohost/config_settings.toml")
|
||||||
|
|
||||||
|
|
||||||
|
old_translate = moulinette.core.Translator.translate
|
||||||
|
|
||||||
|
def _monkeypatch_translator(self, key, *args, **kwargs):
|
||||||
|
|
||||||
|
if key.startswith("global_settings_setting_"):
|
||||||
|
return f"Dummy translation for {key}"
|
||||||
|
|
||||||
|
return old_translate(self, key, *args, **kwargs)
|
||||||
|
|
||||||
|
moulinette.core.Translator.translate = _monkeypatch_translator
|
||||||
|
|
||||||
|
|
||||||
def _get_settings():
|
def _get_settings():
|
||||||
return yaml.load(open(SETTINGS_PATH, "r"))
|
return yaml.load(open(SETTINGS_PATH, "r"))
|
||||||
|
|
||||||
|
@ -59,7 +78,7 @@ def test_settings_get_bool():
|
||||||
assert settings_get("example.example.boolean")
|
assert settings_get("example.example.boolean")
|
||||||
|
|
||||||
|
|
||||||
# FIXME : Testing this doesn't make sense ? This should be tested in a test_config.py ?
|
# FIXME : Testing this doesn't make sense ? This should be tested in test_config.py ?
|
||||||
#def test_settings_get_full_bool():
|
#def test_settings_get_full_bool():
|
||||||
# assert settings_get("example.example.boolean", True) == {'version': '1.0',
|
# assert settings_get("example.example.boolean", True) == {'version': '1.0',
|
||||||
# 'i18n': 'global_settings_setting',
|
# 'i18n': 'global_settings_setting',
|
||||||
|
@ -100,22 +119,22 @@ def test_settings_get_string():
|
||||||
assert settings_get("example.example.string") == "yolo swag"
|
assert settings_get("example.example.string") == "yolo swag"
|
||||||
|
|
||||||
|
|
||||||
def test_settings_get_full_string():
|
#def test_settings_get_full_string():
|
||||||
assert settings_get("example.string", True) == {
|
# assert settings_get("example.example.string", True) == {
|
||||||
"type": "string",
|
# "type": "string",
|
||||||
"value": "yolo swag",
|
# "value": "yolo swag",
|
||||||
"default": "yolo swag",
|
# "default": "yolo swag",
|
||||||
"description": "Dummy string setting",
|
# "description": "Dummy string setting",
|
||||||
}
|
# }
|
||||||
|
|
||||||
|
|
||||||
def test_settings_get_enum():
|
def test_settings_get_select():
|
||||||
assert settings_get("example.enum") == "a"
|
assert settings_get("example.example.select") == "a"
|
||||||
|
|
||||||
|
|
||||||
def test_settings_get_full_enum():
|
#def test_settings_get_full_select():
|
||||||
option = settings_get("example.enum", full=True).get('panels')[0].get('sections')[0].get('options')[0]
|
# option = settings_get("example.example.select", full=True).get('panels')[0].get('sections')[0].get('options')[0]
|
||||||
assert option.get('choices') == ["a", "b", "c"]
|
# assert option.get('choices') == ["a", "b", "c"]
|
||||||
|
|
||||||
|
|
||||||
def test_settings_get_doesnt_exists():
|
def test_settings_get_doesnt_exists():
|
||||||
|
@ -140,7 +159,7 @@ def test_settings_set_int():
|
||||||
assert settings_get("example.example.number") == 21
|
assert settings_get("example.example.number") == 21
|
||||||
|
|
||||||
|
|
||||||
def test_settings_set_enum():
|
def test_settings_set_select():
|
||||||
settings_set("example.example.select", "c")
|
settings_set("example.example.select", "c")
|
||||||
assert settings_get("example.example.select") == "c"
|
assert settings_get("example.example.select") == "c"
|
||||||
|
|
||||||
|
@ -171,7 +190,7 @@ def test_settings_set_bad_type_string():
|
||||||
settings_set("example.example.string", 42)
|
settings_set("example.example.string", 42)
|
||||||
|
|
||||||
|
|
||||||
def test_settings_set_bad_value_enum():
|
def test_settings_set_bad_value_select():
|
||||||
with pytest.raises(YunohostError):
|
with pytest.raises(YunohostError):
|
||||||
settings_set("example.example.select", True)
|
settings_set("example.example.select", True)
|
||||||
with pytest.raises(YunohostError):
|
with pytest.raises(YunohostError):
|
||||||
|
@ -184,7 +203,7 @@ def test_settings_set_bad_value_enum():
|
||||||
|
|
||||||
def test_settings_list_modified():
|
def test_settings_list_modified():
|
||||||
settings_set("example.example.number", 21)
|
settings_set("example.example.number", 21)
|
||||||
assert settings_list()["number"] == 42
|
assert settings_list()["number"] == 21
|
||||||
|
|
||||||
|
|
||||||
def test_reset():
|
def test_reset():
|
||||||
|
@ -218,7 +237,7 @@ def test_reset_all():
|
||||||
# settings_set("example.bool", False)
|
# settings_set("example.bool", False)
|
||||||
# settings_set("example.int", 21)
|
# settings_set("example.int", 21)
|
||||||
# settings_set("example.string", "pif paf pouf")
|
# settings_set("example.string", "pif paf pouf")
|
||||||
# settings_set("example.enum", "c")
|
# settings_set("example.select", "c")
|
||||||
# settings_after_modification = settings_list()
|
# settings_after_modification = settings_list()
|
||||||
# assert settings_before != settings_after_modification
|
# assert settings_before != settings_after_modification
|
||||||
# old_settings_backup_path = settings_reset_all()["old_settings_backup_path"]
|
# old_settings_backup_path = settings_reset_all()["old_settings_backup_path"]
|
||||||
|
|
Loading…
Add table
Reference in a new issue