configpanel: forbid extra props on BaseOption + accordingly fix tests

This commit is contained in:
axolotle 2023-10-25 15:06:10 +02:00
parent 9134515604
commit c4c79c61fe
2 changed files with 14 additions and 1 deletions

View file

@ -16,6 +16,7 @@ from yunohost import app, domain, user
from yunohost.utils.form import ( from yunohost.utils.form import (
OPTIONS, OPTIONS,
FORBIDDEN_PASSWORD_CHARS, FORBIDDEN_PASSWORD_CHARS,
READONLY_TYPES,
ask_questions_and_parse_answers, ask_questions_and_parse_answers,
BaseChoicesOption, BaseChoicesOption,
BaseInputOption, BaseInputOption,
@ -444,6 +445,9 @@ class BaseTest:
def _test_basic_attrs(self): def _test_basic_attrs(self):
raw_option = self.get_raw_option(optional=True) raw_option = self.get_raw_option(optional=True)
if raw_option["type"] in READONLY_TYPES:
del raw_option["optional"]
if raw_option["type"] == "select": if raw_option["type"] == "select":
raw_option["choices"] = ["one"] raw_option["choices"] = ["one"]

View file

@ -258,6 +258,12 @@ class OptionType(str, Enum):
group = "group" group = "group"
READONLY_TYPES = {
OptionType.display_text,
OptionType.markdown,
OptionType.alert,
OptionType.button,
}
FORBIDDEN_READONLY_TYPES = { FORBIDDEN_READONLY_TYPES = {
OptionType.password, OptionType.password,
OptionType.app, OptionType.app,
@ -310,6 +316,7 @@ class BaseOption(BaseModel):
arbitrary_types_allowed = True arbitrary_types_allowed = True
use_enum_values = True use_enum_values = True
validate_assignment = True validate_assignment = True
extra = Extra.forbid
@staticmethod @staticmethod
def schema_extra(schema: dict[str, Any], model: Type["BaseOption"]) -> None: def schema_extra(schema: dict[str, Any], model: Type["BaseOption"]) -> None:
@ -1314,9 +1321,11 @@ class OptionsModel(BaseModel):
"type", "type",
OptionType.select if "choices" in data else OptionType.string, OptionType.select if "choices" in data else OptionType.string,
), ),
"optional": data.get("optional", optional),
} }
if option["type"] not in READONLY_TYPES:
option["optional"] = option.get("optional", optional)
# LEGACY (`choices` in option `string` used to be valid) # LEGACY (`choices` in option `string` used to be valid)
if "choices" in option and option["type"] == OptionType.string: if "choices" in option and option["type"] == OptionType.string:
logger.warning( logger.warning(